Samuel Jacob's Weblog

Just another technical blog

Size of all data structures in a C program

without comments

There are some cases when you want to find size of all data structures in your program/project. For a given program it is easy because we can manually calculate or put a printf sizeof() to calculate a few structures. But for a project with few hundred files it is difficult. There is noway AFAIK in gcc to dump all the structure sizes while it compiles.

Here is a way todo it using DWARF debugging info if you are using gnu compiler tools. Compile your project output file with -g option, this will generate debugging information in your program. Then run objdump to dump the debug info. This debugging information contains all the information about your program, filter the output for “DW_TAG_structure_type” and you will get only structure information.

Here is how I did it:

objdump -W kernel.debug.reloc > debug.info
grep -e "DW_TAG_structure_type" debug.info --after-context=5 --mmap > structs.txt

Written by samueldotj

November 19th, 2009 at 3:58 am

Posted in C,Debugger,gcc