From: eLinux.org

GDB Tips

get element size

Sometimes, with complex structures (arrays of structs containing nested
structs or arrays), it is hard to determine the actual size of a
particular element.

You can use gdb with a program image to get the size of structures, by
looking at the offset of an element of the structure relative to an
address of zero:

Here are some examples:

  1. ${CROSS_COMPILE}gdb vmlinux
  2. GNU gdb (GDB) 7.2
  3. Copyright (C) 2010 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law. Type "show copying"
  7. and "show warranty" for details.
  8. This GDB was configured as "--host=i686-pc-linux-gnu --target=arm-sony-linux-gnueabi".
  9. For bug reporting instructions, please see:
  10. <http://www.gnu.org/software/gdb/bugs/>.
  11. (gdb) p &((struct poll_wqueues *)0)->polling_task
  12. $6 = (struct task_struct **) 0xc
  13. (gdb) p/d &((struct poll_wqueues *)0)->error
  14. $4 = 20

the second example could be read as: “print, in decimal, the address
(offset) of the element error using address 0 cast as a pointer to
struct poll_wqueues”

‘pt’ is the first element of struct poll_wqueues. Here is an example
using array offsets, showing that struct poll_wqueues is 604 bytes
long.

  1. (gdb) p/d &((struct poll_wqueues *)0)[0]->pt
  2. $2 = 0
  3. (gdb) p/d &((struct poll_wqueues *)0)[1]->pt
  4. $3 = 604

Category: