26

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)

2 Answers 2

38

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"
3
  • Thanks for the detailed answer ! Mar 29, 2016 at 8:29
  • Is there a nicer way to print the address of self when self is an object? expr --raw -- &self returns an error: use of extraneous '&'. The only way I've found to get the address is to limit the depth to zero: frame var -L -D 0 self but that seems a bit awkward. Aug 9 at 18:47
  • I gather from the error that you are debugging swift code? This works as expected in ObjC. Swift doesn't use & as "address of", so that's not valid swift code, thus the error. Swift being a 'memory safe' language, it doesn't have much interest in showing you where variables live. You might be able to use the Unsafe... methods to come up with a valid swift expression that returns the location of the variable? But you are fighting against swift's design at that point...
    – Jim Ingham
    Aug 10 at 17:57
0

Please, if I may be allowed to supplement the excellent answer by @Jim Ingham above.

There is a very good tutorial available on this topic Examining Stack Frame State.

Although it may take a few minutes, a good tip is to first dump all available stack frame variables using the command:

frame variable

Having confirmed the desired variable is available, one may then run:

frame variable myVar

Or, include the address as mentioned:

frame variable -L myVar

Personally, I like to add an Xcode debug action - without pausing execution - as follows:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.