在GDB中发生特定断点时如何执行特定操作? [英] How to do an specific action when certain breakpoint is hit in GDB?

查看:233
本文介绍了在GDB中发生特定断点时如何执行特定操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在gdb中遇到特定中断时执行某些操作的方法。

基本上我的程序中有一些memleak。当malloc和free函数命中时,我需要进入函数(step)并且收集一些基本信息,比如addr和size(基本上打印那里的值)。一旦完成恢复我的程序。



我们有什么好的方法可以做到这一点? / div>

例如,下面是当x为正数时,如何使用断点命令打印输入为foo的x值。

 如果x> 0 
命令
沉默
printfx是%d \ n,x
cont
end

如果您在命令列表中指定的第一个命令是 silent ,不会打印关于在断点处停止的常见消息。这对于打印特定消息然后继续的断点可能是理想的。如果其余命令都没有打印任何内容,则不会看到已达到断点的迹象。只有在断点命令列表开始时,silent才有意义。



断点命令的一个应用是补偿一个错误,以便测试另一个错误。在错误的代码行之后放置一个断点,给它一个条件来检测错误已经发生的情况,并且给它命令以将正确的值赋给任何需要它们的变量。以continue命令结束,以便程序不会停止,并从silent命令开始,以便不产生输出。这里有一个例子:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' 4
cont
end


I am looking for a way to do some action when a particular break point hits in gdb.

Basically I have some memleak in my program. When malloc and free function hits, I need to enter into the function (step) and collect some basic information like the addr and size (basically print there values). Once done resume my program.

Do we have any good way to do this?

解决方案

For example, here is how you could use breakpoint commands to print the value of x at entry to foo whenever x is positive.

break foo if x>0
commands
silent
printf "x is %d\n",x
cont
end

If the first command you specify in a command list is silent, the usual message about stopping at a breakpoint is not printed. This may be desirable for breakpoints that are to print a specific message and then continue. If none of the remaining commands print anything, you see no sign that the breakpoint was reached. silent is meaningful only at the beginning of a breakpoint command list.

One application for breakpoint commands is to compensate for one bug so you can test for another. Put a breakpoint just after the erroneous line of code, give it a condition to detect the case in which something erroneous has been done, and give it commands to assign correct values to any variables that need them. End with the continue command so that your program does not stop, and start with the silent command so that no output is produced. Here is an example:

break 403
commands
silent
set x = y + 4
cont
end

这篇关于在GDB中发生特定断点时如何执行特定操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