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

来源:互联网 发布:迪士尼影业 知乎 编辑:程序博客网 时间:2024/06/05 14:09
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
原创粉丝点击