5.3 Setting a breakpoint
A breakpoint stops the execution of a program and returns control
to the debugger, where its variables and memory can be examined before
continuing. Breakpoints can be set for specific functions, lines or
memory locations with the break
command.
To set a breakpoint on a specific function, use the command break
function-name
. For example, the following command sets a
breakpoint at the start of the main
function in the program
above:
$ gdb a.out
(gdb) break main
Breakpoint 1 at 0x80483c6: file null.c, line 6.
The debugger will now take control of the program when the function
main
is called. Since the main
function is the first
function to be executed in a C program the program will stop immediately
when it is run:
(gdb) run
Starting program: a.out
Breakpoint 1, main () at null.c:6
6 int *p = 0; /* null pointer */
(gdb)
The display shows the line that will be executed next (the line number
is shown on the left). The breakpoint stops the program before
the line is executed, so at this stage the pointer p
is
undefined and has not yet been set to zero.