How to Find who is calling me?

Hi,

I was trying to find from the function which is called by some other function but is it possiable by the calling funcation that who is calling me? For example

int function1()
{
// do something
return 0 ;
}

char function2()
{
// do something
function1() ; // is function2 knows function2 is calling me?
return '' ;
}

int main()
{
function1() ; // is function1 knows I called by main?
return 0 ;
}

by doing this we can able to track the program but this facility is there when we use debugger or when we debug our code but how to do that programatically I am not sure unix has stack tracing facility.

any help regarding this?

Thanks in advance
Balasubramanian.

Can you use strace ?

STRACE(1)                                               STRACE(1)

NAME
       strace - trace system calls and signals

SYNOPSIS
       strace [ -dffhiqrtttTvxx ] [ -acolumn ] [ -eexpr ] ...  [ -ofile ] [ -ppid
       ] ...  [ -sstrsize ] [ -uusername ] [ command [ arg ...  ] ]

       strace -c [ -eexpr ] ...  [ -Ooverhead ] [ -Ssortby ] [ command [ arg  ...
       ] ]

DESCRIPTION
       In the simplest case strace runs the specified command until it exits.  It
       intercepts and records the system calls which are called by a process  and
       the  signals  which  are  received  by a process.  The name of each system
       call, its arguments and its return value are printed on standard error  or
       to the file specified with the -o option.

       strace  is a useful diagnostic, instructional, and debugging tool.  System
       adminstrators, diagnosticians and trouble-shooters will find it invaluable
       for  solving  problems  with  programs for which the source is not readily
       available since they do not need to be recompiled in order to trace  them.
       Students,  hackers  and the overly-curious will find that a great deal can
       be learned about a system and its system calls by  tracing  even  ordinary
       programs.   And  programmers will find that since system calls and signals
       are events that happen at the user/kernel interface, a  close  examination
       of  this  boundary  is  very useful for bug isolation, sanity checking and
       attempting to capture race conditions.

... more available on man page

Or, did I misunderstand your question?

OP wants to know if a function can do a stack crawl to find the address of the calling function - I do not know how.

Thanks for your reply, I am not sure how to use strace? also I am not sure strace will give me the flow or sequence I am expecting from any program. I am looking for the function which is already available in Java called getStackTrace similarly if we have any things to do in C/C++ or any easy way to do that.

Thanks for your help,
Balasubramanian

Does the OP mean the address of the CPU stack pointers? If so, I don't think this is available at the OS level. That is why, as I recall (it has been many years), there are "buffer overflows" and similar security issues, because the OS and application does not have direct visibility into the addresses of the CPU H/W stack.

The OP doesn't want an address at all. Inside function1, he wants to do something and get either "function2" or "main" as the result, depending on which of those called him. This might mean obtaining the the returm address as an intermediate result and then scanning a symbol table, but the address is not the objective. This would only be doable if the program was compiled with symbolic debugging information included as for use with a symbolic debugger like gdb. I'm not going to try something that daunting for such a minor result.

If I really needed that, I would expand the argument list of the function1 module to contain a field called "caller". Then I use:
function1("main")
function1("function2")
as needed. function1 would refer to the first argument to find the caller.

Is this way you don't think its kind of hard codeing? I am asking for the generic solution so that we can use for all of our programs without opening the file mens with out analysing the file we can know what are all the functions availabe and in what sequence its called like that. those functionality already available in Rational Rose when we reverse engineering the project it will generate sequence diagram those applications will read our project and generate it but we are developing the application like similar to that.

Thanks for your reply.
Balasubramanian.

Standard C defines a predefined identifier __func__ which stores the name of the function as static const char currently being executed.

int function1(char * name )
{
// do something
printf ( "In f1 - Called by[%s]\n" , name ) ;
return 0 ;
}

char function2(char * name )
{
// do something
printf ( "In f2 - Called by [%s]\n" , name ) ;
function1((char *)__func__) ; // is function2 knows function2 is calling me?
return '' ;
}

int main()
{
function1((char *)__func__) ; // is function1 knows I called by main?
function2((char *)__func__) ;
return 0 ;
}

Thanks Prasad, do you know which header is __func__ is defined? as well in this way we need to change the function signature is it? can I know how to use this facility without changing the function signature? right now we are using hard coded function name in front of every function to make a track of the function. Please let me know if you have any other idea which is better than this.

As stated that __func__ is a predefined identifier which stores the name of the function currently being executed. These are defined and implemented by C language as Predefined Macro Names. There is no need for any header file include directive. Please note that there are other predefined identifier like __DATE__ , __TIME__ ,__FILE__ etc that the language implements.

Regarding function signatures you might have to change the function signatures in case if you are not able to find an efficient alternative.

Hope this helps.