gpt4 book ai didi

c - 在不停止执行的情况下观察 gdb 中的局部变量

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:23 25 4
gpt4 key购买 nike

我试图让 GDB 在变量发生变化时打印它的值。

给定一个示例程序,我想在 func 发生变化时获取 x 的值,但是为了让程序在没有提示的情况下继续:

#include <stdio.h>
#include <stdlib.h>

int func(int x, int y);

int main(void) {

int x = 5;
int y;

y = func(x, 4);

printf("%d\n", x);
printf("%d\n", y);
return EXIT_SUCCESS;
}

int func(int x, int y) {
y *= 2;
x += y;
return x;
}

我尝试过的:

break func
commands
silent
watch x
commands
continue
end
continue
end

虽然这会在 x 改变时成功获取值,但问题是当离开 x 的范围时,gdb 会停止通知我它是离开 x 的范围,它正在删除观察点。有什么方法可以将 GDB 设置为在自动删除观察点时继续执行并在没有用户提示的情况下继续执行?

我遇到了这个问题:gdb: do not break when watchpoint on local variable goes out of scope然而,它从未收到解决方案。

最佳答案

你可以给 gdb 的 watch 命令 -l 选项,当变量超出范围时,watchpoint 不会被删除(也不会停止执行)。

但是使用这种类型的观察点,gdb 将获取其他函数对堆栈上同一地址所做的更改。因此,您可以将条件 if $_caller_is("func", 0) 添加到观察点,这样 gdb 只会在变量在 func 内发生变化时通知您。

(gdb) list func18      int func(int x, int y) {19         y *= 2;20         x += y;21         return x;22      }(gdb) b funcBreakpoint 1 at 0x400580: file s2.c, line 19.(gdb) set $funcbp = $bpnum(gdb) commandsType commands for breakpoint(s) 1, one per line.End with a line saying just "end".># We can only set a watchpoint on a local var># when it's visible, so we'll set it on entry to func.># But we don't want to set it more than once># if func is called more than once,># so we disable the func breakpoint on first use.>disable $funcbp>watch -l x if $_caller_is("func", 0)>commands >continue >end>continue>end(gdb) rStarting program: /home/mp/s2Breakpoint 1, func (x=5, y=4) at s2.c:1919         y *= 2;Hardware watchpoint 2: -location xHardware watchpoint 2: -location xOld value = 5New value = 13func (x=13, y=8) at s2.c:2121         return x;513[Inferior 1 (process 29495) exited normally]

关于c - 在不停止执行的情况下观察 gdb 中的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53173854/

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