Compile With -g and Start Your First GDB Session
Build a C program with gcc -g, start it under GDB, stop at main, run it to the end and quit cleanly. The smallest complete session, with every output shown.

A debugger can only show you what the compiler wrote down. A plain executable knows the machine instructions it contains and, if the symbol table was not stripped, the names of its functions. It does not know that instruction 0x1151 came from line 13 of sum.c, or that the four bytes at some offset on the stack are a variable called total. That mapping is debug information, and GCC writes it only when you ask with -g. This post builds one small program with and without that flag, starts GDB on it, and walks through a complete first session: load, set a stop at main, run, look around, and quit. It uses GDB 16.3 and GCC 14 on x86-64 Linux with glibc 2.40; nothing here depends on those exact versions, but the output text is from that generation of GDB.
The program
Save this as sum.c. It adds six numbers through a function, so there is a call to stop in, a loop to look at and a result to print.
#include <stdio.h>
int sum(const int *v, int n)
{
int total = 0;
for (int i = 0; i < n; i++)
total += v[i];
return total;
}
int main(void)
{
int data[] = {4, 8, 15, 16, 23, 42};
int n = sizeof data / sizeof data[0];
int result = sum(data, n);
printf("sum = %d\n", result);
return 0;
}The numbers add to 108, so the expected output is a single line. Compile and run it the ordinary way first, so you know the program is correct before you debug it.
gcc -Wall -Wextra -o sum sum.c
./sumsum = 108What -g adds
The -g flag tells GCC to emit DWARF, the debugging format that GDB reads on Linux. DWARF is stored in extra sections of the ELF file, named .debug_info, .debug_line, .debug_abbrev and a few others. .debug_line is the table that maps every machine address to a source file and line. .debug_info describes functions, parameters, local variables and types: that n is an int, that data is an array of six of them, and where each lives relative to the frame. None of this changes the instructions the CPU runs, which is why a -g build is not slower than one without it. It only makes the file larger on disk, and the extra sections are not loaded into memory when the program runs.
Build the debug version next to the plain one.
gcc -g -O0 -Wall -Wextra -o sum_g sum.cThe -O0 is the default level and is written out here on purpose. Optimisation moves, merges and deletes code, so with -O2 a line you want to stop on may have no instructions at all, and a variable may live in a register for only part of its life. A first session should use -O0; the post on debugging optimised builds covers what changes when you cannot. If you want a little more information, -g3 also records macro definitions, which lets GDB expand a macro such as MAX when you print it. Plain -g is -g2 and is what most projects use.
You can confirm the difference by asking file or readelf.
file sum sum_g
readelf -S sum_g | grep debugsum: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=9d0c4b1f..., for GNU/Linux 3.2.0, not stripped
sum_g: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=3a7e51c2..., for GNU/Linux 3.2.0, with debug_info, not stripped
[28] .debug_aranges PROGBITS 0000000000000000 00003045
[29] .debug_info PROGBITS 0000000000000000 00003075
[30] .debug_abbrev PROGBITS 0000000000000000 00003155
[31] .debug_line PROGBITS 0000000000000000 000031f2
[32] .debug_str PROGBITS 0000000000000000 0000328bThe build ids and section offsets will differ on your machine; the words with debug_info and the .debug_* sections are what to look for. The second line of readelf output is truncated here, because each section also prints a size and flags line below it.
Starting GDB
Start GDB with the executable as its argument. The -q flag skips the long copyright banner, which is useful once you have read it twice.
gdb -q ./sum_gReading symbols from ./sum_g...
(gdb)"Reading symbols" is GDB loading the DWARF. Had you passed ./sum instead, you would see (No debugging symbols found in ./sum), and most of the commands below would still work on function names but not on source lines or local variables. The (gdb) at the end is the prompt. Nothing is running yet: GDB has parsed the file but has not started a process.
Stop at main, then run
Before running, say where to stop. break main sets a breakpoint, a place where GDB takes control back from the program. Then run starts the process.
(gdb) break main
Breakpoint 1 at 0x1155: file sum.c, line 13.
(gdb) run
Starting program: /home/dev/sum_g
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 1, main () at sum.c:13
13 int data[] = {4, 8, 15, 16, 23, 42};Reading it line by line: GDB placed breakpoint number 1 at an address and says which source line that address belongs to. Your address will differ, and in a position-independent executable the address printed before run is an offset that is relocated once the program loads. After run, GDB prints the program path, notes that it enabled thread debugging (current glibc always loads libthread_db, even for a single-threaded program), and reports the stop: breakpoint 1, function main, file sum.c, line 13, followed by the text of line 13.
Notice that it stopped at line 13 and not at the opening brace on line 12. GDB skips the function prologue, the instructions that build the stack frame, because the first source line with real work is where a person wants to look. The line shown is the line about to run, not the line that just ran. Nothing in main has executed yet, so data holds whatever was on the stack.
Look around and finish
You will learn the stepping and printing commands properly in the next posts of this series; for a first session, three are enough. next runs one source line, print shows an expression, and continue lets the program run until it stops again or exits.
(gdb) next
14 int n = sizeof data / sizeof data[0];
(gdb) next
15 int result = sum(data, n);
(gdb) print n
$1 = 6
(gdb) print data
$2 = {4, 8, 15, 16, 23, 42}
(gdb) continue
Continuing.
sum = 108
[Inferior 1 (process 41873) exited normally]$1 and $2 are the value history: every print stores its result under a numbered name, and you can reuse it later as $1. The program's own output, sum = 108, appears in the middle of GDB's text because they share one terminal. The last line says the process, which GDB calls an inferior, exited normally with status 0. A non-zero exit would read exited with code 01, and a crash would report a signal instead. The process id differs on every run.
Once the program has exited, print n would fail with No frame selected., because there is no stack to read any more. To debug again, type run; GDB keeps your breakpoints between runs.
Quit
quit, or q, or Ctrl-D, leaves GDB. If a process is still alive, GDB asks first.
(gdb) quitAfter the program has exited, it simply returns to your shell. If you quit while the program is stopped at a breakpoint, GDB prints A debugging session is active. followed by Inferior 1 [process 41873] will be killed. and Quit anyway? (y or n). Answering y kills the process. That is the right default for a program you started under GDB, and the wrong one for a service you attached to; the post on attaching with gdb -p covers detach.
Passing arguments and skipping the prompt
Programs usually take arguments. Give them to run, or set them on the command line with --args.
gdb -q --args ./sum_g first secondInside the session, run one two replaces the stored arguments and show args prints them. Redirection works too, so run < input.txt > out.txt feeds a file to the program as GDB starts it, the same way the shell would.
For a one-shot start that does not wait at the prompt, -ex runs a command as GDB starts, and -batch quits when the commands are done.
gdb -q -batch -ex 'break sum' -ex 'run' -ex 'bt' ./sum_gBreakpoint 1 at 0x1134: file sum.c, line 5.
Breakpoint 1, sum (v=0x7fffffffe0d0, n=6) at sum.c:5
5 int total = 0;
#0 sum (v=0x7fffffffe0d0, n=6) at sum.c:5
#1 0x0000555555555188 in main () at sum.c:15Stack addresses depend on your environment and on address-space randomisation, which GDB switches off for the process it starts, so the numbers above are typical for a GDB session and not for a plain run. This is a handy habit for reproducing a crash with the same commands every time.
When -g does not help
Three situations make -g less useful than it looks. A stripped binary has had its symbols removed after the build, and rebuilding with -g is the only fix unless a matching separate debug file exists; the post on debuginfod covers fetching those. An optimised build keeps the DWARF but loses the one-to-one match between source lines and instructions. And a library you link against, such as glibc, ships without debug information on most distributions, so stepping into printf shows only assembly unless you install the debug package or use debuginfod.
If your build system hides the compiler flags, check that your own CFLAGS reach the compile line. A Makefile that sets CFLAGS = -O2 in one place and -g in another is a common reason a "debug build" has no line numbers. For CMake, -DCMAKE_BUILD_TYPE=Debug adds -g; Release does not, and RelWithDebInfo adds -g with -O2.


