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

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

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

例如,这是我的申请:

#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 类 时的注意事项: