Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detecting the caller.... 1

Status
Not open for further replies.

rdalton

Programmer
Dec 5, 2002
24
US
Does anyone know if this is possible...
have a function be able to detect which function or class called it without passing any parameters?

been thinking about this one...

rdalton
 
It depends on your architecture and also the calling conventions used. In most cases, though, when a function calls another function, the return address is pushed onto the current thread's stack. So in the callee function you could analyze the stack to see what function called you.

Your callee function would have to know what address ranges correspond to what functions (and perhaps what line numbers of each function) in your program. Your function could parse its own symbol table to do this, for example.

Anyway, this technique is used a lot in debugging environments, whenever there is a "stack trace" or "back trace" function. Visual C++ does this. You can view the "call stack" in the debugger to see the chain of function calls. The debugger is merely analyzing the current thread's stack and figuring out what functions called what other functions.

By the way, not only can you see function call return addresses, but also any local variables the caller function has. Of course you'd have to know how many variables and their types beforehand. That sort of information is usually not contained on the stack itself.

All of this said, it is pretty unusual to want to do this in a function. Normally if a function is called you really shouldn't care what function called you.
 
the responce to your question is no.
It is possible only in specific situation, and if you can do it in some situation it is not a rule.

Ion Filipski
1c.bmp
 
You can try to analyse the process stack for return addresses. They must be somewhere near to the first local variable in direction to higher addresses:

void my_function(void)
{
int fictive_variable;

printf("%x\n", *((&fictive_variable)+2));
}

int main(void)
{
my_function();

my_function();

return(0);
}

This approach is dependant on architecture and build settings, so it is dangerous. The value of return address for Visual C++ is not inside boundaries of caller function, it only allows to distinguish between different calls. These values as well as the offset in the stack must be found out empirically.
 
In addition to the last post - remember, that these adresses will change after every change of your sources, so you must check them directly before building of each release :). Of course, you can try to analyse call technique of Visual C++ in debugger disasembling mode and make these addresses position independant. Visual C++ uses some sophisticated address tables to call user functions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top