Here is a quick start tutorial for gdb:

/* test.c  */
/* Sample program to debug.  */
#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char **argv) 
{
  if (argc != 3)
    return 1;
  int a = atoi (argv[1]);
  int b = atoi (argv[2]);
  int c = a + b;
  printf ("%d\n", c);
  return 0;
}

Compile with the -g3 option. g3 includes extra information, such as all the macro definitions present in the program.

gcc -g3 -o test test.c

Load the executable, which now contain the debugging symbols, into gdb:

gdb --annotate=3 test.exe 

Now you should find yourself at the gdb prompt. There you can issue commands to gdb. Say you like to place a breakpoint at line 11 and step through the execution, printing the values of the local variables - the following commands sequences will help you do this:

(gdb) break test.c:11
Breakpoint 1 at 0x401329: file test.c, line 11.
(gdb) set args 10 20
(gdb) run
Starting program: c:\Documents and Settings\VMathew\Desktop/test.exe 10 20
[New thread 3824.0x8e8]

Breakpoint 1, main (argc=3, argv=0x3d5a90) at test.c:11
(gdb) n
(gdb) print a
$1 = 10
(gdb) n
(gdb) print b
$2 = 20
(gdb) n
(gdb) print c
$3 = 30
(gdb) c
Continuing.
30

Program exited normally.
(gdb) 

In short, the following commands are all you need to get started using gdb:

break file:lineno - sets a breakpoint in the file at lineno.
set args - sets the command line arguments.
run - executes the debugged program with the given command line arguments.
next (n) and step (s) - step program and step program until it 
                        reaches a different source line, respectively. 
print - prints a local variable
bt -  print backtrace of all stack frames
c - continue execution.

Type help at the (gdb) prompt to get a list and description of all valid commands.

Answer from Vijay Mathew on Stack Overflow
🌐
Baylor
cs.baylor.edu › ~donahoo › tools › gdb › tutorial.html
How to Debug Using GDB
We quit GDB with the quit command. Next we need to change the following line: int fact = 1; Recompile the code and run it, you will get the expected output. This program causes a core dump due to a segmentation fault. We will try to trace the reason for this core dump. Download the program, from here. 1. Compile the program using the following command. ... 3. The core dump generates a file called corewhich can be used for debugging...
🌐
University of Michigan
web.eecs.umich.edu › ~sugih › pointers › summary.html
GDB Tutorial
Gdb will give you a prompt that looks like this: (gdb). From that prompt you can run your program, look at variables, etc., using the commands listed below (and others not listed). Or, you can start gdb and give it the name of the program executable you want to debug by saying
Top answer
1 of 5
33

Here is a quick start tutorial for gdb:

/* test.c  */
/* Sample program to debug.  */
#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char **argv) 
{
  if (argc != 3)
    return 1;
  int a = atoi (argv[1]);
  int b = atoi (argv[2]);
  int c = a + b;
  printf ("%d\n", c);
  return 0;
}

Compile with the -g3 option. g3 includes extra information, such as all the macro definitions present in the program.

gcc -g3 -o test test.c

Load the executable, which now contain the debugging symbols, into gdb:

gdb --annotate=3 test.exe 

Now you should find yourself at the gdb prompt. There you can issue commands to gdb. Say you like to place a breakpoint at line 11 and step through the execution, printing the values of the local variables - the following commands sequences will help you do this:

(gdb) break test.c:11
Breakpoint 1 at 0x401329: file test.c, line 11.
(gdb) set args 10 20
(gdb) run
Starting program: c:\Documents and Settings\VMathew\Desktop/test.exe 10 20
[New thread 3824.0x8e8]

Breakpoint 1, main (argc=3, argv=0x3d5a90) at test.c:11
(gdb) n
(gdb) print a
$1 = 10
(gdb) n
(gdb) print b
$2 = 20
(gdb) n
(gdb) print c
$3 = 30
(gdb) c
Continuing.
30

Program exited normally.
(gdb) 

In short, the following commands are all you need to get started using gdb:

break file:lineno - sets a breakpoint in the file at lineno.
set args - sets the command line arguments.
run - executes the debugged program with the given command line arguments.
next (n) and step (s) - step program and step program until it 
                        reaches a different source line, respectively. 
print - prints a local variable
bt -  print backtrace of all stack frames
c - continue execution.

Type help at the (gdb) prompt to get a list and description of all valid commands.

2 of 5
7

Start gdb with the executable as a parameter, so that it knows which program you want to debug:

gdb ./myprogram

Then you should be able to set breakpoints. For example:

