![]() |
Chapter 6 | ![]() |
Let's use an example program - examples make my life easier. Type in this program;
#includeint main(int argc,char *argv[]) { int a; a= 3/0; }
> gdb gdbex1 GNU gdb 4.18 Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... (gdb) run Starting program: /home/dughi/gdbexamples/gdbex1 Program received signal SIGFPE, Arithmetic exception. 0x80483a6 in main (argc=1, argv=0xbffffd04) at gdbex1.c:6 6         a= 3/0; (gdb) |
As you can see, gdb lists our problem line up front, along with the function it was called in, and the arguments (or pointer to arguments) that it was called with. Want to see more than just the line number it failed on? Use the gdb command 'list', and you'll see a few lines of code before and after the crash point.
(gdb) list 1       #include 2 3       int main(int argc,char *argv[]) { 4         int a; 5 6         a= 3/0; 7       } 8 (gdb) |
In this case of course, it shows the whole program. In larger programs it will show up to 10 lines, either after or around
(but including) the line with the error on it. Gdb uses the 'list' command to display the actual source code of your program; typing
it again will show the next 10 lines (in our example, it will give you an brief error, as line 9 does not exist). Typing 'list -'
will display the previous 10 lines. If you ever want to list a specific line, you can type that in too; 'list 6' will take me back to
the line I erred out on, even though it does very little in this example.
List also has a few other options, like listing a function, or a line in a specific file (or a function in a specific file!). To get a full list of options, type 'help list' at the gdb prompt.
![]() |
Index | ![]() |
6.3 Stopping Gdb | 6.4.2 Examining Variables |