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