Thursday, October 23, 2014

GDB Memo

Intro

GDB was a scaring tool for me years ago; however, I found it's actually pretty handful. And, instead of Googling the commands every time, I am writing down the common ones I use in this post:

Common Commands

General

  • kill: stop exec
  • run: start execution
  • quit
  • help

Monitoring

  • <ctl-c>: break exec
  • continue
  • list: see where's the exec stops
  • next: will go 'over' the function calls
  • step: will go 'into' the function calls
  • print variable: see variable
  • finish: return from a function

Call Stack

  • backtrace
  • info frame
  • info locals
  • info args

Breakpoint

  • break line number
  • break function name
  • tbreak: same as break but only stops once (temporary breakpoint)
  • info breakpoints
  • disable breakpoint number
  • ignore breakpoint number times: ignore the break point for number of times

Watchpoint

  • watch variable
  • rwatch variable: read watchpoint
  • awatch variable: read/write watchpoint
  • * info breakpoints
  • * disable breakpoint number

Memory

char *s = "hello!\n"
  • x/s s: print string
  • x/c s: print s[0]
  • x/4c s: print s[0]~s[3]
  • x/t s: print first 32 bit
  • x/x s: print 8 bytes in hex
  • info registers
  • core core: see core dump crash
  • nexti: 'next' for instruction level
  • stepi: 'step' for instruction level
  • disassemble function name

Other Helpful Commands

  • info proc
  • frame: show where am I

Print Variables (Organised)

  • * info variables: list "All global and static variable names"
  • * info locals: list "Local variables of current stack frame" (names and values), including static variables in that function
  • * info args: list "Arguments of the current stack frame" (names and values)

More: Fork

  • set follow-fork-mode mode: follow which process after fork
    • mode -> parent, child
    • show follow-fork-mode
  • set detach-on-fork mode (reference)
    • mode -> on, off
    • on(default): the child process (or parent process, depending on the value of follow-fork-mode) will be detached and allowed to run independently.
    • off: both processes will be held under the control of GDB. One process (child or parent, depending on the value of follow-fork-mode) is debugged as usual, while the other is held suspended.
    • show detach-on-fork
  • set follow-exec-mode mode
    • mode -> new, same

Reference