如何在 GDB 中打印 C++ 向量的元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/253099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I print the elements of a C++ vector in GDB?
提问by John Carter
I want to examine the contents of a std::vector
in GDB, how do I do it? Let's say it's a std::vector<int>
for the sake of simplicity.
我想查看std::vector
GDB中 a 的内容,我该怎么做?std::vector<int>
为了简单起见,我们假设它是一个。
采纳答案by Micha? Oniszczuk
To view vector std::vector myVector contents, just type in GDB:
要查看向量 std::vector myVector 内容,只需输入 GDB:
(gdb) print myVector
This will produce an output similar to:
这将产生类似于以下内容的输出:
= std::vector of length 3, capacity 4 = {10, 20, 30}
To achieve above, you need to have gdb 7 (I tested it on gdb 7.01) and some python pretty-printer. Installation process of these is described on gdb wiki.
要实现上述目标,您需要有 gdb 7(我在 gdb 7.01 上测试过)和一些 python 漂亮的打印机。gdb wiki上描述了这些的安装过程。
What is more, after installing above, this works well with EclipseC++ debugger GUI (and any other IDE using GDB, as I think).
更重要的是,在上面安装之后,这适用于EclipseC++ 调试器 GUI(以及任何其他使用 GDB 的 IDE,我认为)。
回答by John Carter
With GCC 4.1.2, to print the whole of a std::vector<int> called myVector, do the following:
使用 GCC 4.1.2,要打印整个名为 myVector 的 std::vector<int>,请执行以下操作:
print *(myVector._M_impl._M_start)@myVector.size()
To print only the first N elements, do:
要仅打印前 N 个元素,请执行以下操作:
print *(myVector._M_impl._M_start)@N
Explanation
解释
This is probably heavily dependent on your compiler version, but for GCC 4.1.2, the pointer to the internal array is:
这可能在很大程度上取决于您的编译器版本,但对于 GCC 4.1.2,指向内部数组的指针是:
myVector._M_impl._M_start
And the GDB command to print N elements of an array starting at pointer P is:
打印从指针 P 开始的数组的 N 个元素的 GDB 命令是:
print P@N
Or, in a short form (for a standard .gdbinit):
或者,以简短的形式(对于标准的 .gdbinit):
p P@N
回答by Nikhil
'Watching' STL containers while debugging is somewhat of a problem. Here are 3 different solutions I have used in the past, none of them is perfect.
在调试时“观察”STL 容器有点问题。这是我过去使用过的 3 种不同的解决方案,它们都不是完美的。
1) Use GDB scripts from http://clith.com/gdb_stl_utils/These scripts allow you to print the contents of almost all STL containers. The problem is that this does not work for nested containers like a stack of sets.
1) 使用来自http://clith.com/gdb_stl_utils/ 的GDB 脚本这些脚本允许您打印几乎所有 STL 容器的内容。问题是这对嵌套容器(如一组集合)不起作用。
2) Visual Studio 2005 has fantastic support for watching STL containers. This works for nested containers but this is for their implementation for STL only and does not work if you are putting a STL container in a Boost container.
2) Visual Studio 2005 非常支持观看 STL 容器。这适用于嵌套容器,但这仅适用于 STL 的实现,如果您将 STL 容器放入 Boost 容器中,则不起作用。
3) Write your own 'print' function (or method) for the specific item you want to print while debugging and use 'call' while in GDB to print the item. Note that if your print function is not being called anywhere in the code g++ will do dead code elimination and the 'print' function will not be found by GDB (you will get a message saying that the function is inlined). So compile with -fkeep-inline-functions
3)为调试时要打印的特定项目编写自己的“打印”函数(或方法),并在 GDB 中使用“调用”打印项目。请注意,如果您的打印函数没有在代码中的任何地方被调用,g++ 将执行死代码消除,并且 GDB 将找不到“打印”函数(您将收到一条消息,指出该函数已内联)。所以用 -fkeep-inline-functions 编译
回答by badeip
put the following in ~/.gdbinit
将以下内容放入 ~/.gdbinit
define print_vector
if $argc == 2
set $elem = $arg0.size()
if $arg1 >= $arg0.size()
printf "Error, %s.size() = %d, printing last element:\n", "$arg0", $arg0.size()
set $elem = $arg1 -1
end
print *($arg0._M_impl._M_start + $elem)@1
else
print *($arg0._M_impl._M_start)@$arg0.size()
end
end
document print_vector
Display vector contents
Usage: print_vector VECTOR_NAME INDEX
VECTOR_NAME is the name of the vector
INDEX is an optional argument specifying the element to display
end
After restarting gdb (or sourcing ~/.gdbinit), show the associated help like this
重新启动gdb(或source ~/.gdbinit)后,像这样显示相关的帮助
gdb) help print_vector
Display vector contents
Usage: print_vector VECTOR_NAME INDEX
VECTOR_NAME is the name of the vector
INDEX is an optional argument specifying the element to display
Example usage:
用法示例:
(gdb) print_vector videoconfig_.entries 0
= {{subChannelId = 177 '1', sourceId = 0 'p/x *(&vec[2])@4
0', hasH264PayloadInfo = false, bitrate = 0, payloadType = 68 'D', maxFs = 0, maxMbps = 0, maxFps = 134, encoder = 0 '##代码##0', temporalLayers = 0 '##代码##0'}}
回答by Mike P
A little late to the party, so mostly a reminder to me next time I do this search!
聚会有点晚了,所以下次我做这个搜索时主要是提醒我!
I have been able to use:
我已经能够使用:
##代码##to print 4 elements (as hex) from vec
starting at vec[2]
.
从 .vec
开始打印 4 个元素(作为十六进制)vec[2]
。