加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用c lldb中的重载运算符评估表达式

发布时间:2020-12-15 01:41:29 所属栏目:百科 来源:网络整理
导读:我正在使用lldb调试Xcode 5中的C程序,我想评估调试器中的任意表达式,特别是那些使用重载运算符的表达式. 例如,我创建了一个非常简单的Xcode 5 C项目,其中包含以下main.cpp和所有编译器/链接器/ etc选项设置为默认值: #include iostream#include vectorint m
我正在使用lldb调试Xcode 5中的C程序,我想评估调试器中的任意表达式,特别是那些使用重载运算符的表达式.

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

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

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

然后,在lldb提示符下,打印整个矢量就可以正常工作:

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

但是,我无法访问其成员使用重载的运算符[]:

(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

同样,我无法得到迭代器(虽然我在这里的经验较少,所以我的语法可能是错误的):

(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

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

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

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

(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是否支持超载运算符的评估?

先谢谢你!

解决方法

请注意,C标准库被设置为使得它们可以嵌入所有可以合理内联的模板化函数,并且不存在真正的功能副本.所以例如,当你去调用std :: vector< int> :: begin()时,没有这样的功能.它的所有用途已被内联.

这就是为什么你收到错误的“调用函数…不存在于目标”.可能有函数的内联副本,但没有一个我们实际可以调用.例如,如果我构建一个使得std :: vector的一个C程序,并将一些元素推送到它上面,然后对它们进行迭代,然后执行以下操作:

(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库本身的进一步下降:

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::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::allocator<char> >::begin() const        Address: libc++.1.dylib[0x000000000003e574] (libc++.1.dylib.__TEXT.__text + 252324)

还有一些是basic_string,这就是全部.所以没有任何真正的实现,我们可以调用.那么一旦我们只有一点点可以使用这些std对象的真实世界,那么当你开始推动它们时,世界会以其他奇怪的方式崩溃.

lldb目前还不够聪明,不知道如何从C标准库的头文件中重构一个模板化函数/方法.我们没有足够的环境,您的代码最初编译为执行该任务.

请注意,重载运算符并不是一个问题,编译器使用std库的方式更为问题.对于你自己的课程,事情应该更好,在-O0里面没有太多的内联.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读