info sources
#########################################################################################
之后,即使你的源文件1.cpp 放在 /home/bbb下面也是可以找到的了。因为gdb帮你做了字符串替换。
###############################################
9.5 Specifying Source Directories
Executable programs sometimes do not record the directories of the sourcefiles from which they were compiled, just the names. Even when they do,the directories could be moved between the compilation and your debuggingsession. GDB has a list of directories to search for source files;this is called the source path. Each time GDB wants a source file,it tries all the directories in the list, in the order they are presentin the list, until it finds a file with the desired name.
For example, suppose an executable references the file/usr/src/foo-1.0/lib/foo.c, and our source path is/mnt/cross. The file is first looked up literally; if thisfails, /mnt/cross/usr/src/foo-1.0/lib/foo.c is tried; if thisfails, /mnt/cross/foo.c is opened; if this fails, an errormessage is printed. GDB does not look up the parts of thesource file name, such as /mnt/cross/src/foo-1.0/lib/foo.c.Likewise, the subdirectories of the source path are not searched: ifthe source path is /mnt/cross, and the binary refers tofoo.c, GDB would not find it under/mnt/cross/usr/src/foo-1.0/lib.
Plain file names, relative file names with leading directories, filenames containing dots, etc. are all treated as described above; forinstance, if the source path is /mnt/cross, and the source fileis recorded as ../lib/foo.c, GDB would first try../lib/foo.c, then /mnt/cross/../lib/foo.c, and afterthat—/mnt/cross/foo.c.
Note that the executable search path is not used to locate thesource files.
Whenever you reset or rearrange the source path, GDB clears outany information it has cached about where source files are found and whereeach line is in the file.
When you start GDB, its source path includes only ‘cdir’and ‘cwd’, in that order.To add other directories, use the directory
command.
The search path is used to find both program source files and GDBscript files (read using the ‘-command’ option and ‘source’ command).
In addition to the source path, GDB provides a set of commandsthat manage a list of source path substitution rules. A substitutionrule specifies how to rewrite source directories stored in the program’sdebug information in case the sources were moved to a differentdirectory between compilation and debugging. A rule is made oftwo strings, the first specifying what needs to be rewritten inthe path, and the second specifying how it should be rewritten.In set substitute-path, we name these two parts from andto respectively. GDB does a simple string replacementof from with to at the start of the directory part of thesource file name, and uses that result instead of the original filename to look up the sources.
Using the previous example, suppose the foo-1.0 tree has beenmoved from /usr/src to /mnt/cross, then you can tellGDB to replace /usr/src in all source path names with/mnt/cross. The first lookup will then be/mnt/cross/foo-1.0/lib/foo.c in place of the original locationof /usr/src/foo-1.0/lib/foo.c. To define a source pathsubstitution rule, use the set substitute-path
command(see set substitute-path).
To avoid unexpected substitution results, a rule is applied only if thefrom part of the directory name ends at a directory separator.For instance, a rule substituting /usr/source into/mnt/cross will be applied to /usr/source/foo-1.0 butnot to /usr/sourceware/foo-2.0. And because the substitutionis applied only at the beginning of the directory name, this rule willnot be applied to /root/usr/source/baz.c either.
In many cases, you can achieve the same result using the directory
command. However, set substitute-path
can be more efficient inthe case where the sources are organized in a complex tree with multiplesubdirectories. With the directory
command, you need to add eachsubdirectory of your project. If you moved the entire tree whilepreserving its internal organization, then set substitute-path
allows you to direct the debugger to all the sources with one singlecommand.
set substitute-path
is also more than just a shortcut command.The source path is only used if the file at the original location nolonger exists. On the other hand, set substitute-path
modifiesthe debugger behavior to look at the rewritten location instead. So, iffor any reason a source file that is not relevant to your executable islocated at the original location, a substitution rule is the onlymethod available to point GDB at the new location.
You can configure a default source path substitution rule byconfiguring GDB with the‘--with-relocated-sources=dir’ option. The dirshould be the name of a directory under GDB’s configuredprefix (set with ‘--prefix’ or ‘--exec-prefix’), anddirectory names in debug information under dir will be adjustedautomatically if the installed GDB is moved to a newlocation. This is useful if GDB, libraries or executableswith debug information and corresponding source code are being movedtogether.
-
Add directory dirname to the front of the source path. Severaldirectory names may be given to this command, separated by ‘:’(‘;’ on MS-DOS and MS-Windows, where ‘:’ usually appears aspart of absolute file names) orwhitespace. You may specify a directory that is already in the sourcepath; this moves it forward, so GDB searches it sooner.
You can use the string ‘$cdir’ to refer to the compilationdirectory (if one is recorded), and ‘$cwd’ to refer to the currentworking directory. ‘$cwd’ is not the same as ‘.’—the formertracks the current working directory as it changes during your GDBsession, while the latter is immediately expanded to the currentdirectory at the time you add an entry to the source path.
-
Reset the source path to its default value (‘$cdir:$cwd’ on Unix systems). This requires confirmation.
-
Set the source path to path-list.‘$cdir:$cwd’ are added if missing.
-
Print the source path: show which directories it contains.
-
Define a source path substitution rule, and add it at the end of thecurrent list of existing substitution rules. If a rule with the samefrom was already defined, then the old rule is also deleted.
For example, if the file /foo/bar/baz.c was moved to/mnt/cross/baz.c, then the command
(gdb) set substitute-path /foo/bar /mnt/cross
will tell GDB to replace ‘/foo/bar’ with‘/mnt/cross’, which will allow GDB to find the filebaz.c even though it was moved.
In the case when more than one substitution rule have been defined,the rules are evaluated one by one in the order where they have beendefined. The first one matching, if any, is selected to performthe substitution.
For instance, if we had entered the following commands:
(gdb) set substitute-path /usr/src/include /mnt/include (gdb) set substitute-path /usr/src /mnt/src
GDB would then rewrite /usr/src/include/defs.h into/mnt/include/defs.h by using the first rule. However, it woulduse the second rule to rewrite /usr/src/lib/foo.c into/mnt/src/lib/foo.c.
-
If a path is specified, search the current list of substitution rulesfor a rule that would rewrite that path. Delete that rule if found.A warning is emitted by the debugger if no rule could be found.
If no path is specified, then all substitution rules are deleted.
-
If a path is specified, then print the source path substitution rulewhich would rewrite that path, if any.
If no path is specified, then print all existing source path substitutionrules.
directory dirname …
dir dirname …
directory
set directories path-list
show directories
set substitute-path from to
unset substitute-path [path]
show substitute-path [path]
If your source path is cluttered with directories that are no longer ofinterest, GDB may sometimes cause confusion by finding the wrongversions of source. You can correct the situation as follows:
- Use
directory
with no argument to reset the source path to its default value. - Use
directory
with suitable arguments to reinstall thedirectories you want in the source path. You can add all thedirectories in one command.