b myfile.cpp:25
b some_function
🌐
GeeksforGeeks
geeksforgeeks.org › c language › gdb-step-by-step-introduction
GDB (Step by Step Introduction) - GeeksforGeeks
January 10, 2025 - In this article we have discussed GDB (GNU Debugger) which is a powerful tool in Linux used for debugging C programs. We have discussed some of the following steps so that we can compile your code with debugging information, run GDB, set breakpoint, examine variables, and analyze program behavior.
🌐
Red Hat
developers.redhat.com › articles › the-gdb-developers-gnu-debugger-tutorial-part-1-getting-started-with-the-debugger
The GDB developer's GNU Debugger tutorial, Part 1: Getting started with the debugger | Red Hat Developer
February 27, 2024 - This article is the first in a series demonstrating how to use the GNU Debugger (GDB) effectively to debug applications in C and C++. If you have limited or no experience using GDB, this series will teach you how to debug your code more efficiently.
🌐
BetterExplained
betterexplained.com › articles › debugging-with-gdb
Debugging with GDB – BetterExplained
Debug myprogram with “core” as the core dump file. ... Print the backtrace (function stack) at the point of the crash. Examine variables using the techniques above. Signals are messages thrown after certain events, such as a timer or error. GDB may pause when it encounters a signal; you may wish to ignore them instead.
🌐
University of Toronto
cs.toronto.edu › ~krueger › csc209h › tut › gdb_tutorial.html
gdb tutorial
We now show how to use break points to examine the values of variables we are interested in at the point we like to break. ... This sets a break point. Its basic functionality is to type break and a filename and line number. In our case we want to stop in crash.c line 22, we could do the following in gdb: (gdb) break crash.c:22 Breakpoint 1 at 0x804845b: file crash.c, line 22.
🌐
Medium
medium.com › havingfun › debugging-c-code-with-gdb-90adb2f3da96
Debugging C code With GDB | Having Fun | Having Fun
May 15, 2022 - Let’s continue the execution of the program by going to the next line: ... “5” at the start of the output signals what line of the code the debugger is in. If we now print the i variable, we’ll get the expected value: ... (gdb) next 6 i = 13; (gdb) print j $4 = 1 (gdb) next 8 printf("Hello World!\n"); (gdb) print i $5 = 13 · The command info can be used to print all variables' values
Find elsewhere
🌐
Red Hat
docs.redhat.com › en › documentation › red_hat_developer_toolset › 12 › html › user_guide › chap-gdb
Chapter 8. GNU Debugger (GDB) | User Guide | Red Hat Developer Toolset | 12 | Red Hat Documentation
This starts the gdb debugger in interactive mode and displays the default prompt, (gdb). To quit the debugging session and return to the shell prompt, run the following command at any time: ... Copy to Clipboard Copied! ... Note that you can execute any command using the scl utility, causing it to be run with the Red Hat Developer Toolset binaries used in preference to the Red Hat Enterprise Linux system equivalent.
🌐
BitDegree
bitdegree.org › learn › gdb-debugger
Tutorial on How to Use the GDB Debugger Easily
October 14, 2019 - You can debug your C++ program more efficiently if you use breakpoints. Breakpoints refer to instructions that stop the execution at specific moments. For instance, you should set breakpoints in places you suspect to be problematic. ... Setting a line at which execution stops. Setting a function name to stop execution at. Note: it is a common practice to set breakpoints before running the program. The following example sets a breakpoint at the start of the main function: ... Note: the GDB debugger accepts abbreviations of commands.
🌐
The Ohio State University
u.osu.edu › cstutorials › 2018 › 09 › 28 › how-to-debug-c-program-using-gdb-in-6-simple-steps
How to Debug C Program using gdb in 6 Simple Steps
Note: The above command creates ... below. Launch the C debugger (gdb) as shown below. ... Places break point in the C program, where you suspect errors. While executing the program, the debugger will stop at the break point, and gives you the prompt to debug....
🌐
Built In
builtin.com › software-engineering-perspectives › how-to-use-gdb
How to Use the GNU Debugger (GDB) | Built In
Before you can use GDB, you must compile your code with the -g flag. This flag tells the compiler to include debugging information — such as variable names, function names and line numbers — in the compiled executable.
Published   September 18, 2025
🌐
Kauffman77
kauffman77.github.io › tutorials › gdb.html
Quick Guide to gdb: The GNU Debugger
The below sample run in gdb illustrates the basics of running puzzlebox in the debugger. See the next section for some details on commands like next / run / break. >> gdb -tui ./puzzlebox # Start gdb with text user interface on the puzzlebox GNU gdb (GDB) 8.1 ... Reading symbols from ./puzzlebox...done. (gdb) set args input.txt # set command line arguments to the input.txt (gdb) run # run the program Starting program: puzzlebox input.txt UserID 'kauf0095' accepted: hash value = 1397510491 # program output PHASE 1: A puzzle you say?
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › unixhelp › howto_gdb.php
gdb (and ddd) guide
There are some example programs and some documentation on using gdb to debug them that you can copy from here: ... Getting Started with gdb C and C++ programs compiled with the GNU compiler and the -g option can be debugged using GNU's debugger gdb (actually, you can use gdb on code that is not compiled with -g, but unless you like trying to figure out how assembly code sequences map to your source code I wouldn't recommend doing so).
🌐
University of Michigan
web.eecs.umich.edu › ~sugih › pointers › gdbQS.html
gdb QuickStart
Print out this document. This is so you can have the instructions next to you without trying to flip between the web page and the IDE. Start gdb. Type "gdb [filename]" where [filename] is the name of the compiled file you wish to debug (the name you type to run your program).
🌐
Florida State University
cs.fsu.edu › ~myers › cop3330 › debug › debugger.html
How to use the GDB debugger - basics
To tell the compiler to include the symbol table, you use the -g flag when compiling. So, as an example: ... Once you have compiled an executable file that includes a debugging symbol table, you debug it by opening it in gdb.
🌐
Stanford University
web.stanford.edu › class › cs107 › resources › gdb
CS107 GDB and Debugging
This page will list the basic gdb commands you will need to use as you progress through CS107. However, it takes practice to become proficient in using the tool, and gdb is a large program that has a tremendous number of features. See the bottom of the page for more resources to help you master gdb. Compiling for gdb: gcc does not automatically put debugging information into the executable program, but our Makefiles all include the -g -Og flags that give gdb information about our programs so we can use the debugger efficiently.
🌐
ns-3
nsnam.org › wiki › HOWTO_use_gdb_to_debug_program_errors
HOWTO use gdb to debug program errors - Nsnam
gdb can be invoked on a program executable in the normal way (gdb <program-name>), and to do this with ns-3, it is easiest to use the ns3 shell (to set the library paths correctly). We demonstrate with an example of the third tutorial example. ./ns3 shell cd build/examples/tutorial gdb ...