You can also do stuff like this with grof:
Code:
[root~/C/functions] gcc -pg -fprofile-arcs -ftest-coverage -o factor factorial.c
Press any key to continue...
[root~/C/functions] ./factor X X X X X
720
/* "factor" uses the value of argc, eg, 6 = 720 */
/* also, you must run the executable once to produce a dump for grof */
[root~/C/functions] gprof -b factor
Flat profile:
Each sample counts as 0.01 seconds.
no time accumulated
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1 0.00 0.00 factorial /* recursion not apparent */
Call graph
granularity: each sample hit covers 2 byte(s) no time propagated
index % time self children called name
5 factorial [1]
0.00 0.00 1/1 main [12]
[1] 0.0 0.00 0.00 1+5 factorial [1] /* here's the recursion */
5 factorial [1]
-----------------------------------------------
Index by function name
[1] factorial
Then you can do this with gcov:
Code:
[root~/C/functions] gcov factorial.c
File 'factorial.c'
Lines executed:100.00% of 7
factorial.c:creating 'factorial.c.gcov'
[root~/C/functions] cat factorial.c.gcov
-: 0:Source:factorial.c
-: 0:Graph:factorial.gcno
-: 0:Data:factorial.gcda
-: 0:Runs:2
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:
7: 3:int factorial (int n) { /* recursion evident */
7: 4: if (n==1) return n;
5: 5: return n*factorial(n-1);
-: 6:}
-: 7:
2: 8:int main(int argc, char **argv) {
2: 9: int x=factorial(argc);
2: 10: printf("%d\n",x);
2: 11: return 0;
-: 12:}
I included this because IMO it's hard to figure the "first step" to use these tools with the man page, but once you have the first step the man page makes sense. There are some crazy looking GUI front ends to this stuff which produce pie graphs and flow charts, but I haven't tried them...
NB. that gcov output is cumulative, which is why the numbers are +1 (I actually ran factor once without args, eg, n=1, then once with 5 args, n=6).