Breakpoint
Learn about breakpoints and how they are useful for debugging by executing a code line by line.
The print
command in GDB prints out the value of a variable that’s defined in the current stack.
We’ll continue to use the “go.c” file from the previous lesson, as a reference for this lesson. We’ll try to print the s
variable (which is supposed to be a character buffer).
Let’s first however define a breakpoint.
Inserting a breakpoint
A breakpoint is a flag that will stop the program at a specific line of code. Let’s insert a breakpoint on line 9 of our code. This means the program will stop before executing that line of code and leave us in the GDB debugger. We use the GDB command break
to insert a breakpoint, and then the run
command to run the program:
/usercode$ gdb goGNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2Copyright (C) 2020 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.Type "show copying" and "show warranty" for details.This GDB was configured as "x86_64-linux-gnu".Type "show configuration" for configuration details.For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>.Find the GDB manual and other documentation resources online at:<http://www.gnu.org/software/gdb/documentation/>.For help, type "help".Type "apropos word" to search for commands related to "word"...Reading symbols from go...(gdb) break 9Breakpoint 1 at 0x11ee: file go.c, line 9.(gdb) runStarting program: /usercode/goBreakpoint 1, getWord (maxsize=256) at go.c:99 scanf("%s", s);(gdb)
Now (gdb)
prompt is waiting for input, and we are stopped just short of ...