Reputation: 8860
For a pretty-printer that I'm writing, I would like to know the alignment or the type which is used in a container. Unfortunately using alignof()
or any similar "standard" operator doesn't work (https://sourceware.org/bugzilla/show_bug.cgi?id=17095). Using "typical" macro tricks that work directly in source code also doesn't work:
p ((char *)(&((struct { char c; double _h; } *)0)->_h) - (char *)0)
A syntax error in expression, near `{ char c; double _h; } *)0)->_h) - (char *)0)'.
Is that possible at all, or maybe the only way is to have that supported by GDB internally?
Upvotes: 1
Views: 706
Reputation: 22519
There's no way to get this information, because currently gdb does not have it.
Before DWARF version 5, there was no standard way to express alignment in the debug info. DWARF 5 added DW_AT_alignment
, but gdb still simply ignores this attribute; to expose it via the Python API would require reading it and storing it in gdb's internal struct type
. I don't know offhand whether compilers emit this attribute yet.
If you were very desperate you could do this either using the gdb compile
feature or by running the compiler yourself, and having it emit the alignment in a way that can be extracted.
However, normally alignment is not too difficult to compute from the relevant type sizes, and if your target architectures are relatively limited then it's probably simpler to just roll your own alignment computer.
Upvotes: 2