0

When you set a breakpoint, e.g. b main in lldb, it's pretty easy to see the variables in that current frame:

(lldb) frame variables

But how do you inspect temporary objects? Say I have these functions

std::string func2() {...}
void func(const std::string& a) {...}

and I'm calling like

func(func2());

How do I see the temporary variable created by func2()? Is it possible? The command above only seems to show named variables.

jar
  • 381
  • 3
  • 15

1 Answers1

1

If you step INTO func2 - and then use the "finish" command to step back out again, lldb will show you the return value from the function you stepped out of. For instance:

(lldb) br s -n func2
Breakpoint 2: where = step-into`func2() + 18 at step-into.cpp:12, address = 0x0000000100000d62
(lldb) c
Process 29307 resuming
Process 29307 stopped
* thread #1: tid = 0x300436, function: func2() , stop reason = breakpoint 2.1
    frame #0: 0x0000000100000d62 step-into`func2() at step-into.cpp:12
   9    std::string
   10   func2()
   11   {
-> 12     return std::string("some string");
   13   }
   14   
   15   int

Okay, now you are stopped in the function, so when you finish out lldb will collect the return value and show it in the thread printing:

(lldb) fin
Process 29307 stopped

Process 29333 stopped * thread #1: tid = 0x300e94, 0x0000000100000def step-into`main + 31 at step-into.cpp:18, queue = 'com.apple.main-thread', stop reason = step out Return value: (std::__1::basic_string, std::__1::allocator >) $0 = "some string"

    frame #0: 0x0000000100000def step-into`main at step-into.cpp:18
   15   int
   16   main ()
   17   {
-> 18     func(func2());
   19     return 0;
   20   }

or if you have a customized 'thread-format' that doesn't have the return value in it, you can get at it through the SB API's:

(lldb) script print lldb.thread.GetStopReturnValue()
(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) $0 = "some string"

If you are using Xcode, it will also add an item to the top of the locals view showing the return value of the function you just stepped out of.

Jim Ingham
  • 25,260
  • 2
  • 55
  • 63
  • When I get some spare time, I'll make lldb collect all the return values gotten from stepping over an arbitrarily complex line and show them automatically. The collection part isn't hard, but figuring out how to show them in a way that makes sense for lines with more than one function call and/or several levels of nesting will take a little more doing... – Jim Ingham Aug 12 '15 at 23:13
  • Cool thanks. Is there any way to view the actual address of `$0` after doing `fin`? I'd like to inspect the address of the temporary and then verify that RVO occurred, and a copy wasn't made on return from `func2()`. I suspect it hasn't, but I just wanted to "see for myself" :) – jar Aug 14 '15 at 20:34
  • Not that I can think of. The "$" variable that is made for the return is a copy of the return value (copied locally into lldb) and doesn't remember where it came from. If you want to know this you'll have to dig into the ABI docs to figure out where the return value gets put. – Jim Ingham Aug 14 '15 at 22:40