GO KALI FREE

GDB

Reverse Engineering

Intermediatelow risk

Learn how to use GDB, the GNU Debugger, by building a small C program with debug symbols and following it from the first breakpoint to a crash. Use the same steps to inspect variables, read a backtrace, and tell when a core dump or a look at the assembly would help.

Installation

sudo apt update && sudo apt install gdb -y

Basic Syntax

gdb [options] <binary>

Quick Facts

Full Name
GNU Debugger
License
GPL
Language
C/C++
Platforms
Linux, macOS, Windows (MinGW)
Category
Debugger
Created By
GNU Project

Tool Overview

To use GDB, compile your program with `-g` to include debugging symbols, then open it with `gdb ./program`. Enter `break main` and `run` to pause at the start of your code. Use `step` to move through it and `print` to inspect variables. If it crashes, use `backtrace` to see the calls that led there.

Start in your terminal: install GDB, compile your program with `-g`, and open the executable you just built. The name after `gdb` is the executable, not the source file. Once it loads, type debugging commands at the GDB prompt rather than in your shell. Set a breakpoint somewhere you recognize, such as `main`, and run to that point. When GDB shows a source line at a breakpoint, read it as the line about to run, not as proof that the assignment on that line has happened. Predict what the next statement will do, step once, and check the result with `print`. Use `next` to run a call without stepping into it, or `step` when you need to follow the call. If the program crashes, read the backtrace before restarting so you know where it stopped. Fix the cause in the source, rebuild with `-g`, and test the same case again. In the lab below, the write needs a pointer to a valid, writable `int`; restarting with the same code will not fix it.

Save these eight lines as `crash.c`, with the include on line 1 so the recorded line numbers match: #include <stdio.h> int main() { int *p = NULL; printf("Before crash\n"); *p = 42; printf("After crash\n"); return 0; } Build it with `gcc -g -o crash crash.c`, then open `gdb ./crash`. To watch the crash develop, enter `break main` before `run`, then use `continue` after the breakpoint hits. At the fault, enter `bt`. These are the relevant lines from the recorded run, including the backtrace frame: Program received signal SIGSEGV, Segmentation fault. 0x0000555555555170 in main () at crash.c:5 5 *p = 42; #0 0x0000555555555170 in main () at crash.c:5 Read from the top: `SIGSEGV` tells you the program attempted an invalid memory access. The next line puts the stopped instruction in `main` at line 5 of `crash.c`, and the displayed source line shows the write it tried to perform. `#0` is the innermost stack frame, where execution stopped, not a second crash. Now check the destination: `print p` gave `$1 = (int *) 0x0`. The pointer is null, so there is no `int` at the destination that this program can write to. The program never reaches the line that prints `After crash`. Use `list` to see the surrounding source. The instruction address may differ in your run; compare the function, line, and pointer value rather than the exact address.

On Kali Linux, install GDB with `sudo apt update && sudo apt install gdb -y`. The first part refreshes the package index; the second installs the debugger. Ubuntu and Debian use the same APT command. Run `gdb --version` in the shell to check that GDB starts. The Ubuntu 24.04 test machine reported `GNU gdb (Ubuntu 15.1-1ubuntu1~24.04.1) 15.1`; your version does not need to match that output for this lab. On macOS, use `brew install gdb`. GDB needs code-signing for full use on macOS, so a successful install does not by itself mean it can control a process. On Windows, use MinGW/MSYS2 or work inside a Linux distribution under WSL. Build and debug the example in the same environment so you open the executable you meant to test. On Kali, `sudo apt install gdb-multiarch` adds a debugger that supports multiple architectures. It is not an emulator and does not make a binary for another architecture runnable on your machine. The native crash lab only needs ordinary GDB.

