who called a C routine - get it from the stack frame
Griff Smith
ggs at ulysses.UUCP
Sat Dec 6 11:53:43 AEST 1986
> In article <810 at hropus.UUCP> jgy at hropus.UUCP writes:
> >Can anyone help me with the following problem: I'm looking for a few
> >lines of C or assembly code which can be used at the top of a function
> >to get the address of the function which called it.
>
> Oh, well, you asked! Time to get down into the mud... For System V,
> the following dirty trick should work:
> ...
> A generic System V stack frame looks like:
>
> <n words of passing parameters>
> program address (address after call)
> saved ap (start of previous frame)
> saved fp (start of previous frame's automatics and temporaries)
> <n words for saving registers>
> <automatic and temporary variables>
> |
> | Stack Growth
> V
>
> I don't know if the same trick will work with BSD or any of the other
> <a-hem> variants. And life is too short to RTFS the BSD internals, so
> I'll leave it to someone else to comment on how to do "it" on other
> forms of Unix.
>
> Walt Pesch
> {ihnp4,akgua,et al}!cuuxb!wbp
> cuuxb!wbp at lll-crg
Er, ah, I understand the company policy that UNIX(R) = System V (don't
agree with it, but understand it), but saying UNIX = System V running
on a 3B2 is carrying company loyalty a bit too far.
System V (or BSD, for that matter) on a VAX, or a CCI POWER 6/32 (plus
many others) grows the stack the other way, so you can't access the
return address as an extra arg. You can't easily find the return
address when given only the argument list; you need the value of the
frame pointer. If you really must have the return address, write a
short assembly language function that finds the caller of ITS caller by
following the frame pointer. Make sure you use a different version of
the function for each machine you want to run it on. Better yet, use a
debugging tool that knows what it's doing and quit fooling with machine-
specific hacks.
--
Griff Smith AT&T (Bell Laboratories), Murray Hill
Phone: (201) 582-7736
UUCP: {allegra|ihnp4}!ulysses!ggs
Internet: ggs at ulysses.uucp
More information about the Comp.unix.wizards
mailing list