GDB pretty printer: linux下用gdb调试c++时如何更好的查看STL容器值

本文介绍如何利用GDBprettyprinter显著提升STL容器在GDB中的显示效果,使得debug过程更加直观高效。文章详细展示了配置步骤,并通过实例对比了优化前后的显示效果。
GDB pretty printer 提供了更方便的STL容器显示方法能够方便debug,虽然以前也有通过其它脚本方式显示STL内容的方法,
但是这种方案应该是效果最好的。
看下效果:

对于一个 vector<vector<Leaf> > m_nodeLevel;的显示如下
(gdb) p ngram_builder.m_nodeLevel
$3 = std::vector of length 3, capacity 3 = {std::vector of length 2, capacity 2 = {
    {<ngram::Model::Leaf> = {id = 0, {freq = 908576172, pr = 2.49999994e-06}},
      child = 0, bow = 0.0666622296}, {<ngram::Model::Leaf> = {id = 16777215, {
          freq = 1, pr = 1.40129846e-45}}, child = 99097, bow = 0}},
  std::vector of length 99098, capacity 99098 = {{<ngram::Model::Leaf> = {id = 10,
        {freq = 1037405847, pr = 0.104275875}}, child = 0, bow = 1.53522229},
{<ngram::Model::Leaf> = {id = 129, {freq = 858932647, pr = 4.1507203e-08}},
…..

对于下面的程序,显示一个string
#include <string>
std::string str = "hello world";
int main ()
{
  return 0;
}

原来GDB显示效果:
(gdb) print str
$1 = {static npos = 4294967295,
  _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x804a014 "hello world"}}

现在的显示效果:
(gdb) print str
$1 = hello world

如何使用:
1.下载一个支持python support 的gdb,似乎官网的gdb就可以但是我安装之后显示还是不支持
Python脚本,最后google 到了下面这个可以用的
http://github.com/jellycn/gdb-python/
./configure && make && make install即可

2.下载最新的 Python libstdc++ printers 放到你的机器上
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
3.在你的~路径下的.gdbinit文件中加上下面的东西, 注意'/home/suhaibo/soft/python'替换成2步骤你具体放置的路径。
python
import sys
sys.path.insert(0, '/home/chg/soft/python')   
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值