Choose a stop based on what you need to learn. A breakpoint such as `break main` stops when execution reaches a location. Use `info breakpoints` to check each stop's number, location, and enabled state. A watchpoint such as `watch p` stops when the watched value changes. Set it while `p` is in scope, and do not expect it to stop just because another statement reads or dereferences `p`. In the lab, nothing changes the pointer after it is initialized to null. A conditional breakpoint checks a test at a particular location: `break crash.c:5 if p == 0` stops before the write on line 5 only if `p` is null. Set it before reaching line 5 on the run you want to check. If you go back to the beginning, remember that a watchpoint on a local variable can go away when that variable leaves scope. A catchpoint stops for an event rather than a source location or a value change. On a supported Linux target, `catch syscall open` catches entry to and return from that named system call, not every way a program can open a file. You do not need a catchpoint for the null-pointer crash.

Start with plain GDB for the crash lab, then try one plugin if you want to see more of the program's state at each stop. GEF is a good first choice if you want to see the registers, stack, and current instructions together. It needs GDB 7.7 or newer with Python support; its installer is `bash -c "$(curl -fsSL https://gef.blah.cat/sh)"`. PEDA is an older extension with a compact register, stack, and disassembly display. It is useful when following a lab that already uses PEDA. Set it up with `git clone https://github.com/longld/peda.git ~/peda` followed by `echo "source ~/peda/peda.py" >> ~/.gdbinit`. PwnDbg also shows context at each stop and offers more ways to inspect memory. It needs GDB 12.1 or newer and Python 3.10 or newer; its installer is `curl --proto '=https' --tlsv1.2 -LsSf 'https://install.pwndbg.re' | sh -s -- -t pwndbg-gdb`. Load only one plugin at a time so you know which one provides a given view or command. Review third-party code before running an installer or adding it to your GDB startup file.

When you have authorization to reverse engineer a binary but do not have its source, ask the same questions: where did execution stop, and what was the program doing there? If GDB recognizes the function name, `disassemble main` shows its machine instructions. At a stop, `x/10i $pc` shows ten instructions starting at the program counter, while `x/4xw $sp` shows four words at the stack pointer in hexadecimal. One decodes instructions; the other shows raw data. To keep a code pane visible in the terminal, start with `gdb -tui ./program` or toggle TUI with Ctrl-X A. Use `layout asm` for instructions or `layout src` when source is available. In an authorized CTF practice lab, you can take a function you identified at a GDB stop into Ghidra to read the surrounding code. If you already use pwntools as a local test harness, keep the normal test input the same and use GDB to see where that run stops. Only debug programs you own or have explicit permission to analyze. Keep unfamiliar binaries in an isolated VM, and never debug an untrusted binary on a host you care about.

If the operating system saved a core dump, open it with `gdb ./program core` using the exact executable build that crashed, then run `bt` inside GDB. This is post-mortem debugging: you can inspect the saved state, but you cannot step forward through that run. Where permitted, the standard setup is `ulimit -c unlimited` in the shell before running the crashing program from that shell. The test VM blocked that limit change and did not produce a local core file. If no dump was saved, reproduce the crash in a live GDB session instead. To inspect a running local process you control, use `gdb -p 1234` with its actual PID; attaching pauses the process. For remote debugging on systems you control, start the separate program `gdbserver :1234 ./program` on the target, then connect from GDB with `target remote <target-ip>:1234`. Keep the connection inside your isolated lab. Once you find the function or instruction that explains the stop, take it to Ghidra, Radare2, or Objdump to study that part of the binary without running it again.

Start with one question: which line is about to run, and what value will it use? If the lab stops at line 3, check the reason before you assume it failed. That first stop is the breakpoint you set, not a segmentation fault. Use `next` to get past the pointer initialization before you inspect `p`. A null pointer can exist without crashing the program; the attempted write on line 5 is what causes the fault. In a larger program, follow the bad value back to where it was assigned or passed into the function. If you do not have the source, use Ghidra for static analysis to see where a function is called, then return to GDB to check which path your test run took.

