1

I'm trying to see if gcc can provide more information that I can use to create Control Flow Graph from a C source code and then use it to check which path is taken by an executed test case from Gcov .gcov output. So far my best candidate is using -fdump-tree-cfg with lineno option (-fdump-tree-cfg-lineno) but the resulting file is still limiting for me since node informations are separated for each function. Something like this:

;; Function main (main, funcdef_no=13, decl_uid=3054, cgraph_uid=13, symbol_order=13)

Removing basic block 7
Merging blocks 5 and 6
;; 1 loops found
;;
;; Loop 0
;;  header 0, latch 1
;;  depth 0, outer -1
;;  nodes: 0 1 2 3 4 5 6
;; 2 succs { 3 4 }
;; 3 succs { 5 }
;; 4 succs { 5 }
;; 5 succs { 6 }
;; 6 succs { 1 }
main ()
{
  int number;
  int D.3066;

  <bb 2> [0.00%]:
  [evenodd.c:7:5] printf ([evenodd.c:7:12] "\nEnter a number: ");
  [evenodd.c:8:5] scanf ([evenodd.c:8:11] "%d", [evenodd.c:8:5] &number);
  [evenodd.c:10:2] number.0_1 = number;
  [evenodd.c:10:2] checkNegative (number.0_1);
  [evenodd.c:12:17] number.1_2 = number;
  [evenodd.c:12:17] number.2_3 = (unsigned int) number.1_2;
  [evenodd.c:12:17] _4 = number.2_3 & 1;
  [evenodd.c:12:7] if (_4 == 0)
    goto <bb 3>; [0.00%]
  else
    goto <bb 4>; [0.00%]

  <bb 3> [0.00%]:
  [evenodd.c:13:9] number.3_5 = number;
  [evenodd.c:13:9] printf ([evenodd.c:13:16] "%d is Even\n", number.3_5);
  [0:0] goto <bb 5>; [0.00%]

  <bb 4> [0.00%]:
  [evenodd.c:15:9] number.4_6 = number;
  [evenodd.c:15:9] printf ([evenodd.c:15:16] "%d is Odd\n", number.4_6);

  <bb 5> [0.00%]:
  [evenodd.c:18:9] D.3066 = 0;
  number = {CLOBBER};

<L4> [0.00%]:
  return D.3066;

}



;; Function checkNegative (checkNegative, funcdef_no=14, decl_uid=3057, cgraph_uid=14, symbol_order=14)

;; 1 loops found
;;
;; Loop 0
;;  header 0, latch 1
;;  depth 0, outer -1
;;  nodes: 0 1 2 3 4 5
;; 2 succs { 3 4 }
;; 3 succs { 5 }
;; 4 succs { 5 }
;; 5 succs { 1 }
checkNegative (int number)
{
  <bb 2> [0.00%]:
  [evenodd.c:23:4] if (number < 0)
    goto <bb 3>; [0.00%]
  else
    goto <bb 4>; [0.00%]

  <bb 3> [0.00%]:
  [evenodd.c:24:9] printf ([evenodd.c:24:16] "%d is Negative\n", number);
  [0:0] goto <bb 5>; [0.00%]

  <bb 4> [0.00%]:
  [evenodd.c:26:9] printf ([evenodd.c:26:16] "%d is Positive\n", number);

  <bb 5> [0.00%]:
  [evenodd.c:28:1] return;

}

This means that my parser need to recognize each function call inside the main function and then merge their node information which is a pretty daunting task for me.

So I came accross the -graph option for -fdump-tree from this documentation but I have no idea how to use it. As the doc describe it:

‘graph’

    For each of the other indicated dump files (-fdump-rtl-pass),
  dump a representation of the control flow graph suitable for 
  viewing with GraphViz to file.passid.pass.dot. Each function in
  the file is pretty-printed as a subgraph, so that GraphViz can
  render them all in a single plot.

    This option currently only works for RTL dumps, and the RTL
  is always dumped in slim form.

I don't know how GraphViz works but since it's able to render all function's subgraph as a single plot (I assume that it means all subgraph is merged with the main graph as a single graph), it means that the information provided by -graph is more complete and ease the process of creating the Control Flow Graph.

Since I can't understand what it means, I tried using the option as it is (-fdump-tree-cfg-lineno-graph) but the result is still the same as -fdump-tree-cfg-lineno's result.

How do I use it?

AceVez
  • 291
  • 1
  • 5
  • 19
  • I think you're using it correctly, don't you see `.dot`-file being created? Works here with GCC 5.5.0. – xaizek Apr 17 '18 at 11:52
  • @xaizek apparently it doesn't show up on the machine I was working on. A `.dot` file was created on my own laptop. Both have GCC 7.2.0 (MinGW-W64 project). – AceVez Apr 17 '18 at 12:49
  • @xaizek Yep, it does work. I have tried on another computer and the `.dot` file is created. The fault is on my end. – AceVez Apr 17 '18 at 13:13

1 Answers1

1

Solution is straightforward:

gcc -fdump-tree-all-graph main.c -o main

Also take a look: How can I dump an abstract syntax tree generated by gcc into a .dot file?

funnydman
  • 9,083
  • 4
  • 40
  • 55