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