Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 39

Thread: detecting function calls

  1. #11
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,089
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: detecting function calls

    gprof is the only profiler I have used on Linux. I don't know what alternatives there might be without searching for them.

    For static analysis of a call tree, it's a very only time since I've had a need to do that. I don't recall the name of the tool I used. The current favourite seems to be Cscope which is installable from the Ubuntu repositories. I haven't tried it myself (or its interfaces such as the Vim interface).

    At runtime, would have been great, but static is also manageable.
    I don't understand the purpose. Static analysis tells you what can call what for all possible inputs, without following any particular path through the code. At runtime, you are after tracing the path through functions that are actually called for a given set of inputs. These give you significantly different information. I don't understand your underlying objective I'm afraid.

  2. #12
    Join Date
    Aug 2012
    Beans
    540

    Re: detecting function calls

    Quote Originally Posted by spjackson View Post
    I don't understand the purpose. Static analysis tells you what can call what for all possible inputs, without following any particular path through the code. At runtime, you are after tracing the path through functions that are actually called for a given set of inputs. These give you significantly different information. I don't understand your underlying objective I'm afraid.
    Sir,
    you are right. Yes at runtime is what I need. So are you suggesting that gprof would be a good choice ?

    I shall go through these tools in depth in the coming days, but just to get a bird's eye view, are gprof, cscope, ctags ,ltrace, strace and the backtrace function all used for the same purpose ?

    Thanks.
    Last edited by IAMTubby; October 5th, 2012 at 09:42 AM.

  3. #13
    Join Date
    Aug 2012
    Beans
    540

    Re: detecting function calls

    Quote Originally Posted by spjackson View Post
    I don't understand the purpose. Static analysis tells you what can call what for all possible inputs, without following any particular path through the code.
    As you said, static is not really useful right ?
    In what situations do people use it ? After your reply, I can't think of any scenario where I would want to use the static trace.

    By the way, you can have a static output by looking at the .s file right ?
    Code:
    $gcc -S code.c
    By digging inside code.s, you can get a static trace right ?
    Sorry if I'm wrong.
    Last edited by IAMTubby; October 5th, 2012 at 09:43 AM.

  4. #14
    Join Date
    Jul 2012
    Beans
    57

    Re: detecting function calls

    Quote Originally Posted by IAMTubby View Post
    As you said, static is not really useful right ?
    In what situations do people use it ? After your reply, I can't think of any scenario where I would want to use the static trace.
    I can think of two scenarios.

    One scenario is when you want to find out if you have "dead code", i.e. code which is never reached. With function calls, you can think of this as func A is defined but never called by any other function. Of course, a simplistic static check could miss the case where func A calls func B which then calls func A (i.e. a loop makes it look like all functions are called).

    Typically, static analysis tools (i.e. which are not limited to function call hierarchies) can be useful for finding bugs. As I already mentioned, they can find dead code (not necessarily just a whole function, but blocks like if/else branches. Dead code usually means you have a logic bug because you thought that code would be reached.

    The other scenario for a tool that gives you function call hierarchies is to help people new to a codebase. In other words, the tool helps people who haven't written much of the source code get up-to-speed quicker. If your boss asks you to modify function C you might want to know what functions call it to make sure you don't create any bugs.

  5. #15
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,089
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: detecting function calls

    The other scenario for a tool that gives you function call hierarchies is to help people new to a codebase.
    Yes, I have used this kind of thing in these circumstances. It can sometimes help you get a picture of how a large system is structured.

  6. #16
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,089
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: detecting function calls

    Quote Originally Posted by IAMTubby View Post
    Sir,
    you are right. Yes at runtime is what I need. So are you suggesting that gprof would be a good choice ?
    No, I am not. I know of no tool that gives you exactly the output you described. Nobody else has yet suggested one. If it exists, then it is somewhat obscure.

    I have suggested some tools that do vaguely similar things, but nothing that does exactly what you want.

    I shall go through these tools in depth in the coming days, but just to get a bird's eye view, are gprof, cscope, ctags ,ltrace, strace and the backtrace function all used for the same purpose?
    Those tools are not all for the same purpose.

    gprof is a profiler. It is principally a tool that helps you identify where most of the cpu time is used in your code; it breaks this down by function.

    cscope and ctags appear to have similar purposes to each other in creating a map of where functions are defined and called. Editors can use that information to allow you to jump from one a function call to the definition of the function, for example.

    ltrace and strace also have similar purposes to each other, but different than all the others. These do trace when functions are called, but only particular types of functions. They do not trace your functions in your program.

    backtrace. I don't know what this is unless you mean the backtrace command in gdb.

    If you really want to trace entry to every function, I suggest you use the hooks provided by gcc which were in the article I mentioned http://www.ibm.com/developerworks/li...ry/l-graphvis/

    I have found a more recent example which might be easier for you to follow http://codingrelic.geekhold.com/2010...mentation.html

  7. #17
    Join Date
    Aug 2012
    Beans
    540

    Re: detecting function calls

    I think gprof gives me what I need. But I have 1 question, is it possible to find out the return value of a function and the arguments is takes from a gprof output ?

    Or, is there any other way to get this detail along with the order of function calls ?

    Thanks.

  8. #18
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: detecting function calls

    Maybe if you explain why you need the information, what you are actually trying to acheive - then we might have better luck helping you identify the tools you need.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  9. #19
    Join Date
    Aug 2012
    Beans
    540

    Re: detecting function calls

    Quote Originally Posted by Tony Flury View Post
    Maybe if you explain why you need the information, what you are actually trying to acheive - then we might have better luck helping you identify the tools you need.
    Quote Originally Posted by trent.josephsen View Post
    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.
    Hey Tony, I have to unit test code. For this, I need to know what function calls what, so that I can go and test each function from my code, rather than allowing it's preceding function to call it(like how normally a program works).

    For eg.
    if main() calls fun1() and fun1() calls fun2() as shown below, I want to test fun2() through my code, rather than calling fun1() and let fun1() call fun2().
    Code:
    main()
    {
     fun1();
    }
    
    fun1()
    {
     fun2();
    }
    
    fun2(char* someptr)
    {
    
    }

    My code should be something like,
    Code:
    printf("\nEnter which function you would like to test");
    //At this step, it should show the gprof output on the screen
    [1]fun1 [2]fun2
    
    //Say user selects fun2
    //Then I do,
    printf("\nExecuting test function...");
    myptr = address_containing_some_junkvalue_for_testing;
    status = fun2(myptr);
    
    
    if(status == 0)
     printf("\nunit test passed ");
    else
     printf("\nunit test failed ");
    Any suggestions ?
    Last edited by IAMTubby; October 22nd, 2012 at 09:40 AM.

  10. #20
    Join Date
    May 2008
    Location
    UK
    Beans
    1,448
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: detecting function calls

    Quote Originally Posted by IAMTubby View Post
    Hey Tony, I have to unit test code. For this, I need to know what function calls what, so that I can go and test each function from my code, rather than allowing it's preceding function to call it(like how normally a program works).

    For eg.
    if main() calls fun1() and fun1() calls fun2() as shown below, I want to test fun2() through my code, rather than calling fun1() and let fun1() call fun2().
    Code:
    main()
    {
     fun1();
    }
    
    fun1()
    {
     fun2();
    }
    
    fun2(char* someptr)
    {
    
    }

    My code should be something like,
    Code:
    printf("\nEnter which function you would like to test");
    //At this step, it should show the gprof output on the screen
    [1]fun1 [2]fun2
    
    //Say user selects fun2
    //Then I do,
    printf("\nExecuting test function...");
    myptr = address_containing_some_junkvalue_for_testing;
    status = fun2(myptr);
    
    
    if(status == 0)
     printf("\nunit test passed ");
    else
     printf("\nunit test failed ");
    Any suggestions ?
    Depends how modular your code is - is fun2 only ever called by fun1 - are there a set of 2nd level functions which are only there to help (simplify) the 1st level functions ? If there is a clear boundary between the layers - and opbvious intefaces without global variables (ugggh), then why not break all the 2nd level functions into a separate module - and Cunit test that first. You can then test the first level functions knowing that your helper functions work.

    The other advantage of providing the 2nd level functions in a separate module is that if you want to test your 1st level functions without worrying about the complex functionality in the 2nd level - you could povide a stub for the 2nd Level functions (i.e vey simple code versions which provide known simple answers) and test your 1st level code with that.

    I have always viewed Unit testing as (ideally) black box testing - you treat your code as a box to which you only have access to the input and outputs, and you can't see inside (and so long as the right inputs povide the right outputs you code passes) - if you need to analyse the code you are testig before you test it - then you ae moving towards far more of a white box test - ot wrong just different.
    Last edited by Tony Flury; October 22nd, 2012 at 09:51 AM.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

Page 2 of 4 FirstFirst 1234 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
  •