Standard Method for Stack Trace In 'C'

  • Thread starter David T. Ashley
  • Start date
D

David T. Ashley

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?)
 
B

Ben Pfaff

David T. Ashley said:
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.
 
D

David T. Ashley

Ben Pfaff said:
"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.
 
B

Ben Pfaff

David T. Ashley said:
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).
 
D

drhowarddrfine

Ben said:
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.
 
M

matevzb

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. said:
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>
 
P

Peter Shaggy Haywood

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"?
 
C

Chris Dollin

David said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top