Search code examples
lldb

LLDB Python API SBAddress constructor error


I'm trying to create an SB Value from an address value that I have, and I'm running into an issue with the SBAddress constructor. When I do this:

target = lldb.debugger.GetSelectedTarget()
pointer = target.FindFirstType('node_t').GetPointerType()
root = target.CreateValueFromAddress('root', lldb.SBAddress(0x100004058, target), pointer)

And then I run

root.GetValue()

I get something like 0x0000000100004041

Is there something wrong that I'm doing here with the constructor of the SBAddress?


Solution

  • You have to be careful about what's pointing to what to get this right. Is 0x100004058 the VALUE of the pointer you want to make a type from, or the LOCATION of that pointer? I suspect it is the former...

    The memory at the address which is the value of the pointer holds an object of type "node_t". So when you want to make an SBValue out of that memory, the type you want is "node_t" not "node_t *".

    However, if 0x100004058 were the location of the pointer, then making a value as a pointer to node_t at that address would be correct.

    So, for instance, stopped here:

    * thread #1: tid = 0x4ae2e3, function: take_void , stop reason = breakpoint 1.1
        frame #0: 0x0000000100000f33 pointers`take_void at pointers.c:12
       9    void
       10   take_void(void *input)
       11   {
    -> 12     printf("Got pointer: %p.\n", input);
       13   }
       14       
       15   int
    

    called from:

       15   int
       16   main()
       17   {
       18     struct Foo my_foo = {111, 222};
       19     take_void (&my_foo);
       20     return 0;
       21   }
    

    then:

    (lldb) fr v -L
    0x00007fff5fbff658: (void *) input = 0x00007fff5fbff670
    

    The first address is the location of input, so:

    (lldb) script
    >>> ptr_type = lldb.target.FindFirstType('Foo').GetPointerType()
    >>> root = lldb.target.CreateValueFromAddress("root", lldb.SBAddress(0x00007fff5fbff658, lldb.target), ptr_type)
    >>> root.GetValue()
        '0x00007fff5fbff670'
    

    Which is what you expect. And:

    >>> root = lldb.target.CreateValueFromAddress("root", lldb.SBAddress(0x00007fff5fbff670, lldb.target), type)
    >>> root.GetValue()
    

    That's right because structures don't have values. And:

    >>> root.GetChildAtIndex(0).GetValue()
    '111'
    >>> root.GetChildAtIndex(1).GetValue()
    '222'
    

    Those were the values that I put in the fields, so that is right. And:

    >>> root = lldb.target.CreateValueFromAddress("root", lldb.SBAddress(0x00007fff5fbff670, lldb.target), ptr_type)
    >>> root.GetValue()
    '0x000000de0000006f'
    

    (which is probably the error you were making) makes sense too, because 0xde is 222 and 0x6f is 111 and I'm on a little-endian system...