Gdb notes

From raju


stack specific breakpoints in gdb

break if variable equals string

    (gdb) b prog.c:1474 if strcmp(a.c_str(), "kamaraju") == 0

If the breakpoint is already set, use condition command. For example

    (gdb) condi 3"kamarju") == 0

will set the condition on the 3rd breakpoint.

tag | conditional breakpoint

Run commands when a breakpoint is hit

(gdb) help commands
Set commands to be executed when a breakpoint is hit.
Give breakpoint number as argument after "commands".
With no argument, the targeted breakpoint is the last one set.
The commands themselves follow starting on the next line.
Type a line containing "end" to indicate the end of them.
Give "silent" as the first line to make the breakpoint silent;
then no output is printed when it is hit, except what the commands print.

(gdb) i b

(gdb) commands 3
Type commands for breakpoint(s) 3, one per line.
End with a line saying just "end".
>p myVar

(gdb) commands 18
Type commands for breakpoint(s) 18, one per line.
End with a line saying just "end".
>printf "%s\n",id.c_str()

start gdb and load commands from a file

Sample command file

% cat cmd.gdb
b MathFunctions.C:20
b sqrt
b cube
set args -x 5.1 -y 3.2

Sample run

gdb -q executable -x cmd.gdb

The -x option is documented in "gdb --help", but it is a bit hard to see at first glance.

% gdb --help | grep "\-x\>"
  --command=FILE, -x Execute GDB commands from FILE.

If gdb is already running, use source to run the commands in a file

(gdb) source cmd.gdb

See also:

Which file am I currently debugging?

info files

What is the gdb version I am running?

show version

Things to remember in the TUI mode

  • Switch cursor focus between SRC and CMD windows: ctrl-x o
  • Toggle the TUI mode: ctrl-x a
  • increase the window height

Get the name of window

(gdb) info win
        SRC     (39 lines)  <has focus>
        CMD     (19 lines)

Decrease the window height by 5 lines

(gdb) winheight SRC -5


print enum variables in gdb

This tip shows various ways of printing the values of enum variables in a gdb session. Consider

% cat print_enum.c
enum E {

int main() {
    enum E e = Val1;
    return e;

compile using

% g++ -g print_enum.c


% gdb ./a.out
Reading symbols from ./a.out...done.
(gdb) b 7
Breakpoint 1 at 0x400558: file print_enum.c, line 7.
(gdb) r
Breakpoint 1, main () at print_enum.c:7
7           enum E e = Val1;
(gdb) n
8           return e;

Let's try printing the variables

(gdb) p e
$2 = Val1

This is fine but printing Val1 does not give any useful information.

(gdb) p Val1
$1 = Val1

To print the underlying value, we can do one of the following

(gdb) p (int) Val1
$4 = 0
(gdb) p /d Val1
$5 = 0
(gdb) p Val1+0
$6 = 0

First print it and then use the "up arrow" key

(gdb) p Val1
$7 = Val1
(gdb) <UP>+0
$8 = 0

Omit the variable name, since the expression defaults to $.

(gdb) p Val1
$9 = Val1
(gdb) p /d
$10 = 0



color prompt

gdb issues

This issue is not present in Debian box running

$ gdb --version

GNU gdb (GDB) 7.6.2 (Debian 7.6.2-1)

$ g++ --version

g++ (Debian 4.9.0-7) 4.9.0

Tested it with

$ g++ -g -o foo foo.cpp

Afterwards test it on the Redhat machine.

set breakpoints using regex

Use rbreak as described in the gdb manual. For example, to set breakpoints in all the functions of a class, use

rbreak ^MyClassFoo::

To be sorted

TUI shortcomings

  • It is not possible to scroll back in the CMD window. So if there is lot of output, it is all lost. A work around is to use emacs gdb interface which allows scrolling back.


useful links