1

I am trying to get the address of a struct member from debug info using gdb.

My problem is that I have a structure like:

typedef struct{
  int a;
  int b;
  int c;
}tstructDesc;

tstructDesc myStruct =
{
  1,
  2,
  3,
};

int main()
{
  /* Do something */
}

With gdb I can get the address of the myStruct structure with the command "info address myStruct", but I'd like to get the addresses of the member variables (myStruct.a, myStruct.b, myStruct.c). I discovered the "ptype myStruct" command which returns the definition of the struct, from which I can calculate the relative and absolute addresses, but I think it's not an effective way to get the task done.

Do you know any other way to get the addresses of the struct members?

Thanks in advance, Tamas

  • Tamas it turns out that tsstructDexc is going to be 3*sizeof(int) so the address of myStruct.a is in fact the address of myStruct. in gdb you can set up a breakpoint and do : (gdb) p &myStruct or (gdb) p &mystruct.a – ForeverStudent Nov 12 '15 at 14:14
  • The solution you give is correct but what you mean with "tsstructDexc is going to be 3*sizeof(int)?". To know the size of a struct you must use sizeof(structname): http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member – terence hill Nov 12 '15 at 14:20

1 Answers1

4

In GDB you can:

(gdb) print myStruct
$1 = {a = 1, b = 2, c = 3}
(gdb) print &myStruct
$2 = (tstructDesc *) 0x600a58 <myStruct>
(gdb) print &myStruct.a
$3 = (int *) 0x600a58 <myStruct>
(gdb) print &myStruct.b
$4 = (int *) 0x600a5c <myStruct+4>
(gdb) print &myStruct.c
$5 = (int *) 0x600a60 <myStruct+8>
terence hill
  • 3,354
  • 18
  • 31