A breakpoint is like a tripwire within a program: You set a breakpoint at a particular "place" within your program, and when execution reaches that point, the debugger will pause the program's execution (and will, in the case of a text-based debugger such as GDB, give you a command prompt).
GDB is very flexible about the meaning of "place"; it could mean things as varied as a line of source code, an address of code, a line number within a source file, or the entry into a function.
A snippet of a debug session is shown below to illustrate what happens when GDB breaks at a line of code. In the snippet, we list part of the source code, put a breakpoint at line 35 of the program, and then run the program. GDB hits the breakpoint and pauses.
(gdb) list 30 31 /* Get the size of file in bytes */ 32 if ((fd = open(c.filename, O_RDONLY)) == -1) 33 (void) die(1, "Can't open file."); 34 (void) stat(c.filename, &fstat); 35 c.filesize = fstat.st_size; 36 (gdb) break 35 Breakpoint 1 at 0x8048ff3: file bed.c, line 35. (gdb) run Starting program: binary_editor/bed Breakpoint 1, main (argc=1, argv=0xbfa3e1f4) at bed.c:35 35 c.filesize = fstat.st_size; (gdb)
Let's be very clear about what happened here: GDB executed lines 30 through 34, but line 35 has not executed yet. This can be confusing since many people think that GDB displays the line of code that was last executed, when in fact, it shows which line of code is about to be executed. In this case, GDB is telling us that line 35 is the next line of source code to execute. When GDB's execution hits a breakpoint at line 35, you can think of GDB sitting and waiting between lines 34 and 35 of the source code.
However, as you may know, GDB works with machine language instructions, not lines of source code, and there may be several lines of machine language for a single line of code. GDB can work with lines of source code because of additional information included in the executable. While this fact may not seem terribly important now, it will have implications when we discuss stepping through your program throughout this chapter.