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

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

问题描述

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