1 /* $NetBSD: backtrace.c,v 1.3 2013/08/29 14:58:56 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 #define _BSD_SOURCE
32 #include <sys/cdefs.h>
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
46 #include "execinfo.h"
47
48
rasprintf(char ** buf,size_t * bufsiz,size_t offs,const char * fmt,...)49 static int rasprintf(char **buf, size_t *bufsiz, size_t offs, const char *fmt, ...)
50 {
51 for (;;) {
52 size_t nbufsiz;
53 char *nbuf;
54
55 if (*buf && offs < *bufsiz) {
56 va_list ap;
57 int len;
58
59 va_start(ap, fmt);
60 len = vsnprintf(*buf + offs, *bufsiz - offs, fmt, ap);
61 va_end(ap);
62
63 if (len < 0 || (size_t)len + 1 < *bufsiz - offs)
64 return len;
65 nbufsiz = MAX(*bufsiz + 512, (size_t)len + 1);
66 } else
67 nbufsiz = MAX(offs, *bufsiz) + 512;
68
69 nbuf = realloc(*buf, nbufsiz);
70 if (nbuf == NULL)
71 return -1;
72 *buf = nbuf;
73 *bufsiz = nbufsiz;
74 }
75 }
76
77 /*
78 * format specifiers:
79 * %a = address
80 * %n = symbol_name
81 * %d = symbol_address - address
82 * %D = if symbol_address == address "" else +%d
83 * %f = filename
84 */
format_string(char ** buf,size_t * bufsiz,size_t offs,const char * fmt,Dl_info * dli,const void * addr)85 static ssize_t format_string(char **buf, size_t *bufsiz, size_t offs, const char *fmt,
86 Dl_info *dli, const void *addr)
87 {
88 ptrdiff_t diff = (const char *)addr - (const char *)dli->dli_saddr;
89 size_t o = offs;
90 int len;
91
92 for (; *fmt; fmt++) {
93 if (*fmt != '%')
94 goto printone;
95 switch (*++fmt) {
96 case 'a':
97 len = rasprintf(buf, bufsiz, o, "%p", addr);
98 break;
99 case 'n':
100 len = rasprintf(buf, bufsiz, o, "%s", dli->dli_sname);
101 break;
102 case 'D':
103 if (diff)
104 len = rasprintf(buf, bufsiz, o, "+0x%tx", diff);
105 else
106 len = 0;
107 break;
108 case 'd':
109 len = rasprintf(buf, bufsiz, o, "0x%tx", diff);
110 break;
111 case 'f':
112 len = rasprintf(buf, bufsiz, o, "%s", dli->dli_fname);
113 break;
114 default:
115 printone:
116 len = rasprintf(buf, bufsiz, o, "%c", *fmt);
117 break;
118 }
119 if (len == -1)
120 return -1;
121 o += len;
122 }
123 return o - offs;
124 }
125
format_address(char ** buf,size_t * bufsiz,size_t offs,const char * fmt,const void * addr)126 static ssize_t format_address(char **buf, size_t *bufsiz, size_t offs,
127 const char *fmt, const void *addr)
128 {
129 Dl_info dli;
130
131 memset(&dli, 0, sizeof(dli));
132 (void)dladdr(addr, &dli);
133
134 if (dli.dli_sname == NULL)
135 dli.dli_sname = "???";
136 if (dli.dli_fname == NULL)
137 dli.dli_fname = "???";
138 if (dli.dli_saddr == NULL)
139 dli.dli_saddr = (void *)(intptr_t)addr;
140
141 return format_string(buf, bufsiz, offs, fmt, &dli, addr);
142 }
143
backtrace_symbols_fmt(void * const * trace,size_t len,const char * fmt)144 char **backtrace_symbols_fmt(void *const *trace, size_t len, const char *fmt)
145 {
146
147 static const size_t slen = sizeof(char *) + 64; /* estimate */
148 char *ptr;
149
150 if ((ptr = calloc(len, slen)) == NULL)
151 goto out;
152
153 size_t psize = len * slen;
154 size_t offs = len * sizeof(char *);
155
156 /* We store only offsets in the first pass because of realloc */
157 for (size_t i = 0; i < len; i++) {
158 ssize_t x;
159 ((char **)(void *)ptr)[i] = (void *)offs;
160 x = format_address(&ptr, &psize, offs, fmt, trace[i]);
161 if (x == -1) {
162 free(ptr);
163 ptr = NULL;
164 goto out;
165 }
166 offs += x;
167 ptr[offs++] = '\0';
168 assert(offs < psize);
169 }
170
171 /* Change offsets to pointers */
172 for (size_t j = 0; j < len; j++)
173 ((char **)(void *)ptr)[j] += (intptr_t)ptr;
174
175 out:
176 return (void *)ptr;
177 }
178
backtrace_symbols_fd_fmt(void * const * trace,size_t len,int fd,const char * fmt)179 int backtrace_symbols_fd_fmt(void *const *trace, size_t len, int fd,
180 const char *fmt)
181 {
182 char **s = backtrace_symbols_fmt(trace, len, fmt);
183 if (s == NULL)
184 return -1;
185 for (size_t i = 0; i < len; i++)
186 if (dprintf(fd, "%s\n", s[i]) < 0)
187 break;
188 free(s);
189 return 0;
190 }
191
192 static const char fmt[] = "%a <%n%D> at %f";
193
backtrace_symbols(void * const * trace,size_t len)194 char **backtrace_symbols(void *const *trace, size_t len)
195 {
196 return backtrace_symbols_fmt(trace, len, fmt);
197 }
198
backtrace_symbols_fd(void * const * trace,size_t len,int fd)199 int backtrace_symbols_fd(void *const *trace, size_t len, int fd)
200 {
201 return backtrace_symbols_fd_fmt(trace, len, fd, fmt);
202 }
203
print_backtrace()204 void print_backtrace() {
205 void *bt[10];
206 int size = backtrace(bt, 10);
207
208 for (int i = 0; i < size; i++) {
209 Dl_info info;
210 dladdr(bt[i], &info);
211 if (info.dli_sname) {
212 printf("Address: %p, Symbol: %s\n", bt[i], info.dli_sname);
213 } else {
214 printf("Address: %p\n", bt[i]);
215 }
216 }
217 }