gdb - 创建条件断点时如何使用 GDB 替换变量的当前值

标签 gdb

我希望 GDB 在创建条件断点时执行变量替换。例如:

set variable $my_value = 1
b my_function if my_param == $my_value
set variable $my_value = 5
b my_function if my_param == $my_value

这实际上创建了 2 个相同的断点,当 my_param 等于 $my_value 的当前值时,它们在 my_function() 中中断。因此,当运行我的程序时,只有当 my_param 等于 5 时才会触发断点。我真正想要的是两个不同的条件断点,值分别为 1 和 5。

有没有办法让 GDB 使用便利变量的当前值而不是变量本身来设置这样的条件断点?

我问这个问题是因为我正在尝试创建一个 GDB 脚本来跟踪内存释放,该脚本将自动设置条件断点,例如
# set breakpoint after malloc() statement of interest
b some_file.c:2238
# define commands to execute when the above breakpoint is hit
commands
# $last is set to the allocated memory address
set variable $last = new_pointer
# set conditional breakpoint in free() to check when allocated pointer is released
b free if ptr == $last
continue
end

但是当然我发现这仅适用于最后一个指针值,因为我所有自动生成的断点都是相同的!

我将调查 Python 脚本的使用,看看这是否可以解决我的问题,但由于我没有 Python 经验,我想先发布这个问题!我确信应该可以做我正在努力实现的目标,任何帮助或建议将不胜感激。

最佳答案

为了完整起见,这里是如何在我的原始示例中使用 eval 命令:

set variable $my_value = 1
eval "b my_function if my_param == %d", $my_value
set variable $my_value = 5
eval "b my_function if my_param == %d", $my_value

这会根据需要为值 1 和 5 生成两个断点!

关于gdb - 创建条件断点时如何使用 GDB 替换变量的当前值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4547623/

相关文章:

c++ - 在 GDB 中调试多线程服务器 - 查找每个线程的状态。执行时计数并停止

c++ - 在 GDB 中打印 int **x

gdb - 如何有条件地忽略GDB中的SIGTRAP?

linux - 如何反汇编系统调用?

linux - 最小核心转储(堆栈跟踪+仅限当前帧)

c - 如何通过 mips 中的其他进程获取正在运行的进程堆栈的回溯?

c - gcc -g 标志 : Moving the Source Code

c - 为什么 gdb 在 gcc 输出上工作不正确?

c++ - $fp == $rbp 在 gdb 中吗?

gdb - testb指令的含义是什么?