how to make gcc spit out a mapping from flow graphs to source code line numbers

前端 未结 3 1978
迷失自我
迷失自我 2021-02-08 13:06

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?

<
相关标签:
3条回答
  • 2021-02-08 13:32

    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.

    0 讨论(0)
  • 2021-02-08 13:37

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

    0 讨论(0)
  • 2021-02-08 13:46

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

    0 讨论(0)
提交回复
热议问题