在LLDB中,我可以调用方法并创建c++类的实例吗?

In LLDB, can I call methods and make instances of C++ classes?

本文关键字:c++ 实例 创建 LLDB 我可以 方法 调用      更新时间:2023-10-16

我正在尝试探索使用Clang和LLDB的复杂c++应用程序的行为。我在应用程序中设置了一个断点。一旦到达该断点,我想创建一个简单c++类的实例,然后在该断点的上下文中调用方法

例如,下面是我的应用程序:

#include <iostream>
#include <vector>
struct Point {
  int x;
  int y;
};
int main() {
  std::vector<Point> points;
  points.push_back(Point{3, 4});
  // <--------- Breakpoint here
  int total = 0;
  for (const auto& p : points) {
    total += p.x * p.y;
  }
  std::cout << "Total: " << total << std::endl;
  return 0;
}

在上面的断点内,我想:

  1. 清除points矢量
  2. 创建新的Point实例
  3. 添加到矢量
  4. 继续执行

这个例子很简单,但我经常有一个相当大的应用程序。这是可能的使用expr ?


我在尝试清除点时收到这个错误:

(lldb) expr points.clear()
warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
error: Couldn't lookup symbols:
  __ZNSt3__16vectorI5PointNS_9allocatorIS1_EEE5clearEv

我可以创建一个对象,这很好!

(lldb) expr auto $x = Point{1, 2}
(lldb) expr $x
(Point) $x = {
  x = 1
  y = 2
}

然而,我不能把它推入向量:

(lldb) expr points.push_back($x)
error: Couldn't lookup symbols:
  __ZNSt3__16vectorI5PointNS_9allocatorIS1_EEE9push_backERKS1_

可以在调试器中创建对象。告诉调试器您希望在表达式解析器中创建持久对象的技巧是,在创建或引用它时给它一个以"$"开头的名称。然后lldb将确保该对象继续存在。

但是,请注意: 中提到的使用STL类时的注意事项

使用XCode/LLDB打印/调试libc++ STL