Note: Make sure you compile -o is with 0 not other numbers, (-o3 will use compile optimisation which make the line in the real running program differs from what you did.)
complie with -g
use gdb executablefilename
Set break point
- break functionname : break at function call.
eg:break add(int,int) specifying function name can ambiguity in overloading. - break filename.cpp:linenumber :break at specified file’s line.
- info breakpoints/info break : print break points infos
- delete breakpointname: delete one break point.
Control flow commands
r/run: run the program from start
s/step: single step and step in when encounter a function.
n/next: single step but does not step in function.
c/continue: continue until next break point or error.
Get variables
print varname: get the variable value.
display/undisplay varname: constantly display variable values or turn off.
watch varname: watch a variable, when the value of variable changes, notifies the programer.