Today we’re going to take a quick look at the humble breakpoint in GDB. You can set a breakpoint:
- before you run the program in GDB
- if you interrupt GDB with CTRL-C
You can position your break points in multiple ways in GDB. Let’s look at each in turn.
By filename and line number
Set a breakpoint using the handy shortcut ‘b’ followed by the location. There are lots of ways to specify the location, but the most common way is filename and line number:
(gdb) b MessageSender.cpp:118
Pattern: filename:linenumber. Nice and simple. This Puts a breakpoint at line 118 in the file MessageSender.cpp
By line number of current source file
(gdb) b 267
You can also use the pattern: linenumber. This puts a breakpoint at line 267 in the current source file. If execution has not started, the breakpoint will be inserted in the main program file.
By function name
(gdb) b main
Pattern: functionname. Puts a breakpoint at the beginning of the named function. For C++ class methods, see below.
(gdb) b Message::sendMessage
By classname and method
Pattern: classname::method. Puts a breakpoint at the beginning of the named method. Use tab after the :: to display a list of available methods.
(gdb) b MessageParser.c:getLen
By filename and method
Pattern: filename:function. Puts a breakpoint at the beginning of the named function in the file specified (useful if there is any ambiguity caused by functions with the same name).
Viewing and deleting breakpoints
To see all your breakpoints in a numbered list, type:
(gdb) i b
Delete them all with:
(gdb) d
Or specifically with the breakpoint number from the list:
(gdb) d 4
Preserving your breakpoints
Don’t quit GDB. Just type CTRL-C and then ‘r’ (for run). This will restart your program from the beginning, leaving all your breakpoints intact. Lovely!
You can also save breakpoints for use between sessions.
