Using gdb, the Gnu Debugger
- Compile your program with the -g option (gcc -g file.c)
- The executable and the source should be in the same directory. To start
the debugger
gdb executable for example gdb a.out
- Most commands can be invoked by their first letter; you do not need to
type the entire word.
- Before running the program, set one or more breakpoints with the
break command. These can be set either at line numbers or at the
start of functions. For example
b 136 | break at line 136, or |
b fctnOne | breaks at the start of fctnOne
|
Here are some more useful commands.
- run
- starts running the program. This will run to the first breakpoint. You
can pass arguments to the run command; this passes arguments to your
program. For example
r arg1 arg2
- print
- displays the value of a variable or expression
- display
-
- Display the value of a variable or expression at each step
- step
- steps through the program a line at a time, stepping into functions.
- next
- steps through the program a line at a time, stepping over functions
- finish
- run to the end of the current function (useful if you step into a function by mistake)
- continue
- runs to the next breakpoint
- list
- shows the source code
- quit
- terminates the debugger
- help
- gets more information