7.4. Listing Source Code

To view the source code of the program you are debugging, run the following command:
list
Before you start the execution of the program you are debugging, gdb displays the first ten lines of the source code, and any subsequent use of this command lists another ten lines. Once you start the execution, gdb displays the lines that are surrounding the line on which the execution stops, typically when you set a breakpoint.
You can also display the code that is surrounding a particular line. To do so, run the command in the following form:
list [file_name:]line_number
Similarly, to display the code that is surrounding the beginning of a particular function, run:
list [file_name:]function_name
Note that you can change the number of lines the list command displays by running the following command:
set listsize number

Example 7.4. Listing the Source Code of the fibonacci Binary File

The fibonacci.c file listed in Example 7.1, “Compiling a C Program With Debugging Information” has exactly 17 lines. Assuming that you have compiled it with debugging information and you want the gdb utility to be capable of listing the entire source code, you can run the following command to change the number of listed lines to 20:
(gdb) set listsize 20
You can now display the entire source code of the file you are debugging by running the list command with no additional arguments:
(gdb) list
1       #include <stdio.h>
2       #include <limits.h>
3
4       int main (int argc, char *argv[]) {
5         unsigned long int a = 0;
6         unsigned long int b = 1;
7         unsigned long int sum;
8
9         while (b < LONG_MAX) {
10          printf("%ld ", b);
11          sum = a + b;
12          a = b;
13          b = sum;
14        }
15
16        return 0;
17      }