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
Positioning your breakpoints
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 are:
(gdb) b MessageSender.cpp:118
Pattern: filename:linenumber. Nice and simple. Puts a breakpoint at line 118 in the file MessageSender.cpp
(gdb) b 267
Pattern: linenumber. 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.
(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
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
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
Preserve 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!