Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I derive what members are at what offset in gdb?

Tags:

c++

gcc

gdb

GCC 8.3 is giving me the following warning:

error: '*((void*)& request +128)' may be used uninitialized in this function [-Werror=maybe-uninitialized]

I'd like to know what member is at that offset. It so happens I have a core and I can print the structured data for request like so:

(gdb) p *(Request*)request

This prints the members of Request, but there are many and it's not obvious to me visually which of the members are at offset 128 per the compiler warning. This isn't the first time in gdb I've tried to find what member is at some offset from the beginning of an object. Usually I try to figure this out manually via x/136bx request, in this case, and then compare the two outputs. But I'm wondering: is there a way to tell gdb to print offsets for each of the members it prints in a structured fashion? That is, can it print the structured representation but also annotate with offsets for each of the members? Or, if not that, is there a smarter way for me to find the member at that offset than printing the raw bytes and trying to find which member the bytes of that offset line up with?

like image 973
firebush Avatar asked Oct 19 '25 00:10

firebush


1 Answers

You are probably looking for maintenance print type Request.

For this program:

struct Foo {
  int a;
  double d;
  char c[100];
  double e;
};

int main()
{
  Foo f;
  f.e = 1.0;
}

maint print type f produces:

name 'Foo' (0x2812c60)
code 0x3 (TYPE_CODE_STRUCT)
length 128
...
nfields 4 0x2796480
  [0] bitpos 0 bitsize 0 type 0x27960d0 name 'a' (0x278004a)
...
  [1] bitpos 64 bitsize 0 type 0x2796160 name 'd' (0x2780054)
...
  [2] bitpos 128 bitsize 0 type 0x27963d0 name 'c' (0x278005e)
...
  [3] bitpos 960 bitsize 0 type 0x2796160 name 'e' (0x2780068)
...
like image 131
Employed Russian Avatar answered Oct 20 '25 15:10

Employed Russian