A debugger is a program that simulates/runs another program and allows you to

what’s a debugger

  • A debugger is a program that simulates/runs another program and allows you to:
    • Pause and continue its execution
    • Set “break points” or conditions where the execution pauses so you can look at its state
    • View and “watch” variable values
    • Step through the program line-by-line (or instruction by instruction)
  • GNU Debugger (gdb)

how to use gdb

  • Compile for debugging

    1
    2
    gcc -Wall -g program.c 
    #the -g option is used to preserves identifiers and symbols
  • Start GDB

    1
    gdb a.out
  • Optionally start with CLAs:

    1
    2
    gdb --args a.out arg1 arg2
    #Can also set in gdb
  • Refresh the display: refresh (or control-L)

  • Run your program: run

  • See your code: layout next

  • Set a break point: break POINT, can be a line number, function name, etc.

  • Step: next(n for short)

  • Continue(to next break point): continue

  • Print a variable’s value: print VARIABLE

  • Print an array: print *arr@len

  • Watch a variable for changes: watch VARIABLE