Monday, April 16, 2012

Gdb(GNU Debugger)



A debugger for several languages, including C and C++. It allows you to inspect what the program is doing at a certain point during execution.Errors like segmentation faults may be easier to find with the help of gdb.

You need to build program with -g option to enable built-in debugging support.
gcc [other flags] -g <source files> -o <output file>
  
gdb commands
  • getting help - you can get help of gdb commands
    (gdb)help CommandName
    (gdb)help
    will list classes of commands.
  • Invoking gdb -Once your executable has been generated by the compiler, it is run under the control of gdb by typing.
    >gdb program 
    if you have not specified filename with gdb you can specify later using command file.
    (gdb) file program
    where program is the name of the executable. it loads the debugging information of this file.
    You can also start with both an executable program and a core file
    >gdb program core
    You can, instead, specify a process ID as a second argument, if you want to debug a running process:
    >gdb program 1234 
  • Quiting gdb - you can exit gdb by using quit command.
    (gdb)quit
  • Running the program - When gdb starts, your program is not actually running. It won't run until you tell gdb how to run it.To run the program, just use:
    (gdb) run
    if there is no issue it runs normally, else you (should) get some useful information like the line number where it crashed, and parameters to the function that caused the error.
  • Setting breakpoints - Breakpoints can be used to stop the program run in the middle, at a some point. you can use “break” command.
    (gdb) break prog1.c:6
    sets a breakpoint at line 6, of prog1.c.
    You can also tell gdb to break at a particular function.Suppose you have a function my func:
    int func(int a);
    you can apply breakpoint to this function using command
    (gdb) break func
    Once you’ve set a breakpoint, you can try using the run command again. This time, it should stop where you placed breakpoint.
  • You can proceed onto the next breakpoint by typing “continue”. because typing run will restart the program from beginnning.
    (gdb) continue
  • You can single-step (execute just the next line of code) by typing “step.”
    (gdb) step
  • Similar to “step,” the “next” command single-steps as well, except this one doesn’t execute each line of a sub-routine, it just treats it as one instruction.
    (gdb) next
  • Removing breakpoints - you can use delete command to remove breakpoint.
    (gdb) delete N
    deletes Nth breakpoint. you can get that using command
    (gdb)info break.
  • Passing arguments - you can specify arguments using set args command.
  • Examining variables - The print command prints the value of the specified variables.
    (gdb) print var
  • backtrace - produces a stack trace of the function calls that lead to a seg fault.
    (gdb)backtrace
For quick reference of gdb commands refer link http://users.ece.utexas.edu/~adnan/gdb-refcard.pdf

No comments:

Post a Comment