2010年11月18日 星期四

[gdb 命令] watch : 設定檢查點


語法 :
watch | A watch point stops execution of your program whenever the value of an expression changes.
watch lvalue | Set a write watchpoint on the specified expression.

底下是使用說明 :
This command sets a write watchpoint on the specified expression. When the debuggee writes to the memory location designated by lvalue, it stops.
The debugger does not detect writing if the value of memory is not changed.
Watchpoints are also referred to as data breakpoints.

使用範例 :
考慮代碼如下 :
- watchdemo.c :
  1. #include   
  2.   
  3. int main(void) {  
  4.         int i=10;  
  5.         i=1;  
  6.         i^=i;  
  7.         i=i;  
  8.         i=i;  
  9.         i=i;  
  10.         i+=10;  
  11.         i>>=1;  
  12.         return 0;  
  13. }  

接著編譯後使用 gdb mode 開啟程式 :
linux-tl0r:~/gdbDemo # gcc -g watchdemo.c -o watchdemo <編譯程式>
linux-tl0r:~/gdbDemo # gdb watchdemo <進入 gdb 模式>
(gdb) start <開始執行程式>
Temporary breakpoint 1, main () at watchdemo.c:4
4 int i=10;
(gdb) watch i <設定 watch, 監視變數 i>
Hardware watchpoint 2: i
(gdb) c <繼續執行>
Continuing.
Hardware watchpoint 2: i

Old value = 0
New value = 10
main () at watchdemo.c:5 <在第4行, i 值變動 0->10, 故停在第5行>
5 i=1;
(gdb) c <繼續執行>
Continuing.
Hardware watchpoint 2: i

Old value = 10
New value = 1
main () at watchdemo.c:6 <在第5行, i 值變動 10->1>
6 i^=i;
(gdb) c
Continuing.
Hardware watchpoint 2: i

Old value = 1
New value = 0
main () at watchdemo.c:10 <在第6行, i 值變動 1 -> 0>
10 i+=10;
(gdb) c
Continuing.
Hardware watchpoint 2: i

Old value = 0
New value = 10
main () at watchdemo.c:11 <在第10行, i 值變動 0 -> 10>
11 i>>=1;
(gdb) c
Continuing.
Hardware watchpoint 2: i

Old value = 10
New value = 5
main () at watchdemo.c:12 <在第 11 行, i 值變動 10 -> 5>
12 return 0;
(gdb) c
Continuing.

Watchpoint 2 deleted because the program has left the block in 

which its expression is valid.
0xb7e7eace in __libc_start_main () from /lib/libc.so.6
(gdb)
This message was edited 4 times. Last update was at 17/11/2010 13:28:27

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...