Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to visualise a graph of C structs that contain / point to one another?

I am using Ubuntu 10.04, and studying programming of kernel objects.

I have come across some rather complicated structs which I have difficulties reading, so I thought I'd try to find some tool that can help me visualise them.

Only thing I could find so far is VCG, which has a C Struct Visualization Example, which looks like this:

which looks like something I'd like to use.

First thing, is that the last VCG packaged for Ubuntu is vcg (1.30debian-6) in hardy - but the .deb package can be downloaded and installed in Ubuntu Lucid without problems.

However, it seems this package is only a VCG viewer (similar to vcgviewer, I'd guess). The vcgviewer page notes:

To generate compiler graph data with newest gcc compilers use:
gcc -g -da -dv -fdump-tree-original-raw -fdump-tree-all-all 

So, apparently I'd have to use those switches along with gcc while compiling, to generate .vcg graph files from the C source.

The problem, however, is that I'm building a kernel module, which only references the Linux headers - as I try to avoid as much as I can the recompilation of entire kernel. And it seems, as soon as I try to use -fdump-tree-... switches in that context (kernel module), gcc wants to start compiling the rest of the kernel too! (and obviously fails, in both compilation and generation of .vcg graphs - as I don't have the kernel sources, only headers)

So my question is - is there a tool, that would produce .vcg or .dot graphs of structs - simply using a plain text header file as input? (it would not have to resolve all dependencies - simply those in header files in same directory)

EDIT: it is actually not that important for me that the backend is .vcg or .dot in particular, I mentioned them just because I've found them so far; any sort of software that would allow similar struct visualization, regardless of backend, is welcome :)

PS: Note that if you do not want to use VCG viewers for viewing .vcg graphs, you can convert the .vcg format to a .dot format, and use graphviz instead for the visualisation. What worked for me is to use graph-easy - search.cpan.org for perl - which first got packaged in Ubuntu with Maverick edition, as libgraph-easy-perl (however, the .deb file can - again - be downloaded and installed without problems in Lucid). libgraph-easy-perl installs a graph-easy script, which then allows to do stuff like:

graph-easy test.vcg --as_dot | dot -Tpng -o test.vcg.png 

See also "[graphviz-interest] VCG files" and "Diego Novillo - Re: can't find VCG viewer" for another vcg-to-dot script (which, unfortunately, didn't work for me).

like image 854
sdaau Avatar asked Oct 13 '10 08:10

sdaau


2 Answers

I have had good experiences with using doxygen for that task. It is designed to create documentation out of annotated source files, but it can already give you a lot of things without the annotations, including various graphs. Doxygen uses dot for the graph creation.

like image 102
Bart van Ingen Schenau Avatar answered Nov 05 '22 06:11

Bart van Ingen Schenau


I've managed to successfully build a kernel module with vcg generation by doing the following:

  1. Creating a linked copy of the kernel source or header directory using cp -al /usr/src/linux-srcdir /tmp/tmp-srcdir since gcc wants to write to the current working directory.
  2. Adding EXTRA_CFLAGS="-g -da -dv -fdump-tree-original-raw -fdump-tree-all-all" to the make command line eg. -C /tmp/tmp-srcdir M=pwdEXTRA_CFLAGS="-g -da -dv -fdump-tree-original-raw -fdump-tree-all-all". the vcg files are generated in /tmp/tmp-srcdir
like image 36
Hasturkun Avatar answered Nov 05 '22 06:11

Hasturkun