How to debug with gdb in five easy steps:

 

Step One:        Write program

Step Two:      Compile program, including –g command.

g++ -W –Wall –g testprogram.cc –o testprogram

Step Three:   Start debugger.

gdb testprogram

 

Step Four:   Run broken program.  Find errors.  Set breakpoints, rerun program to trace variables in problem areas.  Rinse. Repeat.

 

Step Five:   Promise yourself you will never rely on “When in doubt, cout!” to debug your programs again!

 

 


The details:

run

Executes your program

break

Set a breakpoint for the debugger in the code. 

break 7    // sets a break at line 7

break sort    // breaks at beginning of function sort( )

next

Executes next line of code, then stops

step

Steps into a function and executes first line.  Otherwise, ‘next’ would execute entire function and return without stopping.

print

At any point while the program is still running, you can type print and a variable name, and see the value of the variable.

print a

print myList[24]

continue

Will continue execution of program until another breakpoint, crash, or the end is reached, whichever comes first.

where

Spits out a backtrace through the stack.  Will show you which functions you have initiated but not completed.  If you don’t understand, play with it a little and the meaning will become clear.

set args

What you must type before ‘run’ if you need to pass arguments into your program.

set args circuitFile.txt

disable

Unsets a breakpoint.

disable 7 // unset break previously set at line 7.

watch

Similar to break, but you assign it to a variable, and the program will pause execution whenever that variable changes value.

watch andGateFive.outputA

quit

How you get out of the debugger.

help

If you really want to know more.