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
🌐
OnlineGDB
onlinegdb.com
GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++
Online GDB is online compiler and debugger for C/C++. You can compile, run and debug code with gdb online. Using gcc/g++ as compiler and gdb as debugger. Currently C and C++ languages are supported.
Online C
OnlineGDB is online IDE with c compiler. Quick and easy way to compile c program online. It supports gcc compiler for c.
Online C++
OnlineGDB is online IDE with C++ compiler. Quick and easy way to compiler c++ program online. It supports g++ compiler for c++.
Java
OnlineGDB is online IDE with java compiler. Quick and easy way to run java program online.
Python
OnlineGDB is online IDE with python compiler. Quick and easy way to compile python program online. It supports python3.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › gdb-step-by-step-introduction
GDB (Step by Step Introduction) - GeeksforGeeks
January 10, 2025 - GDB stands for GNU Project Debugger and is a powerful debugging tool for C (along with other languages like C++). It helps you to poke around inside your C programs while they are executing and also allows you to see what exactly happens when ...
🌐
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
While executing the program, the debugger will stop at the break point, and gives you the prompt to debug. So before starting up the program, let us place the following break point in our program. break 10 Breakpoint 1 at 0x804846f: file factorial.c, line 10. ... You can start running the program using the run command in the gdb ...
🌐
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
In Red Hat Developer Toolset, the GNU Debugger is provided by the devtoolset-12-gdb package and is automatically installed with devtoolset-12-toolchain as described in Section 1.5, “Installing Red Hat Developer Toolset”. To compile a C program with debugging information that can be read by the GNU Debugger, make sure the gcc compiler is run with the -g option:
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › unixhelp › howto_gdb.php
gdb (and ddd) guide
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 ...
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
🌐
University of Chicago
classes.cs.uchicago.edu › archive › 2017 › winter › 51081-1 › LabFAQ › lab2 › gdb.html
Debugging "C" And "C++" Programs Using "gdb"
Or perhaps the program takes very long time to run its initialization code, and starting it with a debugger attached to it will cause this startup time to be much much longer. There are also other reasons, but hopefully you got the point. In order to do that, we will launch the debugger in this way: gdb debug_me 9561 Here we assume that "debug_me" is the name of the program executed, and that 9561 is the process id (PID) of the process we want to debug.
Find elsewhere
🌐
HowtoForge
howtoforge.com › home › how to debug c programs in linux using gdb
How to Debug C Programs in Linux using gdb
I asked the debugger to continue the execution of the program until the next breakpoint, something which can be done using the 'c' command. ... I kept on doing this work, until I saw that the value of 'out' was zero. ... ... ... Breakpoint 1, main () at gdb-test.c:11 11 tot = tot + 0xffffffff/out; (gdb) print out $2 = 99 (gdb) c Continuing.
🌐
Baylor
cs.baylor.edu › ~donahoo › tools › gdb › tutorial.html
How to Debug Using GDB
By taking a closer look at the values printed above, we realize that we are computing fact=fact * j where fact has been initialized to 0; fact should have been initialized to 1. 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.
🌐
Wikipedia
en.wikipedia.org › wiki › GNU_Debugger
GNU Debugger - Wikipedia
1 month ago - The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, Assembly, C, C++, D, Fortran, Haskell, Go, Objective-C, OpenCL C, Modula-2, Pascal, Rust, and partially others.
🌐
The Geek Stuff
thegeekstuff.com › 2010 › 03 › debug-c-program-using-gdb
How to Debug C Program using gdb in 6 Simple Steps
August 27, 2010 - While executing the program, the debugger will stop at the break point, and gives you the prompt to debug. So before starting up the program, let us place the following break point in our program. break 10 Breakpoint 1 at 0x804846f: file factorial.c, line 10. ... You can start running the program using the run command in the gdb debugger.
🌐
GNU Project
sourceware.org › gdb
GDB: The GNU Project Debugger
Those programs might be executing ... or on a simulator. GDB can run on most popular UNIX and Microsoft Windows variants, as well as on macOS. GDB supports the following languages (in alphabetical order): ... Version 16.3 of GDB, the GNU Debugger, is now available for ...
🌐
UCSD
cseweb.ucsd.edu › classes › fa09 › cse141 › tutorial_gcc_gdb.html
Tutorial of gcc and gdb
The optimization options may differ in each platform. For example, the gcc under Mac OS also supports -Os and -Oz to allow optimization for code size. ... gcc is a debugger by GNU project. Gdb can step through your source code line-by-line or even instruction by instruction.
🌐
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).
🌐
BitDegree
bitdegree.org › learn › gdb-debugger
Tutorial on How to Use the GDB Debugger Easily
October 14, 2019 - This tutorial introduces the GDB debugger for checking the way C++ programs work. Learn to find bugs and solve quickly by analyzing code with GDB debugger.
🌐
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
🌐
YouTube
m.youtube.com › watch
GDB Debugging: How to Debug a C/C++ program
Share your videos with friends, family, and the world
Published   October 28, 2019
🌐
Jonathan Cook
cs.nmsu.edu › ~jcook › posts › gdb-intro
C/C++: The GDB Debugger • Jonathan Cook
January 31, 2023 - GDB is the Gnu DeBugger, a tool that lets you control the execution of your C/C++ program and inspect what it is doing at runtime. On top of that, it also allows you to inspect your program after it has had a catastrophic error such as a segmentation fault. Debuggers are extremely important ...