【问题标题】:How to make GDB substitute variables with their current value when a conditional breakpoint is created创建条件断点时如何使 GDB 用其当前值替换变量
【发布时间】:2010-12-28 16:41:06
【问题描述】:

我希望 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 经验,所以我想先发布这个问题!我确信应该可以做我想做的事情,任何帮助或建议都将不胜感激。

【问题讨论】:

    标签: gdb


    【解决方案1】:

    为了完整起见,这里是如何在我的原始示例中使用 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 生成两个断点!

    【讨论】:

      【解决方案2】:

      使用eval 命令(显然在 gdb 7.2 及更高版本中)

      【讨论】:

      • +1 谢谢,我需要这个。一个例子可以改善这个答案。
      猜你喜欢
      • 2017-10-30
      • 2022-07-28
      • 2010-12-16
      • 2021-07-14
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      相关资源
      最近更新 更多