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