【问题标题】:In LLDB, can I call methods and make instances of C++ classes?在 LLDB 中,我可以调用方法并创建 C++ 类的实例吗?
【发布时间】:2017-03-09 23:33:54
【问题描述】:

我正在尝试使用 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_

【问题讨论】:

    标签: c++ debugging clang lldb


    【解决方案1】:

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

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

    Printing/Debugging libc++ STL with XCode/LLDB

    【讨论】:

    • 感谢@Jim,我能够制作一个对象。你知道为什么我不能将它添加到我的列表中吗? (查看更新的问题)
    • 我在答案中提到的链接解决了这个问题。简短的回答,STL 库不会从 STL .h 文件中生成模板方法的离线副本,并且调试器还不能实现 SDK 中的方法。所以没有“push_back”方法可以调用。
    猜你喜欢
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多