Resources

gdb Debugging Tool on matrix



Most operating systems come with a debugging tool.  Many versions of Linux include gdb, the GNU project debugger. 

The gdb shell command can debug and run C programs, amongst others.


Compile and Run

To use gdb, first compile your program with the -g option


 cc -g myProgram.c

This will produce an executable called a.out that can be debugged.  To debug your executable, enter


 gdb a.out

This will display an introductory message followed by the gdb prompt


 (gdb) 

Type your gdb commands at this prompt.


Commands

The gdb commands include:

  • list - lists the 10 lines of source code in the vicinity of where execution has stopped.  Each call advances the current line by 10
  • list m, n - where m and n are line numbers - lists lines m through n inclusive of the source code.  This call advances the current line to n+1
  • break n - where n is a line number - sets a break point at line number n
  • clear n - where n is a line number - clears any break point or trace at line number n
  • delete - clears all breakpoints
  • run - starts the execution of your program from line 1
  • print varname - where varname is a variable name - displays the value of the variable varname
  • cont - continues execution until either your program ends or a breakpoint is encountered
  • step - executes one line of your program
  • help - displays the full set of commands available
  • quit - quits

gdb, like C, is case sensitive.

When you first start gdb, your program pauses.  This is your first opportunity to set breakpoints.  Enter the run command only once you are ready to start execution.


Crashes

If your program fails and produces a core dump, gdb can help you find out where your program crashed.  Enter


 gdb a.out core 

The following commands help to investigate crashes

  • where - displays the function and line number where your program was executing at the time of the crash
  • up - moves up one function in the stack (towards main)
  • down - moves down one function in the stack (away from main)


Help

For online help with a particular command while debugging, enter


 help command

at the gdb prompt, where command is the command in question.

For online help with the gdb command while not debugging, enter


 man gdb

at the matrix prompt.




   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site   

Creative Commons License