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 - Go to your Linux command prompt and type "gdb". ... Gdb open prompt lets you know that it is ready for commands. To exit out of gdb, type quit or q. ... Below is a program that shows undefined behavior when compiled using C99.
🌐
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:
Discussions

debugging - How to step-into, step-over and step-out with GDB? - Unix & Linux Stack Exchange
I typed help while I was in the GDB but didn't find anything about step-into, step-over and step-out. I put a breakpoint in an Assembly program in _start (break _start). Afterwards I typed next and... More on unix.stackexchange.com
🌐 unix.stackexchange.com
July 24, 2016
How many of you actually use GDB from the terminal?
I use it in the terminal, but unless I'm just getting a backtrace from a core dump (most common use case), I usually press Ctrl-X A to enable eXtra-Awesome mode (I don't think it's actually called eXtra-Awesome mode, but that's how I remember the keystroke). I very rarely am stepping through code with gdb though, I tend to rely on printf's or just thinking really hard more often. Edit: looked it up, it's apparently called TUI mode More on reddit.com
🌐 r/C_Programming
79
87
February 9, 2022
How to use Ghidra with gdb?
GADP does not talk to gdb or gdbserver, it is a protocol to talk to another client that directly talks to gdb. IN-VM will directly talk to gdb, but if anything goes wrong it does have the chance of corrupting/crashing ghidra since it's JNI/native. All you should need to do is open your executable in the debugger tool. There should be a debugger menu that has a menu item Debug and there you select in GDB locally IN-VM or in GDB locally via GADP. Choose the GADP option and on that popup just click connect. You should get one more popup and click launch. More on reddit.com
🌐 r/ghidra
5
14
October 15, 2021
How do you manage and share your .gdbinit and GDB extensions?
If gdbinit is just a list of GDB commands can't you just store them in a text file in your code repo and have a shell script that boots GDB and feeds in the commands? Then you just run GDB from that shell script More on reddit.com
🌐 r/embedded
5
13
March 10, 2020
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
🌐
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 Toronto
cs.toronto.edu › ~krueger › csc209h › tut › gdb_tutorial.html
gdb tutorial
The infamous "Segmentation fault" ... kind of invalid memory access. Unfortunately, that is all the compiler tells us. Now, let's see how we can use gdb to spot the problem(s). Starting gdb: To start gdb for our crash.c, on the command prompt type "gdb crash". You'll see the following: $gdb crash GNU gdb Red Hat Linux ...
Find elsewhere
🌐
Timesys LinuxLink
linuxlink.timesys.com › docs › wiki › engineering › HOWTO_Use_GDBServer
How to Use GDB and GDBServer | Timesys LinuxLink
There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "--host=i486-linux-gnu --target=armv7l-timesys-linux-gnueabi". (gdb) file vim Reading symbols from /home/timesys/factory/build_armv7l-timesys-linux-gnueabi/vim-7.2/vim72/src/vim...done. (gdb) target remote 10.5.5.30:10000 Remote debugging using 10.5.5.30:10000 [New Thread 335] 0x400007c0 in ...
🌐
University of Michigan
web.eecs.umich.edu › ~sugih › pointers › summary.html
GDB Tutorial
You can give command line arguments to your program on the gdb command line the same way you would on the unix command line, except that you are saying run instead of the program name: run 2048 24 4 You can even do input/output redirection: run > outfile.txt. A ``breakpoint'' is a spot in your program where you would like to temporarily stop execution in order to check the values of variables, or to try to find out where the program is crashing, etc. To set a breakpoint you use the break command.
🌐
iO Flood
ioflood.com › blog › gdb-linux-command
Linux gdb: GNU Debugger Usage Guide (with Examples)
December 12, 2023 - In this guide, we’ll walk you ... 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....
🌐
UCSD
cseweb.ucsd.edu › classes › fa09 › cse141 › tutorial_gcc_gdb.html
Tutorial of gcc and gdb
All program to be debugged in gdb must be compiled by gcc with the option "-g" turning on. Continue with the "garbage" example, if we want to debug the program "garbage", we can simply start gdb by: gdb ./garbage Then, you will see the gdb environment similar as following: GNU gdb Red Hat Linux (6.3.0.0-1.162.el4rh) Copyright 2004 Free Software Foundation, Inc.
🌐
Medium
medium.com › havingfun › debugging-c-code-with-gdb-90adb2f3da96
Debugging C code With GDB | Having Fun | Having Fun
May 15, 2022 - Additionally, you may use -ggdb: https://stackoverflow.com/questions/668962/what-is-the-difference-between-gcc-ggdb-and-gcc-g ... This is the GDB prompt. This is where commands are sent to inspect your code by interacting 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 - 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.
🌐
Kali Linux
kali.org › tools › gdb
gdb | Kali Linux Tools
2 days ago - Currently, gdb supports C, C++, D, Objective-C, Fortran, Java, OpenCL C, Pascal, assembly, Modula-2, Go, and Ada. A must-have for any serious programmer. Installed size: 12.87 MB How to install: sudo apt install gdb
🌐
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.
🌐
Brendan Gregg
brendangregg.com › blog › 2016-08-09 › gdb-example-ncurses.html
gdb Debugging Full Example (Tutorial): ncurses
This GDB was configured as "x86\_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from /usr/bin/python...(no debugging symbols found)...done. Now to set the breakpoint using b (short for break): (gdb) b *doupdate + 289 No symbol table is loaded. Use the "file" command.
🌐
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.
🌐
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
September 28, 2018 - 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.
🌐
Visual Studio Code
code.visualstudio.com › docs › cpp › config-linux
Using C++ on Linux in VS Code
November 3, 2021 - In this tutorial, you will configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger on Linux.
🌐
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 ...