简体   繁体   English

可视化C结构的内存布局

[英]Visualisation of the memory layout of C structs

I'm currently working on a C project that interfaces heavily with other assembly code. 我目前正在开发一个与其他汇编代码密切接口的C项目。 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. 由于我们对每个程序集控制块都有一个C结构等价,并且我们正在编写一个非常罕见的架构,最简单的方法可能是可视化C结构。

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 wiki页面中的“校验和计算的TCP伪报头(IPv6)”等图形:

TCP pseudo-header for checksum computation (IPv6) 用于校验和计算的TCP伪报头(IPv6)

Sadly, I haven't found any open-source tools that are able to generate such visualizations from C header files. 遗憾的是,我还没有找到任何能够从C头文件生成这种可视化的开源工具。 Is there a way to generate such images or html representations without manually writing them? 有没有办法生成这样的图像或HTML表示而无需手动编写它们?

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. 编辑:感谢Alexey Frunze的想法,使用实用程序pahole,可以从目标文件的DWARF部分提取所有使用的结构的实际内存布局。

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. 其中一种方法是使用这些结构编译C代码,并从对象/可执行文件的调试信息中提取结构信息。 Otherwise you're looking into finding/making structure parsers or hacking clang. 否则你正在寻找/制作结构解析器或黑客铿锵声。

UPD : Never tried it, but there's pycparser , which might be useful. UPD :没试过,但有pycparser ,这可能有用。

The layout of some C aggregate ( struct , union , array) is implementation specific because of data structure alignment constraints (required by your particular ABI ). 由于数据结构对齐约束(特定ABI需要),某些C聚合( structunion ,array)的布局是特定于实现的

You might use the debugger (eg ptype command of gdb ). 您可以使用调试器(例如gdb ptype命令)。 Notice that ddd has a graphical display. 请注意, ddd具有图形显示。

If you have many structures, you could consider customizing your GCC compiler using MELT . 如果您有许多结构,可以考虑使用MELT自定义GCC编译器。 You'll develop your particular MELT extension to show the layout. 您将开发特定的MELT扩展以显示布局。 It could take weeks (because you'll need to understand some of the GCC internals). 这可能需要数周时间(因为您需要了解一些GCC内部)。

I'm developing a C toolbox that can, among other things, diagram C types of any complexity. 我正在开发一个C工具箱,除其他外,它可以绘制任何复杂性的C类型。 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.) 它在用“-g”编译的代码中导入和理解Dwarf信息,并且可以以“点”格式(可以通过任意数量的工具显示)转储出你想要的任何内容。

(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 请参阅: https//github.com/jasonnyberg/j2/wiki/Diagramming-C-types-using-j2

The j2 system can read and comprehend Dwarf information; j2系统可以读取和理解矮人信息; 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. “stack”函数在解释器的顶层堆栈上显示项目,也将这些项目转储到/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!" 要绘制一个项目,只需要按名称引用它(如果它不在堆栈中),然后使用“stack!”调用堆栈函数。 (which references the function "stack" by name, and then evaluates it via the "!" operator.) (按名称引用函数“stack”,然后通过“!”运算符对其进行求值。)

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: 一旦您运行了解释器(请参阅上面的解释器本身的链接以及转储结构图的实际示例运行),您只需要A)“导入”库:

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

then B) reference your imported structure (adding it to the interpreter's stack): 然后B)引用你导入的结构(将它添加到解释器的堆栈):

j2> testlib.teststruct

and finally, C) display the stack (textually, and as a side effect, generating the file /tmp/VMRES_STACK.dot): 最后,C)显示堆栈(文本上,作为副作用,生成文件/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: /tmp/VMRES_STACK.dot中包含的图形可以通过许多graphviz / dot查看器显示,例如xdot:

bash> xdot /tmp/VMRES_STACK.dot

xdot /tmp/VMRES_STACK.dot xdot /tmp/VMRES_STACK.dot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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