tags:

views:

576

answers:

3

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.

+2  A: 

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 ...

claferri
Thanks for your suggestion. This will give me a dynamic callgraph, showing the calls being made as the program is executed, right? I do need that, too, but it's not what I was asking for. But I will definitely try KCacheGrind.
reinierpost
It's not as close as I'd hoped. It doesn't print line numbers with declarations, nor does it print line numbers of callees with calls.
reinierpost
+3  A: 

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.

qrdl
This is almost exactly what I was hoping for. Calls within an if(0) seem to be missing, and there may be more details to take notice of, but it's much farther than anything I'd found thus far. Thanks a lot!
reinierpost
+1  A: 

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.)

Jason Orendorff
I wasn't aware of TreeHydra and I will check it out next time I need this. Thanks!
reinierpost