gdb - the C/C++ symbolic debugger.

 

GDB is a symbolic debugger that works well with code compiled with g++. It allows you to step through your program line by line and see the contents of variables. It can be quite complex but you only need to know a few commands for it to be useful.

Compiling

You must add a flag when compiling source code (-g). It is a good idea to always include the flag when compiling so that you can debug whenever you want. This is also important when you have several object files. Each one must be compiled with the -g oprtion or else you will not be able to debug any functions within that object file.

g++ -o program source.c -g

 

Finding core dumps

One major benefit of using gdb is when you get a core dump. Start gdb by including the program name and core file on the command line. GDB will show you what line the program aborted on!

gdb lab1 core

 

Looking at variables

The other benefits come from stepping thru the program line by line and seeing the contents of the variables. This is more convenient than placing print statements throughout the code. If you have a local variable called temp you would type: p temp to see the current value. However, if temp is a data member in a member function then you must type: p this->temp to print the contents of the variable.

 

Essential Commands

 gdb program [core]  start gdb with your program and optional core
 b function or line#  set a breakpoint at specified location
 l [-] # of lines  display source code for next several lines
 p expr  display the value of an expression such as a variable
 n  next line, stepping over function calls
 s  next line, stepping into function calls
 c  continue running the program
 run [arglist]  run the current program with optional command line arguments
 quit  exit from gdb

Finding Help

 help  list classes of commands
 help class  descriptions of commands in class
 help command  describe command


Home | Information | Academics | People | Resources | Contact

The Department of Computer Science at the University of Illinois at Springfield
Copyright© 2002 University of Illinois at Springfield