GDB源代码查找路径

在gdb程序的时候,有时候会发现源代码文件找不到,对于那些带调试信息的系统库或者第三方库,不少时候当你真正想gdb去追他源代码的时候你会发现gdb根本找不到这些源代码路径。这个时候有两种选择:html

【1】若是gdb这个时候告诉你找不到这个带调试信息库的源文件路径,此时给出的路径地址是绝对路径,好比shell

/home/rickyk/qt-4.8.6/src/corelib/tools/qstring.cpp: 没有那个文件或目录

这种提示的,你就应该用gdb提供的spa

set substitute-path

这个其实很好理解,就是替换规则,你若是想查看当前的替换规则,你能够调试

show substitute-path

好比此时咱们须要qstring.cpp这个文件,但因为某种缘由,目前咱们不能在/home/rickyk/qt-4.8.6/src/corelib/tools/qstring.cpp中找到,但咱们确能够在/home/rickyk/qt-everywhere-opensource-src-4.8.6/src/corelib/tools/qstring.cpp中找到,咱们就code

set substitute-path /home/rickyk/qt-4.8.6 /home/rickyk/qt-everywhere-opensource-src-4.8.6

这是什么意思?其实就是让gdb在看到/home/rickyk/qt-4.8.6的时候他会作自动替换成/home/rickyk/qt-everywhere-opensource-src.4.8.6,也就是说gdb能够正确知道这个文件了。此时咱们再show substitute-path能够看到此时的转换规则已经被咱们加进去了htm

(gdb) show substitute-path 
List of all source path substitution rules:
  `/home/rickyk/qt-4.8.6' -> `/home/rickyk/qt-everywhere-opensource-src-4.8.6'.

【2】若是此时的gdb弹出的错误信息不是绝对路径的概念,而是相对路径的概念blog

./a.cpp 没有那个文件或目录

那么此时你能够用gdb的第二个源代码路径法宝----directory(dir) dirName来指定,也就是说若是咱们此时的a.cpp不在当前目录下,而是在当前目录下的bak文件夹下,咱们只要ip

dir bak

这个时候咱们的gdb就会把你加进去的dir整个替换到相对路径的前面,本身作拼接,也就是说,如今的./a.cpp变成了./bak/a.cpp。ci

注意二者的差异,对于绝对路径来讲,你须要给出替换规则给他作字符串替换,对于相对路径来讲,你须要给他目录来让他作拼接,也有点prefix的意思,这里的prefix由你给出,但相对路径总体结构由gdb给出,而后完成拼接操做。字符串

PS: 同时你须要在.gdbinit上加上一句

set auto-load safe-path /

这样你才能让gdb去正确的在别的目录进行读取源代码 。(这里面的缘由我目前不是很清楚,总之就是我一开始使用set substitute-path的时候怎么用都不成功,加上这句以后才能够,有同窗知道的也能够告诉我缘由),我看了下auto-load的介绍

set auto-load safe-path [ directories]
Set the list of directories (and their subdirectories) trusted for automatic loading and execution of scripts. You can also enter a specific trusted file. Each directory can also be a shell wildcard pattern; wildcards do not match directory separator - see FNM_PATHNAME for system function fnmatch (see fnmatch). If you omit directories, ‘ auto-load safe-path’ will be reset to its default value as specified during gdb compilation.

The list of directories uses path separator (‘:’ on GNU and Unix systems, ‘;’ on MS-Windows and MS-DOS) to separate directories, similarly to thePATHenvironment variable.

感受这是在设置自动能够读取的script路径,不知道这对于set substitute-pah有什么影响。按照参考连接的说法,设置这个命令应该是让gdb去读取可信赖路径的.gdbinit才对。

参考连接: http://stackoverflow.com/questions/16595417/loading-gdbinit-from-current-directory

                  https://vecr.ece.villanova.edu/vecr/doc/gdb/Auto_002dloading-safe-path.html

相关文章
相关标签/搜索