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.
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;
}
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;
}
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.
Re: Is there a good way to find the callers to a particular function?
Quote:
Originally Posted by
hajimeml
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/
Re: Is there a good way to find the callers to a particular function?
Quote:
Originally Posted by
JohnW@Wessex
If you're using Visual Studio, then bring up the
'Call Browser' window.
I am using Borland C++ Builder Professional 6.
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.
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.