3

I am trying to find the exact number of function calls to one of my implemented C function inside my code. The project includes several C files. What is the easiest solution to figure out how many times a function is called during the execution of the program? Specifically, I am interested to know how many times a specific function calls another function. For instance I have a C file like:

//file1.c
int main(){
foo1();
return 0;
}

and other C files like:

//file2.c
void  foo1(){
    foo2();
    ...
    foo2();
}

and

//file3.c
void foo2(){
    foo3();
    foo3();
    foo3();
}

Now I have my final executable a.out and want to know how many times foo3() is called inside foo1(). BTW, I am compiling and running my project on Linux.

A23149577
  • 2,045
  • 2
  • 40
  • 74
  • `foo3` called from other functions? – Ohad Eytan Jul 19 '16 at 20:29
  • If you want to know this from within foo3, you can declare a static variable within foo3 that foo3 will increment on every call (a static variable declared within a function is automatically initialised to zero and retains its value from one call to the next). Otherwise, if you need to know this from anywhere in the program, declare a global variable that foo3 will increment on every call. – clarasoft-it Jul 19 '16 at 20:29
  • 1
    Do you need to code this or can you use an external tool for that? – Ishay Peled Jul 19 '16 at 20:29

3 Answers3

4

You can use 2 global variables (put extern at the places that access the variable outside the file you declare them) :

int foo1_active = 0;
int foo3_counter = 0;

then each time foo1 is called you increment it variable and before the return you decrement it:

void foo1() {
    foo1_active++;
    ...
    foo1_active--;
    return
}

when foo3 is called you check if foo1 active and if it does you increment the counter:

void foo3() {
    if foo1_active > 0 { 
        foo3_counter++;
    }
    ...
}
Community
  • 1
  • 1
Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
  • I defined the `foo3_counter` as `static int` in my main.c file, and also use `extern int foo3_counter` inside my file3.c, but I keep getting `undefined reference to 'foo3_counter'` inside my file3.c. – A23149577 Jul 19 '16 at 21:43
  • @A23149577 remove the `static` – Ohad Eytan Jul 19 '16 at 21:47
1

You have an ubuntu flag, so I assume you are using gcc. I'd strongly consider adding -pg to your CFLAGS and trying out gprof.

Profiling works by changing how every function in your program is compiled so that when it is called, it will stash away some information about where it was called from. From this, the profiler can figure out what function called it, and can count how many times it was called. This change is made by the compiler when your program is compiled with the `-pg' option, which causes every function to call mcount (or _mcount, or __mcount, depending on the OS and compiler) as one of its first operations.

evaitl
  • 1,365
  • 8
  • 16
0

You can count function calls using a static variable instead of global variable.

int inc(){
    static int counter = 1;
    counter++;
    return counter;
}
int main(){
    int i;

    for (i = 0; i < 10; i++)
        printf("%d\n", inc());

    return 0;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Vlad Dohotaru
  • 91
  • 2
  • 4