1*0a6a1f1dSLionel Sambuc /* $NetBSD: backtrace.c,v 1.6 2015/09/25 19:27:31 christos Exp $ */
284d9c625SLionel Sambuc
384d9c625SLionel Sambuc /*-
484d9c625SLionel Sambuc * Copyright (c) 2012 The NetBSD Foundation, Inc.
584d9c625SLionel Sambuc * All rights reserved.
684d9c625SLionel Sambuc *
784d9c625SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
884d9c625SLionel Sambuc * by Christos Zoulas.
984d9c625SLionel Sambuc *
1084d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1184d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
1284d9c625SLionel Sambuc * are met:
1384d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1484d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1584d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1684d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1784d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
1884d9c625SLionel Sambuc *
1984d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2084d9c625SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2184d9c625SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2284d9c625SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2384d9c625SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2484d9c625SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2584d9c625SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2684d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2784d9c625SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2884d9c625SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2984d9c625SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3084d9c625SLionel Sambuc */
3184d9c625SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: backtrace.c,v 1.6 2015/09/25 19:27:31 christos Exp $");
3384d9c625SLionel Sambuc
3484d9c625SLionel Sambuc #include <sys/param.h>
3584d9c625SLionel Sambuc #include <assert.h>
3684d9c625SLionel Sambuc #include <stdio.h>
3784d9c625SLionel Sambuc #include <string.h>
3884d9c625SLionel Sambuc #include <stdlib.h>
3984d9c625SLionel Sambuc #include <stdarg.h>
4084d9c625SLionel Sambuc #include <stdint.h>
4184d9c625SLionel Sambuc #include <stddef.h>
4284d9c625SLionel Sambuc #include <unistd.h>
4384d9c625SLionel Sambuc #include <fcntl.h>
4484d9c625SLionel Sambuc #include <dlfcn.h>
4584d9c625SLionel Sambuc #include <elf.h>
4684d9c625SLionel Sambuc
4784d9c625SLionel Sambuc #include "execinfo.h"
4884d9c625SLionel Sambuc #include "symtab.h"
4984d9c625SLionel Sambuc
5084d9c625SLionel Sambuc #ifdef __linux__
5184d9c625SLionel Sambuc #define SELF "/proc/self/exe"
5284d9c625SLionel Sambuc #else
5384d9c625SLionel Sambuc #include <sys/sysctl.h>
5484d9c625SLionel Sambuc #define SELF "/proc/curproc/file"
5584d9c625SLionel Sambuc #endif
5684d9c625SLionel Sambuc
5784d9c625SLionel Sambuc static int
open_self(int flags)5884d9c625SLionel Sambuc open_self(int flags)
5984d9c625SLionel Sambuc {
6084d9c625SLionel Sambuc const char *pathname = SELF;
6184d9c625SLionel Sambuc #ifdef KERN_PROC_PATHNAME
6284d9c625SLionel Sambuc static const int name[] = {
63*0a6a1f1dSLionel Sambuc CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
6484d9c625SLionel Sambuc };
6584d9c625SLionel Sambuc char path[MAXPATHLEN];
6684d9c625SLionel Sambuc size_t len;
6784d9c625SLionel Sambuc
6884d9c625SLionel Sambuc len = sizeof(path);
6984d9c625SLionel Sambuc if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1)
7084d9c625SLionel Sambuc pathname = path;
7184d9c625SLionel Sambuc #endif
7284d9c625SLionel Sambuc return open(pathname, flags);
7384d9c625SLionel Sambuc }
7484d9c625SLionel Sambuc
7584d9c625SLionel Sambuc
7684d9c625SLionel Sambuc static int __printflike(4, 5)
rasprintf(char ** buf,size_t * bufsiz,size_t offs,const char * fmt,...)7784d9c625SLionel Sambuc rasprintf(char **buf, size_t *bufsiz, size_t offs, const char *fmt, ...)
7884d9c625SLionel Sambuc {
7984d9c625SLionel Sambuc for (;;) {
8084d9c625SLionel Sambuc size_t nbufsiz;
8184d9c625SLionel Sambuc char *nbuf;
8284d9c625SLionel Sambuc
8384d9c625SLionel Sambuc if (*buf && offs < *bufsiz) {
8484d9c625SLionel Sambuc va_list ap;
8584d9c625SLionel Sambuc int len;
8684d9c625SLionel Sambuc
8784d9c625SLionel Sambuc va_start(ap, fmt);
8884d9c625SLionel Sambuc len = vsnprintf(*buf + offs, *bufsiz - offs, fmt, ap);
8984d9c625SLionel Sambuc va_end(ap);
9084d9c625SLionel Sambuc
9184d9c625SLionel Sambuc if (len < 0 || (size_t)len + 1 < *bufsiz - offs)
9284d9c625SLionel Sambuc return len;
9384d9c625SLionel Sambuc nbufsiz = MAX(*bufsiz + 512, (size_t)len + 1);
9484d9c625SLionel Sambuc } else
9584d9c625SLionel Sambuc nbufsiz = MAX(offs, *bufsiz) + 512;
9684d9c625SLionel Sambuc
9784d9c625SLionel Sambuc nbuf = realloc(*buf, nbufsiz);
9884d9c625SLionel Sambuc if (nbuf == NULL)
9984d9c625SLionel Sambuc return -1;
10084d9c625SLionel Sambuc *buf = nbuf;
10184d9c625SLionel Sambuc *bufsiz = nbufsiz;
10284d9c625SLionel Sambuc }
10384d9c625SLionel Sambuc }
10484d9c625SLionel Sambuc
10584d9c625SLionel Sambuc /*
10684d9c625SLionel Sambuc * format specifiers:
10784d9c625SLionel Sambuc * %a = address
10884d9c625SLionel Sambuc * %n = symbol_name
10984d9c625SLionel Sambuc * %d = symbol_address - address
11084d9c625SLionel Sambuc * %D = if symbol_address == address "" else +%d
11184d9c625SLionel Sambuc * %f = filename
11284d9c625SLionel Sambuc */
11384d9c625SLionel Sambuc static ssize_t
format_string(char ** buf,size_t * bufsiz,size_t offs,const char * fmt,Dl_info * dli,const void * addr)11484d9c625SLionel Sambuc format_string(char **buf, size_t *bufsiz, size_t offs, const char *fmt,
11584d9c625SLionel Sambuc Dl_info *dli, const void *addr)
11684d9c625SLionel Sambuc {
11784d9c625SLionel Sambuc ptrdiff_t diff = (const char *)addr - (const char *)dli->dli_saddr;
11884d9c625SLionel Sambuc size_t o = offs;
11984d9c625SLionel Sambuc int len;
12084d9c625SLionel Sambuc
12184d9c625SLionel Sambuc for (; *fmt; fmt++) {
12284d9c625SLionel Sambuc if (*fmt != '%')
12384d9c625SLionel Sambuc goto printone;
12484d9c625SLionel Sambuc switch (*++fmt) {
12584d9c625SLionel Sambuc case 'a':
12684d9c625SLionel Sambuc len = rasprintf(buf, bufsiz, o, "%p", addr);
12784d9c625SLionel Sambuc break;
12884d9c625SLionel Sambuc case 'n':
12984d9c625SLionel Sambuc len = rasprintf(buf, bufsiz, o, "%s", dli->dli_sname);
13084d9c625SLionel Sambuc break;
13184d9c625SLionel Sambuc case 'D':
13284d9c625SLionel Sambuc if (diff)
13384d9c625SLionel Sambuc len = rasprintf(buf, bufsiz, o, "+0x%tx", diff);
13484d9c625SLionel Sambuc else
13584d9c625SLionel Sambuc len = 0;
13684d9c625SLionel Sambuc break;
13784d9c625SLionel Sambuc case 'd':
13884d9c625SLionel Sambuc len = rasprintf(buf, bufsiz, o, "0x%tx", diff);
13984d9c625SLionel Sambuc break;
14084d9c625SLionel Sambuc case 'f':
14184d9c625SLionel Sambuc len = rasprintf(buf, bufsiz, o, "%s", dli->dli_fname);
14284d9c625SLionel Sambuc break;
14384d9c625SLionel Sambuc default:
14484d9c625SLionel Sambuc printone:
14584d9c625SLionel Sambuc len = rasprintf(buf, bufsiz, o, "%c", *fmt);
14684d9c625SLionel Sambuc break;
14784d9c625SLionel Sambuc }
14884d9c625SLionel Sambuc if (len == -1)
14984d9c625SLionel Sambuc return -1;
15084d9c625SLionel Sambuc o += len;
15184d9c625SLionel Sambuc }
15284d9c625SLionel Sambuc return o - offs;
15384d9c625SLionel Sambuc }
15484d9c625SLionel Sambuc
15584d9c625SLionel Sambuc static ssize_t
format_address(symtab_t * st,char ** buf,size_t * bufsiz,size_t offs,const char * fmt,const void * addr)15684d9c625SLionel Sambuc format_address(symtab_t *st, char **buf, size_t *bufsiz, size_t offs,
15784d9c625SLionel Sambuc const char *fmt, const void *addr)
15884d9c625SLionel Sambuc {
15984d9c625SLionel Sambuc Dl_info dli;
16084d9c625SLionel Sambuc
16184d9c625SLionel Sambuc memset(&dli, 0, sizeof(dli));
16284d9c625SLionel Sambuc (void)dladdr(addr, &dli);
16384d9c625SLionel Sambuc if (st)
16484d9c625SLionel Sambuc symtab_find(st, addr, &dli);
16584d9c625SLionel Sambuc
16684d9c625SLionel Sambuc if (dli.dli_sname == NULL)
16784d9c625SLionel Sambuc dli.dli_sname = "???";
16884d9c625SLionel Sambuc if (dli.dli_fname == NULL)
16984d9c625SLionel Sambuc dli.dli_fname = "???";
17084d9c625SLionel Sambuc if (dli.dli_saddr == NULL)
17184d9c625SLionel Sambuc dli.dli_saddr = (void *)(intptr_t)addr;
17284d9c625SLionel Sambuc
17384d9c625SLionel Sambuc return format_string(buf, bufsiz, offs, fmt, &dli, addr);
17484d9c625SLionel Sambuc }
17584d9c625SLionel Sambuc
17684d9c625SLionel Sambuc char **
backtrace_symbols_fmt(void * const * trace,size_t len,const char * fmt)17784d9c625SLionel Sambuc backtrace_symbols_fmt(void *const *trace, size_t len, const char *fmt)
17884d9c625SLionel Sambuc {
17984d9c625SLionel Sambuc
18084d9c625SLionel Sambuc static const size_t slen = sizeof(char *) + 64; /* estimate */
18184d9c625SLionel Sambuc char *ptr;
18284d9c625SLionel Sambuc symtab_t *st;
18384d9c625SLionel Sambuc int fd;
18484d9c625SLionel Sambuc
18584d9c625SLionel Sambuc if ((fd = open_self(O_RDONLY)) != -1)
18684d9c625SLionel Sambuc st = symtab_create(fd, -1, STT_FUNC);
18784d9c625SLionel Sambuc else
18884d9c625SLionel Sambuc st = NULL;
18984d9c625SLionel Sambuc
19084d9c625SLionel Sambuc if ((ptr = calloc(len, slen)) == NULL)
19184d9c625SLionel Sambuc goto out;
19284d9c625SLionel Sambuc
19384d9c625SLionel Sambuc size_t psize = len * slen;
19484d9c625SLionel Sambuc size_t offs = len * sizeof(char *);
19584d9c625SLionel Sambuc
19684d9c625SLionel Sambuc /* We store only offsets in the first pass because of realloc */
19784d9c625SLionel Sambuc for (size_t i = 0; i < len; i++) {
19884d9c625SLionel Sambuc ssize_t x;
19984d9c625SLionel Sambuc ((char **)(void *)ptr)[i] = (void *)offs;
20084d9c625SLionel Sambuc x = format_address(st, &ptr, &psize, offs, fmt, trace[i]);
20184d9c625SLionel Sambuc if (x == -1) {
20284d9c625SLionel Sambuc free(ptr);
20384d9c625SLionel Sambuc ptr = NULL;
20484d9c625SLionel Sambuc goto out;
20584d9c625SLionel Sambuc }
20684d9c625SLionel Sambuc offs += x;
20784d9c625SLionel Sambuc ptr[offs++] = '\0';
20884d9c625SLionel Sambuc assert(offs < psize);
20984d9c625SLionel Sambuc }
21084d9c625SLionel Sambuc
21184d9c625SLionel Sambuc /* Change offsets to pointers */
21284d9c625SLionel Sambuc for (size_t j = 0; j < len; j++)
21384d9c625SLionel Sambuc ((char **)(void *)ptr)[j] += (intptr_t)ptr;
21484d9c625SLionel Sambuc
21584d9c625SLionel Sambuc out:
21684d9c625SLionel Sambuc symtab_destroy(st);
21784d9c625SLionel Sambuc if (fd != -1)
21884d9c625SLionel Sambuc (void)close(fd);
21984d9c625SLionel Sambuc
22084d9c625SLionel Sambuc return (void *)ptr;
22184d9c625SLionel Sambuc }
22284d9c625SLionel Sambuc
22384d9c625SLionel Sambuc int
backtrace_symbols_fd_fmt(void * const * trace,size_t len,int fd,const char * fmt)22484d9c625SLionel Sambuc backtrace_symbols_fd_fmt(void *const *trace, size_t len, int fd,
22584d9c625SLionel Sambuc const char *fmt)
22684d9c625SLionel Sambuc {
22784d9c625SLionel Sambuc char **s = backtrace_symbols_fmt(trace, len, fmt);
22884d9c625SLionel Sambuc if (s == NULL)
22984d9c625SLionel Sambuc return -1;
23084d9c625SLionel Sambuc for (size_t i = 0; i < len; i++)
23184d9c625SLionel Sambuc if (dprintf(fd, "%s\n", s[i]) < 0)
23284d9c625SLionel Sambuc break;
23384d9c625SLionel Sambuc free(s);
23484d9c625SLionel Sambuc return 0;
23584d9c625SLionel Sambuc }
23684d9c625SLionel Sambuc
23784d9c625SLionel Sambuc static const char fmt[] = "%a <%n%D> at %f";
23884d9c625SLionel Sambuc
23984d9c625SLionel Sambuc char **
backtrace_symbols(void * const * trace,size_t len)24084d9c625SLionel Sambuc backtrace_symbols(void *const *trace, size_t len)
24184d9c625SLionel Sambuc {
24284d9c625SLionel Sambuc return backtrace_symbols_fmt(trace, len, fmt);
24384d9c625SLionel Sambuc }
24484d9c625SLionel Sambuc
24584d9c625SLionel Sambuc int
backtrace_symbols_fd(void * const * trace,size_t len,int fd)24684d9c625SLionel Sambuc backtrace_symbols_fd(void *const *trace, size_t len, int fd)
24784d9c625SLionel Sambuc {
24884d9c625SLionel Sambuc return backtrace_symbols_fd_fmt(trace, len, fd, fmt);
24984d9c625SLionel Sambuc }
250