ReadEasy

14 11月, 2011

GDB Usage


因為碰巧翻到Richliu在wiki上關於GDB的筆記

就不小心給他筆記過來了XDDD

基本指令

  • break 設中斷點: ex: break m4_changequote
  • run 設行程式: ex: run
  • n next
    • n [count]
  • s step(會進 subfunction)
    • s [count]
  • p 列出參數: ex: p rqueue
    • p file:variable
    • p function:variable
  • l 列出 source code ex: l
  • quit 離開
  • info breakpoint 看 breakpoint 的 state
  • b 設定中斷點
  • f show frame
  • bt backtrace : 向後追
    • bt [n] backtrace 追幾個
    • info frame/ info f
    • info locals : print the local variables of the selected frame 可以列出 frame 內的 local 變數


Setting breakpoints

  • break function
  • break +offset
  • break -offset
  • break linenum
  • break filename:linenum
  • break fllename:function
  • break *address 給沒有 debug information 用
  • break Without any arguments, break sets a breakpoint at the next instruction.
  • break .... if cond
  • rbreak regex 用 Regular expression 去 break 所有的 function
  • info breakpoints [n]
  • info break [n]
  • info watchpoints [n]
  • watch expr 寫入 expr 時中斷
  • rwatch expr 讀出 expr 時中斷
  • awatch expr 讀寫 expr 時中斷

Setting Catch point

  • catch event
    • throw : The throw of a C++ exception
    • catch : The catching of a C++ exception

Delete breakpoint

  • clear  : Delete any breakpoints
  • clear function
  • clear filename:functin
  • clear filename:linenum
  • delete [breakpoints] [range...] 刪除所有的 breakpoint, watchpoints or catchpoints
  • disable [breakpoints] [range...]
  • enable [breakpoints] [range...]
  • delete [breakpoints] once range...
  • delete [breakpoints] delete range...

Breakpoint Commands

ex:
break foo if x>0
commands
silent
printf "x is %d\n",x
cont
end

Signal

  • info signals
  • info handle print table of all the kinds of signals and how GDB has been told to handle each one.
  • handle signal keywords.
    • keywords
      • nostop 當 signal 發生時不會停
      • stop 當 signal 發生時會停止
      • print 當 signal 發生時會印出訊息
      • noprint 和 nostop 一樣
      • pass/noignore 讓程式看到 signal 並且 handle it
      • nopass/ignore 不謞程式看到/handle signal

Stopping and starting multi-thread programs

  • break linespac thead threano
  • break linespaec thread thradno if ...
 ex: break frik.c:13 thread 28 if bartab > lim

Debugging programs with multiple threads

GDB provodes there facilities for debugging multi-thread programs:
  • automatic notification of new threads
  • 'thread threadno', a comand to switch among threads
  • 'info threads', a command to inquire about existing threads
  • 'thread apply [theadno] [all] args', a command to apply a command to a list of threads.
  • thread-specific breakpoints

Examining Source Files

  • list linenum
  • list function
  • list - # Print lines just before the lines last printed
  • set listsize count
  • set listsize
  • list first,last # first, 和 last 可拿掉其中一個, 要保留 ,

Searching Source files

  • forward-search regexp
  • search regexp
  • reverse-search regexp

Source and Machine Code

  • set disassembly-flavor instruction-set
    • intel : intel style assembly
    • att : AT&T style assembly
  • step 1
(gdb) info line 9
Line 9 of "test.c" starts at address 0x804839e 
and ends at 0x80483b1
.
  • step 2
(gdb) info line *0x804839e
Line 9 of "test.c" starts at address 0x804839e 
and ends at 0x80483b1
.
  • step 3
(gdb) disas  0x804839e 0x80483b1
Dump of assembler code from 0x804839e to 0x80483b1:
0x0804839e 
: mov 0xfffffffc(%ebp),%eax 0x080483a1
: mov %eax,0x4(%esp) 0x080483a5
: movl $0x80484c8,(%esp) 0x080483ac
: call 0x80482a8 <_init+56> End of assembler dump.

Examining Memory

  • x/nfu addr
  • x addr
  • x
    • n, 重複次數
    • f, 顯示格式, 和 printf 一樣
    • u, 單位格式
      • b Bytes.
      • h Halfwords (Two Bytes)
      • w Words (four bytes). Default
      • g Giant words ( eight bytes)
    • addr 開始顯示位置, 可以用 info btrakpoints , info line , print 找出來

Automatic Display

每一個 Step 都顯示 Variables
  • display expr
  • display /fmt expr
  • display /fmt addr
  • delete display [dnum]
  • disable display [dnum]
  • enable display [dnum]
  • info display

Print settings

  • set print address/ set print address on # default
(gdb) s
evalaa (a=0xbffffa04, b=0xbffffa00) at test.c:4
4          return *a+*b;
(gdb) x 0xbffffa04
0xbffffa04:     0x00000001
(gdb) x 0xbffffa00
0xbffffa00:     0x00000002
(gdb) f
#0  evalaa (a=0xbffffa04, b=0xbffffa00) at test.c:4
4          return *a+*b;
  • set print address off
(gdb) f
#0  evalaa (a=, b=) at test.c:4
4          return *a+*b;

Register

  • info register 列出常用的 Register
  • info all-registers 列出所有的 Register, ex: mmx
  • info registers regname
也可以用這種方式看 register
p/x $PC
x/i $PC
如果要改變 stack
set $sp +=4
  • info float 查看浮點運算器

Memory Region Attributes

  • mem address1 address2 attributes
  • delete mem nums
  • disable mem nums
  • enable mem nums
  • info mem
    • Attributes
      • ro
      • wo
      • rw
      • 8 use 8 bit memory accesses
      • 16
      • 32
      • 64
      • cache : Enable GDB to cache target memory
      • nocache : Disable GDB from caching target memory.

沒有留言:

張貼留言