Search code examples
cgdb

Dereference twice in gdb


I find myself doing the following to find the location of a pointer and then the string that it points to:

// char* strings[2] = {"Hello", "Brando"};

>>> x/g $rbp-32               
0x7fffffffe0d0: 0x0000555555554a3e
>>> x/s 0x0000555555554a3e <-- manually typed in now
0x555555554a3e: "Hello"

Is there a better way to do a double dereference? Ultimately I would like to do something like?

>>> xx $rbp-32
0x7fffffffe0d0: 0x0000555555554a3e: "Hello"

Solution

  • (gdb) p *(char**)($rbp-0x20)
    $1 = 0x555555556004 "Hello"
    
    (gdb) p *(char**)($rbp-0x20)@2
    $2 = {0x555555556004 "Hello", 0x55555555600a "Brando"}
    
    # This could easily be transformed into a user-defined command so as to avoid repetition.
    (gdb) printf "0x%x: 0x%x: %s\n", ($rbp-0x20), *(char**)($rbp-0x20), *(char**)($rbp-0x20)
    0xffffdb00: 0x55556004: Hello
    
    

    could you please explain what the @2 does?

    From the documentation:

    you can print the contents of array with
    
    p *array@len
    The left operand of ‘@’ must reside in memory. Array values made with ‘@’ in this way behave just like other arrays in terms of subscripting, and are coerced to pointers when used in expressions.