how to make gcc spit out a mapping from flow graph

2020-06-03 03:11发布

问题:

Can gcc spit out, given a C file, a list of the all function calls that occur, with filename and line number both for the call itself and for the function's declaration?

I know gcc somehow retains this information with -g (debuggers rely on it) and that it can dump control flow graphs with -dr (but without filenames or line numbers); but is there a ready-to-use tool that takes gcc output and does what I want?

The reason I want such a tool to use gcc is that this will allow me to use it with the standard build system most gcc-based software comes with (e.g. ./configure && make) even in cases where tools that rely on their own preprocessor and/or parser are a major hassle to fit in. I'm already aware of several such tools, e.g. ctags. So this question is a followup to question 525899.

回答1:

Try gcc option -fdump-tree-fixupcfg-lineno.

It will "pretty print" parsing AST (with line numbers) in a way that can easily be parsed using relatively simple lexer or any regex engine. Just find all non-keywords preceded by '=' and followed by '(' - it will be function calls.

All complex expressions will be split into several lines so no two function calls will appear on one line.

Take simple program:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PI (3.1415926536)

int main(int argc, char *argv[]) {
    double  angle = PI / 2.0;
    printf("Sine = %lf, cosine = %lf\n", sin(angle), cos(angle));
    return EXIT_SUCCESS;
}

Compile it with -fdump-tree-fixupcfg-lineno and you get something like this:

main (argc, argv)
{
  double angle;
  int D.3381;
  double D.3380;
  double D.3379;

  # BLOCK 2, starting at line 8
  # PRED: ENTRY (fallthru)
  [test.c : 8] angle = 1.57079632680000003119857865385711193084716796875e+0;
  [test.c : 9] D.3379 = [test.c : 9] cos (angle);
  [test.c : 9] D.3380 = [test.c : 9] sin (angle);
  [test.c : 9] printf (&"Sine = %lf, cosine = %lf\n"[0], D.3380, D.3379);
  [test.c : 10] D.3381 = 0;
  return D.3381;
  # SUCC: EXIT

}

You won't get any complex expressions - just assignments and function call and no CPP macros, very easy to parse. Loops and conditionals don't make it much more difficult.



回答2:

Valgrind and KCacheGrind seems a good tool for this use :

valgrind --tool=callgrind --dump-instr=yes ./your_binary

This will give you a file called callgrind.out.pid that you can open with KCacheGrind. This will let you see lots of informations like call graph, filename ...



回答3:

You might try Treehydra, a GCC plugin that gives you read-only access to GCC internal representations of code during compilation. (However, it's a bit of a chore to build, and I'm not sure it'll give better results than -fdump-* for this problem.)



标签: c gcc call-graph