Python脚本支持
GDB 7.0版本后的gdb打印stl容器功能其实是pretty-printer
这个python脚本来提供的。通过在gdb中info pretty-printer
查看是否有该脚本支持。
设置pretty-printer
方法如下:
-
获得python脚本,建议使用gcc默认安装的版本。
sudo find / -name "*libstdcxx*"
-
若本机查找不到python脚本,建议下载gcc对应版本源码包,相对目录如下。
gcc-4.8.1/libstdc++-v3/python
-
也可直接下载最新版本。
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
4. 复制python所在目录。
5.在个人目录下创建一份.gdbinit文件,内容很简单,如下。
set auto-load safe-path /
python
import sys
sys.path.insert(0, '/usr/share/gcc-9.3.0/python') # python目录
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
6. 再次启动gdb,输出信息如下:
[fuchangyao@longquan-cloud-dm-0002 z_leetcode_test]$ gdb test
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/fuchangyao/workspacefcy/west/z_leetcode_test/test...done.
7. 查看info pretty-printer
(gdb) info pretty-printer
global pretty-printers:
libstdc++-v6
__gnu_cxx::_Slist_iterator
__gnu_cxx::__8::_Slist_iterator
__gnu_cxx::__8::__normal_iterator
__gnu_cxx::__8::slist
__gnu_cxx::__normal_iterator
__gnu_cxx::slist
__gnu_debug::_Safe_iterator
std::_Deque_const_iterator
std::_Deque_iterator
std::_Fwd_list_const_iterator
std::_Fwd_list_iterator
std::_List_const_iterator
std::_List_iterator
std::_Node_handle
std::_Rb_tree_const_iterator
std::_Rb_tree_iterator
std::__8::_Deque_const_iterator
std::__8::_Deque_iterator
std::__8::_Fwd_list_const_iterator
std::__8::_Fwd_list_iterator
std::__8::_List_const_iterator
std::__8::_List_iterator
std::__8::_Node_handle
std::__8::_Rb_tree_const_iterator
std::__8::_Rb_tree_iterator
std::__8::__cxx11::__cxx1998::list
std::__8::__cxx11::basic_string
std::__8::__cxx11::list
---Type <return> to continue, or q <return> to quit---
8. 查看STL容器值
(gdb) l
75 // B *pb = &c;
76 // C *pc = &c;
77 // printf("%p, %p, %p\n", pa, pb, pc);
78 // printf("%d, %d, %d\n", sizeof(*pa), sizeof(*pb), sizeof(*pc));
79 // return 0;
80 // }
81
82 int main()
83 {
84 int i1 = 32;
(gdb)
85 int i2 = 45;
86 double d = i1 + i2 / 3;
87
88 vector<string> vstr;
89 vstr.push_back("Hello");
90 vstr.push_back("World");
91 vstr.push_back("!");
92
93 map<string, int> m_si;
94 m_si["A"] = 12;
(gdb)
95 m_si["D"] = 93;
96 m_si["B"] = 77;
97
98 return 0;
99 }(gdb)
Line number 100 out of range; test.cpp has 99 lines.
(gdb) b 98
Breakpoint 1 at 0x401118: file test.cpp, line 98.
(gdb) r
Starting program: /home/fuchangyao/workspacefcy/west/z_leetcode_test/test
Breakpoint 1, main () at test.cpp:98
98 return 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-324.el7_9.x86_64
(gdb) p vstr
$1 = std::vector of length 3, capacity 4 = {"Hello", "World", "!"}
(gdb) p m_si
$2 = std::map with 3 elements = {
["A"] = 12,
["B"] = 77,
["D"] = 93
}
(gdb)
参考资料:100-gdb-tips/print-STL-container.md at master · hellogcc/100-gdb-tips · GitHub