gdb重置工程根目录

在gdb里面查看源码。
list 一下提醒找不到相关文件,列出来的是绝对路径的完整文件名。
help files   看一下帮助,可以加载符号,源文件等,自己看一下。
dir 设置源码路径无效,show directories  看到设置成功,但是还是找不到文件。
info sources
应该是绝对路径的问题。
因为igcc 根据你编译的时候指定的是绝对路径还是  ../../XXX.cpp之类的相对路径,在生成debug_info的时候,也把这个路径保存为debug_info 里面的文件名字,就是最后 gdb list 里面找到的文件名字。
这个可以list  查看是不是绝对路径,然后可以用命令
 readelf -p .debug_str  exe_or_so_file 
看到里面保存是是完整的绝对路径。
#########################################################################################
如果gcc里面-g  生成的debug_info 使用的绝对路径了,最简单的办法就是你把源码也放到相应的位置上去了。
但如果你不想使用这个绝对路径,那也还是有办法的。 
GDB还提供另外一个选择,可以让你修改源码搜索路径,把源码绝对路径里面的一个path映射到另一个path上去,这样即使你debug info了里面的是绝对路径,源码也可以放到另外的目录。
这就是命令
set substitute-path from_path  to_path
比如 list显示的源码是   /home/aaa/1.cpp
那么设置了 set substitute-path /home/aaa/   /home/bbb/

之后,即使你的源文件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 directorycommand. 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-pathallows 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.

directory dirname dir dirname

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.

directory

Reset the source path to its default value (‘$cdir:$cwd’ on Unix systems). This requires confirmation.

set directories path-list

Set the source path to path-list.‘$cdir:$cwd’ are added if missing.

show directories

Print the source path: show which directories it contains.

set substitute-path from to

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.

unset substitute-path [path]

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.

show substitute-path [path]

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.

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:

  1. Use directory with no argument to reset the source path to its default value.
  2. Use directory with suitable arguments to reinstall thedirectories you want in the source path. You can add all thedirectories in one command.



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值