天天看點

GDB中檢視STL容器類的内容

使用GDB的"p variable-name"檢視STL容器類,隻會顯示該容器的一些資訊,并不能很友好的顯示該容器的内容。使用stl-views.gdb這個腳本可以很好地解決這一問題。下載下傳stl-veiws.gdb檔案,将其放到~/目錄下,直接将其改名為~/.gdbinit,或者在你已有的.gdbinit檔案中用source ~/.stl-views.gdb指令将其包含。這樣你就可以用下面的指令顯示STL容器類了。

 容器類型 GDB 指令
std::vector<T> pvector stl_variable
std::list<T> plist stl_variable T
std::map<T,T> pmap stl_variable
std::multimap<T,T> pmap stl_variable
std::set<T> pset stl_variable T
std::multiset<T> pset stl_variable
std::deque<T> pdequeue stl_variable
std::stack<T> pstack stl_variable
std::queue<T> pqueue stl_variable
std::priority_queue<T> ppqueue stl_variable
std::bitset<n><td> pbitset stl_variable
std::string pstring stl_variable
std::widestring pwstring stl_variable

舉例:

如果你的C++代碼中有定義: set<string> s;

則在GDB中可以使用如下指令檢視該set的資訊與内容:

pset s - 列印該集合s的定義和大小

pset s char* - 列印該集合s的大小以及該集合的所有元素