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
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › c language › gdb-step-by-step-introduction
GDB (Step by Step Introduction) - GeeksforGeeks
January 10, 2025 - Here are a few useful commands to get started with GDB. Now, type "l" at gdb prompt to display the code. ... Let's introduce a break point, say line 5.
🌐
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 a.out file which will be used for debugging as shown 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.
🌐
Reddit
reddit.com › r/c_programming › a quick intro to gdb.
r/C_Programming on Reddit: A quick intro to gdb.
May 28, 2023 -

GDB Quick Guide

I found out I didn't need cgdb by the way, it was better to just gdb -tui on Debian. Less modem noise in the form of uninterpreted escape sequences too.

So it paid up to do gdb -help.

The guide doesn't mention watch or watch -l but it points to Debugging with GDB where you can find everything.

It is a good quick start, with a couple of examples.

🌐
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
This is a brief description of some of the most commonly used features of gdb. To prepare your program for debugging with gdb, you must compile it with the -g flag. So, if your program is in a source file called memsim.c and you want to put the executable in the file memsim, then you would ...
🌐
Caltech
users.cms.caltech.edu › ~mvanier › CS11_C › misc › gdb.html
CS 11 C track: Using gdb for debugging
It’s a good habit to always use these options.) The -g option puts debugging information into the executable. Most importantly, it puts the text of the source code file into the executable so you can examine it as the program executes (we’ll see how below). Type gdb myprog (for the example ...
🌐
University of Toronto
cs.toronto.edu › ~krueger › csc209h › tut › gdb_tutorial.html
gdb tutorial
In order to run crash.c with gdb, we must compile it with the -g option which tells the compiler to embed debugging information for the debugger to use.
Find elsewhere
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
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › unixhelp › howto_gdb.php
gdb (and ddd) guide
Also, do not compile with an optimization flag (i.e. don't use -O2), or gdb will have a hard time mapping optimized machine code to your source code. For example: ... To start gdb, invoke gdb on the executable file.
🌐
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.
🌐
HowtoForge
howtoforge.com › home › how to debug c programs in linux using gdb
How to Debug C Programs in Linux using gdb
For example, in case of the gcc compiler, which we'll be using to compile the example C program later in this tutorial, you need to use the -g command line option while compiling your code. To know what the gcc compiler's manual page says about this command line option, head here. Next step is to make sure that you have GDB installed on your system.
🌐
Suchprogramming
suchprogramming.com › debugging-with-gdb-part-1
Such Programming - Debugging C Programs with GDB – Part 1
To provide more information to the debugger about this program I’ll use the -g flag when building it. # Makefile CC=gcc -g -o $@ -Wall $< all: test test: test.c $(CC) debug: test gdb -q ./test
🌐
TutorialsPoint
tutorialspoint.com › gnu_debugger › gdb_quick_guide.htm
GDB - Quick Guide
An ANSI-compliant C compiler (gcc is recommended - note that gdb can debug codes generated by other compilers) 115 MB of free disk space is required on the partition on which you're going to build gdb. 20 MB of free disk space is required on the partition on which you're going to install gdb. ... The make utility - the GNU version is known to work without a problem, others probably do as well. step 2: Download the gdb source distribution from ftp.gnu.org/gnu/gdb. (We used gdb-6.6.tar.gz for these instructions.)
🌐
UCSD
cseweb.ucsd.edu › classes › fa09 › cse141 › tutorial_gcc_gdb.html
Tutorial of gcc and gdb
For example, if we want to break at the beginning of main function in garbage.c, we can also try below: (gdb) break *0x1f7b Breakpoint 1 at 0x1f7b: file garbage.c, line 8. To show the current breakpoints we have, we may use the "info breakpoint" command as:
🌐
University of Chicago
classes.cs.uchicago.edu › archive › 2017 › winter › 51081-1 › LabFAQ › lab2 › gdb.html
Debugging "C" And "C++" Programs Using "gdb"
Once we invoked the debugger, we can run the program using the command "run". If the program requires command line parameters (like our debug_me program does), we can supply them to the "run" command of gdb. For example: run "hello, world" "goodbye, world" Note that we used quotation marks to denote that "hello, world" is a single parameter, and not to separate parameters (the debugger assumes white-space separates the parameters).
🌐
YouTube
youtube.com › low level
GDB is REALLY easy! Find Bugs in Your Code with Only A Few Commands - YouTube
Join me and learn how to debug a program written in C using GDB. In this video, we go over how to compile a program written in C so that GDB can present it, ...
Published   April 17, 2021
Views   230K
🌐
Built In
builtin.com › software-engineering-perspectives › how-to-use-gdb
How to Use the GNU Debugger (GDB) | Built In
Create a company profile. ... GDB, short for GNU Debugger, is a debugger tool used to inspect a program’s internal state while it is running, allowing users to find and fix bugs in languages such as C, C++, Ada, Go and Fortran.
Published   September 18, 2025