gdb積累學習

2021-06-18 23:21:34 字數 3700 閱讀 5078

gdb積累學習

1、檢視當前檔案

通過gdb進行除錯的時候,有的時候因為跳轉的太多,所以不知道已經執行到了哪乙個檔案,這個時候,如果我們想看一下當前檔案的名字,可以使用下面的命令:

(gdb) info source

current source file is cdbstatement.cpp

compilation directory is /account/work/ymm/cdb

located in /account/work/ymm/cdb/cdbstatement.cpp

contains 983 lines.

source language is c++.

compiled with dwarf 2 debugging format.

does not include preprocessor macro info.

通過上面的方式,我們就不但可以檢視到所在的檔案,如果整個程式使用不同的檔案話,我們還可以看到該檔案所在的路徑,這樣在gdb的時候,就不會迷路了。

當然,我們進入gdb之後,我們首先應該進入檔案,否則就會出現下面的情況:

[billing_dx@bmcs1 account]$ gdb main

gnu gdb (gdb) red hat enterprise linux (7.2-56.el6)

license gplv3+: gnu gpl version 3 or later

this is free software: you are free to change and redistribute it.

there is no warranty, to the extent permitted by law.  type "show copying"

and "show warranty" for details.

this gdb was configured as "x86_64-redhat-linux-gnu".

for bug reporting instructions, please see:

...reading symbols from /account/work/ymm/account/main...done.

(gdb) info source

no current source file.

(gdb) 

擴充套件閱讀:  

2、顯示執行的行數(bt檢視堆疊)

雖然我們通過next、setp的方式執行或者list的方式都可以顯示行號,但是,有的時候因為我們通過list顯示了過多的內容,這個時候顯示的行數有可能都是執行行數下面很多行之後,如果這個時候我們不想繼續執行(如果繼續執行的話,可以肯定將要出錯,而跟蹤到目前為止很麻煩),我們就可以通過檢視堆疊的方式檢視當前行數。

gdb) bt

#0  cdproductimpl::getgroupinfo (this=0x690b50, sztablenames=0x7fffffffd160 "", szuri=0x7fffffffd7f0 "/serv/byid", 

idec=@0x7fffffffd26c, iuni=@0x7fffffffd268) at cdproductimpl.cpp:1627

#1  0x00007ffff5907ad0 in cdproductimpl::get (this=0x690b50, szuri=0x7fffffffd7f0 "/serv/byid", 

szparamlist=0x7fffffffd6f0 "serv_id =1004", pbuf=0x7fffffffd950, inum=@0x7fffffffd96c, szmsg=0x7fffffffd2f0 "")

at cdproductimpl.cpp:24

#2  0x00007ffff59077bc in cdproduct::get (this=0x677300, szuri=0x7fffffffd7f0 "/serv/byid", 

szparamlist=0x7fffffffd6f0 "serv_id =1004", pbuf=0x7fffffffd950, inum=@0x7fffffffd96c, szmsg=0x7fffffffd2f0 "")

at cdproduct.cpp:24

#3  0x0000000000402a05 in cuserdataqry::getuserofferbyservid (this=0x690d80) at cuserdataqry.cpp:127

#4  0x0000000000402367 in cuserdataqry::getuserinfo (this=0x690d80, lservid=1004, szaccnbr=0x7fffffffda00 "1234", 

lproductid=87654, puserinfo=0x7fffffffda30) at cuserdataqry.cpp:37

#5  0x00000000004020c8 in main (argc=1, argv=0x7fffffffdb58) at main.cpp:42

通過上面的提示,我們可以很容易的看到程式的運**況,而且可以明確當前的位置是cdproductimpl.cpp:1627。

(gdb) bt

no stack.

(gdb) 

我們還可以通過通過"info stack"命令,也可以檢視堆疊的內容,顯示的結果一樣。

而"bt"命令應該就是"backtrace"簡寫,回溯,向後追蹤的意思。

3、顯示當前檔案路徑

和linux命令視窗一樣,使用pwd就可以檢視當前路徑

(gdb) pwd   

working directory /account/work/wangcq/ymm1.

4、設定斷點時的警告

在除錯的時候,我們或許會在動態庫的地方設定斷點,因為該檔案的原始檔不在當前工程中,所以在設定斷點的時候會出現警告,如下:

(gdb) b cdproductimpl.cpp:24

no source file named cdproductimpl.cpp.

make breakpoint pending on future shared library load? (y or [n]) 

這個時候,我們只需要設定一下"set breakpoint pending on"就可以在新增不能識別的斷點的時候,也不出現警告了。如下

(gdb) set breakpoint pending on

(gdb) b a.cpp:10

no source file named a.cpp.

breakpoint 1 (a.cpp:10) pending.

(gdb) 

雖然會提示該檔案不存在,但是我們的斷點依舊設定成功。

附:pending breakpoints:是指設定在程式開始除錯後載入的動態庫中的位置處的breakpoints

a.set breakpoint pending auto: gdb預設設定,詢問使用者是否要設定pending breakpoint

b.set breakpoint pending on: gdb當前不能識別的breakpoint自動成為pending breakpoint

c.set breakpoint pending off: gdb當前不能識別某個breakpoint時,直接報錯

d.show breakpoint pending: 檢視gdb關於pending breakpoint的設定的行為(auto, on, off)

擴充套件閱讀:  

Ubuntu學習積累

步驟如下 解壓 tar zxvf install flash player 11 linux.x86 64.tar.gz 然後發現解壓檔案中只有乙個libflashplayer.so的檔案,然後在網上檢視怎麼去處理這個檔案,提供的方式是直接複製到 usr lib mozilla plugins資料夾...

jQuery學習積累

目的 為了保護包預設引數,也就是defaults裡面的引數。當外掛程式使用時,預設使用defaults裡面的引數值,當options有值時,則會將defaults中的值替換掉,不存在的保留 第一引數 defaults 第二引數 options 第三引數 測試 var defaults var opt...

vue學習積累

1.乙個元素佔據一行 2.高度寬度可控制 3.寬度沒有設定時,預設為100 4.塊級元素可以包含塊級和行級 1.和其他元素在一行 2.高度寬度以及內邊距都是不可控的 3.寬高就是內容的寬高,不可以改變 4.行內元素只能包含行內元素 1.既有行內元素特性,又有塊級元素特性 2.寬度 高度 行高內外邊距...