Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLDB C++ debugging

Tags:

c++

stl

vector

lldb

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.

like image 985
BRabbit27 Avatar asked Oct 15 '14 19:10

BRabbit27


People also ask

What is LLDB in C?

LLDB is the default debugger in Xcode on macOS and supports debugging C, Objective-C and C++ on the desktop and iOS devices and simulator. All of the code in the LLDB project is available under the “Apache 2.0 License with LLVM exceptions”.

Is LLDB better than GDB?

Both GDB and LLDB are of course excellent debuggers without doubt. GDB is debugger part of the GNU project created to work along the GNU compiler. LLDB is debugger part of the LLVM project created to work along LLVM compiler. The majority of the commands are the same.

Is LLDB the same as GDB?

In brief, LLDB and GDB are two debuggers. The main difference between LLDB and GDB is that in LLDB, the programmer can debug programs written in C, Objective C and C++ while, in GDB, the programmer can debug programs written in Ada, C, C++, Objective C, Pascal, FORTRAN and Go.


2 Answers

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].

like image 195
James Bedford Avatar answered Oct 12 '22 11:10

James Bedford


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.

like image 39
BRabbit27 Avatar answered Oct 12 '22 12:10

BRabbit27