next up previous contents
Next: Strip 程式執行檔瘦身 Up: 除錯器 gdb Previous: 除錯器 gdb 工具說明   Contents

除錯器 gdb 練習

  1. gdb 工具 display,執行每一步驟都看變數 b 的值。
    [dywang@dyH gcc]$ gdb -q gdbtest1
    Reading symbols from gdbtest1...done.
    (gdb) l
    1	int main()
    2	{
    3		int a, b, c;
    4		a=5;
    5		b=10;
    6		b+=a;
    7		c=b+a;
    8		return 0;
    9	}
    (gdb) b 2
    Breakpoint 1 at 0x400478: file gdbtest1.c, line 2.
    (gdb) r
    Starting program: gdbtest1 
    
    Breakpoint 1, main () at gdbtest1.c:4
    4		a=5;
    (gdb) display b
    1: b = 0
    (gdb) n
    5		b=10;
    1: b = 0
    (gdb) n
    6		b+=a;
    1: b = 10
    (gdb) n
    7		c=b+a;
    1: b = 15
    (gdb) n
    8		return 0;
    1: b = 15
    (gdb) n
    9	}
    1: b = 15
    (gdb) c
    Continuing.
    
    Program exited normally.
    (gdb) q
    
  2. 重新執行 gdb 工具,使用指令 info 查看狀況,目前沒有變數 display,有一個中斷點。
    [dywang@dyH gcc]$ gdb -q gdbtest1
    Reading symbols from gdbtest1...done.
    (gdb) b 2
    Breakpoint 1 at 0x400478: file gdbtest1.c, line 2.
    (gdb) r
    Starting program: gdbtest1 
    
    Breakpoint 1, main () at gdbtest1.c:4
    4		a=5;
    (gdb) info display
    There are no auto-display expressions now.
    (gdb) info break
    Num     Type           Disp Enb Address            What
    1       breakpoint     keep y   0x0000000000400478 in main at gdbtest1.c:2
    	breakpoint already hit 1 time
    
  3. 產生變數 a 的 display,並用 info 查看狀況為 enable。
    (gdb) display a 
    1: a = 32767
    (gdb) info display
    Auto-display expressions now in effect:
    Num Enb Expression
    1:   y  a
    
  4. disable 變數 a 的 display,再用 info 查看狀況為 disable。
    (gdb) disable display  1
    (gdb) info display
    Auto-display expressions now in effect:
    Num Enb Expression
    1:   n  a
    
  5. disable 中斷點 1,再用 info 查看狀況為 disable。
    (gdb) disable break 1
    (gdb) info break
    Num     Type           Disp Enb Address            What
    1       breakpoint     keep n   0x0000000000400478 in main at gdbtest1.c:2
    	breakpoint already hit 1 time
    
  6. 增加變數 b 與 c 的 display
    (gdb) display b
    2: b = 0
    (gdb) display c
    3: c = 0
    (gdb) info display
    Auto-display expressions now in effect:
    Num Enb Expression
    3:   y  c
    2:   y  b
    1:   n  a
    
  7. enable 變數 a 的 display
    (gdb) enable display  1
    (gdb) info display
    Auto-display expressions now in effect:
    Num Enb Expression
    3:   y  c
    2:   y  b
    1:   y  a
    
  8. 繼續下一步, a, b, c 三個變數的值皆 display
    (gdb) n
    5		b=10;
    3: c = 0
    2: b = 0
    1: a = 5
    
  9. delete 變數的 display
    (gdb) delete display 1
    (gdb) info display
    Auto-display expressions now in effect:
    Num Enb Expression
    3:   y  c
    2:   y  b
    (gdb) delete display 2
    (gdb) info display
    Auto-display expressions now in effect:
    Num Enb Expression
    3:   y  c
    


2015-01-16