在C ++ lldb中使用重载运算符评估表达式 [英] Evaluating an expression with overloaded operators in c++ lldb

查看:81
本文介绍了在C ++ lldb中使用重载运算符评估表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用lldb在Xcode 5中调试C ++程序,我想在调试器中评估任意表达式,尤其是那些使用重载运算符的表达式.

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.

例如,我使用以下main.cpp创建了一个非常简单的Xcode 5 C ++项目,并将所有编译器/链接器/etc选项设置为默认值:

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:

#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;
}

我在return 0;行上设置一个断点并运行程序.

I set a breakpoint on the return 0; line and ran the program.

然后,在lldb提示符下,将向量作为一个整体打印就可以了:

Then, at the lldb prompt, printing the vector as a whole works fine:

(lldb) expr vec
(std::__1::vector<int, std::__1::allocator<int> >) $0 = size=1 {
  [0] = 42
}

但是,我无法使用重载的operator[]来访问其成员:

However, I can't access its members using the overloaded operator[]:

(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

类似地,我无法获得迭代器(尽管我在这里的经验较少,所以我的语法可能是错误的):

Similarly, I can't get the iterator (though I have less experience here, so my syntax may be wrong):

(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

(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

类似地,打印一个简单的字符串可以正常工作:

Analogously, printing a simple string works fine:

(lldb) expr string("a")
(std::__1::string) $0 = "a"

但是,简单的字符串连接失败:

However, a simple string concatenation fails:

(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

我做错了什么? lldb是否支持使用重载运算符进行评估?

What am I doing wrong? Does lldb support evaluation with overloaded operators?

提前谢谢!

推荐答案

请注意,已建立C ++标准库,以便它们内联所有可以合理内联的模板化函数,并且不存在实际的函数副本.因此,例如,当您调用std::vector<int>::begin()时,没有这样的功能.它的所有用途都已内联.

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 ++程序,使它成为std :: vector,然后将一些元素压入它,然后对其进行迭代,然后执行:

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:

    (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

因此,开始&的所有实例内联std::vector<int>的末端访问器.进一步讲讲来自std c库本身的部分:

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:

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)

以及basic_string的更多内容,仅此而已.因此,我们没有真正可以调用的实现.然后,一旦我们只获得了这些std对象的真实世界的一点点,当您开始推动世界时,世界就会以其他奇怪的方式瓦解.

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.

lldb目前还不够聪明,无法弄清楚如何从C ++标准库的头文件中重构模板化的函数/方法.我们最初在其中编译代码的环境不足,无法完成该任务.

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.

请注意,这实际上不是重载运算符的问题,而与编译器使用std库的方式有关的则更多.对于您自己的班级来说,事情应该做得更好,在-O0处没有太多内联.

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.

这篇关于在C ++ lldb中使用重载运算符评估表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