If GDB cannot show a source line or `print p` cannot find the variable, check the build and the selected frame before changing tools. Make sure you compiled with `-g` and loaded that executable, not a copy from an older build. Try `list` when you expect source: even if the binary contains line information, GDB still needs the source file to show its contents. If the source is visible but `p` is missing, confirm that the selected frame is in `main`. Use `info locals` to inspect the locals in that frame, or `info args` to check the function arguments. For terminal-based static work, use Radare2 for CLI analysis to read the code separately, then use GDB to confirm what happened during the run.

Keep a short record of the executable build, the input, the reason GDB stopped, and the values you found. If there is no source, note the instruction and the function when GDB can name it. Use Objdump for static disassembly to see which instructions are in the file. If the file and the live session show different absolute addresses, compare the function offset or the surrounding instructions rather than assuming the address numbers must agree. After fixing a source bug, rebuild and retry the same case. Check that the program reaches the code it missed before, not just that the original error message went away. For `crash.c`, that means checking that execution gets past the write to the statement that prints `After crash`.

Quick Reference Commands

Verified quick-reference commands for GDB
CommandPurpose
gdb <program>Load executable symbols without starting the program.
gdb <program> <core-file>Perform post-mortem debugging.
break <function>Set a breakpoint at a function.
backtracePrint the current stack trace.
GNU GDB Manual

Common Commands

gdb ./programLoad an executable into GDB for debugging
gdb --args ./program arg1 arg2Load a program together with its command-line arguments
gdb -p 1234Attach GDB to an already running process by PID
gdb ./program coreDebug a program using a saved core dump file
gdb -q ./programStart GDB quietly, suppressing the banner text
gdb -batch -ex run -ex bt ./programRun scripted commands non-interactively then exit
runStart execution of the loaded program (alias: r)
break mainSet a breakpoint at the function main (alias: b)
break file.c:42Set a breakpoint at a specific source file and line
continueResume execution until the next breakpoint (alias: c)
nextStep over the next source line without entering calls (alias: n)
stepStep into the next source line, entering function calls (alias: s)
stepiStep a single machine instruction (alias: si)
finishRun until the current function returns
backtracePrint the current call stack (alias: bt)
info registersDisplay the contents of all CPU registers
print variableEvaluate and print an expression or variable (alias: p)
x/16xw $spExamine 16 words of memory in hex starting at the stack pointer
disassemble mainShow the assembly for a function (alias: disas)
watch variableSet a hardware watchpoint that stops when a value changes
set var x = 5Modify the value of a variable during execution
info breakpointsList all defined breakpoints and their hit counts
layout asmOpen the TUI assembly view alongside the command window
quitExit GDB, detaching from any attached process (alias: q)
info argsShow the arguments of the function in the selected stack frame.
info localsShow the local variables in the selected stack frame.
x/10i $pcDisplay ten instructions starting at the current program counter.
catch syscall openStop on entry to and return from the open system call on supported targets.
tbreak mainSet a breakpoint at main that GDB removes after the first hit.
disable 1Turn off breakpoint number 1 without deleting it.
attach 1234Attach the current GDB session to the running local process with PID 1234 and pause it.
target remote :1234Connect GDB to a remote debugging stub reachable on local TCP port 1234.

Step-by-Step Guide

  1. 1On Kali, check for GDB with `gdb --version`, use `sudo apt update && sudo apt install gdb -y` if it is missing, and repeat the version check after installation.
  2. 2Save the eight-line example as `crash.c` and compile it with `gcc -g -o crash crash.c` so GDB can match instructions to source lines and variable names.
  3. 3Start `gdb ./crash` in the directory holding the executable to load the lab without running it yet.
  4. 4At the GDB prompt, enter `break main` so the next run pauses in `main` before the null-pointer write.
  5. 5Enter `run` to hit the breakpoint, then enter `continue` and read the `SIGSEGV` stop at `crash.c:5`.
  6. 6Use `bt` to read the stack and inspect frame #0, the only frame in this trace and the one GDB has already selected at the crash.
  7. 7Use `print p` to check the pointer itself and `x/4xw $sp` to inspect four hexadecimal words at the stack pointer without dereferencing `p`.
  8. 8Restart with `run`, then use `step` on the pointer initialization and `next` over the print call to stop before the write on line 5.
  9. 9While `p` is in scope, enter `watch p` to track changes to the pointer and `break crash.c:5 if p == 0` to stop before the write on a fresh run when `p` is null.
  10. 10From a shell, open an existing dump with `gdb ./program core` and read it with `bt`, or attach to a live process you control with `gdb -p 1234`, then take the relevant function to Ghidra for static analysis.

