xcode 在 c++ lldb 中使用重载运算符评估表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20387942/
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
Evaluating an expression with overloaded operators in c++ lldb
提问by Daniel Golden
I'm debugging a C++ program in Xcode 5 using lldb, and I would like to evaluate arbitrary expressions in the debugger, particularly those that use overloaded operators.
我正在使用 lldb 在 Xcode 5 中调试 C++ 程序,我想评估调试器中的任意表达式,尤其是那些使用重载运算符的表达式。
For example, I created a very simple Xcode 5 C++ project with the following main.cpp and all compiler/linker/etc options set to the default:
例如,我创建了一个非常简单的 Xcode 5 C++ 项目,其中的 main.cpp 和所有编译器/链接器/等选项都设置为默认值:
#include <iostream>
#include <vector>
int main(int argc, const char * argv[])
{
std::vector<int> vec;
vec.push_back(42);
std::cout << "vec[0] = " << vec[0] << std::endl;
return 0;
}
I set a breakpoint on the return 0;
line and ran the program.
我return 0;
在行上设置了一个断点并运行了程序。
Then, at the lldb prompt, printing the vector as a whole works fine:
然后,在 lldb 提示符下,将向量作为一个整体打印可以正常工作:
(lldb) expr vec
(std::__1::vector<int, std::__1::allocator<int> >) (lldb) expr vec[0]
error: call to a function 'std::__1::vector<int, std::__1::allocator<int> >::operator[](unsigned long)' ('_ZNSt3__16vectorIiNS_9allocatorIiEEEixEm') that is not present in the target
error: The expression could not be prepared to run in the target
= size=1 {
[0] = 42
}
However, I can't access its members using the overloaded operator[]
:
但是,我无法使用重载访问其成员operator[]
:
(lldb) expr vector<int>::iterator it = vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression
Similarly, I can't get the iterator (though I have less experience here, so my syntax may be wrong):
同样,我无法获得迭代器(虽然我在这里的经验较少,所以我的语法可能是错误的):
(lldb) expr (vector<int>::iterator) vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression
and
和
(lldb) expr string("a")
(std::__1::string) (lldb) expr string("a") + string("b")
error: invalid operands to binary expression ('string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') and 'string')
error: 1 errors parsing expression
= "a"
Analogously, printing a simple string works fine:
类似地,打印一个简单的字符串工作正常:
(lldb) p vec.__begin_[i]
(int) = 100
However, a simple string concatenation fails:
但是,简单的字符串连接失败:
(lldb) image lookup -r -n begin
2 matches found in /private/tmp/vector:
Address: vector[0x0000000100000eaf] (vector.__TEXT.__text + 1071)
Summary: vector`main + 1071 [inlined] std::__1::vector<int, std::__1::allocator<int> >::begin() at vector.cpp:12
vector`main + 1071 at vector.cpp:12 Address: vector[0x0000000100000eaf] (vector.__TEXT.__text + 1071)
Summary: vector`main + 1071 [inlined] std::__1::vector<int, std::__1::allocator<int> >::begin() at vector.cpp:12
vector`main + 1071 at vector.cpp:12
What am I doing wrong? Does lldb support evaluation with overloaded operators?
我究竟做错了什么?lldb 是否支持使用重载运算符进行评估?
Thank you in advance!
先感谢您!
回答by Dimaleks
I just ran into the same issue and apparently found a simple work-around.
You can access i-th element of a vector vec
like this:
我刚刚遇到了同样的问题,显然找到了一个简单的解决方法。您可以像这样访问向量的第i个元素vec
:
12 matches found in /usr/lib/libc++.1.dylib:
Address: libc++.1.dylib[0x000000000003e4ec] (libc++.1.dylib.__TEXT.__text + 252188)
Summary: libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::begin() Address: libc++.1.dylib[0x000000000003e51c] (libc++.1.dylib.__TEXT.__text + 252236)
Summary: libc++.1.dylib`std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::begin() const Address: libc++.1.dylib[0x000000000003e574] (libc++.1.dylib.__TEXT.__text + 252324)
回答by Jim Ingham
Note that the C++ standard libraries are set up so that they inline all the templated functions that they can sensibly inline, and no real function copies exist. So for instance, when you go to call std::vector<int>::begin()
, there is no such function. All its uses have been inlined.
请注意,C++ 标准库的设置是为了内联所有可以合理内联的模板化函数,并且不存在真正的函数副本。因此,例如,当您去 call 时std::vector<int>::begin()
,没有这样的功能。它的所有用途都已内联。
That is why you are getting errors about "call to function... not present in target." There may be inlined copies of the function, but none we can actually call. As an example, if I build a little C++ program that makes a std::vector, and pushes some elements onto it and then iterates over them, and then do:
这就是为什么您会收到有关“调用函数...目标中不存在”的错误的原因。可能有函数的内联副本,但没有一个我们可以实际调用。举个例子,如果我构建一个小 C++ 程序来创建一个 std::vector,并将一些元素推送到它上面,然后迭代它们,然后执行:
##代码##So all the instances of the begin & end accessors for std::vector<int>
are inlined. And further down in the part that comes from the std c library itself:
所以所有的开始和结束访问器的实例std::vector<int>
都是内联的。再往下是来自 std c 库本身的部分:
and a few more for basic_string, and that's all. So there aren't any real implementations that we can call. Then once we've only got a little bit of the real world of these std objects available to us, the world falls apart in other odd ways as you start to push on it.
还有一些basic_string,仅此而已。所以没有任何我们可以调用的真正实现。然后,一旦我们只获得了这些 std 对象的真实世界的一小部分可供我们使用,当您开始推动它时,世界就会以其他奇怪的方式分崩离析。
lldb isn't currently smart enough to figure out how to reconstitute a templated function/method from the C++ standard library's header files. We don't have enough of the environment in which your code was originally compiled to do that task.
lldb 目前还不够聪明,无法弄清楚如何从 C++ 标准库的头文件中重构模板化函数/方法。我们没有足够的环境来完成您的代码最初编译的任务。
Note that this isn't really a problem with overloaded operators, it is more a problem with the way the std libraries are used by the compiler. Things should work better for your own classes, where at -O0 there isn't so much inlining.
请注意,这不是重载运算符的真正问题,更多的是编译器使用 std 库的方式的问题。对于您自己的类,事情应该会更好,其中 -O0 没有太多内联。