Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!clyde!cuae2!ltuxa!cuuxb!wbp From: wbp@cuuxb.UUCP (Walt Pesch) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: who called a C routine - get it from the stack frame Message-ID: <961@cuuxb.UUCP> Date: Thu, 4-Dec-86 23:42:45 EST Article-I.D.: cuuxb.961 Posted: Thu Dec 4 23:42:45 1986 Date-Received: Fri, 5-Dec-86 06:47:44 EST References: <810@hropus.UUCP> Reply-To: wbp@cuuxb.UUCP (Walt Pesch) Distribution: comp Organization: AT&T-IS, Customer Support, Lisle, Il. Lines: 38 Keywords: stack frame from within user code Summary: getting down into the mud and good and dirty Xref: mnetor comp.lang.c:256 comp.unix.wizards:255 In article <810@hropus.UUCP> jgy@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. I can then map >this address to the calling functions name using "nm". Oh, well, you asked! Time to get down into the mud... For System V, the following dirty trick should work: When defining the actual function, which is normally passed "n" variables, define the function to have "n+1" variables. By the nature of the stack frame, the "n+1"'th variable will contain the program address for returning. Back up x words for the length of the jump instruction, and call "nm"... good luck. This is an interesting way to get at the entire stack frame, and needless to say, all sorts of fun! A generic System V stack frame look like: program address (address after call) saved ap (start of previous frame) saved fp (start of previous frame's automatics and temporaries) | | Stack Growth V I don't know if the same trick will work with BSD or any of the other 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@lll-crg