[英]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);
}
這段代碼通過不小心離開數組末尾而更改了thing.after
。 我想嘗試找到使用gdb更改thing.after
的行。 因此,我使用-g進行編譯,在第12行上放置一個斷點,然后在thing.after
上放置一個觀察點,但是即使在第14行上放置一個斷點確實顯示了那thing.after
,之后觀察點也不會觸發。
我什至嘗試使用thing.after
的地址並thing.after
設置觀察點,但是它仍然不會觸發。
每次輸入foo
函數時,都需要重新添加監視點(請注意,在監視局部變量時,它將在堆棧幀退出后無效,並在foo
返回后自動刪除)。 同樣,如果監視的變量在當前要執行的行上更改,則監視點不會被觸發(不確定原因)。 對我來說,當我添加監視點watch thing.after
時, int* ip = &thing.after;
之后,在輸入foo之后 。 當我繼續時,監視點命中2次。
您沒有說使用哪個平台,什么版本的GDB或用來設置觀察點的命令。
在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.