Warnings

Use Cases

Crash Analysis

Debug segmentation faults and crashes.

Malware Debugging

Analyze malware behavior at runtime.

Exploit Development

Develop and test exploits with PEDA/GEF.

Binary Analysis

Step through compiled code to understand logic.

Segfault Debugging

Find the crashing line in minutes with a backtrace, then inspect the values used on that line to check the cause.

CTF Binary Exploitation

For the debugging part of an authorized CTF exercise, pause a practice binary at a breakpoint and use GEF or PwnDbg to inspect memory and diagnose a crash.

Key Features

Related Tools

Ghidra

Reverse Engineering

This hands-on guide shows how to use Ghidra to inspect a program you compile yourself. You'll compare decompiled code with the underlying assembly, follow cross-references, and document what the binary does without executing it.

Radare2

Reverse Engineering

Radare2 (r2) is a complete open-source framework for reverse engineering and binary analysis. It bundles a disassembler, debugger, hex editor, and scripting engine into a single lightweight command-line toolkit that supports dozens of architectures and executable formats.

objdump

Reverse Engineering

objdump displays information about object files. It can disassemble executables, show headers, symbols, and more.

Objection

Reverse Engineering

Objection is a runtime mobile exploration toolkit, powered by Frida, built to help you assess the security of your mobile applications without needing a jailbroken or rooted device.

Frida

Reverse Engineering

Frida is a dynamic instrumentation toolkit for developers, reverse-engineers, and security researchers. It allows you to inject snippets of JavaScript or your own library into native apps on various platforms — making it the industry standard for runtime analysis and mobile app security testing.

strings

Reverse Engineering

strings prints the sequences of printable characters in files. Useful for finding hidden text, URLs, and clues in binaries.

Related Cybersecurity Guides

Ethical Hacking · Intermediate

Reverse Engineering for Beginners: Learn to Analyze Binaries with Ghidra & GDB

Learn reverse engineering from scratch: analyze binaries with Ghidra and GDB, read assembly, crack a practice binary, and build a starter toolkit.

Security Operations · Advanced

Detection Engineering: Creating Security Alerts and Rules

Learn detection engineering principles including rule development, Sigma rules, testing methodologies, false positive management, and building a detection program for security operations.

Tools · Beginner

How to Use Dirb: Beginner's Guide to Directory Brute-Forcing in Kali Linux

Learn how to use Dirb in Kali Linux: run your first directory scan, pick the right wordlist, and read the results — with a worked example and cheat sheet.

OSINT · Beginner

How to Use Whois: Beginner's Guide to Domain Lookups in Kali Linux

Learn how to use whois in Kali Linux: look up any domain, IP or AS number, read the record, and spot red flags — with a worked example and cheat sheet.

Frequently Asked Questions

What is GDB used for?

GDB is used for debugging programs and binaries. It lets you inspect memory, set breakpoints, step through code, and analyze crashes.

Is GDB useful for reverse engineering?

Yes, GDB is essential for dynamic analysis in reverse engineering. It allows you to debug malware and analyze binary behavior at runtime.

What are GDB plugins?

Plugins like PEDA, GEF, and pwndbg enhance GDB with better interfaces, exploit development tools, and visual memory layouts.

Can GDB debug remote targets?

Yes, GDB supports remote debugging via gdbserver for debugging programs on other machines or embedded devices.

How do I use GDB in Kali Linux?

Build the lab with `gcc -g -o crash crash.c` and start it with `gdb ./crash` from the same directory. At the GDB prompt, use `break main` and `run` to pause before the crash. Use `next` to move past the pointer initialization and `print p` to inspect it, then use `bt` after `continue` stops at the fault.

