next up previous contents
Next: Debugging Programs with Dynamic Up: Using the gdb Debugger Previous: Command Summary   Contents

Example: Debugging the Zoo Program

Although it is trivial, there is enough in our Zoo program to try out the debugger. If you want to follow along, then build the program from scratch with:-

g++ -g -o Zoo.exe *.cpp Zoo.cc

and then run the debugger:-

gdb Zoo.exe

Set a breakpoint at the start of the program and start running the program:-

  break main
  run

when the debugger stops it will show you the current line that it is about to execute. Type

list

to look at the lines about the source line. Now type

next

repeatedly (actually you only need to type it once - after that just press RETURN to repeat the last command) to watch the top level program execution.

When it finishes type:-

run

again. The debugger will ask for confirmation as there is already a program running so type 'y' to confirm. This time, when it stops at the start of the program type:-

  break Human::Talk
  cont

This sets a breakpoint at the start of the function Human::Talk and then continues execution until this breakpoint is reached. Type:-

info stack

to see the function calls that lead to this point. Now type:-

print this

to look at the current object pointer. It will just tell that it is a pointer to Human. If you want to print out the contents of the current object type:-

print *this

In this case the output looks rather complicated because the Human object contains some complicated STL string objects and they are get printed too.

To look at something simpler, try:-

print fAge

to check on the Human's age and finally:-

  set fAge = 50
  cont

to modify the age and resume execution. Now type:-

quit

to leave the debugger.


next up previous contents
Next: Debugging Programs with Dynamic Up: Using the gdb Debugger Previous: Command Summary   Contents
P.D. Gronbech (IT Staff) 2015-10-02