如何使用GDB 7.x查看STL容器的内容

时间:2010-03-22 12:06:33

标签: c++ stl gdb

我一直在使用宏解决方案,因为它是概述here。但是,有一个关于如何在没有宏的情况下查看它们的提及。我指的是GDB版本7及更高版本。

有人会说明怎么做?

谢谢

2 个答案:

答案 0 :(得分:21)

从SVN获取python查看器

svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

将以下内容添加到~/.gdbinit

python
import sys
sys.path.insert(0, '/path/to/pretty-printers/dir')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

然后打印就可以了:

std::map<int, std::string> the_map;
the_map[23] = "hello";
the_map[1024] = "world";

在gdb中:

(gdb) print the_map 
$1 = std::map with 2 elements = { [23] = "hello", [1024] = "world" }

要返回旧视图,请使用print /r/r是原始视图)。

另请参阅:http://sourceware.org/gdb/wiki/STLSupport

答案 1 :(得分:2)

libstdcxx_printers包含在最新版本的GCC中,所以如果您使用的是GCC 4.5或更高版本,那么您不需要做任何事情,相当打印Just Works。

(gdb) p v
$1 = std::vector of length 3, capacity 3 = {std::set with 3 elements = {
    [0] = 1, [1] = 2, [2] = 3}, std::set with 2 elements = {[0] = 12, 
    [1] = 13}, std::set with 1 elements = {[0] = 23}}
(gdb) p v[1]
$2 = std::set with 2 elements = {[0] = 12, [1] = 13}

要禁用漂亮的打印,请使用p/rprint/r来获取“原始”输出。