Set Breakpoints by Function, File and Line in GDB
Use GDB's break command with a function name, a line number, file:line, an address or an offset, then list, disable, delete and make breakpoints temporary.

A breakpoint is a promise to stop. You tell GDB a place in the program, and when execution reaches that place GDB takes control, reports where it is and waits for your next command. Everything else in a debugging session hangs off that first stop, so it is worth knowing every way to name a place. GDB calls the place a location, and break accepts about a dozen forms of it. This post covers the ones you will use daily: a function, a line, a file and line, a relative offset and a raw address. It then covers how to list, disable, delete and make breakpoints that remove themselves. Output is from GDB 16.3 on x86-64 Linux, debugging programs built with GCC 14 and -g -O0.
The program
Two source files give us a reason to name a file. main.c calls into stats.c.
#ifndef STATS_H
#define STATS_H
int clamp(int value, int low, int high);
int total(const int *values, int count);
#endif#include "stats.h"
int clamp(int value, int low, int high)
{
if (value < low)
return low;
if (value > high)
return high;
return value;
}
int total(const int *values, int count)
{
int sum = 0;
for (int i = 0; i < count; i++)
sum += clamp(values[i], 0, 100);
return sum;
}#include <stdio.h>
#include "stats.h"
int main(void)
{
int readings[] = {12, 250, 37, -5, 90};
int count = sizeof readings / sizeof readings[0];
int sum = total(readings, count);
printf("total = %d\n", sum);
return 0;
}The readings 250 and -5 are clamped to 100 and 0, so the total is 12 + 100 + 37 + 0 + 90 = 239. Build with debug information and start GDB.
gcc -g -O0 -Wall -Wextra -o stats main.c stats.c
gdb -q ./statsReading symbols from ./stats...
(gdb)Break on a function
The simplest location is a function name. GDB resolves it to the first line of the function body after the prologue, and the command can be written break or just b.
(gdb) break clamp
Breakpoint 1 at 0x1139: file stats.c, line 5.
(gdb) run
Starting program: /home/dev/stats
Breakpoint 1, clamp (value=12, low=0, high=100) at stats.c:5
5 if (value < low)The stop line shows the function with its arguments at that moment, which is often all you needed. A function name that exists in more than one file produces a single breakpoint with several locations, which GDB reports as Breakpoint 1 at 0x...: clamp. (2 locations). If two static functions in different files share a name, say which with the file:function form below.
Break on a line
A bare number means a line in the current source file. Before the program runs, "current" is the file that holds main, so in this session break 8 would mean line 8 of main.c. The unambiguous form includes the file.
(gdb) break stats.c:16
Breakpoint 2 at 0x1167: file stats.c, line 16.
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000001139 in clamp at stats.c:5
breakpoint already hit 1 time
2 breakpoint keep y 0x0000000000001167 in total at stats.c:16Line 16 is sum += clamp(values[i], 0, 100);. A stop there happens once per loop iteration, before the call. If you name a line that has no code, such as a blank line or a closing brace of a declaration, GDB moves the breakpoint to the next line that does, and it reports the line it actually chose. Always read the reply: Breakpoint 2 at 0x1167: file stats.c, line 17. tells you where it landed, which may not be where you asked.
The file:function form picks a function within one file.
(gdb) break stats.c:total
Breakpoint 3 at 0x115b: file stats.c, line 14.Offsets and the next line
break +2 sets a breakpoint two lines after the line of the current stop, and break -1 one line before. With no argument, break sets one at the address where the selected frame will resume, which is the instruction after the call you are paused in. In a stopped frame that is a convenient way to say "stop when I get back here" without counting lines.
(gdb) continue
Continuing.
Breakpoint 2, total (values=0x7fffffffe0d4, count=5) at stats.c:16
16 sum += clamp(values[i], 0, 100);
(gdb) break +1
Breakpoint 4 at 0x1180: file stats.c, line 17.Line 17 is return sum;, the line after the stop. Offsets count source lines, not executions: the loop will run that line only once, after the last iteration, so this breakpoint does not fire on the next continue.
Break on an address
When you have no source, or want an exact instruction, put an asterisk before an address or expression.
(gdb) break *0x1139
Breakpoint 5 at 0x1139: file stats.c, line 5.
(gdb) break *clamp
Breakpoint 6 at 0x1129: file stats.c, line 3.break *clamp stops at the very first instruction of the function, before the prologue, which is why GDB reports line 3, the line of the function header. That is the place to stop when you want to read the arguments straight from registers as the calling convention left them, or when the prologue itself is the thing you are studying. Addresses in a position-independent executable are offsets until the program starts, and absolute afterwards, so set address breakpoints after run when you take the number from info registers or a backtrace.
Manage the list
info breakpoints (short form i b) is the table you saw above. Each breakpoint has a number, and commands take the number.
(gdb) disable 5 6
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000001139 in clamp at stats.c:5
breakpoint already hit 1 time
2 breakpoint keep y 0x0000000000001167 in total at stats.c:16
breakpoint already hit 1 time
3 breakpoint keep y 0x000000000000115b in total at stats.c:14
4 breakpoint keep y 0x0000000000001180 in total at stats.c:17
5 breakpoint keep n 0x0000000000001139 in clamp at stats.c:5
6 breakpoint keep n 0x0000000000001129 in clamp at stats.c:3The Enb column shows y or n. disable keeps a breakpoint but ignores it, which is better than deleting one you will want again. enable brings it back, and enable once 3 re-enables it for a single hit. delete 5 removes one, delete with no argument removes all of them after asking, and clear clamp removes every breakpoint at a location by naming it, which is handy when you no longer remember the number.
The Disp column, short for disposition, says what happens after a hit: keep stays, del removes it, dis disables it. Two commands create the other kinds.
Temporary breakpoints
tbreak sets a breakpoint that deletes itself after the first hit. start is tbreak main followed by run, which is the usual way to begin.
(gdb) tbreak main
Temporary breakpoint 1 at 0x1155: file main.c, line 6.
(gdb) run
Starting program: /home/dev/stats
Temporary breakpoint 1, main () at main.c:6
6 int readings[] = {12, 250, 37, -5, 90};
(gdb) info breakpoints
No breakpoints or watchpoints.Once it fires, it is gone and the table is empty. tbreak is the right tool for "get me to this one place once," such as reaching the start of a function that is called hundreds of times without stopping on every call. until and advance do similar jobs for a place in the current function; the next post in this series, on stepping, covers them.
Pending breakpoints and shared libraries
A function in a shared library is not known until the library loads. If you type break puts before run on a dynamically linked program, GDB may answer Function "puts" not defined. and ask Make breakpoint pending on future shared library load? (y or [n]). Answering y creates a pending breakpoint that resolves later. In scripts, set breakpoint pending on answers yes automatically, which avoids a prompt that would block a batch run. The related setting set breakpoint pending off makes it an error instead.
When not to use a breakpoint
A breakpoint at a hot function stops every call and quickly becomes useless. A conditional breakpoint, covered in a later post, or a temporary one narrows it. A breakpoint also changes timing, so it is a poor tool for a race between threads; logging with dprintf stops nothing and may expose the same bug. And for a crash with no obvious place to stop, you do not need a breakpoint at all: run to the crash and read the backtrace.


