gdb

the Gnu DeBugger

  1. In order to prepare your code for use in gdb, it must be compiled with the '-g' flag.
  2. set breakpoints
    • break prog.cpp:100
      • Set break point at line 100 in 'prog.cpp'.
    • break swap
      • Set breakpoint at the swap function.
    • break foo_object::foo_method
      • Set a breakpoint on the foo_method in the foo_object.
  3. set watchpoints
      • watch i
        • Will cause the program to stop if i changes.
      • awatch i
        • Will cause the program to stop if i is accessed ( read or write ).
      • watch foo_instance.foo_method
        • Will stop the first time foo_data in the instance foo_instance is changed.
      • watch foo_object::foo_data
        • Will stop the first time foo_data in any instance of foo_object is changed. This will clear the watchpoint after stopping.
  4. run
    1. Type 'run' followed by any arguements or redirection you would normally use in your program.
  5. list
    1. List the source code at your current location in the program.
  6. print values
    • print i
      • Print the value of the variable 'i'.
    • print num[i]
      • Print the value of the variable 'num[i]'.
    • print strcmp( str1, str2 )
      • Print the return value of the strcmp( str1, str2 ) function call. This can be used to determine values of any function.
    • print num1 < num2
      • This can be used to determine values of any expression.
  7. continue, step, next, and finish
      • continue - c
        • Run the program until completion, or the next breakpoint.
      • step - s
        • Advance to the next source line. This will go into functions and treat each line there as another line. 'step 10' will advance 10 lines.
      • next - n
        • This is similar to step, but it will not go into a function. 'next 10' will advance 10 lines.
      • finish - f
        • This will run until the current stack frame returns. The value returned is printed.
  8. clear breakpoints
      • clear
        • Clear the breakpoint you are currently at.
      • del
        • Delete all breakpoints, this will ask for confirmation.
      • info breakpoints
        • Shows the breakpoints that are set and thier id numbers.
      • disable i
        • Disables the breakpoint by id number.

notes:

  • Hitting enter with no command will repeat the last command.
  • Most commands have one letter abbreviations ( n,s,c for next, step, continue for example ).
  • The up arrow will recall old commands, just like bash or tcsh.
  • There is help available, just type 'help', or if you want help on a specific command like break, type 'help break'.
  • If you want to set certain breakpoints and watches for a project, you can put the gdb commands in a file and start gdb with "-x filename". This will run those commands just like they were typed on the console.
  • If you every run into problems with the namemangling, "nm" will show the C style names for your class methods.

Other References