wiki

Help! I'm trapped in a wiki!

Site Tools


programs:gdb

gdb

gdb is a debugging tool you can use to run your programs line by line. It is indispensable if you're tying to troubleshoot something.

When using gdb standalone, you should run it with -tui so you can see the source code in parallel.

To go over your program:

  • start execution with run <args>
    • continue from here with continue
    • run normally until location: jump loc
  • instruction by instruction
    • stepi
      • This goes over every assembly instruction executed by your CPU!
    • step
      • go through code thoroughly
      • enters functions (instruction by instruction)
  • line by line: use next
    • if function, execute function and pause
    • faster than step
  • function by function: use finish
    • step out of functions quickly
  • run to function hello: use advance hello (continue to temporary breakpoint at this place)

Values

You can set values for your variables while the program is running in gdb with set (i = 20), for example.
To view values while running:

  • use display for the duration of the entire program
    • undisplay to remove them
  • view value one-time: print i
    • use printf "%08X\n". i for fancier (formatted) output
  • watch a variable/condition: when is a variable
    • written to: watch i
    • read from: rwatch x
    • accessed (read/write): awatch p
    • condition changes: watch x == 3
      • if x changes from 3 to 2, for example, this watchpoint will pause the program

You can delete a breakpoint or watchpoint with, well, delete

Functions

You can call an arbitrary function with call func. For example, you can call strlen(str)

To view the function stack (list of function calls) use bt
To move between calls (frames) use up, down and return

Core dumps

When a program crashes, it usually leaves around a core dump, which is a snapshot of the program's memory at the instant it crashed.
If you compiled your program with debugging info (-g) then you can go ahead and load it into gdb to view its variables, the stack trace and other useful things.
To do this, just run gdb prog core

Signals

gdb can hande signals in a variety of ways. By default, it pauses the program at any signal (??? test this out with a program in the wild)

If you want to ignore a specific signal, you can:

  • handle SIGINT nostop → ???
  • handle SIGINT noprint → ???
  • handle SIGINT ignore → ???

See gdb docs for this section!

Layouts

You can use layouts to change to more or less useful views into your program.
For example you can see the source on top, assembly instructions in the middle and gdb command window at the bottom with layout split

programs/gdb.txt · Last modified: by 127.0.0.1