Table of Contents

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:

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:

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:

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