xref: /freebsd/contrib/libexecinfo/backtrace.c (revision 287472b39c7985d968be84ea145c3e75a3e6b875)
1 /*	$NetBSD: backtrace.c,v 1.2 2012/07/09 03:11:59 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.2 2012/07/09 03:11:59 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 		goto out;
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 			ptr = NULL;
184 			goto out;
185 		}
186 		offs += x;
187 		ptr[offs++] = '\0';
188 		assert(offs < psize);
189 	}
190 
191 	/* Change offsets to pointers */
192 	for (size_t j = 0; j < len; j++)
193 		((char **)(void *)ptr)[j] += (intptr_t)ptr;
194 
195 out:
196 	symtab_destroy(st);
197 	if (fd != -1)
198 		(void)close(fd);
199 
200 	return (void *)ptr;
201 }
202 
203 int
204 backtrace_symbols_fd_fmt(void *const *trace, size_t len, int fd,
205     const char *fmt)
206 {
207 	char **s = backtrace_symbols_fmt(trace, len, fmt);
208 	if (s == NULL)
209 		return -1;
210 	for (size_t i = 0; i < len; i++)
211 		if (dprintf(fd, "%s\n", s[i]) < 0)
212 			break;
213 	free(s);
214 	return 0;
215 }
216 
217 static const char fmt[] = "%a <%n%D> at %f";
218 
219 char **
220 backtrace_symbols(void *const *trace, size_t len)
221 {
222 	return backtrace_symbols_fmt(trace, len, fmt);
223 }
224 
225 int
226 backtrace_symbols_fd(void *const *trace, size_t len, int fd)
227 {
228 	return backtrace_symbols_fd_fmt(trace, len, fd, fmt);
229 }
230