aboutsummaryrefslogtreecommitdiff
path: root/gdb/source.c
diff options
context:
space:
mode:
authorMike Gulick <mgulick@mathworks.com>2019-09-12 11:16:06 -0400
committerAndrew Burgess <andrew.burgess@embecosm.com>2019-09-17 16:20:19 -0400
commitf1b620e9b4eea4bfd2f35a3039672fa8a5925bcb (patch)
treeab68956943ff5f00bad36d0e6b7165c260a4c963 /gdb/source.c
parent67f3ed6afef86d08ef9989cc251eac585e9ef9cf (diff)
downloadbinutils-f1b620e9b4eea4bfd2f35a3039672fa8a5925bcb.zip
binutils-f1b620e9b4eea4bfd2f35a3039672fa8a5925bcb.tar.gz
binutils-f1b620e9b4eea4bfd2f35a3039672fa8a5925bcb.tar.bz2
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.
Diffstat (limited to 'gdb/source.c')
-rw-r--r--gdb/source.c69
1 files changed, 58 insertions, 11 deletions
diff --git a/gdb/source.c b/gdb/source.c
index b27f210..0171f27 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -654,6 +654,36 @@ info_source_command (const char *ignore, int from_tty)
}
+/* Helper function to remove characters from the start of PATH so that
+ PATH can then be appended to a directory name. We remove leading drive
+ letters (for dos) as well as leading '/' characters and './'
+ sequences. */
+
+const char *
+prepare_path_for_appending (const char *path)
+{
+ /* For dos paths, d:/foo -> /foo, and d:foo -> foo. */
+ if (HAS_DRIVE_SPEC (path))
+ path = STRIP_DRIVE_SPEC (path);
+
+ const char *old_path;
+ do
+ {
+ old_path = path;
+
+ /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
+ while (IS_DIR_SEPARATOR(path[0]))
+ path++;
+
+ /* ./foo => foo */
+ while (path[0] == '.' && IS_DIR_SEPARATOR (path[1]))
+ path += 2;
+ }
+ while (old_path != path);
+
+ return path;
+}
+
/* Open a file named STRING, searching path PATH (dir names sep by some char)
using mode MODE in the calls to open. You cannot use this function to
create files (O_CREAT).
@@ -747,17 +777,9 @@ openp (const char *path, openp_flags opts, const char *string,
goto done;
}
- /* For dos paths, d:/foo -> /foo, and d:foo -> foo. */
- if (HAS_DRIVE_SPEC (string))
- string = STRIP_DRIVE_SPEC (string);
-
- /* /foo => foo, to avoid multiple slashes that Emacs doesn't like. */
- while (IS_DIR_SEPARATOR(string[0]))
- string++;
-
- /* ./foo => foo */
- while (string[0] == '.' && IS_DIR_SEPARATOR (string[1]))
- string += 2;
+ /* Remove characters from the start of PATH that we don't need when PATH
+ is appended to a directory name. */
+ string = prepare_path_for_appending (string);
alloclen = strlen (path) + strlen (string) + 2;
filename = (char *) alloca (alloclen);
@@ -1033,7 +1055,32 @@ find_and_open_source (const char *filename,
openp_flags flags = OPF_SEARCH_IN_PATH;
if (basenames_may_differ)
flags |= OPF_RETURN_REALPATH;
+
+ /* Try to locate file using filename. */
result = openp (path, flags, filename, OPEN_MODE, fullname);
+ if (result < 0 && dirname != NULL)
+ {
+ /* Remove characters from the start of PATH that we don't need when
+ PATH is appended to a directory name. */
+ const char *filename_start = prepare_path_for_appending (filename);
+
+ /* Try to locate file using compilation dir + filename. This is
+ helpful if part of the compilation directory was removed,
+ e.g. using gcc's -fdebug-prefix-map, and we have added the missing
+ prefix to source_path. */
+ std::string cdir_filename (dirname);
+
+ /* Remove any trailing directory separators. */
+ while (IS_DIR_SEPARATOR (cdir_filename.back ()))
+ cdir_filename.pop_back ();
+
+ /* Add our own directory separator. */
+ cdir_filename.append (SLASH_STRING);
+ cdir_filename.append (filename_start);
+
+ result = openp (path, flags, cdir_filename.c_str (), OPEN_MODE,
+ fullname);
+ }
if (result < 0)
{
/* Didn't work. Try using just the basename. */