Gengsheng Liu
Gengsheng Liu

Reputation: 1

How to list all structure definition and get the structure size in gdb?

ptype can be used when you know your structure name, but is there anyway to list all structure definition rather than looking at source code?

print sizeof() can be used to print the structure size, but can we print all the structure size without knowing their name in advance?

Upvotes: -2

Views: 1241

Answers (1)

user3629249
user3629249

Reputation: 16540

EDITED:

gdb uses the struct 'tag' name for displaying details about the struct (field definitions, field values, sizeof, etc) (and a named struct definition is depreciated for defining structs)

so define the struct as

struct struct_t
{
    int field1;
    ...
};

then gdb can find/display all the details of the struct within the code

here is an example

#include <stdio.h>

struct myStruct
{
    int alpha;
    int brovo;
};



 int main ( void )
 {
     struct myStruct structInstance;

     structInstance.alpha = 1;
     structInstance.brovo = 2;

     printf( "%d %d\n", structInstance.alpha, structInstance.brovo );
 } // end function: main

Lets say the resulting executable is named untitled

Then run gdb as in:

gdb untitled
br main
r

then show the contents of the struct:

p structInstance

which will output the following

$1 = {alpha = -8352, brovo = 32767}

Note that the p command was before the struct was initialized.

s
s
p structInstance

which will output:

$2 = {alpha = 1, brovo = 2}

Upvotes: -1

Related Questions