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

相關文章
相關標籤/搜索