gdb教程:4. How do I use breakpoints?

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值