backtrace.c revision 1.6 1 1.6 christos /* $NetBSD: backtrace.c,v 1.6 2015/09/25 19:27:31 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.6 christos __RCSID("$NetBSD: backtrace.c,v 1.6 2015/09/25 19:27:31 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.3 christos #include <sys/sysctl.h>
54 1.1 christos #define SELF "/proc/curproc/file"
55 1.1 christos #endif
56 1.1 christos
57 1.3 christos static int
58 1.3 christos open_self(int flags)
59 1.3 christos {
60 1.3 christos const char *pathname = SELF;
61 1.3 christos #ifdef KERN_PROC_PATHNAME
62 1.3 christos static const int name[] = {
63 1.6 christos CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
64 1.3 christos };
65 1.3 christos char path[MAXPATHLEN];
66 1.3 christos size_t len;
67 1.3 christos
68 1.3 christos len = sizeof(path);
69 1.3 christos if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1)
70 1.3 christos pathname = path;
71 1.3 christos #endif
72 1.3 christos return open(pathname, flags);
73 1.3 christos }
74 1.3 christos
75 1.3 christos
76 1.1 christos static int __printflike(4, 5)
77 1.1 christos rasprintf(char **buf, size_t *bufsiz, size_t offs, const char *fmt, ...)
78 1.1 christos {
79 1.1 christos for (;;) {
80 1.1 christos size_t nbufsiz;
81 1.1 christos char *nbuf;
82 1.1 christos
83 1.1 christos if (*buf && offs < *bufsiz) {
84 1.1 christos va_list ap;
85 1.1 christos int len;
86 1.1 christos
87 1.1 christos va_start(ap, fmt);
88 1.1 christos len = vsnprintf(*buf + offs, *bufsiz - offs, fmt, ap);
89 1.1 christos va_end(ap);
90 1.1 christos
91 1.4 christos if (len < 0 || (size_t)len + 1 < *bufsiz - offs)
92 1.1 christos return len;
93 1.1 christos nbufsiz = MAX(*bufsiz + 512, (size_t)len + 1);
94 1.1 christos } else
95 1.1 christos nbufsiz = MAX(offs, *bufsiz) + 512;
96 1.1 christos
97 1.1 christos nbuf = realloc(*buf, nbufsiz);
98 1.1 christos if (nbuf == NULL)
99 1.1 christos return -1;
100 1.1 christos *buf = nbuf;
101 1.1 christos *bufsiz = nbufsiz;
102 1.1 christos }
103 1.1 christos }
104 1.1 christos
105 1.1 christos /*
106 1.1 christos * format specifiers:
107 1.1 christos * %a = address
108 1.1 christos * %n = symbol_name
109 1.1 christos * %d = symbol_address - address
110 1.1 christos * %D = if symbol_address == address "" else +%d
111 1.1 christos * %f = filename
112 1.1 christos */
113 1.1 christos static ssize_t
114 1.1 christos format_string(char **buf, size_t *bufsiz, size_t offs, const char *fmt,
115 1.1 christos Dl_info *dli, const void *addr)
116 1.1 christos {
117 1.1 christos ptrdiff_t diff = (const char *)addr - (const char *)dli->dli_saddr;
118 1.1 christos size_t o = offs;
119 1.1 christos int len;
120 1.1 christos
121 1.1 christos for (; *fmt; fmt++) {
122 1.1 christos if (*fmt != '%')
123 1.1 christos goto printone;
124 1.1 christos switch (*++fmt) {
125 1.1 christos case 'a':
126 1.1 christos len = rasprintf(buf, bufsiz, o, "%p", addr);
127 1.1 christos break;
128 1.1 christos case 'n':
129 1.1 christos len = rasprintf(buf, bufsiz, o, "%s", dli->dli_sname);
130 1.1 christos break;
131 1.1 christos case 'D':
132 1.1 christos if (diff)
133 1.1 christos len = rasprintf(buf, bufsiz, o, "+0x%tx", diff);
134 1.1 christos else
135 1.1 christos len = 0;
136 1.1 christos break;
137 1.1 christos case 'd':
138 1.1 christos len = rasprintf(buf, bufsiz, o, "0x%tx", diff);
139 1.1 christos break;
140 1.1 christos case 'f':
141 1.1 christos len = rasprintf(buf, bufsiz, o, "%s", dli->dli_fname);
142 1.1 christos break;
143 1.1 christos default:
144 1.1 christos printone:
145 1.1 christos len = rasprintf(buf, bufsiz, o, "%c", *fmt);
146 1.1 christos break;
147 1.1 christos }
148 1.1 christos if (len == -1)
149 1.1 christos return -1;
150 1.1 christos o += len;
151 1.1 christos }
152 1.1 christos return o - offs;
153 1.1 christos }
154 1.1 christos
155 1.1 christos static ssize_t
156 1.1 christos format_address(symtab_t *st, char **buf, size_t *bufsiz, size_t offs,
157 1.1 christos const char *fmt, const void *addr)
158 1.1 christos {
159 1.1 christos Dl_info dli;
160 1.1 christos
161 1.1 christos memset(&dli, 0, sizeof(dli));
162 1.1 christos (void)dladdr(addr, &dli);
163 1.1 christos if (st)
164 1.1 christos symtab_find(st, addr, &dli);
165 1.1 christos
166 1.1 christos if (dli.dli_sname == NULL)
167 1.1 christos dli.dli_sname = "???";
168 1.1 christos if (dli.dli_fname == NULL)
169 1.1 christos dli.dli_fname = "???";
170 1.1 christos if (dli.dli_saddr == NULL)
171 1.1 christos dli.dli_saddr = (void *)(intptr_t)addr;
172 1.1 christos
173 1.1 christos return format_string(buf, bufsiz, offs, fmt, &dli, addr);
174 1.1 christos }
175 1.1 christos
176 1.1 christos char **
177 1.1 christos backtrace_symbols_fmt(void *const *trace, size_t len, const char *fmt)
178 1.1 christos {
179 1.1 christos
180 1.1 christos static const size_t slen = sizeof(char *) + 64; /* estimate */
181 1.1 christos char *ptr;
182 1.1 christos symtab_t *st;
183 1.1 christos int fd;
184 1.1 christos
185 1.3 christos if ((fd = open_self(O_RDONLY)) != -1)
186 1.1 christos st = symtab_create(fd, -1, STT_FUNC);
187 1.1 christos else
188 1.1 christos st = NULL;
189 1.1 christos
190 1.1 christos if ((ptr = calloc(len, slen)) == NULL)
191 1.2 christos goto out;
192 1.1 christos
193 1.1 christos size_t psize = len * slen;
194 1.1 christos size_t offs = len * sizeof(char *);
195 1.1 christos
196 1.1 christos /* We store only offsets in the first pass because of realloc */
197 1.1 christos for (size_t i = 0; i < len; i++) {
198 1.1 christos ssize_t x;
199 1.1 christos ((char **)(void *)ptr)[i] = (void *)offs;
200 1.1 christos x = format_address(st, &ptr, &psize, offs, fmt, trace[i]);
201 1.1 christos if (x == -1) {
202 1.1 christos free(ptr);
203 1.2 christos ptr = NULL;
204 1.2 christos goto out;
205 1.1 christos }
206 1.1 christos offs += x;
207 1.1 christos ptr[offs++] = '\0';
208 1.1 christos assert(offs < psize);
209 1.1 christos }
210 1.1 christos
211 1.1 christos /* Change offsets to pointers */
212 1.1 christos for (size_t j = 0; j < len; j++)
213 1.1 christos ((char **)(void *)ptr)[j] += (intptr_t)ptr;
214 1.1 christos
215 1.2 christos out:
216 1.1 christos symtab_destroy(st);
217 1.1 christos if (fd != -1)
218 1.1 christos (void)close(fd);
219 1.1 christos
220 1.1 christos return (void *)ptr;
221 1.1 christos }
222 1.1 christos
223 1.1 christos int
224 1.1 christos backtrace_symbols_fd_fmt(void *const *trace, size_t len, int fd,
225 1.1 christos const char *fmt)
226 1.1 christos {
227 1.1 christos char **s = backtrace_symbols_fmt(trace, len, fmt);
228 1.1 christos if (s == NULL)
229 1.1 christos return -1;
230 1.1 christos for (size_t i = 0; i < len; i++)
231 1.1 christos if (dprintf(fd, "%s\n", s[i]) < 0)
232 1.1 christos break;
233 1.1 christos free(s);
234 1.1 christos return 0;
235 1.1 christos }
236 1.1 christos
237 1.1 christos static const char fmt[] = "%a <%n%D> at %f";
238 1.1 christos
239 1.1 christos char **
240 1.1 christos backtrace_symbols(void *const *trace, size_t len)
241 1.1 christos {
242 1.1 christos return backtrace_symbols_fmt(trace, len, fmt);
243 1.1 christos }
244 1.1 christos
245 1.1 christos int
246 1.1 christos backtrace_symbols_fd(void *const *trace, size_t len, int fd)
247 1.1 christos {
248 1.1 christos return backtrace_symbols_fd_fmt(trace, len, fd, fmt);
249 1.1 christos }
250