Page 1 of 4 123 ... LastLast
Results 1 to 10 of 39

Thread: detecting function calls

  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!

    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
    Join Date
    Feb 2009
    Beans
    1,469

    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!

    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,288
    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 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: detecting function calls

    You can use profiling to tell you how often functions were called, average length of execution etc. There is also a tool to do static analysis that will give you a tree of what calls what. But neither of these seem to be what you want, i.e. an automatic call trace at runtime that prints a trace whenever a function is called.

    ltrace and strace do tracing, but not at this level. I'm not aware of a standard tool that does just what you want, but there may be one.

    Someone's had a go here, but it seems that it hasn't been updated for a while.

    This gives some strong clues, but it is 7 years old, and I don't know if it would actually work.
    Last edited by spjackson; October 3rd, 2012 at 03:55 PM. Reason: Minor formatting mods.

  8. #8
    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.

  9. #9
    Join Date
    Aug 2012
    Beans
    623

    Re: detecting function calls

    Quote Originally Posted by dwhitney67 View Post
    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.
    dwhitney, yes Sir, thanks a lot.
    I tried running a sample program using gprof, was very helpful .
    But I need 1 day to think about it.
    I shall try out and get back to you tomorrow.

    Thanks.
    Last edited by IAMTubby; October 4th, 2012 at 11:37 AM.

  10. #10
    Join Date
    Aug 2012
    Beans
    623

    Re: detecting function calls

    Quote Originally Posted by spjackson View Post
    You can use profiling to tell you how often functions were called, average length of execution etc.
    Thanks spjackson, but what is the name of the profiling tool ? gprof ? I just tried a sample program using that and liked it. Any other profiling tool?

    There is also a tool to do static analysis that will give you a tree of what calls what.
    Sir, would you be able to tell me the name of the tool ?

    But neither of these seem to be what you want, i.e. an automatic call trace at runtime that prints a trace whenever a function is called.
    At runtime, would have been great, but static is also manageable.

    ltrace and strace do tracing, but not at this level. I'm not aware of a standard tool that does just what you want, but there may be one.
    Sir, I shall try them and get back to you in 1 day's time. Currently, held up with some other personal commitment.

    Someone's had a go here, but it seems that it hasn't been updated for a while.

    This gives some strong clues, but it is 7 years old, and I don't know if it would actually work.
    I shall refer the links and get back.

    Thanks

Page 1 of 4 123 ... LastLast

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
  •