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
🌐
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.
🌐
HowtoForge
howtoforge.com › home › how to debug c programs in linux using gdb
How to Debug C Programs in Linux using gdb
So, to debug the code, the first step would be to compile the program with -g. Here's the command: ... Next up, let's run GDB and let it know which executable we want to debug.
🌐
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 ...
🌐
Medium
medium.com › havingfun › debugging-c-code-with-gdb-90adb2f3da96
Debugging C code With GDB | Having Fun | Having Fun
May 15, 2022 - The most useful, perhaps, will be to set breakpoints at the entry of functions, e.g. break main. Now running the code, it should stop at the breakpoint at #4 · (gdb) run Thread 2 hit Breakpoint 1, main () at source.c:4 4 int i = 0;
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
🌐
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
By continuing or stepping through you could have found that the issue is because we have not used the <= in the ‘for loop’ condition checking. So changing that from < to <= will solve the issue. Use following shortcuts for most of the frequent gdb operations.
🌐
Ubuntu
manpages.ubuntu.com › manpages › lunar › man1 › gdb.1.html
Ubuntu Manpage: gdb - The GNU Debugger
You can run "gdb" with no arguments or options; but the most usual way to start GDB is with one argument or two, specifying an executable program as the argument: gdb program You can also start with both an executable program and a core file specified: gdb program core You can, instead, specify a process ID as a second argument or use option "-p", if you want to debug a running process: gdb program 1234 gdb -p 1234 would attach GDB to process 1234.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › gdb-command-in-linux-with-examples
gdb command in Linux with examples - GeeksforGeeks
September 2, 2024 - To start the debugger of the above 'gfg' executable file, enter the command 'gdb gfg'. It opens the gdb console of the current program, after printing the version information. This command runs the current executable file.
🌐
Baylor
cs.baylor.edu › ~donahoo › tools › gdb › tutorial.html
How to Debug Using GDB
Watching changes We can step through the program and examine the values using the print command. (gdb) n 9 for (int j = 0; j <= number; j++) { (gdb) n 10 fact = fact * j; (gdb) n 9 for (int j = 0; j <= number; j++) { (gdb) print fact $2 = 0 (gdb) n 13 return fact; (gdb) quit The print command (abbreviated p) reveals that the value of fact never changes.
🌐
VITUX
vitux.com › how-to-use-gdb-to-debug-programs-in-ubuntu
How to Use GDB to Debug Programs in Ubuntu 20.04 – VITUX
Usually, a C code is compiled in GCC using the following command: ... Another argument needs to be provided to include symbols in the binary. These symbols are used by GDB to track and debug the program.
🌐
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:
🌐
LinuxConfig
linuxconfig.org › home › gdb debugging tutorial for beginners
GDB debugging tutorial for beginners
September 21, 2025 - As you can see, on the first line we called gdb with as first option our binary and as second option the core file. Simply remember binary and core. Next we see GDB initialize, and we are presented with some information. You can use the apropos command to search for related GDB commands if you need help.
🌐
Gdbtutorial
gdbtutorial.com › tutorial › how-use-gdb
How to use GDB? | GDB Tutorial
Below steps will guide how to run program with GDB. Step 1: Compile and Build program with debugging symbols $ gcc -g main.cYou can see -g flag is provided to compile program. This will generate debug symbols of program. Which is necessary to debug any program with GDB.
🌐
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.
🌐
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.

🌐
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
🌐
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.
🌐
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.