Skip to main content

A Humble Introduction to the Basics of GDB

What is GDB?

GDB is a user process that attaches on to another user process.

That's all.

Project Build Type

  • Debug build: gcc -g
    • Works with gdb
    • Shows source code
    • Easy mode
  • Release with debug information build:
    • Works with gdb
    • Shows source code, however messy because of optimizations
    • Hard mode
  • Release build: gcc -O2
    • Works with gdb
    • No source code information available
    • Debug with disassembled application
    • Hell mode

Debugging Type

Local Debugging

> gdb [program]
(gdb) run [argument list]

Remote Debugging

# Target: (Machine to debug)
> gdbserver hostname:port program args
> gdbserver hostname:port --attach [pid]

# Host: (Machine debugging)
(gdb) target remote hostname:port

GDB Commands Cheatsheet

| GDB help | Descriptions | |-------------------|---------------------------------------------------------------| | help [command] | command description. eg 'help show' to list the show commands | | help aliases | show current aliases | | help breakpoints | | | help data | show commands about debugging program data | | help files | show commands about examining files | | help internals | show gdb maintenance commands | | help obscure | show experimental commands | | help running | | | help stack | | | help status | | | help support | | | help tracepoints | | | help user-defined | |

| Modifying Environment | Description | |---------------------------------|----------------------------------------| | path [dir] | prepends directory to $PATH | | show paths | echo $PATH | | show environment [varname] | echo $varname | | set environment [var] = [value] | export var=value | | unset environment [var] | unset var | | cd [dir] | change gdb's current working directory |

| Logging | Description | |-------------------------------|--------------------------------------------------| | show logging | show current logging options | | set logging file [filename] | | | set logging [on/off] | | | set logging redirect [on/off] | stop printing to terminal, only print to logfile |

| Break and Watch | Descriptions | |--------------------------------------------|-----------------------------------------------------------------------------------------| | break [function name] | | | break [line number] | | | break [ClassName::functionName] | | | break +offset; break -offset | break offset number of lines forward/backward | | break *address | suspend processing at an instruction address. used when no source is available. | | break [line number] if [condition] | break when the condition evaluates to true. i.e. break 15 if (x > 5) | | break [line number] thread [thread-number] | break in thread at specified line number. Use 'info threads' to display thread numbers. | | rbreak | Set a breakpoint for all functions matching REGEXP | | rbreak . | break on all functions | | rbreak [filename]::. | break on all functions in [filename] | | tbreak | temporary break. break only once and then the breakpoint is removed | | watch [variable] | suspend processing when the variable is changed | | watch [expression] | suspend processing when expression evaluates to 1. i.e. watch (x > 5) | | rwatch [expression] | suspend processing when the expression is read. | | awatch [expression] | suspend processing when the expression is either read or written. | | clear | clear all breakpoints | | clear [function] | clear all breakpoints in function | | clear [line number] | clear breakpoint at line number | | delete | clear all breakpoints | | delete [breakpoint number] | clear the specified breakpoint. use 'info breakpoints' to display breakpoint numbers | | delete [range] | clear all the breakpoints in the specified range. i.e. delete 10-30 | | disable/enable [breakpoint number] | disable/enable breakpoint, will not delete the breakpoint | | disable/enable [range] | disable/enable breakpoints in range, will not delete the breakpoint |

| Execution | Description | |-----------------------------|-----------------------------------------------------------------------------------------------------------------| | where | shows current line number and which function you are in. | | step | step to next line of code. will step into a function. | | step [number] | do step [number] times | | next | execute next line of code. will not step into a function. | | next [number] | do next [number] times | | stepi/nexti | step/next assembly/processor instruction. | | stepi/nexti [number] | | | until[line number] | continue processing until you reach a specified line number | | until [function name] | | | until [address] | | | file [executable] | load executable for debugging | | run [args] | run program with args | | continue | continue processing until meeting with a breakpoing | | continue [number] | continue [number] times / ignore breakpoints [number] times | | finish | continue to the end of the function | | kill | stop current execution | | quit | exit debugger | | info signals/handle | show current signal handling | | handle [signal name] option | perform the following option when signal recieved: nostop, stop, print, noprint, pass/noignore or nopass/ignore |

| Source code | Descriptions | |----------------------------|--------------------------------------------------------------------------------------------------------------------------| | show listsize | show number of lines listed when 'list' command given | | set listsize count | | | list | list source code (move forward) | | list - | list source code (move backward) | | list [line number] | | | list [function] | | | list [start],[end] | list source code from start to end. i.e. list 1,30 | | list [filename:function] | | | show directories | show the search path for finding source files. | | directory [directory name] | add specified directory to front of source code path. | | directory | clear source code path | | info line | displays the start and end position in object code for the current line in source. | | info line [line number] | display position in object code for a specified line in source. | | info line [function name] | display position in object code for a specified function name. | | disassemble [start] [end] | disassemble the range from start to end (use 'info line' to get instruction address). i.e. disassemble 0x4015e6 0x401600 |

| Examine Variables | Descriptions | |------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | print [variable name] | print value stored in variable. | | print [file name]::[variable name] | | | print *[array variable]@[length] | print pointer as array of [length] in length. i.e. print *(arr)@10 | | print/x [variable name] | print integer variable in hex. | | print/d [variable name] | print as signed integer | | print/u [variable name] | print as unsigned integer | | print/o [variable name] | print in octal | | print/t [variable name] | print in binary | | x [address] | examine the contents of memory. | | x/nfu [address] | examine the contents of memory and specify formatting: n -- number of display items to print; f -- specify the format for the output; u -- specify the size of the data unit (eg. byte, word, ...). i.e. x/4dw var | | ptype [variable] | Prints type definition of the variable or declared variable type. Helpful for viewing class, struct and enum definitions while debugging. |

| Stack | Descriptions | |----------------|-------------------------------------------------------------------------------------------------------| | backtrace | show trace of where you are currently, which functions you are in, and prints stack backtrace | | backtrace full | backtrace with printing local variables | | frame | show current stack frame (function where you are stopped) | | frame [number] | select frame number. use 'info frame' to show frames. | | up | Move up a single frame | | up [number] | up number times | | down | move down a single frame | | down [number] | down number times. | | info frame | list address, language, address of arguments/local variables and which registers were saved in frame. | | info args | info arguments of current frame | | info locals | info local variables of current frame | | info catch | info exception handlers of current frame |

Extending GDB with Python

Raw Text Documentation:

HTML Documentation:

References:

Comments

Comments powered by Disqus