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
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 › linux-unix › gdb-command-in-linux-with-examples
gdb command in Linux with examples - GeeksforGeeks
September 2, 2024 - gdb console can be opened using the command gdb command. To debug the executables from the console, file [executable filename] command is used.
🌐
Sergioprado
sergioprado.blog › home › debugging the linux kernel with gdb
Debugging the Linux kernel with GDB - sergioprado.blog
January 17, 2024 - Connect to the kernel debugger using a GDB client in the host. Let’s start by enabling KGDB support in the Linux kernel. To use KGDB, it is necessary to build the kernel with a few configuration options enabled:
🌐
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.
🌐
University of Toronto
cs.toronto.edu › ~krueger › csc209h › tut › gdb_tutorial.html
gdb tutorial
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1". (gdb) Good! We have successfully loaded gdb with crash. Let's run the program with command "run" to see what kind of information we will get. ... The "run" command starts the program. If we do not set up any "breakpoints" (we'll see how to use this command later) the program will run until it terminates or core dumps.
🌐
Linux Man Pages
man7.org › linux › man-pages › man1 › gdb.1.html
gdb(1) - Linux manual page
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 ...
🌐
University of Michigan
web.eecs.umich.edu › ~sugih › pointers › summary.html
GDB Tutorial
To start gdb, just type gdb at the unix prompt. 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 ...
🌐
iO Flood
ioflood.com › blog › gdb-linux-command
Linux gdb: GNU Debugger Usage Guide (with Examples)
December 12, 2023 - Let’s get started and master the GDB Linux command! To use GDB in Linux, you first compile your code with the -g flag, then run it with gdb.
Find elsewhere
🌐
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 - Note that --batch will silence even more output than -q to facilitate using GDB in scripts: $ # All commands complete without error $ gdb -batch -x hello.gdb myprogram Reading symbols from myprogram... hello $ echo $? 0 $ # Command raises an exception $ gdb -batch -ex "set foo bar" No symbol "foo" in current context. $ echo $? 1 $ # Demonstrate the order of script execution $ gdb -x hello.gdb -iex 'echo before\n' -ex 'echo after\n' simple GNU gdb (GDB) Red Hat Enterprise Linux 9.2-2.el8 Copyright (C) 2020 Free Software Foundation, Inc.
🌐
TutorialsPoint
tutorialspoint.com › unix_commands › gdb.htm
gdb Command in Linux
If you want to see the call stack and understand the sequence of function calls leading to the current point, simply run the backtrace command − ... Running the above command will show the call stack, which is useful for debugging. Once you are done with your debugging session, you can exit gdb using the quit command − ... This will exit the gdb session. Thats how you can use the gdb command on your Linux system.
🌐
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.
🌐
Kali Linux
kali.org › tools › gdb
gdb | Kali Linux Tools
2 days ago - --configuration Print details about GDB configuration and then exit. --help Print this message and then exit. --version Print version information and then exit. Remote debugging options: -b BAUDRATE Set serial port baud rate used for remote debugging. -l TIMEOUT Set timeout in seconds for remote debugging. Other options: --cd=DIR Change current directory to ...
🌐
Yolinux
yolinux.com › TUTORIALS › GDB-Commands.html
Linux Tutorial - GNU GDB Debugger Command Cheat Sheet
Sl Jun07 1:18 /opt/bin/myapp [prompt]$ gdb /opt/bin/myapp 2812 OR [prompt]$ gdb /opt/bin/myapp --pid=2812 · Command line options: (version 6. Older versions use a single "-") ... Compile with the "-g" option (for most GNU and Intel compilers) which generates added information in the object code so the debugger can match a line of source code with the step of execution. Do not use compiler optimization directive such as "-O" or "-O2" which rearrange computing operations to gain speed as this reordering will not match the order of execution in the source code and it may be impossible to follow.
🌐
Baylor
cs.baylor.edu › ~donahoo › tools › gdb › tutorial.html
How to Debug Using GDB
The distinction doesn't matter here since there are no functions. You may use the shortest, unambigious spelling of a GDB command to save some typing. Here we use n and s instead of next and step, respectively. If the command is simply a repeat of the previous command, you can just hit return, ...
🌐
Timesys LinuxLink
linuxlink.timesys.com › docs › wiki › engineering › HOWTO_Use_GDBServer
How to Use GDB and GDBServer | Timesys LinuxLink
(gdb) target remote 10.5.5.30:10000 Remote debugging using 10.5.5.30:10000 [New Thread 335] 0x400007c0 in _start () from ~/timesys/omap3530zoom/toolchain/lib/ld-linux.so.3 (gdb)
🌐
LabEx
labex.io › tutorials › linux-linux-gdb-command-with-practical-examples-422698
Linux gdb Command with Practical Examples | LabEx
In this lab, we will introduce ... We will start by ensuring that gdb is installed on our Ubuntu 22.04 Docker container, then create a simple C program with a runtime error and use gdb to debug it....
🌐
Baeldung
baeldung.com › home › scripting › debugging programs using the gdb command
Debugging Programs Using the GDB Command | Baeldung on Linux
March 18, 2024 - Since we’re going to do post-release debugging, we should first compile our example. To that end, we’ll use gcc (GNU C Compiler). To make full use of GDB, it’s best we compile with the -g or -ggdb flags to gcc.
🌐
LinuxConfig
linuxconfig.org › home › gdb debugging tutorial for beginners
GDB debugging tutorial for beginners
September 21, 2025 - Learn how to effectively debug C/C++ using GDB on Linux. Discover key GDB commands like backtrace & frame inspection to resolve core dumps.