Home | History | Annotate | Line # | Download | only in libexecinfo
backtrace.c revision 1.1
      1  1.1  christos /*	$NetBSD: backtrace.c,v 1.1 2012/05/26 22:02:29 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Christos Zoulas.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos #include <sys/cdefs.h>
     32  1.1  christos __RCSID("$NetBSD: backtrace.c,v 1.1 2012/05/26 22:02:29 christos Exp $");
     33  1.1  christos 
     34  1.1  christos #include <sys/param.h>
     35  1.1  christos #include <assert.h>
     36  1.1  christos #include <stdio.h>
     37  1.1  christos #include <string.h>
     38  1.1  christos #include <stdlib.h>
     39  1.1  christos #include <stdarg.h>
     40  1.1  christos #include <stdint.h>
     41  1.1  christos #include <stddef.h>
     42  1.1  christos #include <unistd.h>
     43  1.1  christos #include <fcntl.h>
     44  1.1  christos #include <dlfcn.h>
     45  1.1  christos #include <elf.h>
     46  1.1  christos 
     47  1.1  christos #include "execinfo.h"
     48  1.1  christos #include "symtab.h"
     49  1.1  christos 
     50  1.1  christos #ifdef __linux__
     51  1.1  christos #define SELF	"/proc/self/exe"
     52  1.1  christos #else
     53  1.1  christos #define SELF	"/proc/curproc/file"
     54  1.1  christos #endif
     55  1.1  christos 
     56  1.1  christos static int __printflike(4, 5)
     57  1.1  christos rasprintf(char **buf, size_t *bufsiz, size_t offs, const char *fmt, ...)
     58  1.1  christos {
     59  1.1  christos 	for (;;) {
     60  1.1  christos 		size_t nbufsiz;
     61  1.1  christos 		char *nbuf;
     62  1.1  christos 
     63  1.1  christos 		if (*buf && offs < *bufsiz) {
     64  1.1  christos 			va_list ap;
     65  1.1  christos 			int len;
     66  1.1  christos 
     67  1.1  christos 			va_start(ap, fmt);
     68  1.1  christos 			len = vsnprintf(*buf + offs, *bufsiz - offs, fmt, ap);
     69  1.1  christos 			va_end(ap);
     70  1.1  christos 
     71  1.1  christos 			if (len < 0 || (size_t)len < *bufsiz - offs)
     72  1.1  christos 				return len;
     73  1.1  christos 			nbufsiz = MAX(*bufsiz + 512, (size_t)len + 1);
     74  1.1  christos 		} else
     75  1.1  christos 			nbufsiz = MAX(offs, *bufsiz) + 512;
     76  1.1  christos 
     77  1.1  christos 		nbuf = realloc(*buf, nbufsiz);
     78  1.1  christos 		if (nbuf == NULL)
     79  1.1  christos 			return -1;
     80  1.1  christos 		*buf = nbuf;
     81  1.1  christos 		*bufsiz = nbufsiz;
     82  1.1  christos 	}
     83  1.1  christos }
     84  1.1  christos 
     85  1.1  christos /*
     86  1.1  christos  * format specifiers:
     87  1.1  christos  *	%a	= address
     88  1.1  christos  *	%n	= symbol_name
     89  1.1  christos  *	%d	= symbol_address - address
     90  1.1  christos  *	%D	= if symbol_address == address "" else +%d
     91  1.1  christos  *	%f	= filename
     92  1.1  christos  */
     93  1.1  christos static ssize_t
     94  1.1  christos format_string(char **buf, size_t *bufsiz, size_t offs, const char *fmt,
     95  1.1  christos     Dl_info *dli, const void *addr)
     96  1.1  christos {
     97  1.1  christos 	ptrdiff_t diff = (const char *)addr - (const char *)dli->dli_saddr;
     98  1.1  christos 	size_t o = offs;
     99  1.1  christos 	int len;
    100  1.1  christos 
    101  1.1  christos 	for (; *fmt; fmt++) {
    102  1.1  christos 		if (*fmt != '%')
    103  1.1  christos 			goto printone;
    104  1.1  christos 		switch (*++fmt) {
    105  1.1  christos 		case 'a':
    106  1.1  christos 			len = rasprintf(buf, bufsiz, o, "%p", addr);
    107  1.1  christos 			break;
    108  1.1  christos 		case 'n':
    109  1.1  christos 			len = rasprintf(buf, bufsiz, o, "%s", dli->dli_sname);
    110  1.1  christos 			break;
    111  1.1  christos 		case 'D':
    112  1.1  christos 			if (diff)
    113  1.1  christos 				len = rasprintf(buf, bufsiz, o, "+0x%tx", diff);
    114  1.1  christos 			else
    115  1.1  christos 				len = 0;
    116  1.1  christos 			break;
    117  1.1  christos 		case 'd':
    118  1.1  christos 			len = rasprintf(buf, bufsiz, o, "0x%tx", diff);
    119  1.1  christos 			break;
    120  1.1  christos 		case 'f':
    121  1.1  christos 			len = rasprintf(buf, bufsiz, o, "%s", dli->dli_fname);
    122  1.1  christos 			break;
    123  1.1  christos 		default:
    124  1.1  christos 		printone:
    125  1.1  christos 			len = rasprintf(buf, bufsiz, o, "%c", *fmt);
    126  1.1  christos 			break;
    127  1.1  christos 		}
    128  1.1  christos 		if (len == -1)
    129  1.1  christos 			return -1;
    130  1.1  christos 		o += len;
    131  1.1  christos 	}
    132  1.1  christos 	return o - offs;
    133  1.1  christos }
    134  1.1  christos 
    135  1.1  christos static ssize_t
    136  1.1  christos format_address(symtab_t *st, char **buf, size_t *bufsiz, size_t offs,
    137  1.1  christos     const char *fmt, const void *addr)
    138  1.1  christos {
    139  1.1  christos 	Dl_info dli;
    140  1.1  christos 
    141  1.1  christos 	memset(&dli, 0, sizeof(dli));
    142  1.1  christos 	(void)dladdr(addr, &dli);
    143  1.1  christos 	if (st)
    144  1.1  christos 		symtab_find(st, addr, &dli);
    145  1.1  christos 
    146  1.1  christos 	if (dli.dli_sname == NULL)
    147  1.1  christos 		dli.dli_sname = "???";
    148  1.1  christos 	if (dli.dli_fname == NULL)
    149  1.1  christos 		dli.dli_fname = "???";
    150  1.1  christos 	if (dli.dli_saddr == NULL)
    151  1.1  christos 		dli.dli_saddr = (void *)(intptr_t)addr;
    152  1.1  christos 
    153  1.1  christos 	return format_string(buf, bufsiz, offs, fmt, &dli, addr);
    154  1.1  christos }
    155  1.1  christos 
    156  1.1  christos char **
    157  1.1  christos backtrace_symbols_fmt(void *const *trace, size_t len, const char *fmt)
    158  1.1  christos {
    159  1.1  christos 
    160  1.1  christos 	static const size_t slen = sizeof(char *) + 64;	/* estimate */
    161  1.1  christos 	char *ptr;
    162  1.1  christos 	symtab_t *st;
    163  1.1  christos 	int fd;
    164  1.1  christos 
    165  1.1  christos 	if ((fd = open(SELF, O_RDONLY)) != -1)
    166  1.1  christos 		st = symtab_create(fd, -1, STT_FUNC);
    167  1.1  christos 	else
    168  1.1  christos 		st = NULL;
    169  1.1  christos 
    170  1.1  christos 	if ((ptr = calloc(len, slen)) == NULL)
    171  1.1  christos 		return NULL;
    172  1.1  christos 
    173  1.1  christos 	size_t psize = len * slen;
    174  1.1  christos 	size_t offs = len * sizeof(char *);
    175  1.1  christos 
    176  1.1  christos 	/* We store only offsets in the first pass because of realloc */
    177  1.1  christos 	for (size_t i = 0; i < len; i++) {
    178  1.1  christos 		ssize_t x;
    179  1.1  christos 		((char **)(void *)ptr)[i] = (void *)offs;
    180  1.1  christos 		x = format_address(st, &ptr, &psize, offs, fmt, trace[i]);
    181  1.1  christos 		if (x == -1) {
    182  1.1  christos 			free(ptr);
    183  1.1  christos 			return NULL;
    184  1.1  christos 		}
    185  1.1  christos 		offs += x;
    186  1.1  christos 		ptr[offs++] = '\0';
    187  1.1  christos 		assert(offs < psize);
    188  1.1  christos 	}
    189  1.1  christos 
    190  1.1  christos 	/* Change offsets to pointers */
    191  1.1  christos 	for (size_t j = 0; j < len; j++)
    192  1.1  christos 		((char **)(void *)ptr)[j] += (intptr_t)ptr;
    193  1.1  christos 
    194  1.1  christos 	symtab_destroy(st);
    195  1.1  christos 	if (fd != -1)
    196  1.1  christos 		(void)close(fd);
    197  1.1  christos 
    198  1.1  christos 	return (void *)ptr;
    199  1.1  christos }
    200  1.1  christos 
    201  1.1  christos int
    202  1.1  christos backtrace_symbols_fd_fmt(void *const *trace, size_t len, int fd,
    203  1.1  christos     const char *fmt)
    204  1.1  christos {
    205  1.1  christos 	char **s = backtrace_symbols_fmt(trace, len, fmt);
    206  1.1  christos 	if (s == NULL)
    207  1.1  christos 		return -1;
    208  1.1  christos 	for (size_t i = 0; i < len; i++)
    209  1.1  christos 		if (dprintf(fd, "%s\n", s[i]) < 0)
    210  1.1  christos 			break;
    211  1.1  christos 	free(s);
    212  1.1  christos 	return 0;
    213  1.1  christos }
    214  1.1  christos 
    215  1.1  christos static const char fmt[] = "%a <%n%D> at %f";
    216  1.1  christos 
    217  1.1  christos char **
    218  1.1  christos backtrace_symbols(void *const *trace, size_t len)
    219  1.1  christos {
    220  1.1  christos 	return backtrace_symbols_fmt(trace, len, fmt);
    221  1.1  christos }
    222  1.1  christos 
    223  1.1  christos int
    224  1.1  christos backtrace_symbols_fd(void *const *trace, size_t len, int fd)
    225  1.1  christos {
    226  1.1  christos 	return backtrace_symbols_fd_fmt(trace, len, fd, fmt);
    227  1.1  christos }
    228