LAB Ⅱ LECTURE 05
Debugging
Seoul National University
Graphics & Media Lab
YoungKyoon-Shin
Previously...
• Stream & Buffer
Monitor
Keyboard
Program Stream
Stream
buffer
H e l l o
goodbit no error
Previously... badbit irrecoverable stream error
• istream input/output operation failed
failbit
(formatting or extraction error)
– input class
associated input sequence has
– operator >> eofbit
reached end-of-file
– cin
– Flag bit #include <iostream>
– cin.clear() using namespace std;
– cin.ignore int main(){
int a, b;
while(1){
cout << "a: ";
cin >> a;
if(cin.good()) break;
if(cin.fail()){
cout << "wrong input"<< endl;
cin.clear();
cin.ignore(numeric_limits <streamsize> ::max(), '\n’);
}
}
cout << "b: ";
cin >> b;
cout << "sum: "<< a+b << endl;
}
Previously...
#include <iostream>
• ostream using namespace std;
– output class int main(){
– operator << cout << ‘H’ << “\n”;
cout.flush();
– cout }
• endl
– Insert newline (“\n”)
– Flush
• Print buffer to monitor
• Clear buffer
Contents
• Error & Bug
• Debugging
– Debugging techniques
– Process
– Set breakpoints
– Start debugging
– Inspect the code & value
• Mission
Error & Bug
• Error
– An incorrect user action that
produces incorrect result.
– Exception handling.
• Compilation Error
– The grammatical errors of a programming language (syntax error).
• Link Error
– An error that occurs when linking scattered source code files after
compiling them individually.
• Runtime Error
– An error that occurs during program execution, causing the program to
terminate abnormally.
Error & Bug
• Bug
– Deviation between expected
behavior to actual behavior
– Produced by programmer
Debugging
• What is ‘Debugging’?
– The process of finding and resolving defects or problems
within the program that prevent correct operation of
computer software or a system.
Debugging Techniques
• Print debugging (sometimes called printf debugging)
– By using printf function in C
– In C++, use std::cout
• Interactive debugging (Visual Studio Debugger)
– Studied in this class
• Rubber Duck Debugging
• Post-mortem debugging
• “Wolf-fence” algorithm
• Delta debugging
• Saff Squeeze
• …
Print Debugging
• Good way to test hypothesis
• When to print
– enter function
– parameters
– function results
Debugger
• What is ‘Debugger’(= Debugging tool)?
– A computer program that is used to test and debug other
programs (the “target” program).
– A tool for you to monitor your code’s variable value at
runtime. (Identifying which code is incorrect?)
• Visual Studio/Xcode has built-in debugger.
Process
Start
Set breakpoints
Inspect the code & value
No
Want to stop?
Yes
End
Build Mode - windows
• Debug
– When build program, add
information needed at debugging
• Release
– No debugging information
– Optimization process
– No initialization
Build Mode - mac
• Product > Scheme > Edit Scheme...
• Run > Info > Build Configuration
Sample code
#include <iostream> for (int i = 0; i < 3; i++) {
#include <string> a++;
b[i] = i;
}
using namespace std;
c = add(b[1], b[2]);
int add(int x, int y) { d = "Hello world!";
return x + y;
} a = addTest();
cout << a << endl;
int addTest() { for (int i = 0; i < 3; i++)
return add(10, 20); cout << b[i] << endl;
}
cout << d << endl;
int main() { return 0;
int a = 0; }
float b[3];
int c = 0;
string d;
Breakpoints
• Breakpoint is an intentional stopping or pausing place in a program,
put in place for debugging purposes.
• Breakpoint pauses the execution of the code and allows developers
to examine controls and variables before the program continues to
execute.
Run
Pause here
Breakpoints - windows
• Toggle(set/remove) breakpoint
– By clicking in the left margin of a source code file.
– By clicking your cursor on a line of code and pressing F9.
• Enable/Disable breakpoint
– Enables or disables the breakpoint on the current line of code.
– By putting your cursor on the breakpoint, and clicking Enable/Disable
breakpoint (or press CTRL+F9).
left margin
Breakpoints - mac
• Toggle(set/remove) breakpoint
– By clicking on the line number of a source code file.
– control + click to show the pop-up menu.
• Enable/Disable breakpoint
Breakpoints - windows
• Function breakpoint
– You can set a breakpoint by entering Function Name (with
Conditions and Actions)
– Debug > New Breakpoint > Function Breakpoint (or press CTRL+K -> B)
Breakpoints - mac
• Function breakpoint
– You can set a breakpoint by entering Function Name (with
Conditions and Actions)
– Debug > Breakpoints > Create Symbolic Breakpoints...
– or press option + command + \
Breakpoints - windows
• Managing breakpoints
– You can use the Breakpoints window to see all the breakpoints you
have set in your solution.
– Debug > Windows > Breakpoints (or press CTRL+ALT+B)
– You can enable/disable by checking the box
Breakpoints - mac
• Managing breakpoints
– You can use the Breakpoints window to see all the breakpoints you
have set in your product.
– You can enable/disable by clicking blue icon.
Debug Control - windows
• Break mode control
– Continue
• Resumes the running process of a program until next breakpoint.
• Debug > Continue (or press F5)
– Step Into
• Executes the next line of code. If there is a function invocation at the
current line, it will jump into the function execution.
• Debug > Step Into (or press F11)
– Step Out
• Executes the remaining lines of a function in which the current execution
point lies.
• Debug > Step Out (or press SHIFT+F11)
– Step Over
• Proceeds to the next line of code but does not step into any function calls.
• Debug > Step Over (or press F10)
– Restart
• Restart the program execution. It is usually used when the code is changed
during debugging.
Debug Control - mac
• Break mode control
– Continue 1
– Step Over 2
– Step Into 3
– Step Out 4
1 2 3 4
Inspect the code & value
• Debugger windows
• Windows
• Mac
Inspect the code & value
• A powerful feature of these windows is that they allow the variables
to be modified.
Mission
#include <iostream>
#define num 10
using namespace std;
int main() {
int *a, b[num];
cout << "1 ~ 10" << " in ascending order" << endl;
for (int i = 0; i < num; i++) {
b[i] = i + 1;
cout << b[i] << " ";
}
cout << endl;
cout << endl;
a = b;
cout << "1 ~ 10" << " in descending order" << endl;
for (int i = 0; i < num; i++) {
a[i] = b[num - i];
//DO NOT CHANGE THIS
cout << a[i] << " ";
}
cout << endl;
return 0;
}