1

I am trying to explore the behaviour of a complex C++ application using Clang and LLDB. I have set a breakpoint in my applicaiton. Once I reach that breakpoint, I would like to create an instance of a simple C++ class and then call a method in the context of that breakpoint.

For example, here is my application:

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

Inside the breakpoint above, I would like to:

  1. Clear the points vector
  2. Create a new Point instance
  3. Add it to the vector
  4. Continue execution

This example is trivial, but often I have a considerably bigger application. Is this possible using expr?


Update

I receive this error when trying to clear the points:

(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

I can create an object, which is good!

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

However, I cannot push it into my vector:

(lldb) expr points.push_back($x)
error: Couldn't lookup symbols:
  __ZNSt3__16vectorI5PointNS_9allocatorIS1_EEE9push_backERKS1_
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245

1 Answers1

2

You can create objects in the debugger. The trick to telling the debugger you want to create a persistent object in the expression parser is to give it a name starting with a "$" when you create or refer to it. Then lldb will make sure the object persists.

Note, however, the caveats when working with STL classes mentioned in:

Printing/Debugging libc++ STL with XCode/LLDB

Community
  • 1
  • 1
Jim Ingham
  • 25,260
  • 2
  • 55
  • 63
  • Thanks @Jim, I was able to make an object. Do you know why I can't add it to my list? (See updated question) – sdgfsdh Nov 02 '16 at 11:46
  • 2
    That question is addressed by the link I mentioned in the answer. Short answer, the STL library doesn't produce out of line copies of template methods from the STL .h files, and the debugger isn't yet able to realize the methods from the SDK. So there is no "push_back" method to call. – Jim Ingham Nov 02 '16 at 21:46