Results 1 to 10 of 39

Thread: detecting function calls

Hybrid View

  1. #1
    Join Date
    Aug 2012
    Beans
    623

    detecting function calls

    Hey,
    Is there a way to know what functions were called by a program during it's execution ?

    Say, I have a code, as follows
    Code:
    #include <stdio.h>
    
    int main(void)
    {
     fun1();
     fun2();
     fun3();
    
     return 0;
    }
    
    int fun1(){}
    int fun2(){fun2_1();}
    int fun3(){}
    
    int fun2_1(){}
    Here, the output should be something like

    Functions called are as follows
    fun1()
    fun2()
    fun2_1()
    fun3()


    Is there an option on gcc for this ?
    Last edited by IAMTubby; October 3rd, 2012 at 02:16 PM.

  2. #2
    Join Date
    Nov 2010
    Location
    India
    Beans
    Hidden!
    Distro
    Ubuntu 15.10 Wily Werewolf

    Re: detecting function calls

    Quote Originally Posted by IAMTubby View Post
    Hey,
    Is there a way to know what functions were called by a program during it's execution ?

    Say, I have a code, as follows
    Code:
    #include <stdio.h>
    
    int main(void)
    {
     fun1();
     fun2();
     fun3();
    
     return 0;
    }
    
    
    int fun1(){}
    int fun2(){fun2_1();}
    int fun3(){}
    
    int fun2_1(){}
    Here, the output should be something like

    Functions called are as follows
    fun1()
    fun2()
    fun2_1()
    fun3()


    Is there an option on gcc for this ?

    simple one , how about placing a printf line ?
    Dont miss anything even it is small. one small pin is enough to bring down a man.


  3. #3
    Join Date
    Aug 2012
    Beans
    623

    Re: detecting function calls

    Quote Originally Posted by raja.genupula View Post
    simple one , how about placing a printf line ?
    Hey raja,
    hmm.. not too sure if I want to do that, I want to write something which would automatically tell me what functions are called.
    Placing a printf is manual right ?

  4. #4

    Re: detecting function calls

    I don't know of any "automatic" method. Calling a function doesn't leave a trace on the call stack after you return, so you'd have to insert some code at the beginning of every function. The simplest way to do this would be just to stick an extra line in the C source.

    gdb might be able to do something like this; it still wouldn't be fully "automatic" though.

    Perhaps you should describe what problem you're trying to solve, rather than how you want to solve it, and we can suggest other solutions.

  5. #5
    Join Date
    Nov 2010
    Location
    India
    Beans
    Hidden!
    Distro
    Ubuntu 15.10 Wily Werewolf

    Re: detecting function calls

    this is for statements , how about using it for your function call

    http://stackoverflow.com/questions/8...s-is-executing
    Dont miss anything even it is small. one small pin is enough to bring down a man.


  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,270
    Distro
    Kubuntu

    Re: detecting function calls

    You can run your C or C++ program through a profiler.

    Look into gprof. You will also need to compile your applications using the -pg option.

  7. #7
    Join Date
    Aug 2012
    Beans
    623

    Re: detecting function calls

    Quote Originally Posted by raja.genupula View Post
    this is for statements , how about using it for your function call

    http://stackoverflow.com/questions/8...s-is-executing
    raja.genupla, I shall read up on pstack and get back to you. I need 1 day to try out some of the things mentioned on your link. Shall try out and get back surely.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •