How to use gdb
From ODF::Wiki
Using gdb can save coders a lot of time finding bugs. Here is a very short 5-minute-introduction to show you how to use it.
Contents |
[edit] Getting started
[edit] Notice about compiling
You should note that it is recommended to compile your programs with the -g or (especially for the gdb compiler) with the -ggdb flag.
[edit] Starting it
Usually, you would start a program with parameters on the commandline. Using gdb, the programs name should
- either not be passed when starting gdb (but passed when you will type run)
- or be passed using gdb [gdb-options] --args program arguments
As an example, "./doh -f readme" would be written
- either as gdb ./doh ... run -f readme
- or as gdb --args ./doh -f readme
[edit] The prompt
After having started gdb, you will see a prompt, like
(gdb)
You will be able to type commands here. Whenever you will type quit, you will close gdb.
[edit] Commands
[edit] help
Type "help <command>" to show help about the desired command.
[edit] run
The probably most important command is run. It will start the program as you are used to, and it will only stop if the program stops usually - or if it hits a breakpoint. Breakpoints are points where the execution will be paused to let you display some information. See the break command for more information.
Note that you can run a program again after it has stopped without restarting gdb. Also note that, if you have not specified the arguments on starting gdb, you can do it now by typing
run <arguments>
For accessing files in the argument list, you can usually use your tab key as if you were in a shell:
./doh -f re<TAB> could result in ./doh -f readme
[edit] break
The break command lets you set a breakpoint at a specific line in the source code. Breakpoints can either be specified by giving a function name or a file and a line number.
(gdb) break tarfile.h:60 (gdb) break odf::TarFile::TarFile(char, char const*)
Those could also be achieved using the tab completion:
(gdb) break tarf<TAB>:60 (gdb) break odf::Ta<TAB>::T<TAB>
Note that you sometimes need to specify a namespace, though.
[edit] step
Once you arrived at a breakpoint, you can step every line of sourcecode. That way, you can easily watch which way your command goes. Watch this:
(gdb) run
Starting program: /home/johannes/cprogs/ODF/doh/doh -f readme
DOH - The better HOG-utility!
:::::::::::::::::::::::::::::
Version 1.0
Breakpoint 1, main (argc=3, argv=0xbfffefc4) at ./src/main.cpp:61
61 if(!strcmp(archive_name,"")) {
(gdb) step
67 switch( what )
(gdb) step
111 exit(0);
(gdb)
[edit] print
With the print command, you can print variables or even the results of functions. E.g. print what or print w<TAB> might print you the results of the variable what. Note that sometimes, you will need to prepend namespaces and classnames.
[edit] finish
If you are stepping and arriving in an stl function, like, e.g. vector::push_back(), you might not want to watch what's happening there. Try the finish command to skip that output.
[edit] continue
The continue command will let the program run on from the point you have stepped to. If you want to run from the begining, try the run command instead.
[edit] Backtrace
If your program is hanging somewhere, you can press Ctrl+C. Afterwards you can still debug.
In such situations, it is often useful to see where your program currently is. You can use the bt command to make a backtrace:
bt
If you have multiple threads, you can view a backtrace of each thread using
thread apply all bt
[edit] Discovering gdb
Most commands can be used with single letters:
- help - h
- quit - q
- run - r
- break - b
- step - s
- print - p
- finish - f
- continue - c
There are lots of interesting programs using gdb. Try, e.g.
- watch
- display
- ...
For more information, type "man gdb" on a linux shell, or visit The gdb homepage

