gdb

Compiling with support for gdb

  • Compiling C with support for GDB: gcc -ggdb <file> -o <output>
  • Copies the symbols from a binary: objcopy --only-keep-debug <binary> <debugfile>
  • Remove the symbols from a binary: strip --strip-debug <binary>
  • Completely removes the symbols from a binary: strip --strip-debug --strip-unneeded <binary>
  • Add the symbols to the binary: objcopy --add-gnu-debuglink=<debugfile> <binary>

Commands inside

Only works if the source code is available

  • Show source code: list or l
  • Show source from line: list <line #> or l <line #>
  • Show functions in code: info functions
  • Show source files : info sources
  • Show global variables: info variables
  • Show scopes in which local varaiables can exist: info scope
  • Show variables inside a scope: info scope <function>
  • Show values inside the registers: info registers
  • Add symbols at runtime: symbol-file <debugfile>
  • Show Memory x/<count><fmt><size> <address>
    • Formats
      • o = octal
      • x = hex
      • d = decimal
      • u = unsigned decimal
      • t = bin
      • f = float
      • a = address
      • i = instruction
      • c = char
      • s = string
    • Size
      • w = word
  • Print the contents of a Variable: print <var>
  • Dissasemble function: disassemble <func>
  • Get help: help <cmd>

Breakpoints

  • Show all of the currently set breakpoints: info breakpoints
  • Set a breakpoint in a function: break <func>
  • Set a breakpoint at an address: break *<addr>
  • Set a breakpoint at line number: break <line>
  • Disable a breakpoint: disable <breakpoint #>
  • Enable a breakpoint: enable <breakpoint #>
  • Delete a breakpoint: delete <breakpoint #>
  • Continuing from Breakpoint: continue
  • Stepping: step
  • Step into (step one instruction): stepi

NM (Symbol listing)

  • Run nm to get Symbol infromation
  • Get sorted by address: nm -n <debug>
  • Get external symbols: nm -e <debug>
  • Get Storage: nm -S <debug>
  • Full list available via: man nm

NM Symbol Types

  • A = Absolute Symbol
  • B = In the Uninitialized Data Section (BSS) - Variables without initialization
  • D = In the initialized Data Section - Variables with initialization
  • N = Debugging Symbol
  • T = In the Text Secion
  • U = Symbol Undefined right now - Functions that are pulled from external libraries / dynamic loading
  • Casing
    • Lower case is Local
    • Upper case is External

strace

Traces the System Calls. Shows you function by function call of the execution of the binary.

  • execute: strace <binary>
  • execute with timestamp: strace -t <bin>
  • execute with relative timestamp: strace -r <bin>
  • execute with output: strace -o <output> <bin>
  • Limit output to only certian function calls: strace -e <func>, <func> <bin>
  • Attach to a running process: sudo strace -p <pid>

  • Get statistics of reference calls: strace -c <bin>

Resources