From owner-freebsd-hackers Sat Jul 17 5:42:49 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from flood.ping.uio.no (flood.ping.uio.no [129.240.78.31]) by hub.freebsd.org (Postfix) with ESMTP id DACBE14CE6 for ; Sat, 17 Jul 1999 05:42:45 -0700 (PDT) (envelope-from des@flood.ping.uio.no) Received: (from des@localhost) by flood.ping.uio.no (8.9.3/8.9.1) id OAA14709; Sat, 17 Jul 1999 14:42:43 +0200 (CEST) (envelope-from des) To: hackers@freebsd.org Subject: Determining the return address From: Dag-Erling Smorgrav Date: 17 Jul 1999 14:42:43 +0200 Message-ID: Lines: 51 X-Mailer: Gnus v5.5/Emacs 19.34 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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