memory-addresslldb

lldb : Printing a variable's address


I am trying to print a variable's address with lldb. However, calling print &(myVar) prints the variable's content instead of its address.

(lldb) print &(myVar)
(const string *) $18 = "hello"

Same for expression &(myVar).

(lldb) expression &(myVar)
(const string *) $19 = "hello"

I also tried expression's -L option :

(lldb) expression -L -- &(myVar)
0x00000000021aea80: (const string *) $20 = "hello"

(lldb) expression -L -- myVar
0x0000000002a15430: (std::string) $23 = "hello"

However the address outputted changes each time I invoke expression -L. Hence I am assuming that it does not correspond to the variable's address in memory.

How do I get the variable's address in memory ?

(I use lldb 3.4)


Solution

  • Yes, the -L location is telling you about the variable that lldb makes to represent the expression result, so that isn't what you want. Even though the common command alias print makes it seem like this command just prints values, it does a lot more than that: e.g. creating new entities in the running program. So the location of the expression result is not trivially related to the expression you evaluated.

    Anyway, there are two easy ways to get at this. The first is to turn off the string summary, so you can see the actual result of printing the address:

    (lldb) expr --raw -- &my_string
    (string *) $14 = 0x00007fff5fbff618
    

    Another way to get at the same data is to use the "frame variable" command. This command gives you access to local variables, without the overhead of the full expression parser. Since frame variable prints the variables directly as reported by the debug info, in that case the -L option is exactly the variable's location:

    (lldb) frame var -L my_string
    0x00007fff5fbff618: (std::__1::string) my_string = "Some string here"