CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2009
    Posts
    30

    Smile Is there a good way to find the callers to a particular function?

    Hello. I am trying to understand and modify a complex programs written by others. I found the function which I need to modify and would like to get a list of caller(s) to this function. Given that there are hundreds of files and classes, it would be ineffective to go through each file to search for the callers. Is there a good way to do that? I know that there are programs which display the relationships among classes graphically. Something like that for member functions would be great. I am looking for a solution without the use of a debugger. Thanks.

  2. #2
    Join Date
    Aug 2009
    Location
    china
    Posts
    14

    Re: Is there a good way to find the callers to a particular function?

    #include <iostream>
    using namespace std;

    /*
    //this is the function u want to monitor the caller.
    void foo()
    {
    cout << "foo" << endl;
    }
    */

    //now, u can modify foo as follow
    //in your project, u should modify both the header file and the implemention file of function foo
    //when foo has some papameter, it is similar
    void foo_rename()
    {
    cout << "foo" << endl;
    }
    void foo_wrapper(const char* name)
    {
    cout << name << endl;
    foo_rename();
    }

    #define foo() foo_wrapper(__FUNCTION__)


    int main()
    {
    foo();
    return 0;
    }
    Last edited by baihacker; August 11th, 2009 at 08:18 AM.

  3. #3
    Join Date
    Aug 2009
    Location
    china
    Posts
    14

    Re: Is there a good way to find the callers to a particular function?

    eh, when the function has a default parameter, it becomes more difficult.

    #include <iostream>
    using namespace std;

    /*
    void foo(int x)
    {
    cout << x << endl;
    }
    */
    void foo_rename(int x)
    {
    cout << x << endl;
    }
    void foo_wrapper(int x, const char* name)
    {
    cout << name << endl;
    foo_rename(x);
    }

    #define foo(x) foo_wrapper(x, __FUNCTION__)


    int main()
    {
    foo(1);
    return 0;
    }

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Is there a good way to find the callers to a particular function?

    If you're using Visual Studio, then bring up the 'Call Browser' window.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Is there a good way to find the callers to a particular function?

    Quote Originally Posted by hajimeml View Post
    Hello. I am trying to understand and modify a complex programs written by others. I found the function which I need to modify and would like to get a list of caller(s) to this function. Given that there are hundreds of files and classes, it would be ineffective to go through each file to search for the callers. Is there a good way to do that? I know that there are programs which display the relationships among classes graphically. Something like that for member functions would be great. I am looking for a solution without the use of a debugger. Thanks.

    Have you tried simply searching for the function name in all of the .cpp files using the directory search tools provided by the OS?

    Here is another option. You could download the free full trial and perhaps your company will buy it if you think it is useful. I have seen others at my organization use it and it does a pretty good job of building graphical function call charts.
    http://www.scitools.com/

  6. #6
    Join Date
    Apr 2009
    Posts
    30

    Re: Is there a good way to find the callers to a particular function?

    Quote Originally Posted by JohnW@Wessex View Post
    If you're using Visual Studio, then bring up the 'Call Browser' window.
    I am using Borland C++ Builder Professional 6.

  7. #7
    Join Date
    May 2007
    Posts
    811

    Re: Is there a good way to find the callers to a particular function?

    Mistype your function name and compile, you should get errors where it is called. Heavy handed but it works.

  8. #8
    Join Date
    Apr 2009
    Posts
    30

    Re: Is there a good way to find the callers to a particular function?

    The scitools's Understand software seems to be what I need eventually. I will give it a try. Thank you very much for letting me know.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured