GDB stands for GNU Project Debugger.
It allows you to observe what does happen when a program runs or by using a core file when a program crashed.
Install GDB
Install GDB in Debian:
$ sudo aptitude install gdb
Run a program
Debug a program:
$ gdb ./my_program
A (gdb) prompt appears when gdb executes:
Pass some arguments:
(gdb) set args -o file_name
Set a breakpoint at a function:
(gdb) break function_name
Run the program:
(gdb) run
Starting program: my_program -o file_name
Quit GDB
(gdb) quit
(gdb) q
Show GDB help
GDB man page:
$ man gdb
Get help inside GDB:
(gdb) help
(gdb) h
List of classes of commands:
aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands
Get help about a specific command:
(gdb) help run
(gdb) h run
(gdb) h r
Start debugged program. You may specify arguments to give it.
Args may include "*", or "[...]"; they are expanded using "sh".
Input and output redirection with ">", "<", or ">>" are also allowed.
With no arguments, uses arguments last specified (with "run" or "set args").
To cancel previous arguments and run with no arguments,
use "set args" without arguments.
Show source code
List source code:
(gdb) list
(gdb) l
130 /**
131 * @brief Process a running thread
132 */
133 void* processing_thread(void *_thread_id)
134 {
135 long thread_id = (long) _thread_id;
136 char pcap_error_buffer[PCAP_ERRBUF_SIZE];
137
138 if (!quiet_mode) {
139 printf("Running thread %ld...\n", thread_id);
List around a specific line of source code:
(gdb) list 120
(gdb) l 120
info command
(gdb) help info
Generic command for showing things about the program being debugged.
List all breakpoints:
(gdb) info break
Show local variables of current stack frame:
(gdb) i locals
rkm = 0x55fa3d905f40
err =
errnox = 8732417
Debug a crash using a core file
You may have cores disabled for your shell:
$ ulimit -c
0
To enable them:
$ ulimit -c unlimited
Systemd places coredum by default in /var/lib/systemd/coredump directory.
If core is in lz4 format, to decompress it:
$ unlz4 core.my_program.1000.a5a9801317064638a15481fbebfa686f.2010317.1582046615000000000000.lz4
Run GDB using that core file:
$ gdb my_program core_file
E.g:
$ gdb my_program core.my_program.1000.a5a9801317064638a15481fbebfa686f.2010317.1582046615000000000000
Show stack
backtrace (bt) or where command shows the call stack:
(gdb) bt
or
(gdb) where
Show full stack
Show call stack and local variables:
(gdb) bt full
or
(gdb) where full
Breakpoints
Help about commands related to breakpoints:
(gdb) help breakpoints
Set a breakpoint at the beginning of a function:
(gdb) break function_name
(gdb) b function_name
List current breakpoints:
(gdb) info b
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep n 0x00007ffff7c14210 in rd_kafka_msg_partitioner
at rdkafka_msg.c:945
breakpoint already hit 1 time
Continue execution after a breakpoint:
(gdb) continue
(gdb) c
Disable breakpoints
Disable all breakpoints:
(gdb) disable breakpoints
Disable breakpoint number 1:
(gdb) disable breakpoints 1
Enable breakpoints
Enable all breakpoints:
(gdb) enable breakpoints
Enable breakpoint number 1:
(gdb) enable breakpoints 1
Step through the source code
Step into next line but do not enter in functions:
next
n
You can also execute a number of lines:
next number
n 5
# Execute 5 lines.Step into next line but enter inside functions:
step
s
s 3
# Execute 3 lines.Print value of a variable
(gdb) print my_variable
(gdb) p my_variable
REFERENCE
$ man gdb
https://www.gnu.org/software/gdb/
https://darkdust.net/files/GDB%20Cheat%20Sheet.pdf