简体   繁体   中英

Visualisation of the memory layout of C structs

I'm currently working on a C project that interfaces heavily with other assembly code. We're having self-made visualizations of control blocks and structs that we're using and I'm locking for a process on how to automate this process.

Since we have a C struct equivalency for every assembly control block and we're programming for a pretty rare architecture, the easiest way would probably to visualize the C structs.

I'm more or less looking for a way to automatically obtain graphs like the "TCP pseudo-header for checksum computation (IPv6)" from the TCP wiki page:

TCP pseudo-header for checksum computation (IPv6)

Sadly, I haven't found any open-source tools that are able to generate such visualizations from C header files. Is there a way to generate such images or html representations without manually writing them?

EDIT: Thanks to Alexey Frunze for the idea, with the utility pahole it's possible to extract the real memory layout of all used structs from the DWARF sections of an object file.

One of the ways to do it is to compile your C code with those structs and extract the structure information from the debugging info of the object/executable file. Otherwise you're looking into finding/making structure parsers or hacking clang.

UPD : Never tried it, but there's pycparser , which might be useful.

The layout of some C aggregate ( struct , union , array) is implementation specific because of data structure alignment constraints (required by your particular ABI ).

You might use the debugger (eg ptype command of gdb ). Notice that ddd has a graphical display.

If you have many structures, you could consider customizing your GCC compiler using MELT . You'll develop your particular MELT extension to show the layout. It could take weeks (because you'll need to understand some of the GCC internals).

I'm developing a C toolbox that can, among other things, diagram C types of any complexity. It imports and comprehends Dwarf information in code compiled with "-g", and and can dump out whatever you want in "dot" format (which can be displayed via any number of tools.)

(It's also a programming language that uses this Dwarf comprehension ability to access library/program internals directly, with zero glue-code or linking necessary, at runtime.)

See: https://github.com/jasonnyberg/j2/wiki/Diagramming-C-types-using-j2

The j2 system can read and comprehend Dwarf information; As a debugging feature, it can also dump items you select in the form of the "dot" language, which allows the type information hierarchy to be displayed in graphical form. The "stack" function, which displays items on the interpreter's top layer of stack, also dumps these items out to /tmp/VMRES_STACK.dot.

To diagram an item, all that's needed is to reference it by name (if it's not already on the stack), and then invoke the stack function with, "stack!" (which references the function "stack" by name, and then evaluates it via the "!" operator.)

Once you have the interpreter running (see link above for the interpreter itself and an actual example run which dumps a graph of a structure), you just need to A) "import" the library:

j2> loadlib([test/build/libtestlib.so]) @testlib

then B) reference your imported structure (adding it to the interpreter's stack):

j2> testlib.teststruct

and finally, C) display the stack (textually, and as a side effect, generating the file /tmp/VMRES_STACK.dot):

j2> stack!

The graph contained within /tmp/VMRES_STACK.dot can be displayed via any of a number of graphviz/dot viewers, such as xdot:

bash> xdot /tmp/VMRES_STACK.dot

xdot /tmp/VMRES_STACK.dot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM