如何调试指针参数是否通过函数修改

How to debug whether a pointer argument was modified by a function?

本文关键字:是否 函数 修改 参数 指针 何调试 调试      更新时间:2023-10-16

我当前正在尝试分析大量指针输入的大型复杂功能的行为。考虑以下签名。

int myfunc(typeA *paramA, typeB *paramB);

被称为

myfunc(argA, argB);

是否可以通过调试器观看argAargB的指针位置是否写入?还是只能观察内存位置是否改变(在我的情况下绝对不会发生(?

我想检查函数调用之前和之后这些指针参数的差异。这款手表可能吗?

请注意,通过这些类/结构的这些类别/结构是巨大的,在类/结构上有其他指针。因此,观看每个变量将是我的最后一个度假胜地

由于您已经用Clion标记了帖子,因此我认为这是您正在使用的IDE。您可能需要阅读这篇文章:https://blog.jetbrains.com/clion/2015/05/debug-clion/

特别是手表上的零件:

在每个点捕获每个变量会导致太多信息。有时,您需要关注特定变量及其在整个程序执行过程中的变化方式,包括当所涉及的变量不是您正在检查的代码本地的情况时监视更改。这是调试工具窗口的手表区域。

要开始观看变量,只需按添加按钮(alt insert(Windows/linux(/⌘n(OS X((,然后输入要观看的变量的名称。代码完成也可以在此处提供。

根据您的评论:您有选项可以查看记忆何时写入:我可以在GDB中的"内存访问"上设置一个断点?

否则,如果您只想知道是否更改了该值以进行调试,请在调用函数之前复制该值:

typeA copyOfA = *argA;
myfunc(&copyOfA, argB);
if (copyOfA != *argA)
{
    // It's changed
}

不确定我是否会准确地得到您的问题,我不知道Clion是否可以让您访问LLDB脚本解释器,但是给出了此示例:

struct Foo
{
  int a;
  int b;
  int c;
};
void ChangeFoo(struct Foo *input)
{
  input->a += 10;
}
int
main()
{
  struct Foo my_foo = {10, 20, 30};
  ChangeFoo(&my_foo);
  return 0;
}

从命令行LLDB您可以做:

(lldb) br s -l 17
Breakpoint 1: where = tryme`main + 39 at tryme.c:17, address = 0x0000000100000f97
(lldb) br s -l 18
Breakpoint 2: where = tryme`main + 46 at tryme.c:18, address = 0x0000000100000f9e
(lldb) run
Process 16017 launched: '/tmp/tryme' (x86_64)
Process 16017 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100000f97 tryme`main at tryme.c:17
   14   main()
   15   {
   16     struct Foo my_foo = {10, 20, 30};
-> 17     ChangeFoo(&my_foo);
          ^
   18     return 0;
   19   }
Target 0: (tryme) stopped.
(lldb) script value = lldb.frame.FindVariable("my_foo")
(lldb) script print value
(Foo) my_foo = {
  a = 10
  b = 20
  c = 30
}
(lldb) n
Process 16017 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x0000000100000f9e tryme`main at tryme.c:18
   15   {
   16     struct Foo my_foo = {10, 20, 30};
   17     ChangeFoo(&my_foo);
-> 18     return 0;
          ^
   19   }
Target 0: (tryme) stopped.
(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> for i in range(0,value.GetNumChildren()):
...     print(i, " ", value.GetChildAtIndex(i).GetValueDidChange())
... 
(0, ' ', True)
(1, ' ', False)
(2, ' ', False)
>>> print value.GetChildAtIndex(0)
(int) a = 20

注意,如果上面的my_foo是指针,我们只会提取您不想比较的指针值。在这种情况下,当您捕获值时,请执行:

(lldb(脚本值= lldb.frame.findvariable(" my_foo_ptr"(。dereference((

最初获得值的位置,然后之后的所有内容都将如上所述。

相关文章: