LANGUAGE » C

Debug

Gdb

The binary must be compiled with -g.

If using a more complex Makefile, use CFLAGS = -g -O0 (See compile).

shell
gcc -g -o myapp main.c

Start gdb with the app and optionally a core dump file.

shell
gdb myapp
gdb myapp core

TIP

Check where core dumps are created in your system by seeing the contents of /proc/sys/kernel/core_pattern.

The command ulimit -c unlimited may also be useful.

The most commonly used commands are:

CommandDescription
b breakSet breakpoint at specified location. Assign location as script.c:10 func_name (see more here)
r runStart execution.
p printPrint value of expression EXPR.
n next nextiExecute next line (step-over).
s step stepiExecute subroutine (step-into).
fin finishExecute until selected stack frame returns (step-out).
u untilExecute until past the current line, useful for loops.
c continueContinue program until it finishes or until next breakpoint.

Debug curses app

Information extracted from the neomutt documentation.

Use two terminals, in the first one run:

shell
tty
sleep 1d

In the second one, start gdb and configure it with:

shell
# Inside gdb prompt
tty /dev/pts/5  # Device of the first terminal
handle SIGPIPE noprint nostop
set args -d 2   # Default parameters
run

Press Ctrl+C in the second terminal to insert more debug commands (breakpoint etc).