Getting Control Flow Graph from ANSI C code

Eloar picture Eloar · May 6, 2013 · Viewed 17.7k times · Source

I'm building tool for testing ansi c applications. Simply load code, view control flow graph, run test, mark all vertexes which was hit. I'm trying to build CFG all by myself from parsing code. Unfortunately It gets messed up if code is nested. GCC gives ability to get CFG from compiled code. I might write parser for its output, but I need line numbers for setting breakpoints. Is there way for getting line numbers when outputting Control Flow Graph with -fdump-tree-cfg or -fdump-tree-vcg?

Answer

Ioannis Filippidis picture Ioannis Filippidis · Jul 24, 2013

For the control flow graph of a C Program you could look at existing Python parsers for C:

Call graphs are a closely related construct to control flow graphs. There are several approaches available to create call graphs (function dependencies) for C code. This might prove of help for progressing with control flow graph generation. Ways to create dependency graphs in C:

The following tools unfortunately require that the code be compilable, because they depend on output from gcc:

  • CodeViz (GPL v2) (weak point: needs compilable source, because it uses gcc to dump cdepn files)
  • gcc +egypt +dot (GPL v*, Perl = GPL | Artistic license, EPL v1) (egypt uses gcc to produce RTL, so fails for any buggy source code, or even in case you just want to focus on a single file from a larger project. Therefore, it is not very useful compared to the more robust cflow-based toolchains. Note that egypt has by default good support for excluding library calls from the graph, to make it cleaner.

Also, file dependency graphs for C/C++ can be created with crowfood.