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

Re: Backtrace function

Subject: Re: Backtrace function
From: Flash Gordon
Date: Fri, 28 Mar 2008 22:13:55 +0000
Newsgroups: comp.lang.c


jacob navia wrote, On 28/03/08 21:39:
Kaz Kylheku wrote:
On Mar 28, 12:47 pm, Keith Thompson <ks...@xxxxxxx> wrote:
sophia <sophia.ag...@xxxxxxxxx> writes:
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...
There's no portable way to do that in standard C.

Also, there is no nonportable way to do that in standard C, either.

:)

No, In non portable C you can do it

Sometimes. Sometimes you can't.

1) Read the frame pointer (This is the only assembly yo need)

If there is one.

2) See at what offset from the frame pointer is the pushed return
   address

If there is anything to tell you other than the function prologue. Of course the return address might not have been saved to RAM either due to function inlining (as an optimisation) or because it did not need to for some other reason.

3) The value stored in the saved frame pointer position points to
   the next frame.

If there is a saved frame pointer. Not all implementations use a separate frame pointer.

Here you have the next frame and the address of some point in the
calling procedure.

Maybe, if all your assumtions are true so far.

4) Read the debug information.

If there is any which there might not be.

5) Find out into which function the return address points to.

6) Is that the function "main"?

7) If not, get the next frame pointer and go to (2)

A problem if main was called recursively.

---------------

This supposes that

(1) you have a frame pointer
(2) The calling function has a frame pointer

GDB is written in C, and the lcc-win debugger is
written in C. So there is OBVIOUSLY a way of doing
this in C!

No, obviously under some specific circumstances it is possible but it is not always possible or if it is then might be more complex than you have suggested.
--
Flash Gordon

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