栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

GDB调试,如何查看STL容器值?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

GDB调试,如何查看STL容器值?

Python脚本支持

GDB 7.0版本后的gdb打印stl容器功能其实是pretty-printer这个python脚本来提供的。通过在gdb中info pretty-printer查看是否有该脚本支持。

设置pretty-printer

方法如下:

  1. 获得python脚本,建议使用gcc默认安装的版本。

    sudo find / -name "*libstdcxx*"

  2. 若本机查找不到python脚本,建议下载gcc对应版本源码包,相对目录如下。

    gcc-4.8.1/libstdc++-v3/python

  3. 也可直接下载最新版本。

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 
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 /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  to continue, or q  to quit---

8. 查看STL容器值

(gdb) l
75      //     B *pb = &c;
76      //     C *pc = &c;
77      //     printf("%p, %p, %pn", pa, pb, pc);
78      //     printf("%d, %d, %dn", 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 vstr;
89          vstr.push_back("Hello");
90          vstr.push_back("World");
91          vstr.push_back("!");
92       
93          map 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664818.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号