Home > Archive > Unix Programming > January 2008 > finding sizeof structure using gdb
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
finding sizeof structure using gdb
|
|
| junky_fellow@yahoo.co.in 2008-01-28, 4:33 am |
| Guys,
Is there any way by which I can find out the size of a particular
structure using GDB ?Also, is there a way, I can find out the offset
of a member inside a structure ?
Many thanks for any help..
| |
| fnegroni 2008-01-28, 4:33 am |
| I think the command you are looking for is:
print _expr_
where _expr_ is the expression you want to evaluate at runtime.
You can place a breakpoint anywhere in your program where you want to
evaluate an expression, and type 'print' followed by the expression
such as 'sizeof (struct_type)'
To find out the offset of a member it necessitates some casts:
Say you have a struct defined as:
struct kelp_file {
FILE *f;
char *name;
};
and at some point an object of type struct kelp_file called k comes
into visibility.
Use gdb to print offset of name compared to start of structure:
print (size_t)&(k.name) - (size_t)(&k)
| |
|
| junky fellow wrote:
> Is there any way by which I can find out the size of a particular
> structure using GDB?
Did you try the obvious 'print sizeof(my_type)'
> Also, is there a way, I can find out the offset of a member inside
> a structure ?
You could define a wrapper in your code and call that.
| |
| junky_fellow@yahoo.co.in 2008-01-28, 8:23 am |
| On Jan 28, 2:54=A0pm, fnegroni <f.e.negr...@googlemail.com> wrote:
> I think the command you are looking for is:
> print _expr_
> where _expr_ is the expression you want to evaluate at runtime.
> You can place a breakpoint anywhere in your program where you want to
> evaluate an expression, and type 'print' followed by the expression
> such as 'sizeof (struct_type)'
> To find out the offset of a member it necessitates some casts:
>
> Say you have a struct defined as:
>
> struct kelp_file {
> FILE *f;
> char *name;
>
> };
>
> and at some point an object of type struct kelp_file called k comes
> into visibility.
>
> Use gdb to print offset of name compared to start of structure:
>
> print (size_t)&(k.name) - (size_t)(&k)
Thanks Guys, that was really helpful.
Can you please also tell me how to typecast a memory address to a
particular structure type and print the values of various membes of
that structure.
For eg. Say I have some "structure x" at address 0xffff1000
Now, I want to typecast this memory address 0xffff1000 with the
"structure x" and print the contents of all the members in "structure
x". Can it be done in GDB Or I have to dump the contents of memory and
manually interpret the value of each member ?
| |
| fnegroni 2008-01-28, 8:23 am |
| Yes, you can typecast an address and gdb will try and reinterpret the
memory location (it is what in C++ is known as reinterpret cast)
For example, recalling the example I gave earlier, say that you
encunter a variable called r which effectively is the address of a
struct kelp_file.
You can issue the command:
print *((struct kelp_file *)ret)
And gdb will print something like
$2 = {
f = 0x5fbff6a000000000,
name = 0x15bc00007fff <Address 0x15bc00007fff out of bounds>
}
Obviously if ret is not the address of a struct kelp type object, you
are printing garbage... but if it is the address of a portion of a
buffer in memory, that could work well... provided the alignment and
padding is the same.
|
|
|
|
|