12

I am new to LLDB and I am working with various std::vectors in my code, however when I try to print the values of a vector or to query the size of my vector with something like expr '(int)myVector[0]' or expr '(int)myVector.size()' the debugger prints values that have nothing to do with the values I know there are in the vector.

As I'm learning to debug with command line and LLDB, I'm sure I'm missing something here, can anyone spot my error or give some advise?

EDIT Forgot to say that I'm under OS X Mavericks with the latest command-line tools installed.

BRabbit27
  • 6,333
  • 17
  • 90
  • 161

2 Answers2

20

I found the answer myself. Apparently the overloaded operators like [] are not allowed since they are inlined, see this question for a better explanation on that.

Moreover, I don't know why did I put single quotes for the statement I wanted to evaluate (I'm pretty sure I saw it in other place ... what do they actually mean in LLDB?) like so expr 'printf("Hey")'

So, taking out the quotes and using the answer in the cited question it suffices with something like

expr (int) myVector.__begin_[0]

to get the single value of a position in the vector.

Community
  • 1
  • 1
BRabbit27
  • 6,333
  • 17
  • 90
  • 161
  • However, one thing still remains to be solved, how to get the size of a vector? – BRabbit27 Oct 15 '14 at 20:56
  • `expr (int)myVector.size()` doesn't work? (Or using `p` instead..?) – James Bedford Oct 15 '14 at 21:02
  • Nope, getting the following `error: call to a function 'std::__1::vector >::size() const' ('_ZNKSt3__16vectorImNS_9allocatorImEEE4sizeEv') that is not present in the target error: 0 errors parsing expression error: The expression could not be prepared to run in the target` – BRabbit27 Oct 15 '14 at 21:04
  • How about (untested) `expr (int) (myVector.__end_ - myVector.__begin_)`? But that might give you the size in bytes, rather than in terms of 'number of entries in the vector' – Marshall Clow Oct 17 '14 at 12:37
  • This works for getting the size (without the int cast): `expr (myVector.__end_ - myVector.__begin_)` – bu11d0zer Aug 15 '17 at 19:48
3

Use p myVector or po myVector. These will print out the contents of your vector (alongside the size) in a couple of different formats.

To print a single value from the vector, you can use something like p (int)myVector[0].

James Bedford
  • 28,702
  • 8
  • 57
  • 64
  • "po" will probably work but honestly I don't see the point - a C++ vector isn't an ObjC object, and what "po" is for is specifically printing ObjC objects via their -description/-debugDescription method. Since a C++ vector is not one of those, "po" ends up being almost the same as "p" – Enrico Granata Oct 15 '14 at 20:06
  • Also, OP, showing your code would help diagnose the issue. What you're doing should be legit (assuming your vector truly contains integers that is) – Enrico Granata Oct 15 '14 at 20:06
  • `p` and `po` effectively prints the contents of the vector, but I am looking for a way to just print the single value of a position in the vector because with `p` a lot of info about the vector is printed out. – BRabbit27 Oct 15 '14 at 20:13
  • Use `p`. E.g. `p (int)myVector[0]`. – James Bedford Oct 15 '14 at 20:23
  • 4
    frame variable myVector[0] – Enrico Granata Oct 16 '14 at 16:57
  • In my case (Xcode 8) `po` print only the size of the vector. – DEADBEEF Apr 06 '17 at 15:53
  • in my case (CLion2017.3, osx), it doesn't work. terminal gives an error, error: Couldn't lookup symbols. however, selected answer (expr) works. – Heefan Mar 13 '18 at 03:59