blob: 3aa13d2567b98c592610f399d65d92767bafea80 [file] [log] [blame]
Luca Clementi327064b2013-07-23 00:11:35 -07001/*
2 * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "defs.h"
28#include <limits.h>
29#include <libunwind-ptrace.h>
30
31/*
32 * Кeep a sorted array of cache entries,
33 * so that we can binary search through it.
34 */
35struct mmap_cache_t {
36 /**
37 * example entry:
38 * 7fabbb09b000-7fabbb09f000 r--p 00179000 fc:00 1180246 /lib/libc-2.11.1.so
39 *
40 * start_addr is 0x7fabbb09b000
41 * end_addr is 0x7fabbb09f000
42 * mmap_offset is 0x179000
43 * binary_filename is "/lib/libc-2.11.1.so"
44 */
45 unsigned long start_addr;
46 unsigned long end_addr;
47 unsigned long mmap_offset;
48 char* binary_filename;
49};
50
Masatake YAMATO2d534da2014-04-16 15:33:04 +090051/*
52 * Type used in stacktrace walker
53 */
54typedef void (*call_action_fn)(void *data,
55 char *binary_filename,
56 char *symbol_name,
57 unw_word_t function_off_set,
58 unsigned long true_offset);
59typedef void (*error_action_fn)(void *data,
60 const char *error,
61 unsigned long true_offset);
62
63
Luca Clementi327064b2013-07-23 00:11:35 -070064static unw_addr_space_t libunwind_as;
65
66void
Masatake YAMATO61413922014-04-16 15:33:02 +090067unwind_init(void)
Luca Clementi327064b2013-07-23 00:11:35 -070068{
69 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
70 if (!libunwind_as)
71 error_msg_and_die("failed to create address space for stack tracing");
72}
73
74void
Masatake YAMATO61413922014-04-16 15:33:02 +090075unwind_tcb_init(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -070076{
77 tcp->libunwind_ui = _UPT_create(tcp->pid);
78 if (!tcp->libunwind_ui)
79 die_out_of_memory();
80}
81
82void
Masatake YAMATO61413922014-04-16 15:33:02 +090083unwind_tcb_fin(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -070084{
Masatake YAMATO61413922014-04-16 15:33:02 +090085 unwind_cache_invalidate(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -070086 _UPT_destroy(tcp->libunwind_ui);
87 tcp->libunwind_ui = NULL;
88}
89
90/*
91 * caching of /proc/ID/maps for each process to speed up stack tracing
92 *
93 * The cache must be refreshed after some syscall: mmap, mprotect, munmap, execve
94 */
Masatake YAMATOb65042f2014-04-16 15:33:00 +090095static void
Luca Clementi327064b2013-07-23 00:11:35 -070096alloc_mmap_cache(struct tcb* tcp)
97{
98 unsigned long start_addr, end_addr, mmap_offset;
99 char filename[sizeof ("/proc/0123456789/maps")];
100 char buffer[PATH_MAX + 80];
101 char binary_path[PATH_MAX];
102 struct mmap_cache_t *cur_entry, *prev_entry;
103 /* start with a small dynamically-allocated array and then expand it */
104 size_t cur_array_size = 10;
105 struct mmap_cache_t *cache_head;
106 FILE *fp;
107
108 sprintf(filename, "/proc/%d/maps", tcp->pid);
109 fp = fopen(filename, "r");
110 if (!fp) {
111 perror_msg("fopen: %s", filename);
112 return;
113 }
114
115 cache_head = calloc(cur_array_size, sizeof(*cache_head));
116 if (!cache_head)
117 die_out_of_memory();
118
119 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
120 binary_path[0] = '\0'; // 'reset' it just to be paranoid
121
122 sscanf(buffer, "%lx-%lx %*c%*c%*c%*c %lx %*x:%*x %*d %[^\n]",
123 &start_addr, &end_addr, &mmap_offset, binary_path);
124
125 /* ignore special 'fake files' like "[vdso]", "[heap]", "[stack]", */
126 if (binary_path[0] == '[') {
127 continue;
128 }
129
130 if (binary_path[0] == '\0') {
131 continue;
132 }
133
134 if (end_addr < start_addr)
135 perror_msg_and_die("%s: unrecognized maps file format",
136 filename);
137
138 cur_entry = &cache_head[tcp->mmap_cache_size];
139 cur_entry->start_addr = start_addr;
140 cur_entry->end_addr = end_addr;
141 cur_entry->mmap_offset = mmap_offset;
142 cur_entry->binary_filename = strdup(binary_path);
143
144 /*
145 * sanity check to make sure that we're storing
146 * non-overlapping regions in ascending order
147 */
148 if (tcp->mmap_cache_size > 0) {
149 prev_entry = &cache_head[tcp->mmap_cache_size - 1];
150 if (prev_entry->start_addr >= cur_entry->start_addr)
151 perror_msg_and_die("Overlaying memory region in %s",
152 filename);
153 if (prev_entry->end_addr > cur_entry->start_addr)
154 perror_msg_and_die("Overlaying memory region in %s",
155 filename);
156 }
157 tcp->mmap_cache_size++;
158
159 /* resize doubling its size */
160 if (tcp->mmap_cache_size >= cur_array_size) {
161 cur_array_size *= 2;
162 cache_head = realloc(cache_head, cur_array_size * sizeof(*cache_head));
163 if (!cache_head)
164 die_out_of_memory();
165 }
166 }
167 fclose(fp);
168 tcp->mmap_cache = cache_head;
169}
170
171/* deleting the cache */
172void
Masatake YAMATO61413922014-04-16 15:33:02 +0900173unwind_cache_invalidate(struct tcb* tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700174{
175 unsigned int i;
176 for (i = 0; i < tcp->mmap_cache_size; i++) {
177 free(tcp->mmap_cache[i].binary_filename);
178 tcp->mmap_cache[i].binary_filename = NULL;
179 }
180 free(tcp->mmap_cache);
181 tcp->mmap_cache = NULL;
182 tcp->mmap_cache_size = 0;
183}
184
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900185/*
186 * walking the stack
187 */
188static void
189stacktrace_walk(struct tcb *tcp,
190 call_action_fn call_action,
191 error_action_fn error_action,
192 void *data)
Luca Clementi327064b2013-07-23 00:11:35 -0700193{
194 unw_word_t ip;
195 unw_cursor_t cursor;
196 unw_word_t function_off_set;
197 int stack_depth = 0, ret_val;
198 /* these are used for the binary search through the mmap_chace */
199 unsigned int lower, upper, mid;
200 size_t symbol_name_size = 40;
201 char * symbol_name;
202 struct mmap_cache_t* cur_mmap_cache;
203 unsigned long true_offset;
204
205 if (!tcp->mmap_cache)
206 alloc_mmap_cache(tcp);
207 if (!tcp->mmap_cache || !tcp->mmap_cache_size)
208 return;
209
210 symbol_name = malloc(symbol_name_size);
211 if (!symbol_name)
212 die_out_of_memory();
213
214 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
215 perror_msg_and_die("Can't initiate libunwind");
216
217 do {
218 /* looping on the stack frame */
219 if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) {
220 perror_msg("Can't walk the stack of process %d", tcp->pid);
221 break;
222 }
223
224 lower = 0;
225 upper = tcp->mmap_cache_size - 1;
226
227 while (lower <= upper) {
228 /* find the mmap_cache and print the stack frame */
229 mid = (upper + lower) / 2;
230 cur_mmap_cache = &tcp->mmap_cache[mid];
231
232 if (ip >= cur_mmap_cache->start_addr &&
233 ip < cur_mmap_cache->end_addr) {
234 for (;;) {
235 symbol_name[0] = '\0';
236 ret_val = unw_get_proc_name(&cursor, symbol_name,
237 symbol_name_size, &function_off_set);
238 if (ret_val != -UNW_ENOMEM)
239 break;
240 symbol_name_size *= 2;
241 symbol_name = realloc(symbol_name, symbol_name_size);
242 if (!symbol_name)
243 die_out_of_memory();
244 }
245
246 true_offset = ip - cur_mmap_cache->start_addr +
247 cur_mmap_cache->mmap_offset;
248 if (symbol_name[0]) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900249 call_action(data,
250 cur_mmap_cache->binary_filename,
251 symbol_name,
252 function_off_set,
253 true_offset);
Luca Clementi327064b2013-07-23 00:11:35 -0700254 } else {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900255 call_action(data,
256 cur_mmap_cache->binary_filename,
257 symbol_name,
258 0,
259 true_offset);
Luca Clementi327064b2013-07-23 00:11:35 -0700260 }
Luca Clementi327064b2013-07-23 00:11:35 -0700261 break; /* stack frame printed */
262 }
263 else if (mid == 0) {
264 /*
265 * there is a bug in libunwind >= 1.0
266 * after a set_tid_address syscall
267 * unw_get_reg returns IP == 0
268 */
269 if(ip)
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900270 error_action(data,
271 "backtracing_error", 0);
Luca Clementi327064b2013-07-23 00:11:35 -0700272 goto ret;
273 }
274 else if (ip < cur_mmap_cache->start_addr)
Masatake YAMATOb4a2de82014-04-16 15:32:59 +0900275 upper = mid;
Luca Clementi327064b2013-07-23 00:11:35 -0700276 else
277 lower = mid + 1;
278
279 }
280 if (lower > upper) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900281 error_action(data,
282 "backtracing_error", ip);
Luca Clementi327064b2013-07-23 00:11:35 -0700283 goto ret;
284 }
285
286 ret_val = unw_step(&cursor);
287
288 if (++stack_depth > 255) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900289 error_action(data,
290 "too many stack frames", 0);
Luca Clementi327064b2013-07-23 00:11:35 -0700291 break;
292 }
293 } while (ret_val > 0);
294ret:
295 free(symbol_name);
296}
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900297
298/*
299 * printing an entry in stack
300 */
301/*
302 * we want to keep the format used by backtrace_symbols from the glibc
303 *
304 * ./a.out() [0x40063d]
305 * ./a.out() [0x4006bb]
306 * ./a.out() [0x4006c6]
307 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
308 * ./a.out() [0x400569]
309 */
310#define STACK_ENTRY_SYMBOL_FMT \
311 " > %s(%s+0x%lx) [0x%lx]\n", \
312 binary_filename, \
313 symbol_name, \
314 function_off_set, \
315 true_offset
316#define STACK_ENTRY_NOSYMBOL_FMT \
317 " > %s() [0x%lx]\n", \
318 binary_filename, true_offset
319#define STACK_ENTRY_BUG_FMT \
320 " > BUG IN %s\n"
321#define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
322 " > %s [0x%lx]\n", error, true_offset
323#define STACK_ENTRY_ERROR_FMT \
324 " > %s\n", error
325
326static void
327print_call_cb(void *dummy,
328 char *binary_filename,
329 char *symbol_name,
330 unw_word_t function_off_set,
331 unsigned long true_offset)
332{
333 if (symbol_name)
334 tprintf(STACK_ENTRY_SYMBOL_FMT);
335 else if (binary_filename)
336 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
337 else
338 tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__);
339
340 line_ended();
341}
342
343static void
344print_error_cb(void *dummy,
345 const char *error,
346 unsigned long true_offset)
347{
348 if (true_offset)
349 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
350 else
351 tprintf(STACK_ENTRY_ERROR_FMT);
352
353 line_ended();
354}
355
356/*
357 * printing stack
358 */
359void
360unwind_print_stacktrace(struct tcb* tcp)
361{
362 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
363}