This page was already viewed 263times
GDB is a powerful command-line tool used for debugging programs written in languages like C and C++. It allows developers to inspect the internal workings of a running application, step through code line by line, analyze variables, set breakpoints, and even reverse-engineer binaries. Whether you're troubleshooting a crash or tracking down elusive bugs, GDB gives you full control over program execution and memory analysis—essential for deep diagnostics and performance tuning.
The following article presents various GDB commands for debugging binaries.
To explore GDB’s features and commands in detail, refer to the GDB manpage.
Run this command to launch the GDB shell.
*File selectionauditer@linux:#gdb
From within the GDB shell, execute the following command to specify the file to be analyzed (e.g., unknown.bin)
*Binary execution>(gdb)file unknown.bin
It’s recommended to execute the binary at least once before conducting further analysis, as this helps obtain accurate assembly addresses.
*Continue execution after a breakpoint>(gdb)run
When execution halts at a breakpoint, this command resumes the binary’s execution until either the next breakpoint or the program’s termination.
>(gdb)c
Display all defined functions in the loaded binary, including their memory addresses.
*Filter functions by Name>(gdb)info functions
Display only functions matching a Name Replace (Example: `info functions main` will show only functions matching "main".
*Inspect a function content (disas)>(gdb)info functions main
The following command disassembles a function to analyze its behavior (e.g., main).
>(gdb)disas main
The following command list the defined breakpoint.
*Add a breakpoint>(gdb)info b
This command sets a new breakpoint at a specified memory address (I.e 0x000055555555532f).
*Delete a breakpoint>(gdb)b *0x000055555555532f
This command delete a previously set breakpoint defined by its breakpoint number (I.e 1).
*Delete all breakpoints>(gdb)delete 1
This command delete all previously set breakpoints.
>(gdb)delete
If the binary was compiled with debug symbols (-g flag in GCC/Clang), GDB can easily list variables
Lists all global and static variables.
>(gdb)info variables
Shows local variables in the current stack frame.
>(gdb)info locals
Shows function arguments in the current frame.
>(gdb)info args
Lists all functions (can help infer variable usage).
>(gdb)info functions
Show a variable content. The variable should exist in the souce code when triggered (I.e variable str).
*Without Debug Symbols>(gdb)x/s str
If the binary is stripped, variable names are gone. You’ll need to reverse-engineer memory structures.
View the contents of CPU registers.
>(gdb)info registers
To include floating-point and vector registers (like xmm, ymm, st0–st7), use:
>(gdb)info all-registers
Inspect memory manually: Use x/<format> <address or register> to examine memory regions (s for string). $rax, $rbx, $rsp, etc., depending on the architecture (x86 vs x86_64) can be used.
>(gdb)x/s $eax
The registers readable by GDB depend on the CPU architecture of the debugged program. Here's a breakdown of common registers for popular architectures.
*Architecture identificationcheck the architecture of a binary
*x86 (32-bit)auditer@linux:#file
Register | Purpose |
---|---|
eax, ebx, ecx, edx | General-purpose |
esi, edi | Source/destination index |
esp | Stack pointer |
ebp | Base/frame pointer |
eip | Instruction pointer |
eflags | Flags register |
cs, ds, ss, es, fs, gs | Segment registers |
Register | Purpose |
---|---|
rax, rbx, rcx, rdx | General-purpose |
rsi, rdi | Source/destination index |
rsp | Stack pointer |
rbp | Base/frame pointer |
rip | Instruction pointer |
r8–r15 | Additional general-purpose registers |
eflags | Flags |
cs, ds, ss, es, fs, gs | Segment registers |
Register | Purpose |
---|---|
r0–r12 | General-purpose |
sp | Stack pointer |
lr | Link register (return address) |
pc | Program counter |
cpsr | Current program status register |
Register | Purpose |
---|---|
x0–x30 | General-purpose |
sp | Stack pointer |
pc | Program counter |
fp | Frame pointer |
lr | Link register |
nzcv | Condition flags |