How do I install GDB in Kali Linux?

Run `sudo apt update && sudo apt install gdb -y` in your terminal, then check the installation with `gdb --version`. Your version may differ from the Ubuntu 24.04 test run, which is fine for this lab. If you need a debugger for multiple architectures, Kali also offers `sudo apt install gdb-multiarch`.

Is GDB for C or C++?

GDB can debug both C and C++ programs. The lab uses C, but the same breakpoint, stepping, and backtrace commands apply to a C++ executable built with debug symbols.

What is GDB full form?

GDB stands for GNU Debugger. It lets you pause a running program, inspect its values, and read the call stack when something goes wrong.

How to use GDB for C++?

Build your C++ program with `-g` to include debugging symbols, then load its executable with `gdb ./program`, replacing `program` with its name. Use `break main` and `run` for your first stop, then use `next` to move past a call or `step` to follow a call into code with source-level debug information. If it crashes inside a call, use `backtrace` to see how your code got there.

How do I use GDB to debug a segmentation fault?

Run the program inside GDB and check the file and line reported after the `SIGSEGV` message. Use `bt` to read the call stack, then inspect the variables used on the stopped line. In this example, `print p` shows a null pointer and `*p = 42;` shows the attempted write through it. Not every segfault is a null-pointer error, so use the stopped line and the actual values to confirm the cause.

How do I install GEF for GDB?

GEF needs GDB 7.7 or newer with Python support. Its installer is `bash -c "$(curl -fsSL https://gef.blah.cat/sh)"`, which downloads and runs a script. To review the file first, download it with `wget -O ~/.gdbinit-gef.py -q https://github.com/hugsy/gef/raw/main/gef.py` and inspect it before using `echo source ~/.gdbinit-gef.py >> ~/.gdbinit` to load it on startup. Load only one debugger plugin at a time.

How do I debug a core dump with GDB?

If your system saved a dump, open it with the same executable build that crashed using `gdb ./program core`, then use `bt` to read the saved stack. Replace `core` with the actual dump path if your system saved it under a different name or in a different directory. To prepare for a fresh crash, the standard shell step is `ulimit -c unlimited` before running the program, if your environment permits it. The test VM blocked that change and did not produce a local core file.

How do I use GDB TUI / layout mode?

Start with `gdb -tui ./program` or press Ctrl-X followed by A inside GDB to toggle the text interface. Use `layout src` to follow source when it is available, or `layout asm` to follow disassembled instructions. If the source pane is blank because the source is unavailable, switch to the assembly view.

How do I set a watchpoint in GDB?

Pause where the variable is in scope and enter `watch p` to watch the pointer value in this lab. GDB stops when that value changes, not when a statement reads it or tries to write through it. Since this example never changes `p` after initialization, a breakpoint on line 5 is a better way to stop just before the bad write.

Tags

#reverse-engineering#debugger#binary-analysis

Output Explanation

`Program received signal SIGSEGV, Segmentation fault.` tells you the program attempted an invalid memory access, but not which value caused it. The next line, `0x0000555555555170 in main () at crash.c:5`, maps the stopped instruction to line 5 of `crash.c`, where the code tries to execute `*p = 42;`. The number at the front is the instruction's address, not the address it tried to write to. In `bt`, `#0  0x0000555555555170 in main () at crash.c:5` is the innermost stack frame. It repeats where execution stopped rather than reporting a second fault. `print p` returns `$1 = (int *) 0x0`: `$1` labels the printed result, `(int *)` is the pointer type, and `0x0` tells you the pointer is null. In `info registers`, `rip` is `0x555555555170 <main+39>`, the same instruction address without the leading zeros, 39 bytes from the start of `main`. Read the two values separately: `rip` tells you where the processor stopped, while `p` tells you why the write on that line could not succeed.

Ethical Usage Notice

This tool is designed for authorized security testing, educational purposes, and legitimate network administration only. Unauthorized access to computer systems is illegal.