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

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

问题描述

我正在寻找一种在gdb中遇到特定断点时采取某些措施的方法.

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

基本上,我的程序中存在一些memleak.当malloc和free函数命中时,我需要进入函数(步骤)并收集一些基本信息,例如addr和size(基本上在此打印值).完成后,恢复我的程序.

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.

我们有什么好办法吗?

推荐答案

例如,以下是当x为正数时,如何使用断点命令在foo的入口处打印x的值.

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

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

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.

一个用于断点命令的应用程序是为了补偿一个错误,因此您可以测试另一个错误.在错误的代码行之后放置一个断点,为它提供条件以检测发生错误的情况,并为它提供命令以将正确的值分配给需要它们的任何变量.以continue命令结尾,以便您的程序不会停止,并以silent命令开头,以便不产生任何输出.这是一个示例:

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天全站免登陆