[lldb-dev] What's the difference between GetLocation, AddressOf and GetLoadAddress

Greg Clayton gclayton at apple.com
Mon Mar 30 13:03:14 PDT 2015


If your variables are both pointers, then you just get the value as unsigned:

SBValue value1 = ...;
SBValue value2 = ...;
if (value1.GetType().IsPointerType() && value2.GetType().IsPointerType())
{
    if (value1.GetValueAsUnsigned() == value2.GetValueAsUnsigned())
    {
        // Pointers are the same
    }
}



> On Mar 30, 2015, at 11:50 AM, Ziming Song <s.ziming at hotmail.com> wrote:
> 
> I want to know if two pointer point to the same variable, so I want to get the location of each variable and the value of the pointer.
> In the API doc, I found three functions that seem to do the work, they are:
> 
> - SBValue::GetLocation -> const char*
> - SBValue::AddressOf -> SBValue
> - SBValue::GetLoadLocation -> addr_t (uint64_t)
> 
Why do you want the location? A pointer will often be in a register like "rax". And if a pointer is on the stack, why do you want the address of the pointer (which is what SBValue::GetLocation() would tell you)?

So: SBValue::GetLocation() will tell you where a variable is stored. It might be something like "rax" when it is in a register. Or some address when it is on the stack, heap or .data section. But this is the location of the value itself (address of the pointer). 

SBValue::AddressOf() will return a new SBValue that represents the address of the value itself. If your variable is in a register, this will be invalid. If you already have a pointer like a "Foo *", then calling AddressOf will return you a value that is a "Foo **".

If you want to know if something is loaded in memory and not in a register, you can call SBValue::GetLoadLocation() which will only return a valid address _if_ the value is actually in memory. If you have a pointer that is in "rax" you will get back LLDB_INVALID_ADDRESS.

So to sum up: if you have a SBValue that represents a pointer, you can ask the value for its value as unsigned using SBValue::GetValueAsUnsigned() since that will be the pointer value itself. You don't want the location (the address of) the value. The SBValue::AddressOf() can be used on a variable that is actually an instance of "Foo". So if you have a variable whose type is "Foo", and a pointer to a "Foo":

SBValue foo1 = get_instance_of_foo();
SBValue foo2 = get_pointer_to_foo();

You can still check if these point to the same instance if you wanted to:

lldb::addr_t foo1_load_location = LLDB_INVALID_ADDRESS;
lldb::addr_t foo2_load_location = LLDB_INVALID_ADDRESS;
if (foo1.GetType().IsPointerType())
    foo1_load_location = foo1.GetValueAsUnsigned();
else
    foo1_load_location = foo1.GetLoadLocation();

if (foo2.GetType().IsPointerType())
    foo2_load_location = foo2.GetValueAsUnsigned();
else
    foo2_load_location = foo2.GetLoadLocation();

if (foo1_load_location == foo2_load_location && foo2_load_location != LLDB_INVALID_ADDRESS)
{
    // Two SBValue represent the same value in memory
}

Again you really need to understand what you are asking of the value.


Greg Clayton





More information about the lldb-dev mailing list