Create a call schedule for C code

I am writing a tool and I need to create a callgraph for some C projects. I was able to create a callgraph of one file using clang, but I was not able to figure out how to create a call graph for the whole project, which contains dozens of headers and source files.

Any tool that can generate a callgraph for a file that can be parsed will be fine. A useful library will be better.

+6
source share
2 answers

Also worth mentioning is the excellent GNU cflow :

GNU cflow parses the collection of C source files and prints a graph showing the control flow inside the program.

GNU cflow is capable of creating both direct and inverted flow graphs for C sources. Perhaps a list of cross-references can be generated. Two output formats are implemented: POSIX and GNU (advanced).

Before analysis, input files can be pre-processed.

Edit
Regarding the library request. You can "customize" output.c and do something else with the data instead of printing. The internal thread is organized into output handlers, so I think writing your own handler could already do the trick. It's not out of the box, though.

+3
source

Turning my comment into a response.

You can see the output of the assembly and the process using the script. Assuming gcc on linux, you pass the -S flag to gcc and process the result like this:

 perl -ne '/^([^. \t#].*):/ and $f=$1;/call\s+([^*]\S*)/ and print "$f -> $1\n";' *.S 

This will give you a string for each static call containing the calling and called functions. You can add a little template around this file and pass the result to dot or whatever you do with it.

You can omit part of the β€œdo not have to start with a star” regular expression to get some indication of indirect calls. You still cannot tell what functions will be called at that moment, but at least you will find out that there is something else to know.

+1
source

Source: https://habr.com/ru/post/921407/


All Articles