2015-10-21 47 views
0

我知道當讀取/寫入expresssion時,GDB中的awatch命令會中斷。但是,爲什麼當我將awatch命令設置爲我的結構指針的地址時,它只停止了一次。以下是我的GDB和代碼片段的屏幕截圖。請指教。謝謝。awatch不停止在指定地址

enter image description here

Testing.c

College_Record *college = NULL; 
college = malloc(sizeof(College_Record)); 
printf("college %p\n", college); 
free(college); 
printf("college %p\n", college); 
college = NULL; 
printf("college %p\n", college); 

printf("***************************\n"); 
printf("\tDONE OKAY\n"); 
printf("***************************\n"); 

return 0; 

Testing.h

typedef struct { 
    int college_id; 
    char school[20]; 
} College_Record; 
+1

您已經刪除指針,因此可能是0x602010。或者,awatch -l 0x602010。簡單的方法可能是awatch -l * college –

+0

awatch -l * college說「表達式中的語法錯誤,靠近'{...} *)0x0000000000602010'。」另外,awatch 0x602010或awatch -l 0x602010表示「無法觀看常量值0x602010」。「 – Marss

回答

1

下面是一個示例程序:

struct college { 
    int x; 
    int y; 
}; 

struct college college_glob; 

struct college *college; 

void 
setwatch(void) 
{ 

    college = &college_glob; 
} 

void 
brkgdb(void) 
{ 
} 

int 
main(void) 
{ 

    setwatch(); 
    brkgdb(); 

    return college_glob.x; 
} 

這裏的GDB會話:

> gdb /tmp/watch 
GNU gdb (GDB) Fedora 7.9.1-19.fc22 
Copyright (C) 2015 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. Type "show copying" 
and "show warranty" for details. 
This GDB was configured as "x86_64-redhat-linux-gnu". 
Type "show configuration" for configuration details. 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>. 
Find the GDB manual and other documentation resources online at: 
<http://www.gnu.org/software/gdb/documentation/>. 
For help, type "help". 
Type "apropos word" to search for commands related to "word"... 
Reading symbols from /tmp/watch...done. 
(gdb) b brkgdb 
Breakpoint 1 at 0x40050c: file /tmp/watch.c, line 20. 
(gdb) run 
Starting program: /tmp/watch 

Breakpoint 1, brkgdb() at /tmp/watch.c:20 
20 } 
(gdb) awatch *college 
Hardware access (read/write) watchpoint 2: *college 
(gdb) c 
Continuing. 
Hardware access (read/write) watchpoint 2: *college 

Value = {x = 0, y = 0} 
Hardware access (read/write) watchpoint 2: *college 

Value = {x = 0, y = 0} 
main() at /tmp/watch.c:30 
30 } 
(gdb) list 
25 
26  setwatch(); 
27  brkgdb(); 
28 
29  return college_glob.x; 
30 } 
(gdb) q 
A debugging session is active. 

    Inferior 1 [process 15761] will be killed. 

Quit anyway? (y or n) y 
+0

感謝您的示例程序。 – Marss

+0

@Marss不客氣。每當我需要測試我從未使用過的某個功能(例如語言構造,調試cmd,庫調用等)時,我都會這樣做。創建一個小型的測試/診斷程序,只需做一件事。我創建了什麼_guaranteed_來引用glob。在你的程序中,你有兩個問題:(1)是否有工作? (2)程序實際上是否參考內存?測試程序只有(1)與(通過設計)抗衡。我骯髒的小祕密:甚至之後,我不得不嘗試_several_ [未顯示;-)] awatch的變體來找到它。 –