Breakpoints are a way of telling gdb that you want it to stop your program at certain lines of code. You can also have it stop when your program makes specific function calls. Once the program is stopped, you can poke around in memory and see what the values of all your variables are, examine the stack, and step through your program's execution.
4.1 How do I set a breakpoint on a line?
The command to set a breakpoint is break. If you only have one source file, you can set a breakpoint like so:
(gdb) break 19 Breakpoint 1 at 0x80483f8: file test.c, line 19
If you have more than one file, you must give the break command a filename as well:
(gdb) break test.c:19 Breakpoint 2 at 0x80483f8: file test.c, line 19
4.2 How do I set a breakpoint on a C function?
To set a breakpoint on a C function, pass it's name to break.
(gdb) break func1 Breakpoint 3 at 0x80483ca: file test.c, line 10
4.3 How do I set a breakpoint on a C++ function?
Setting a breakpoint on a C++ function is similar to setting a breakpoint on a C function. However C++ is polymorphic, so you must tell break which version of the function you want to break on (even if there is only one). To do this, you tell it the list of argument types.
(gdb) break TestClass::testFunc(int) Breakpoint 1 at 0x80485b2: file cpptest.cpp, line 16.
4.4 How do I set a temporary breakpoint?
Use the tbreak command instead of break. A temporary breakpoint only stops the program once, and is then removed.
4.5 How do I get a list of breakpoints?
Use the info breakpoints command.
(gdb) info breakpoints Num Type Disp Enb Address What 2 breakpoint keep y 0x080483c3 in func2 at test.c:5 3 breakpoint keep y 0x080483da in func1 at test.c:10
4.6 How do I disable breakpoints?
Use the disable command. Pass the number of the breakpoint you wish to disable as an argument to this command. You can find the breakpoint number in the list of breakpoints, as shown above. In the example below we can see that breakpoint number 2 has been disabled (there is an 'n' under the Enb column).
(gdb) disable 2 (gdb) info breakpoints Num Type Disp Enb Address What 2 breakpoint keep n 0x080483c3 in func2 at test.c:5 3 breakpoint keep y 0x080483da in func1 at test.c:10
4.7 How do I skip breakpoints?
To skip a breakpoint a certain number of times, we use the ignore command. Theignore command takes two arguments: the breakpoint number to skip, and the number of times to skip it.
(gdb) ignore 2 5 Will ignore next 5 crossings of breakpoint 2.