--------------------------------------------------------------------------------
__ _____ __ _ / /___ _ / __(_)___ ____ ____/ /___ _____ (_) / / __ `/ / /_/ / __ \/_ / / __ / __ `/ __ \/ / / / /_/ / / __/ / / / / / /_/ /_/ / /_/ / / / / / /_/\__,_/ /_/ /_/_/ /_/ /___/\__,_/\__,_/_/ /_/_/
-------------------------------------------------------------------------------- | blog | blog_archive | about | contact | tutoring | --------------------------------------------------------------------------------
If you've never run GDB before, you can start it up with the following command:gdb <program>
If you've been compiling to a.out
then you'd start GDB up withgdb ./a.out
Make sure to add the -g
flag when compiling your program! This would look something likeg++ -g my_program.cpp
And here's some conventions I'm using:
[arg]
-- This is an optional argument<arg>
-- This is a required argument<foo|bar>
-- This is a required argument that can be either foo
or bar
There's a few tricks I've learned that make GDB easier to use. First of all, you don't actually need to type out the full command name to run a command. You can type the first few characters of a command, or potentially even a single character. This means instead of typing layout
you can type lay
, and instead of typing break
you can type b
.
Sometimes you want to run the same command a bunch of times in a row. Typing next
a bunch of times is a pain. However, if you enter an empty command and just hit Enter
then GDB will repeat the last command. Instead of typing next
a bunch of times, you can type it a single time and then repeatedly hit the Enter
key.
run [args]
-- This command will start your program. You can optionally specify arguments, these will get passed as command-line arguments to your program.
layout src
-- This command displays the source code of your program above the main GDB command window.
backtrace
-- This command will show all function calls that were made to reach the current location. This is particularly useful if your code breaks inside of a library--you can see which of your functions called the library's function.
frame <frame number>
-- Each function that backtrace shows you has its own stack frame, with a frame number shown in the leftmost column. You can enter one of these frames with the frame
command, bringing GDB back to that function. This shows you the relevant code and lets you look at variables inside of that function.
print <variable>
-- This command prints out the contents of a variable. Any valid C/C++ will work here, you can access arrays (a[10]
), access fields of structs or classes (person.name
), and even dereference pointers (*ptr
).
display <variable>
-- This command works similarly to print
. However, display
will print out your variable whenever you move to a new line of code.
break <function|function:label|file:line|line>
-- This command sets a breakpoint at the desired location. GDB will stop execution once the breakpoint is hit, letting you inspect problematic areas of code. The breakpoint location can be given in a couple of forms. You can use function names as breakpoint locations, or you can give a filename and line number in the form file.cpp:42
. If you find yourself using file:line
on the same location repeatedly, consider adding a label there, and using function:label
to give it a stable location and convenient name.
continue
-- This will continue the execution of your program after a breakpoint is hit.
del [breakpoint number]
-- This will either delete all breakpoints if given no arguments, or delete a single breakpoint specified by the breakpoint number. When creating a new breakpoint GDB will show you its number.
step [count]
-- This will make GDB move to the next line of code, moving into any functions encountered. A count
can be specified to step
multiple times
next [count]
-- This command works almost the same as step
. However, next
will step over functions. This can be useful when you want to avoid stepping through library functions.
help [command]
-- If no command
is given this will show you information about all GDB commands. Otherwise, if a command
is given, help
will show more information about that specific command.
layout <reg|asm>
-- There are layouts designed specifically for debugging assembly code and for reverse engineering programs. Similar to src
, asm
will show the relevant assembly code in a pane above the main GDB window. reg
shows both the assembly code and the values of all registers, each in their own pane.
x[/FMT] <memory location|variable|$register>
-- x is used to examine the value of a variable or memory location. x
, like print
, is used to print out information about your program. However, x
will work with arbitrary memory locations and allows you to examine the contents of registers. The FMT
string lets you change how the value is displayed, you can display values as hex, decimal, and in many different sizes. help x
has more information on FMT
.
info registers
-- This command prints out the values of all registers.
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. If you're interested in modifying this document you can obtain the original markdown at https://finzdani.net/gdb.md