LLDB 有时显示矢量数据,有时不显示
LLDB sometimes displays vector data and other times does not
在调试的大多数情况下,如果我有一个向量(在 Xcode 9 中),我会看到一个索引列表,代表向量中的值。
需要
其他时候,我得到这种无用的表示:
不需要
我无法弄清楚是什么条件导致 LLDB 以不希望的方式显示向量。
问题
是什么导致了不良行为?是否可以在不重写代码的情况下修复它?这是 LLDB 中的错误吗?
这是一个重现不良行为的简短代码示例:
#include <iostream>
#include <vector>
std::vector<int> createVector()
{
std::vector<int> v = { 1, 2, 3 };
return v;
}
int main(int argc, const char * argv[])
{
const auto& v = createVector();
std::cout << v.front() << std::endl;
return 0;
}
这是 Xcode 项目的 link:
http://s000.tinyupload.com/?file_id=21020556485232357417
抱歉,我在答案中添加我的评论,因为 Stack Overflow 评论不支持格式化。
肯定是lldb的问题。您的图片不包含完整的 v 描述:
v = (const std::__1::vector<int, std::__1::allocator<int> > &) size=1
size=1
不正确。
除了 lldb 控制台中的打印命令正确打印 v
:
(lldb) p v
(const std::__1::vector<int, std::__1::allocator<int> >) = size=3 {
[0] = 1
[1] = 2
[2] = 3
}
似乎Xcode使用lldb frame var
命令来显示变量。这显示的输出与 Xcode:
显示的输出完全相同
(lldb) frame variable -T
(int) argc = 1
(const char **) argv = 0x00007fff5fbff710
(const std::__1::vector<int, std::__1::allocator<int> > &) v = size=1: {
(std::__1::__vector_base<int, std::__1::allocator<int> >) std::__1::__vector_base<int, std::__1::allocator<int> > = {
(std::__1::__vector_base<int, std::__1::allocator<int> >::pointer) __begin_ = 0x000000010103fa40
(std::__1::__vector_base<int, std::__1::allocator<int> >::pointer) __end_ = 0x000000010103fa4c
(std::__1::__compressed_pair<int *, std::__1::allocator<int> >) __end_cap_ = {
(std::__1::__libcpp_compressed_pair_imp<int *, std::__1::allocator<int>, 2>) std::__1::__libcpp_compressed_pair_imp<int *, std::__1::allocator<int>, 2> = {
(int *) __first_ = 0x000000010103fa4c
}
}
}
}
我认为问题出在以下事实:变量 v
最初是在另一个堆栈帧中创建和初始化的,因此当传递初始向量时,有关向量的一些信息在较低的堆栈帧中是未知的作为函数调用的结果。
这是 std::vector 数据摘要和格式化程序如何处理参考变量的已知错误。请注意,在 expr v
中,表达式解析器实际上将 v 视为直线向量,而不是对向量的引用...这就是打印有效的原因。
在调试的大多数情况下,如果我有一个向量(在 Xcode 9 中),我会看到一个索引列表,代表向量中的值。
需要
其他时候,我得到这种无用的表示:
不需要
我无法弄清楚是什么条件导致 LLDB 以不希望的方式显示向量。
问题
是什么导致了不良行为?是否可以在不重写代码的情况下修复它?这是 LLDB 中的错误吗?
这是一个重现不良行为的简短代码示例:
#include <iostream>
#include <vector>
std::vector<int> createVector()
{
std::vector<int> v = { 1, 2, 3 };
return v;
}
int main(int argc, const char * argv[])
{
const auto& v = createVector();
std::cout << v.front() << std::endl;
return 0;
}
这是 Xcode 项目的 link:
http://s000.tinyupload.com/?file_id=21020556485232357417
抱歉,我在答案中添加我的评论,因为 Stack Overflow 评论不支持格式化。
肯定是lldb的问题。您的图片不包含完整的 v 描述:
v = (const std::__1::vector<int, std::__1::allocator<int> > &) size=1
size=1
不正确。
除了 lldb 控制台中的打印命令正确打印 v
:
(lldb) p v
(const std::__1::vector<int, std::__1::allocator<int> >) = size=3 {
[0] = 1
[1] = 2
[2] = 3
}
似乎Xcode使用lldb frame var
命令来显示变量。这显示的输出与 Xcode:
(lldb) frame variable -T
(int) argc = 1
(const char **) argv = 0x00007fff5fbff710
(const std::__1::vector<int, std::__1::allocator<int> > &) v = size=1: {
(std::__1::__vector_base<int, std::__1::allocator<int> >) std::__1::__vector_base<int, std::__1::allocator<int> > = {
(std::__1::__vector_base<int, std::__1::allocator<int> >::pointer) __begin_ = 0x000000010103fa40
(std::__1::__vector_base<int, std::__1::allocator<int> >::pointer) __end_ = 0x000000010103fa4c
(std::__1::__compressed_pair<int *, std::__1::allocator<int> >) __end_cap_ = {
(std::__1::__libcpp_compressed_pair_imp<int *, std::__1::allocator<int>, 2>) std::__1::__libcpp_compressed_pair_imp<int *, std::__1::allocator<int>, 2> = {
(int *) __first_ = 0x000000010103fa4c
}
}
}
}
我认为问题出在以下事实:变量 v
最初是在另一个堆栈帧中创建和初始化的,因此当传递初始向量时,有关向量的一些信息在较低的堆栈帧中是未知的作为函数调用的结果。
这是 std::vector 数据摘要和格式化程序如何处理参考变量的已知错误。请注意,在 expr v
中,表达式解析器实际上将 v 视为直线向量,而不是对向量的引用...这就是打印有效的原因。