Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Tools for Practical <strong>C++</strong> Development 17<br />
(gdb) next<br />
10 double r = reciprocal(i);<br />
If you want to see what is going on inside reciprocal, step into the function with step:<br />
(gdb) step<br />
reciprocal (i=5) at reciprocal.cc:5<br />
5 return 1.0 / i;<br />
The next statement to be executed is the return statement. Check the value of i and then issue<br />
the continue command to execute to the next breakpoint (which doesn’t exist, so the program<br />
terminates):<br />
(gdb) print i<br />
$2 = 5<br />
(gdb) continue<br />
Continuing.<br />
argv[1] = 5, 1/argv[1] = 0.2<br />
Program exited normally.<br />
A5. Try the debugger by working through the example. Experiment with the commands (the<br />
table below gives short explanations of some useful commands). Commands may be<br />
abbreviated and you may use tab for command completion. Ctrl-a takes you to the<br />
beginning of the command line, Ctrl-e to the end, and so on. Use the help facility and the<br />
manual if you want to learn more.<br />
help [command] Get help about gdb commands<br />
run [args...] Run the program with arguments specified.<br />
continue Continue execution.<br />
next Step to the next line over called functions.<br />
step Step to the next line into called functions.<br />
where Print the call stack.<br />
up Go up to caller.<br />
down Go down to callee.<br />
list [nbr] List 10 lines around the current line or around line nbr (the<br />
following lines if repeated).<br />
break func Set a breakpoint on the first line of a function func.<br />
break nbr Set a breakpoint at line nbr in the current file.<br />
catch [arg] Break on events, for example exceptions that are thrown or caught<br />
(catch throw, catch catch).<br />
info [arg] Generic command for showing things about the program, for<br />
example information about breakpoints (info break).<br />
delete [nbr] Delete all breakpoints or breakpoint nbr.<br />
print expr Print the value of the expression expr.<br />
display var Print the value of variable var every time the program stops.<br />
undisplay [nbr] Delete all displays or display nbr.<br />
set var = expr Assign the value of expr to var.<br />
kill Terminate the debugger process.<br />
watch var Set a watchpoint, i.e., watch all modifications of a variable. This<br />
can be very slow but can be the best solution to find bugs when<br />
”random” pointers are used to write in the wrong memory location.