Computer Systems – Working with GDB




1.    gdb <file>

      gdb -h            (lists command line options)


Examining code


      print/a $eip      (print the instruction pointer)

      print $esp        (print the stack pointer)

      disas             (display the function around the current line)

2.    disas ADDR        (display the function around the address)

      disas ADDR1 ADDR2 (display the function between the addresses)




      break FUNCTION    (set a breakpoint at the entry to the function)

3.    break *ADDRESS    (set a breakpoint at the specified address)


      disable <NUM>     (disable the breakpoint with that number)

      enable <NUM>      (enable the breakpoint with that number)


      delete <NUM>      (deletes the breakpoint with that number)

      delete            (deletes all breakpoints)


General commands


4.    run               (start your program)

      kill              (stop the program)


Autodisplaying information


5.    display /i $eip         (display next instruction to execute)

      display $eax            (print contents of EAX every time the

                               program stops)

      display                 (print the auto-displayed items)

      delete display <NUM>    (stop displaying item NUM)


Examining data


6a.   print $REG        (print the contents of register REG)

      print/x $eax      (print the contents of EAX as hex)

      print/a $eax      (print the contents of EAX as an address)

      print/d $eax      (print the contents of EAX as decimal)

      print/t $eax      (print the contents of EAX as binary)

      print/c $eax      (print the contents of EAX as a character)


      print /c $esp     (see what’s on top of the stack)


      print 0x100       (print decimal repr. of hex value)

      print/x 555       (print hex repr. of decimal value)

Examining data (contd.)


6b.   x ADDR            (print the contents of memory at address ADDR)

      x/NFU ADDR        (print the contents at ADDR in memory:

                              N = number of units to display

                              F = display format

                              U = b (bytes), h (2 bytes), w (4 bytes))

      x/s ADDR          (print the string in memory starting at address ADDR)



Working at breakpoints


7a.   stepi             (or si; execute one machine code instruction)

      stepi <NUM>       (execute NUM instructions)

      step              (execute one C statement)


7b.   nexti             (or ni; like stepi, but goes into subroutine calls)

      nexti <NUM>



      until LOCATION    (continue running until LOCATION is reached)


7c.   continue          (continue execution till next breakpoint or end)


      finish            (run until the current function returns)





8.    quit


      Note: Ctrl-c does not exit from gdb, but halts the current gdb command




Useful information commands


      help info

      info program            (current status of the program)

      info functions          (functions in program)

      info stack              (backtrace of the stack)

      info frame              (information about the current stack frame)

      info scope             (variables local to the scope)

      info variables          (global and static variables)

      info registers          (registers and their contents)

      info breakpoints        (status of user-settable breakpoints)

      info address SYMBOL     (use for looking up addresses of functions)