p4plus2
p4plus2

Reputation: 462

Recursive sizeof in gdb?

Is there a way using gdb, or perhaps another tool, to find a total size of some struct/class including memory pointed to by all accessible pointer members? There is a basic sizeof, however this doesn't account for the size of members which are pointers, only the pointer size itself. So, perhaps more clearly, what I want is a sizeof operation which will add the sizeof a pointer member and the sizeof the memory pointed to by said pointer for each pointer member (and so on for further nesting).

I am okay with approximations, absolute precision is unnecessary.

I would prefer a solution which supports c++, however a solution which works with c is better than nothing. Though, I imagine most solutions will probably work with both, assuming a solution exists.

Hopefully that description makes sense, I appreciate any advice on how to accomplish this task, thanks.

Upvotes: 0

Views: 405

Answers (1)

Tom Tromey
Tom Tromey

Reputation: 22559

There is no built-in way to do this in gdb. I also do not know of another pre-canned tool for doing this.

However, I think it would be pretty easy to write for gdb, using the Python scripting ability. At least, this is true if you are using "regular" data structures. In some cases it isn't possible to do this with the information readily available to gdb. For example, if you have a struct containing a "void *", there's no way to know what it points to using just the debuginfo. Also sometimes classes can be written in an obscure style that prevents automated discovery -- there are a couple of classes in libstdc++ like this.

If you want to go even further you could maybe adapt the gdb-heap project, which might let you dig up information about the "void *" case and others.

Upvotes: 1

Related Questions