By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Standard Method for Stack Trace In 'C'

P: n/a
Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?

Clearly, one can dissect the stack frame and trace it back. But this method
isn't very portable (it requires a knowledge of the machine).

Is there any method supported by the language or by standards? (Maybe,
similar to the way variable-length argument lists are handled?)
Jan 6 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"David T. Ashley" <dt*@e3ft.comwrites:
Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?
No.

If you're using GCC, then there's a GCC-specific extension to
help with this. See the section in the GCC manual titled
"Getting the Return or Frame Address of a Function" for more
information.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Jan 6 '07 #2

P: n/a
"Ben Pfaff" <bl*@cs.stanford.eduwrote in message
news:87************@blp.benpfaff.org...
>
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Ben, just out of curiousity, what practice was McConnell referring to?

Thanks.
Jan 6 '07 #3

P: n/a
"David T. Ashley" <dt*@e3ft.comwrites:
"Ben Pfaff" <bl*@cs.stanford.eduwrote in message
news:87************@blp.benpfaff.org...
>>
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell

Ben, just out of curiousity, what practice was McConnell referring to?
I couldn't say without rereading _Code Complete_ in its entirety
(not that that is a bad idea).
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Jan 6 '07 #4

P: n/a
Ben Pfaff wrote:
"David T. Ashley" <dt*@e3ft.comwrites:
>"Ben Pfaff" <bl*@cs.stanford.eduwrote in message
news:87************@blp.benpfaff.org...
>>"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Ben, just out of curiousity, what practice was McConnell referring to?

I couldn't say without rereading _Code Complete_ in its entirety
(not that that is a bad idea).
From Page 309 of the second edition:

Use Named Constants Consistently. It's dangerous to use a named
constant in one place and a literal in another to represent the same entity.
Jan 6 '07 #5

P: n/a
On Jan 6, 2:03 am, "David T. Ashley" <d...@e3ft.comwrote:
Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?
Unfortunately, no.
Clearly, one can dissect the stack frame and trace it back. But this method
isn't very portable (it requires a knowledge of the machine).
<OT>Not only that, it's not as clear as one might think on some
machines (the foo PA-RISC comes to mind). x86 and SPARC are pretty
straight forward though, you don't need a lot of machine specific
knowledge.</OT>
Is there any method supported by the language or by standards? (Maybe,
similar to the way variable-length argument lists are handled?)
<OT>As far as I know, there isn't even a POSIX way of doing this, so
consider yourself lucky if e.g. dladdr() is implemented on a specific
machine.</OT>
--
WYCIWYG - what you C is what you get

Jan 6 '07 #6

P: n/a
Groovy hepcat David T. Ashley was jivin' on Fri, 5 Jan 2007 20:03:21
-0500 in comp.lang.c.
Standard Method for Stack Trace In 'C''s a cool scene! Dig it!
>Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?

Clearly, one can dissect the stack frame and trace it back. But this method
isn't very portable (it requires a knowledge of the machine).
Implement your own stack containing the names of called functions.
When you enter a function, push its name on the stack. When you exit a
function, pop the name off the top of the stack. It could get messy if
your functions have many exit points, though.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Jan 8 '07 #7

P: n/a
David T. Ashley wrote:
Is there any portable method for identifying the caller of a function, its
caller, and so on, at runtime, without using a debugger?
Yes: pass the arguments in yourself.
Is there any method supported by the language or by standards? (Maybe,
similar to the way variable-length argument lists are handled?)
No.

(Thank the gods: it would kill one of the single most useful
optimisations in the world. IMAO.)

--
Chris "yes, I hate Java for that as well" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Jan 8 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.