If you are stopped in the inferior at a location where you cannot view the source then you can try the info source command, here's an example of its output:
(gdb) info source
Current source file is hello.c
Compilation directory is /tmp
Source language is c.
Producer is GNU C17 9.3.1 20200408 (Red Hat 9.3.1-2) -mtune=generic -march=x86-64 -g3 -O0.
Compiled with DWARF 4 debugging format.
Includes preprocessor macro info.
(gdb)
The other interesting setting is:
(gdb) show directories
Source directories searched: $cdir:$cwd
The directories setting controls where GDB looks for source files. The default value consists of $cdir, the compilation directory, and $cwd, the current working directory.
So, in our example the source file is hello.c and the compilation directory is /tmp. If our current working directory is /root then GDB will look in the following locations, in this order:
- /tmp/hello.c
- /root/hello.c
If you repeat this experiment and you think that the source files should be locatable then you can update the question with this information.
If the source files are not located in one of the paths GDB will check then you can (a) move the source to a location where GDB can find it, or (b) tell GDB where the source is.
To do (b) we use the directory command, like this:
(gdb) directory /blah
Source directories searched: /blah:$cdir:$cwd
This has added /blah/ to the start of the list of locations to check, so now GDB will check these locations, in this order:
- /blah/hello.c
- /tmp/hello.c
- /root/hello.c
If you want to remove entries from the directories list then you can use set directories. Unlike the directories command which just adds new entries to the front of the list, set directories allows you to completely rewrite the list.
The above is the most likely solution to missing source files, but GDB also has the ability to rewrite source paths if needed. The full documentation of GDB's source path handling can be found here.