lldb: Couldn't materialize: couldn't get the value of variable

Anthony Kong picture Anthony Kong · Aug 16, 2014 · Viewed 15.6k times · Source

I have compiled a cpp file with this command line: g++ -g test.cpp

It throws an exception at line 28. I want to investigate the cause by inspecting the variables in lldb. I set a break point at line 28 and run the a.out in lldb.

(lldb) n
Process 84233 stopped
* thread #1: tid = 0xa44b86, 0x00000001000017fb a.out`say(s=<unavailable>) + 987 at so.cpp:28, queue = 'com.apple.main-thread', stop reason = step over
    frame #0: 0x00000001000017fb a.out`say(s=<unavailable>) + 987 at so.cpp:28
   25       }
   26       else{
   27           s.insert(0, to_string(sz));
-> 28           s.erase(2, sz-1);
   29       }
   30       return s;
   31   }
(lldb) po s
error: Couldn't materialize: couldn't get the value of variable s: variable not available
Errored out in Execute, couldn't PrepareToExecuteJITExpression

Why the error message? How can I inspect the variable s?

lldb version: lldb-320.4.115.3

g++ version: Configured with: --prefix=/Applications/Xcode6-Beta5.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.0 (clang-600.0.45.3) (based on LLVM 3.5svn) Target: x86_64-apple-darwin13.3.0 Thread model: posix

Answer

Jim Ingham picture Jim Ingham · Aug 19, 2014

That error means the debug information does mention the variable, but says it has no storage location at the current PC.

That can be because the variable got optimized out (unlikely given you are just calling a function on the variable) or because the compiler flubbed the debug info for the variable and lost track of where it went.

Make sure you are compiling the code you are trying to debug at -O0 as there aren't many compilers that emit good debug information at higher optimization levels. If you are compiling at -O0, this is a compiler bug. You should probably report it to the gcc folks. You could see if you have better luck with clang. Otherwise, you have to read the assembly of the function to figure out where the variable actually lives, then tell the debugger to print the appropriately cast address.