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