›INDEX
Last Updated:

Notes on GDB

GDB stands for the "GNU debugger," and it's one of the most elaborate and valuable debuggers you'll come across. However, it will also be one of the most complex debuggers you'll ever use.

You can use GDB to see everything your computer is doing at any point in your program. To the level of 0s and 1s. You can inspect the registers (memory used by the CPU), the memory (RAM), and other such details.

Basics

I will demonstrate how to inspect C programs in GDB, but you can also use GDB to debug C++ code.

Compiling for GDB

Here's an example of a C program:

#include <stdio.h>

int main () {
    printf("Hello world!\n");
    return 0;
}

Let's say the above code is in the file: hello.c. When we compile the program, we want to add the -g flag so that the debugger has the names of the variables we used.

Compile command:

$ gcc -o hello -g hello.c

If you don't include the -g file, you can still view it using gdb, but you won't have the source code as a reference, and you won't see any of your variable names.

Starting GDB

Once you have a compiled file, you can start GDB with the program by running the command gdb <command>. This will start gdb with the file loaded.

Here's what you should see:

$ gdb hello
GNU gdb (GDB) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
pwndbg: loaded 192 commands. Type pwndbg [filter] for a list.
pwndbg: created $rebase, $ida gdb functions (can be used with print/break)
Reading symbols from hello...
pwndbg>

Basic Commands

You'll need to know the commands before you can do anything with gdb. I'll give you both the whole command and the short form of the command. Feel free to use either of the options.

  • start: Start the program and stop at line 1.
  • step: (s) Run the next line of code.
  • step <num>: (s) Run the next num line of code.
  • next: (n) Similar to step but does not step into function calls.
  • run: (r) Run the program till the end or breakpoint.
  • finish: (f) Run till the end of the current function.
  • break <num>: (b) Add a breakpoint at that line in the code.
  • break <func>: (b) Add a breakpoint at the start of the specified function.
  • continue: (c) continue after stopping at a breakpoint.
  • print <exp>: (p) Print the expression.
  • info break: (i b) List all breakpoints in your code.
  • quit: (q) Exit GDB.

Breakpoints are places in your code where the debugger will stop and let you inspect the current state of variables and other things.

Basic Demo

Let's start a program, go in and inspect the contents of the variables and see what's happening.

NOTE: I'll be adding statements that start with ##, which indicates a comment that I've added, and it's not a part of the actual GDB output.

Enjoy the notes on this website? Consider supporting me in this adventure in you preferred way: Support me.