Debugging a program from assembly ( thanks to this tutorial! )

  • compile such that it will correlate with the assembler
    • gcc -S foo.c
    • gcc -Wa,--gstabs, foo.s
      • it also seems acceptable to simply do gcc -g foo.s

  • run it
    • gdb ./a.out
    • b main (easy way to find start of program)
    • r (run till breakpoint)
    • info registers (shows all registers)
    • p/x $eax (shows register eax in hex)
    • p/t $-16 (shows the number 16 in binary)
    • display/i $pc print instruction as you go

Misc

  • Tell gdb to save its history
    • set history save on
    • It is convenient to put this in .gdbinit in the directory you are working in

  • specify arguments for program at launch time
    • use the --args command... gdb --args ./mpg123 -C -a /dev/dsp1 /share/music/Trance/*

  • set breakpoint: break <line # or function name&gt
    • info break - list breakpoints

  • clear breakpoints: clear <line #, filename:line#, function>
    • delete all breakpoints: delete
    • delete breakpoint #2: delete 2
    • disable all breakpoints: disable

  • memory dump:
    • dump 40 values starting at 10 bytes before the value pointed to by esp: x/40 ($esp - 10)

  • memory write:
    • set {int}0x7fbffff71c = 4 sticks 4 at the shown memory location

  • step, jump over function calls: n
  • step, enter function calls: s
  • start a run from the beginning: run
  • continue till next breakpoint: c
  • continue till return to guy that called you finish
  • call stack (backtrace): bt

  • print: p <variable>
    • print in hex: p/x <variable>
      • ...or x &<variable>
    • show a value at each prompt: display <value>
    • stop showing values at each prompt: undisplay
    • print sse registers in hex: p/x $xmm0

    • x/2d $rsp - print two 4-byte words starting at address in $rsp in decimal
    • x/g  $rsp - print one 8-byte word starting at address in $rsp.
    • x/gd $rsp - print one 8-byte word starting at address in $rsp in decimal
    • x/20b sum - print first 20 opcode bytes of function sum
    • x/10i sum - print first 10 instructions of function sum

  • show all registers
    • info registers

  • program list: l with...
    • line number - list centered around this line
    • function name - list from start of function

  • tell gdb to catch a signal, but also let the debugged program catch it
    • handle SIGINT pass

-- MattWalsh - 03 Aug 2004

Topic revision: r10 - 07 Jan 2011 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback