2011-06-24 23 views
4

我正在調試具有段錯誤的C應用程序。我已經確定了導致問​​題的變量。但是,我還不知道,什麼時候分配了變量,以致導致分段錯誤。GDB:指定斷點以更改符號

如果一個新值被分配給一個現有的變量,有沒有辦法在GDB中設置斷點?

回答

5
#include <stdio.h> 

struct foo { 
    int i[12]; 
    int j; 
}; 

int main(void) { 
    struct foo foo = {{0}}; 
    int *p; 

    p = foo.i; 
    p[12] = 42; 
    printf("j is %d\n", foo.j); 
    return 0; 
} 
 
gdb ./a.out 
[...] 
Reading symbols from a.out...done. 
(gdb) break main 
Breakpoint 1 at 0x40052c: file 6469109.c, line 9. 
(gdb) run 
Starting program: a.out 

Breakpoint 1, main() at 6469109.c:9 
9   struct foo foo = {{0}}; 
(gdb) watch foo.j 
Hardware watchpoint 2: foo.j 
(gdb) cont 
Continuing. 
Hardware watchpoint 2: foo.j 

Old value = -7936 
New value = 0 
0x0000000000400545 in main() at 6469109.c:9 
9   struct foo foo = {{0}}; 
(gdb) cont 
Continuing. 
Hardware watchpoint 2: foo.j 

Old value = 0 
New value = 42 
main() at 6469109.c:14 
14  printf("j is %d\n", foo.j); 
(gdb) quit 
A debugging session is active. 

     Inferior 1 [process 572] will be killed. 

Quit anyway? (y or n) y 
+0

+1:我喜歡看到'foo.j'通過的程序化誤差變化的例子。我希望我能+2這個。 – dolphy