From 06d02944c794a2c0c1e26df21bf56e98a8769853 Mon Sep 17 00:00:00 2001 From: Ed Maste Date: Thu, 29 Aug 2013 15:20:12 +0000 Subject: Vendor import of NetBSD's libexecinfo at 2013-08-29 --- backtrace.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'backtrace.c') diff --git a/backtrace.c b/backtrace.c index 02ad1480372c..e5a6e5824ae6 100644 --- a/backtrace.c +++ b/backtrace.c @@ -1,4 +1,4 @@ -/* $NetBSD: backtrace.c,v 1.2 2012/07/09 03:11:59 christos Exp $ */ +/* $NetBSD: backtrace.c,v 1.3 2013/08/29 14:58:56 christos Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -29,7 +29,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ #include -__RCSID("$NetBSD: backtrace.c,v 1.2 2012/07/09 03:11:59 christos Exp $"); +__RCSID("$NetBSD: backtrace.c,v 1.3 2013/08/29 14:58:56 christos Exp $"); #include #include @@ -50,9 +50,29 @@ __RCSID("$NetBSD: backtrace.c,v 1.2 2012/07/09 03:11:59 christos Exp $"); #ifdef __linux__ #define SELF "/proc/self/exe" #else +#include #define SELF "/proc/curproc/file" #endif +static int +open_self(int flags) +{ + const char *pathname = SELF; +#ifdef KERN_PROC_PATHNAME + static const int name[] = { + CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1, + }; + char path[MAXPATHLEN]; + size_t len; + + len = sizeof(path); + if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1) + pathname = path; +#endif + return open(pathname, flags); +} + + static int __printflike(4, 5) rasprintf(char **buf, size_t *bufsiz, size_t offs, const char *fmt, ...) { @@ -162,7 +182,7 @@ backtrace_symbols_fmt(void *const *trace, size_t len, const char *fmt) symtab_t *st; int fd; - if ((fd = open(SELF, O_RDONLY)) != -1) + if ((fd = open_self(O_RDONLY)) != -1) st = symtab_create(fd, -1, STT_FUNC); else st = NULL; -- cgit v1.3