如何调试指针参数是否通过函数修改
How to debug whether a pointer argument was modified by a function?
我当前正在尝试分析大量指针输入的大型复杂功能的行为。考虑以下签名。
int myfunc(typeA *paramA, typeB *paramB);
被称为
myfunc(argA, argB);
是否可以通过调试器观看argA
和argB
的指针位置是否写入?还是只能观察内存位置是否改变(在我的情况下绝对不会发生(?
我想检查函数调用之前和之后这些指针参数的差异。这款手表可能吗?
请注意,通过这些类/结构的这些类别/结构是巨大的,在类/结构上有其他指针。因此,观看每个变量将是我的最后一个度假胜地
由于您已经用Clion标记了帖子,因此我认为这是您正在使用的IDE。您可能需要阅读这篇文章:https://blog.jetbrains.com/clion/2015/05/debug-clion/
特别是手表上的零件:
在每个点捕获每个变量会导致太多信息。有时,您需要关注特定变量及其在整个程序执行过程中的变化方式,包括当所涉及的变量不是您正在检查的代码本地的情况时监视更改。这是调试工具窗口的手表区域。
要开始观看变量,只需按添加按钮(alt insert(Windows/linux(/⌘n(OS X((,然后输入要观看的变量的名称。代码完成也可以在此处提供。
根据您的评论:您有选项可以查看记忆何时写入:我可以在GDB中的"内存访问"上设置一个断点?
否则,如果您只想知道是否更改了该值以进行调试,请在调用函数之前复制该值:
typeA copyOfA = *argA;
myfunc(©OfA, 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((
最初获得值的位置,然后之后的所有内容都将如上所述。
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 函数作为模板参数,是否对返回类型强制约束
- visual是否可以在c++中创建一个接收无限数量相同类型(或至少相当数量)参数的函数
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 函数是否可以访问传递给main()的参数
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 在这种情况下,java对象是否可以调用本机函数
- 检查函数返回类型是否与STL容器类型值相同
- 根据某个函数是否存在启用模板
- 在C++中,使用带有 std::optional 参数的函数<T>来表示可选参数是否有意义?
- 无论如何,我可以确定构造函数是否存在吗?
- 是否可以将函数导入命名空间,但不能导出它?
- 返回指向对象的指针的函数调用是否为 prvalue?
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 是否有任何建议来统一函数类型限定符并简化可恶的函数类型?
- 在函数范围内在堆栈上分配的数组在离开函数时是否总是被释放?
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本