backtrace.c revision 1.1 1 /* $NetBSD: backtrace.c,v 1.1 2012/05/26 22:02:29 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 __RCSID("$NetBSD: backtrace.c,v 1.1 2012/05/26 22:02:29 christos Exp $");
33
34 #include <sys/param.h>
35 #include <assert.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <stdarg.h>
40 #include <stdint.h>
41 #include <stddef.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #include <dlfcn.h>
45 #include <elf.h>
46
47 #include "execinfo.h"
48 #include "symtab.h"
49
50 #ifdef __linux__
51 #define SELF "/proc/self/exe"
52 #else
53 #define SELF "/proc/curproc/file"
54 #endif
55
56 static int __printflike(4, 5)
57 rasprintf(char **buf, size_t *bufsiz, size_t offs, const char *fmt, ...)
58 {
59 for (;;) {
60 size_t nbufsiz;
61 char *nbuf;
62
63 if (*buf && offs < *bufsiz) {
64 va_list ap;
65 int len;
66
67 va_start(ap, fmt);
68 len = vsnprintf(*buf + offs, *bufsiz - offs, fmt, ap);
69 va_end(ap);
70
71 if (len < 0 || (size_t)len < *bufsiz - offs)
72 return len;
73 nbufsiz = MAX(*bufsiz + 512, (size_t)len + 1);
74 } else
75 nbufsiz = MAX(offs, *bufsiz) + 512;
76
77 nbuf = realloc(*buf, nbufsiz);
78 if (nbuf == NULL)
79 return -1;
80 *buf = nbuf;
81 *bufsiz = nbufsiz;
82 }
83 }
84
85 /*
86 * format specifiers:
87 * %a = address
88 * %n = symbol_name
89 * %d = symbol_address - address
90 * %D = if symbol_address == address "" else +%d
91 * %f = filename
92 */
93 static ssize_t
94 format_string(char **buf, size_t *bufsiz, size_t offs, const char *fmt,
95 Dl_info *dli, const void *addr)
96 {
97 ptrdiff_t diff = (const char *)addr - (const char *)dli->dli_saddr;
98 size_t o = offs;
99 int len;
100
101 for (; *fmt; fmt++) {
102 if (*fmt != '%')
103 goto printone;
104 switch (*++fmt) {
105 case 'a':
106 len = rasprintf(buf, bufsiz, o, "%p", addr);
107 break;
108 case 'n':
109 len = rasprintf(buf, bufsiz, o, "%s", dli->dli_sname);
110 break;
111 case 'D':
112 if (diff)
113 len = rasprintf(buf, bufsiz, o, "+0x%tx", diff);
114 else
115 len = 0;
116 break;
117 case 'd':
118 len = rasprintf(buf, bufsiz, o, "0x%tx", diff);
119 break;
120 case 'f':
121 len = rasprintf(buf, bufsiz, o, "%s", dli->dli_fname);
122 break;
123 default:
124 printone:
125 len = rasprintf(buf, bufsiz, o, "%c", *fmt);
126 break;
127 }
128 if (len == -1)
129 return -1;
130 o += len;
131 }
132 return o - offs;
133 }
134
135 static ssize_t
136 format_address(symtab_t *st, char **buf, size_t *bufsiz, size_t offs,
137 const char *fmt, const void *addr)
138 {
139 Dl_info dli;
140
141 memset(&dli, 0, sizeof(dli));
142 (void)dladdr(addr, &dli);
143 if (st)
144 symtab_find(st, addr, &dli);
145
146 if (dli.dli_sname == NULL)
147 dli.dli_sname = "???";
148 if (dli.dli_fname == NULL)
149 dli.dli_fname = "???";
150 if (dli.dli_saddr == NULL)
151 dli.dli_saddr = (void *)(intptr_t)addr;
152
153 return format_string(buf, bufsiz, offs, fmt, &dli, addr);
154 }
155
156 char **
157 backtrace_symbols_fmt(void *const *trace, size_t len, const char *fmt)
158 {
159
160 static const size_t slen = sizeof(char *) + 64; /* estimate */
161 char *ptr;
162 symtab_t *st;
163 int fd;
164
165 if ((fd = open(SELF, O_RDONLY)) != -1)
166 st = symtab_create(fd, -1, STT_FUNC);
167 else
168 st = NULL;
169
170 if ((ptr = calloc(len, slen)) == NULL)
171 return NULL;
172
173 size_t psize = len * slen;
174 size_t offs = len * sizeof(char *);
175
176 /* We store only offsets in the first pass because of realloc */
177 for (size_t i = 0; i < len; i++) {
178 ssize_t x;
179 ((char **)(void *)ptr)[i] = (void *)offs;
180 x = format_address(st, &ptr, &psize, offs, fmt, trace[i]);
181 if (x == -1) {
182 free(ptr);
183 return NULL;
184 }
185 offs += x;
186 ptr[offs++] = '\0';
187 assert(offs < psize);
188 }
189
190 /* Change offsets to pointers */
191 for (size_t j = 0; j < len; j++)
192 ((char **)(void *)ptr)[j] += (intptr_t)ptr;
193
194 symtab_destroy(st);
195 if (fd != -1)
196 (void)close(fd);
197
198 return (void *)ptr;
199 }
200
201 int
202 backtrace_symbols_fd_fmt(void *const *trace, size_t len, int fd,
203 const char *fmt)
204 {
205 char **s = backtrace_symbols_fmt(trace, len, fmt);
206 if (s == NULL)
207 return -1;
208 for (size_t i = 0; i < len; i++)
209 if (dprintf(fd, "%s\n", s[i]) < 0)
210 break;
211 free(s);
212 return 0;
213 }
214
215 static const char fmt[] = "%a <%n%D> at %f";
216
217 char **
218 backtrace_symbols(void *const *trace, size_t len)
219 {
220 return backtrace_symbols_fmt(trace, len, fmt);
221 }
222
223 int
224 backtrace_symbols_fd(void *const *trace, size_t len, int fd)
225 {
226 return backtrace_symbols_fd_fmt(trace, len, fd, fmt);
227 }
228