GDB

Cheat sheet

Starting

  • # gdb <prog-file>
  • # gdb –args <prog-file> <prog-args…> : start gdb with program arguments
  • # gdb <prog-file> <core-file> : inspect the core dump
  • # gdb <prog-file> <pid> : attach to the running process
  • # gdb –pid=<pid> –batch -ex gcore : dump the core of an alive process

Running

  • run <prog-args…> (short “r”) : start the program
  • step [count] (short “s”) : step one line
  • stepi [count] (short “si”) : step one instruction
  • next [count] (short “n”) : step one line without entering in functions
  • continue (short “c”) : continue the program
  • finish (short “fin”) : execute until selected stack frame returns
  • advance [source-file]:<line> (short “adv”) : continue up to the given location
  • call <func-name> (short “ca”) : call a function (the context is copied so that it is not modified)
  • kill (short “k”) : stop the program
  • backtrace (short “bt”) : show the stack
  • frame <frame-number> (short “f”) : move to the stack drame number
  • list, list -, list [source-file]:<line|function> (short “l”) : show source code

Threads

  • thread <thread-num> (short “t”) : switch to thread
  • info threads (short “i th”) : show list of current threads
  • thread apply all bt (short “t a a bt”) : show backtraces of all threads

Breakpoints

  • break [source-file]:<line|function> [thread <thread-num>] [if <condition>] (short “b”) : set a breakpoint
  • info breakpoints (short “i b”) : show all current breakpoints
  • condition <breakpoint-number> <condition> (short “cond”) : specify the breakpoint to break only if condition is true
  • delete breakpoints <breakpoint-number> (short “del b”, “del”) : delete a breakpoint
  • enable/disable <breakpoint-numbers…> (short “en” and “dis”) : enable or disable some breakpoints

Inspecting data

  • print <var-name> (short “p”) : print the value/content of the variable
  • print *<pointer>@<nelements> : print nelements of the array
  • print/<format> <var-name> : print with a certain format (x for hexa, …)
  • display <var-name> (short “disp”) : display the value/content of the variable at every step
  • awatch <var-name> (short “aw”) : stops the execution whenever the variable is read or written
  • watch <cond-on-var> (short “wa”) : stops the execution whenever the condition on the variable becomes true
  • ptype <var-name> : type of variable
  • x <addr> : print the content of memory, can format with /<format> too
  • set variable <var-name> = <value> : change variable value
  • set $variable = <exp> : create an external variable

Automation

  • source <file-name> : load a set of commands from a file
  • You can define macros in ~/.gdbinit, eg to automatically set some breakpoints.
software/gdb.txt · Last modified: 2013/09/19 16:40 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0