gpt4 book ai didi

debugging - GDB:如何在函数返回后强制不删除观察点?

转载 作者:行者123 更新时间:2023-12-04 23:49:35 25 4
gpt4 key购买 nike

函数局部变量上的观察点通常在函数返回时被删除,并显示一条消息“观察点 7 已删除,因为程序已将块留在其中”。插图:

struct mystruct{
int a, b, c;
};

void MyFunc(){
mystruct obj;
obj.a = 2;
}

int main(){
MyFunc();
}

gdb session 示例
(gdb) b 7
Breakpoint 1 at 0x4004f1: file /tmp/test2.cpp, line 7.
(gdb) r
Starting program: /tmp/test2

Breakpoint 1, MyFunc () at /tmp/test2.cpp:7
7 obj.a = 2;
(gdb) wa obj
Hardware watchpoint 2: obj
(gdb) c
Continuing.
Hardware watchpoint 2: obj

Old value = {a = 4195600, b = 0, c = 4195328}
New value = {a = 2, b = 0, c = 4195328}
MyFunc () at /tmp/test2.cpp:8
8 }
(gdb) c
Continuing.

Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
main () at /tmp/test2.cpp:12
12 }

我试着像 wa *(mystruct *)&obj 一样转换它和 wa *(mystruct *)(void*)&obj ,无济于事。

我需要它,因为我正在使用的嵌入式 ARM 设备上的 GDB 坏了:有时它会无缘无故地删除一个观察点;回溯看起来像标有“??”的行标志,以及有关损坏堆栈的消息。即使应用程序实际上很好。

最佳答案

GDB: Setting Watchpoints说,

GDB automatically deletes watchpoints that watch local (automatic) variables, or expressions that involve such variables, when they go out of scope, that is, when the execution leaves the block in which these variables were defined.



但是,从 7.3 版开始(感谢 @Hi-Angel 和 IRC 上的用户群指出这一点;我没有在文档中看到它), watch命令接受 -location争论:

Ordinarily a watchpoint respects the scope of variables in expr (see below). The -location argument tells GDB to instead watch the memory referred to by expr. In this case, GDB will evaluate expr, take the address of the result, and watch the memory at that address. The type of the result is used to determine the size of the watched memory.



在旧版本的 GDB 上,您可以使用问题中的示例运行它:
eval "watch *(mystruct *)%p", &obj

请注意,如果您正在查看的内存被另一个函数的局部变量重用,则查看堆栈上的位置可能会导致虚假通知。

作为替代方案,您可以在不断进出范围的自动变量上自动设置观察点。在范围内的某个点设置断点 - 例如,在声明它的函数或块的开头 - 然后附加一个 watchcontinue命令:
(gdb) break MyFunc
(gdb) commands $bpnum
>watch obj
>continue
>end

关于debugging - GDB:如何在函数返回后强制不删除观察点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25938830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com