gdb: Look for compilation directory relative to directory search path
The 'directory' command allows the user to provide a list of filesystem
directories in which to search for source code. The directories in this
search path are used as the base directory for the source filename from
the debug information (DW_AT_name). Thus the directory search path
provides alternatives to the existing compilation directory from the
debug information (DW_AT_comp_dir). Generally speaking, DW_AT_name
stores the filename argument passed to the compiler (including any
directory components), and DW_AT_comp_dir stores the current working
directory from which the compiler was executed. For example:
$ cd /path/to/project/subdir1
$ gcc -c a/test.c -g
The corresponding debug information will look like this:
DW_AT_name : a/test.c
DW_AT_comp_dir : /path/to/project/subdir1
When compiling with the -fdebug-prefix-map GCC option, the compilation
directory can be arbitrarily rewritten. In the above example, we may
rewrite the compilation directory as follows:
$ gcc -c a/test.c -g -fdebug-prefix-map=/path/to/project=
In this case, the corresponding debug information will look like:
DW_AT_name : a/test.c
DW_AT_comp_dir : /subdir1
This prevents GDB from finding the corresponding source code based on
the debug information alone. In some cases, a substitute-path command
can be used to re-map a consistent prefix in the rewritten compilation
directory to the real filesystem path. However, there may not be a
consistent prefix remaining in the debug symbols (for example in a
project that has source code in many subdirectories under the project's
root), thereby requiring multiple substitute-path rules. In this case,
it is easier to add the missing prefix to the directory search path via
the 'directory' command.
The function find_and_open_source currently searches in:
SEARCH_PATH/FILENAME
where SEARCH_PATH corresponds to each individual entry in the directory
search path (which is guaranteed to contain the compilation directory
from the debug information, as well as the current working directory).
FILENAME corresponds to the source filename (DW_AT_name), which may have
directory components in it. In addition, GDB searches in:
SEARCH_PATH/FILE_BASENAME
where FILE_BASENAME is the basename of the DW_AT_name entry.
This change modifies find_and_open_source to additionally search in:
SEARCH_PATH/COMP_DIR/FILENAME
where COMP_DIR is the compilation directory from the debug symbols. In
the example given earlier, running:
(gdb) directory /path/to/project
will now allow GDB to correctly locate the source code from the debug
information.
gdb/ChangeLog:
* source.c (prepare_path_for_appending): New function.
(openp): Make use of new function.
(find_and_open_source): Search for the compilation directory and
source file as a relative path beneath the directory search path.
gdb/doc/ChangeLog:
* gdb.texinfo (Source Path): Additional text to better describe
how the source path directory list is used when searching for
source files.
gdb/testsuite/ChangeLog:
* gdb.base/source-dir.exp: Add extra test for mapped compilation
directory.