Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Jul 1999 16:01:50 -0500 (EST)
From:      Alfred Perlstein <bright@rush.net>
To:        Dag-Erling Smorgrav <des@flood.ping.uio.no>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: Determining the return address
Message-ID:  <Pine.BSF.3.96.990717155538.14320Z-100000@cygnus.rush.net>
In-Reply-To: <xzpwvvzo3b0.fsf@flood.ping.uio.no>

next in thread | previous in thread | raw e-mail | index | archive | help
On 17 Jul 1999, Dag-Erling Smorgrav wrote:

> Is there any (evidently non-portable) way of determining a function
> instance's return address? I have an idea or two that involves the
> return address and dladdr(). The code I currently use looks like this:
> 
> int
> log_print(log_t *log, char *fmt, ...)
> {
>     char date[32], str[MAX_LOG_LINE];
>     struct iovec iov[10];
>     Dl_info info;
>     size_t len;
>     va_list ap;
>     int n;
> 
>     len = log_makedate(date, sizeof date);
>     iov[n=0].iov_base = date;
>     iov[n].iov_len = len;
> 
>     if (dladdr(*(((void**)&log)-1), &info) == 0)
> 	iov[++n].iov_base = "(unknown)";
>     else
> 	iov[++n].iov_base = (char *)info.dli_sname;
>     iov[n].iov_len = strlen(iov[n].iov_base);

...

> 
> 
> Is it correct? (empirical evidence suggests it is) Is there any better
> way to do it? Will it work on the Alpha?

This looks like what you are doing is trying to grab the data on the
stack before "log" which is the return address.  I doubt this is
at all portable and may fail because of optimizations and ABI, such
as archs that store the return address in a register...

gdb and glibc have some functions to assist in runtime backtraces,
perhaps a look there may help?

> 
> BTW, is dladdr() signal-safe?

not according to the sigaction man page.

-Alfred



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.3.96.990717155538.14320Z-100000>