You can extract this information using gcov
.
Compile the sources like:
gcc -ftest-coverage -fprofile-arcs x.c
Run the executable
./a.out
Run gcov -b
on a source file. The option -b
tells it to generate branch probability information.
gcov -b x.c
gcov
will create text file x.c.gcov
, which will contain the required information, here's a sample:
-: 5:int
function foo called 1 returned 100% blocks executed 100%
1: 6:foo (unsigned int N)
-: 7:{
1: 8: int i, s = 0;
-: 9:
10001: 10: for (i = 0; i < N; ++i)
branch 0 taken 99%
branch 1 taken 1% (fallthrough)
-: 11: {
10000: 12: if ((rand () % 100) < 30)
call 0 returned 100%
branch 1 taken 30% (fallthrough)
branch 2 taken 70%
3027: 13: s++;
-: 14: else
6973: 15: s--;
-: 16: }
-: 17:
1: 18: return s;
-: 19:}