[comp.sys.sun] Reading a Function's Return Address

marcs@uunet.uu.net (Marc San Soucie) (07/20/90)

Has anyone attempted and succeeded in coding a 'C' fragment which can be
used on a SPARC to determine a function's caller's return address from
within the function? I am trying to generate some tracking software, which
would like to show who has called a function. The old trick of walking
backwards on the stack is none too effective for this.

E-mail would be nice, but I'll watch this group.

    Marc San Soucie
    Servio Corporation
    Beaverton, Oregon
    marcs@slc.com

tac@cs.brown.edu (Theodore A. Camus) (07/24/90)

>Has anyone attempted and succeeded in coding a 'C' fragment which can be
>used on a SPARC to determine a function's caller's return address from
>within the function? 

Just put the following in  'file.s'  :

	.seg	"text"
	.global _getaddr
_getaddr:
	retl
	add	%i7,8,%o0

and link in file.s (just pretend it's a C source file).  Now you can just
say :  this_functions_return_address_is  = getaddr(); in the callee.  This
will give you the point in the caller where the callee will return to.
This value minus 8 is the address of the statement which actually called
the function you're in.  

                           - Ted