0

我想定义一个新命令,它基本上在一行上设置断点,打印某个变量的值,然后继续执行。不幸的是我遇到了问题。这是我正在使用的代码

(gdb) define print_and_continue
Type commands for definition of "print_and_continue".
End with a line saying just "end".
>break $arg0
>command $bpnum
 >print $arg1
 >continue
 >end
>end

所以我想打印len定义在linked_list.h:109. 我执行以下代码:

(gdb) print_and_continue linked_list.h:111 len
Breakpoint 1 at 0x388a: linked_list.h:111. (12 locations)
(gdb) r
...

Breakpoint 1, linked_list<test_struct<1>, 1>::remove_if<run_test<1, 1, 1>(std::vector<int, std::allocator<int> >&)::{lambda(test_struct<1> const&)#1}>(run_test<1, 1, 1>(std::vector<int, std::allocator<int> >&)::{lambda(test_struct<1> const&)#1}&&) (this=0x7fffffffdca0, condition=...) at linked_list.h:112
112         linked_list_node* prev = nullptr;
$1 = void

似乎$arg1in printfunction 没有被实际参数替换。我究竟做错了什么?

4

1 回答 1

1

似乎打印函数中的 $arg1 没有被实际参数替换。

我不相信那是实际发生的事情。相反,后面的所有内容command $bpnum都按字面意思附加到新创建的断点(根本没有任何扩展)。您可以看到发生了这种情况info break,这将显示如下内容:

Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000001136 at ...
        print $arg1
        continue

这通常是您想要的(推迟评估参数直到时间断点被击中)。否则,您将打印if 您使用的当前值,当您想要打印断点时的值时。lenprint lenlen

当然,当断点被命中时,周围任何地方都没有$arg1(或$arg0),因此您将获得与尝试打印任何其他不存在的 GDB 变量相同的输出。

我究竟做错了什么?

您正在使用“一种语言的快速破解”(这是“本机”GDB 脚本语言),而不是使用适当的编程语言。

我 99.99% 确定使用嵌入式 Pythonprint_and_continue进行定义是可能的(并且可能非常容易)。

就是说,我不认为这print_and_continue有什么用(在我使用 GDB 的 20 多年中,我从来不需要这样的东西)。

于 2020-06-15T03:10:56.293 回答