comp.lang.c
[Top] [All Lists]

Re: Backtrace function

Subject: Re: Backtrace function
From: Richard Heathfield
Date: Fri, 28 Mar 2008 19:36:25 +0000
Newsgroups: comp.lang.c

sophia said:

> Dear all,
> 
> can any one suggest ways of implementing a backtrace function in ANSI/
> standard C as given in the following link ?
> 
> 
http://pramode.net/2006/09/12/getting-a-backtrace-from-running-c-code/#more-144

When implementing functions such as backtrace(), backtrace_symbols(), and 
backtrace_symbols_fd(), the best place to be sitting is the desk of the 
compiler-writer. You *can* do something similar in a portable ISO C 
program, but AFAIK the only way to do so is to set up a voluntary 
mechanism whereby each function, on entry, pushes its name onto a 
stack-like data structure provided for the purpose - and which then pops 
the name off again just before returning. I've done this myself on two or 
three sites, and seen it done on several more. It's not terribly difficult 
- the trickiest bit is making sure that it still works even in low-memory 
situations, which means either steering clear of malloc&co or getting your 
memory request in nice and early.

-- 
Richard Heathfield <                                www.cpax.org.uk">http://www.cpax.org.uk>
Email: -                                www">http://www. +rjh@
Google users: <                                www.cpax.org.uk/prg/writings/googly.php">http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy