【问题标题】:how to view contents of STL containers using GDB 7.x如何使用 GDB 7.x 查看 STL 容器的内容
【发布时间】:2011-01-30 07:48:30
【问题描述】:

我一直在使用宏解决方案,正如here 所概述的那样。但是,提到了如何在没有宏的情况下查看它们。我指的是 GDB 7 及以上版本。

有人能说明一下吗?

谢谢

【问题讨论】:

标签: c++ stl gdb


【解决方案1】:

从 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

【讨论】:

  • 以及如何在 GDB 中实际使用它来打印内容?有什么需要设置的。谢谢
  • 一旦加载代码在 .gdbinit 中,那么 print 应该可以正常工作,要返回旧视图,您可以执行 print /r(/r 用于 raw)(格式化显然在 cmets 中很糟糕,我很抱歉)std::map the_map; the_map[23] = "你好"; the_map[1024] = "世界"; (gdb) print the_map $1 = std::map with 2 个元素 = { [23] = "hello", [1024] = "world" }
  • 考虑编辑您的答案以将此信息包含在您的评论中。这样格式就不会烂了。
  • 请将此代码示例放入答案中。我无法在 cmets 中读取它。
【解决方案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 来获得“原始”输出。

【讨论】:

    猜你喜欢
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    相关资源
    最近更新 更多