C 在gdb中监视局部变量而不停止执行
我试图让GDB在变量改变时打印变量的值 给定一个示例程序,我希望在C 在gdb中监视局部变量而不停止执行,c,gdb,watchpoint,C,Gdb,Watchpoint,我试图让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)
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;
}
当它改变时,这将成功获取x
的值,问题是当离开x
的范围时,gdb将停止通知我它正在离开x
的范围,并且它正在删除观察点。有没有办法设置GDB在自动删除观察点时,在没有用户提示的情况下继续执行
我遇到了一个问题:
然而,它从未得到解决方案
有没有办法设置GDB在自动删除观察点时,在没有用户提示的情况下继续执行
没有
但是,您可以在返回时添加断点,并将命令附加到该断点以删除监视点并继续。这样,GDB就不会自动删除任何活动的观察点,因此当函数返回时,它也不会停止。您可以为GDB的
watch
命令提供-l
选项,并且当变量超出范围时,观察点不会被删除(也不会停止执行)
但是使用这种类型的观察点,gdb将接收其他函数对堆栈上相同地址所做的更改。因此,如果$\u caller\u为(“func”,0),您可以将限定条件添加到监视点,这样gdb只会在func
中的变量发生变化时通知您
(gdb) list func
18 int func(int x, int y) {
19 y *= 2;
20 x += y;
21 return x;
22 }
(gdb) b func
Breakpoint 1 at 0x400580: file s2.c, line 19.
(gdb) set $funcbp = $bpnum
(gdb) commands
Type 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) r
Starting program: /home/mp/s2
Breakpoint 1, func (x=5, y=4) at s2.c:19
19 y *= 2;
Hardware watchpoint 2: -location x
Hardware watchpoint 2: -location x
Old value = 5
New value = 13
func (x=13, y=8) at s2.c:21
21 return x;
5
13
[Inferior 1 (process 29495) exited normally]
(gdb)列表函数
18整数函数(整数x,整数y){
19 y*=2;
20x+=y;
21返回x;
22 }
(gdb)b func
0x400580处的断点1:文件s2.c,第19行。
(gdb)设置$funcbp=$bpnum
(gdb)命令
键入断点1的命令,每行一个。
用一句话结束,只说“结束”。
>#我们只能在本地变量上设置观察点
>#当它可见时,我们将在条目中将其设置为func。
>#但我们不想设置多次
>#如果多次调用func,
>#因此,我们在第一次使用时禁用func断点。
>禁用$funcbp
>如果$\u caller\u为(“func”,0),则监视-lx
>命令
>继续
>结束
>继续
>结束
(gdb)r
启动程序:/home/mp/s2
s2处的断点1,func(x=5,y=4)。c:19
19 y*=2;
硬件观察点2:-位置x
硬件观察点2:-位置x
旧值=5
新值=13
s2处的func(x=13,y=8)。c:21
21返回x;
5.
13
[次1(进程29495)正常退出]
你不想在一个完美的世界里使用printf
?@FiddlingBits,是的,那将是理想的。然而,令人讨厌的现实世界有时会出现,并带来不太理想的约束。我理解为什么人们会认为这是一个可能的XY问题,但我更希望在这种情况下,如果我们只是从表面上看,有必要使用GDB来观察变量,最好不要停止程序来提示用户输入。这种方法的问题是GDB在返回时也没有任何内置中断。我看到了你制定返程休息的解决方案,但它需要一些手动工作来找到返程点,这并不理想。我希望能够做一些类似于gdb-x attach
的事情,并让它在使用不同版本的代码编译的二进制文件上工作。
(gdb) list func
18 int func(int x, int y) {
19 y *= 2;
20 x += y;
21 return x;
22 }
(gdb) b func
Breakpoint 1 at 0x400580: file s2.c, line 19.
(gdb) set $funcbp = $bpnum
(gdb) commands
Type 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) r
Starting program: /home/mp/s2
Breakpoint 1, func (x=5, y=4) at s2.c:19
19 y *= 2;
Hardware watchpoint 2: -location x
Hardware watchpoint 2: -location x
Old value = 5
New value = 13
func (x=13, y=8) at s2.c:21
21 return x;
5
13
[Inferior 1 (process 29495) exited normally]