GDB (GNU Debugger) is a powerful command-line tool for debugging C programs. It allows you to run your program step-by-step, inspect variables, set breakpoints, and examine the call stack when errors like segmentation faults occur.
To use GDB with C:
Compile your code with debugging information using the
-gflag:gcc -g -Wall your_program.c -o your_program.Start GDB with your compiled executable:
gdb ./your_program.Use key commands at the
(gdb)prompt:runorr: Start executing the program.breakorb: Set a breakpoint at a specific line (e.g.,break 10) or function (e.g.,break main).listorl: Display the source code around the current line.stepors: Execute the next line, stepping into function calls.nextorn: Execute the next line, but do not step into function calls.printorp: Display the value of a variable (e.g.,print x).continueorc: Resume program execution until the next breakpoint.backtraceorbt: Show the current call stack, which is crucial for diagnosing crashes.quitorq: Exit GDB.
GDB is essential for efficiently identifying and fixing bugs in C programs, especially complex ones involving pointers or memory management.
Videos
GDB Quick Guide
I found out I didn't need cgdb by the way, it was better to just gdb -tui on Debian. Less modem noise in the form of uninterpreted escape sequences too.
So it paid up to do gdb -help.
The guide doesn't mention watch or watch -l but it points to Debugging with GDB where you can find everything.
It is a good quick start, with a couple of examples.