Skip site navigation (1)Skip section navigation (2)
Date:      17 Jul 1999 14:42:43 +0200
From:      Dag-Erling Smorgrav <des@flood.ping.uio.no>
To:        hackers@freebsd.org
Subject:   Determining the return address
Message-ID:  <xzpwvvzo3b0.fsf@flood.ping.uio.no>

next in thread | raw e-mail | index | archive | help
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);

    iov[++n].iov_base = ": ";
    iov[n].iov_len = 2;
    
    va_start(ap, fmt);
    len = lvformat(str, sizeof str, fmt, ap);
    va_end(ap);

    while (len > 0 && isspace(str[len-1]))
	--len;
    
    iov[++n].iov_base = str;
    iov[n].iov_len = len;

    iov[++n].iov_base = "\n";
    iov[n].iov_len = 1;
    
    return writev(log->fd, iov, ++n);
}

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

BTW, is dladdr() signal-safe?

DES
-- 
Dag-Erling Smorgrav - des@flood.ping.uio.no


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?xzpwvvzo3b0.fsf>