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 gdbRun a program
Debug a program:
$ gdb ./my_program A (gdb) prompt appears when gdb executes:
Pass some arguments:
(gdb) set args -o file_nameSet a breakpoint at a function:
(gdb) break function_nameRun the program:
(gdb) runStarting program: my_program -o file_name
Quit GDB
(gdb) quit(gdb) qShow GDB help
GDB man page:
$ man gdbGet help inside GDB:
(gdb) help(gdb) hList 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 rStart 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) l130 /**
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 120info command
(gdb) help infoGeneric command for showing things about the program being debugged.
List all breakpoints:
(gdb) info breakShow local variables of current stack frame:
(gdb) i localsrkm = 0x55fa3d905f40
err =
errnox = 8732417
Debug a crash using a core file
You may have cores disabled for your shell:
$ ulimit -c0
To enable them:
$ ulimit -c unlimitedSystemd 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.lz4Run GDB using that core file:
$ gdb my_program core_fileE.g:
$ gdb my_program core.my_program.1000.a5a9801317064638a15481fbebfa686f.2010317.1582046615000000000000Show stack
backtrace (bt) or where command shows the call stack:
(gdb) btor
(gdb) whereShow full stack
Show call stack and local variables:
(gdb) bt fullor
(gdb) where fullBreakpoints
Help about commands related to breakpoints:
(gdb) help breakpointsSet a breakpoint at the beginning of a function:
(gdb) break function_name(gdb) b function_name List current breakpoints:
(gdb) info b(gdb) i bNum 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) cDisable breakpoints
Disable all breakpoints:
(gdb) disable breakpointsDisable breakpoint number 1:
(gdb) disable breakpoints 1Enable breakpoints
Enable all breakpoints:
(gdb) enable breakpointsEnable breakpoint number 1:
(gdb) enable breakpoints 1Step through the source code
Step into next line but do not enter in functions:
nextnYou can also execute a number of lines:
next numbern 5 # Execute 5 lines.Step into next line but enter inside functions:
stepss 3 # Execute 3 lines.Print value of a variable
(gdb) print my_variable(gdb) p my_variableREFERENCE
$ man gdbhttps://www.gnu.org/software/gdb/
https://darkdust.net/files/GDB%20Cheat%20Sheet.pdf