简体   繁体   English

当变量从数组末尾更改时,gdb监视点将不起作用

[英]gdb watchpoint won't work when variable changes from going off end of array

#include <stdio.h>

typedef struct ThingStruct {
    int arr[8];
    int after;
} Thing;

void foo(int i) {
    Thing thing;
    int* ip = &thing.after;
    thing.after = 12345;
    printf("beforehand\n");
    thing.arr[i] = 55;
    printf("done\n");
}

int main() {
    foo(8);
}

This code changes thing.after by accidentally going off the end of the array. 这段代码通过不小心离开数组末尾而更改了thing.after I want to try to find the line where where thing.after is changing by using gdb. 我想尝试找到使用gdb更改thing.after的行。 So I compile with -g , put a breakpoint on line 12, then put a watchpoint on thing.after , but the watchpoint doesn't trigger, even though putting a breakpoint on line 14 does show that thing.after did change. 因此,我使用-g进行编译,在第12行上放置一个断点,然后在thing.after上放置一个观察点,但是即使在第14行上放置一个断点确实显示了那thing.after ,之后观察点也不会触发。

I even tried taking the address of thing.after and setting a watchpoint on that, but it still does not trigger. 我什至尝试使用thing.after的地址并thing.after设置观察点,但是它仍然不会触发。

Watch point needs to be re-added each time the foo function is entered (Note that, as you are watching the local variable, it will not be valid after the stack frame exits and will be automatically deleted after the foo returns). 每次输入foo函数时,都需要重新添加监视点(请注意,在监视局部变量时,它将在堆栈帧退出后无效,并在foo返回后自动删除)。 Also, if the watched variable changes on the current line to be executed, then the watch point is not getting triggered (not sure why). 同样,如果监视的变量在当前要执行的行上更改,则监视点不会被触发(不确定原因)。 For me it works when I add the watch point watch thing.after just after entering foo when on line int* ip = &thing.after; 对我来说,当我添加监视点watch thing.after时, int* ip = &thing.after;之后,在输入foo之后 . When I continue, the watch point hits 2 times. 当我继续时,监视点命中2次。

You didn't say which platform, what version of GDB, or what command you used to set the watchpoint. 您没有说使用哪个平台,什么版本的GDB或用来设置观察点的命令。

Using gdb 7.9 on Ubuntu/x86_64, things work as I expect them to work: 在Ubuntu / x86_64上使用gdb 7.9,事情按我预期的那样工作:

(gdb) b foo
Breakpoint 1 at 0x400538: file t.c, line 10.
(gdb) r
Starting program: /tmp/a.out 

Breakpoint 1, foo (i=8) at t.c:10
10          int* ip = &thing.after;
(gdb) watch thing.after
Hardware watchpoint 2: thing.after
(gdb) c
Continuing.
Hardware watchpoint 2: thing.after

Old value = 4195712
New value = 12345
foo (i=8) at t.c:12
12          printf("beforehand\n");
(gdb) c
Continuing.
beforehand
Hardware watchpoint 2: thing.after

Old value = 12345
New value = 55
foo (i=8) at t.c:14
14          printf("done\n");

(gdb) q (gdb)q

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM