blob: ef5a404ceb0eb3cd6df0e460e089d4578fd7a36e [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
Masatake YAMATO4e121e52014-04-16 15:33:05 +090031#define DPRINTF(F, A, ...) if (debug_flag) fprintf(stderr, " [unwind(" A ")] " F "\n", __VA_ARGS__)
32
Luca Clementi327064b2013-07-23 00:11:35 -070033/*
34 * Кeep a sorted array of cache entries,
35 * so that we can binary search through it.
36 */
37struct mmap_cache_t {
38 /**
39 * example entry:
40 * 7fabbb09b000-7fabbb09f000 r--p 00179000 fc:00 1180246 /lib/libc-2.11.1.so
41 *
42 * start_addr is 0x7fabbb09b000
43 * end_addr is 0x7fabbb09f000
44 * mmap_offset is 0x179000
45 * binary_filename is "/lib/libc-2.11.1.so"
46 */
47 unsigned long start_addr;
48 unsigned long end_addr;
49 unsigned long mmap_offset;
50 char* binary_filename;
51};
52
Masatake YAMATO2d534da2014-04-16 15:33:04 +090053/*
54 * Type used in stacktrace walker
55 */
56typedef void (*call_action_fn)(void *data,
57 char *binary_filename,
58 char *symbol_name,
59 unw_word_t function_off_set,
60 unsigned long true_offset);
61typedef void (*error_action_fn)(void *data,
62 const char *error,
63 unsigned long true_offset);
64
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090065/*
66 * Type used in stacktrace capturing
67 */
68struct call_t {
69 struct call_t* next;
70 char *output_line;
71};
72
73struct queue_t {
74 struct call_t *tail;
75 struct call_t *head;
76};
Masatake YAMATO9bc65612014-04-16 15:33:07 +090077
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090078static void queue_print(struct queue_t *queue);
Masatake YAMATO9bc65612014-04-16 15:33:07 +090079static void delete_mmap_cache(struct tcb *tcp, const char *caller);
Masatake YAMATO2d534da2014-04-16 15:33:04 +090080
Luca Clementi327064b2013-07-23 00:11:35 -070081static unw_addr_space_t libunwind_as;
Masatake YAMATO9bc65612014-04-16 15:33:07 +090082static unsigned int mmap_cache_generation;
Luca Clementi327064b2013-07-23 00:11:35 -070083
84void
Masatake YAMATO61413922014-04-16 15:33:02 +090085unwind_init(void)
Luca Clementi327064b2013-07-23 00:11:35 -070086{
87 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
88 if (!libunwind_as)
89 error_msg_and_die("failed to create address space for stack tracing");
90}
91
92void
Masatake YAMATO61413922014-04-16 15:33:02 +090093unwind_tcb_init(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -070094{
95 tcp->libunwind_ui = _UPT_create(tcp->pid);
96 if (!tcp->libunwind_ui)
97 die_out_of_memory();
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090098
99 tcp->queue = malloc(sizeof(*tcp->queue));
100 if (!tcp->queue)
101 die_out_of_memory();
102 tcp->queue->head = NULL;
103 tcp->queue->tail = NULL;
Luca Clementi327064b2013-07-23 00:11:35 -0700104}
105
106void
Masatake YAMATO61413922014-04-16 15:33:02 +0900107unwind_tcb_fin(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700108{
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900109 queue_print(tcp->queue);
110 free(tcp->queue);
111 tcp->queue = NULL;
112
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900113 delete_mmap_cache(tcp, __FUNCTION__);
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900114
Luca Clementi327064b2013-07-23 00:11:35 -0700115 _UPT_destroy(tcp->libunwind_ui);
116 tcp->libunwind_ui = NULL;
117}
118
119/*
120 * caching of /proc/ID/maps for each process to speed up stack tracing
121 *
122 * The cache must be refreshed after some syscall: mmap, mprotect, munmap, execve
123 */
Masatake YAMATOb65042f2014-04-16 15:33:00 +0900124static void
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900125build_mmap_cache(struct tcb* tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700126{
127 unsigned long start_addr, end_addr, mmap_offset;
128 char filename[sizeof ("/proc/0123456789/maps")];
129 char buffer[PATH_MAX + 80];
130 char binary_path[PATH_MAX];
131 struct mmap_cache_t *cur_entry, *prev_entry;
132 /* start with a small dynamically-allocated array and then expand it */
133 size_t cur_array_size = 10;
134 struct mmap_cache_t *cache_head;
135 FILE *fp;
136
137 sprintf(filename, "/proc/%d/maps", tcp->pid);
138 fp = fopen(filename, "r");
139 if (!fp) {
140 perror_msg("fopen: %s", filename);
141 return;
142 }
143
144 cache_head = calloc(cur_array_size, sizeof(*cache_head));
145 if (!cache_head)
146 die_out_of_memory();
147
148 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
149 binary_path[0] = '\0'; // 'reset' it just to be paranoid
150
151 sscanf(buffer, "%lx-%lx %*c%*c%*c%*c %lx %*x:%*x %*d %[^\n]",
152 &start_addr, &end_addr, &mmap_offset, binary_path);
153
154 /* ignore special 'fake files' like "[vdso]", "[heap]", "[stack]", */
155 if (binary_path[0] == '[') {
156 continue;
157 }
158
159 if (binary_path[0] == '\0') {
160 continue;
161 }
162
163 if (end_addr < start_addr)
164 perror_msg_and_die("%s: unrecognized maps file format",
165 filename);
166
167 cur_entry = &cache_head[tcp->mmap_cache_size];
168 cur_entry->start_addr = start_addr;
169 cur_entry->end_addr = end_addr;
170 cur_entry->mmap_offset = mmap_offset;
171 cur_entry->binary_filename = strdup(binary_path);
172
173 /*
174 * sanity check to make sure that we're storing
175 * non-overlapping regions in ascending order
176 */
177 if (tcp->mmap_cache_size > 0) {
178 prev_entry = &cache_head[tcp->mmap_cache_size - 1];
179 if (prev_entry->start_addr >= cur_entry->start_addr)
180 perror_msg_and_die("Overlaying memory region in %s",
181 filename);
182 if (prev_entry->end_addr > cur_entry->start_addr)
183 perror_msg_and_die("Overlaying memory region in %s",
184 filename);
185 }
186 tcp->mmap_cache_size++;
187
188 /* resize doubling its size */
189 if (tcp->mmap_cache_size >= cur_array_size) {
190 cur_array_size *= 2;
191 cache_head = realloc(cache_head, cur_array_size * sizeof(*cache_head));
192 if (!cache_head)
193 die_out_of_memory();
194 }
195 }
196 fclose(fp);
197 tcp->mmap_cache = cache_head;
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900198 tcp->mmap_cache_generation = mmap_cache_generation;
199
200 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p",
201 "cache-build",
202 tcp->mmap_cache_generation,
203 mmap_cache_generation,
204 tcp, tcp->mmap_cache);
Luca Clementi327064b2013-07-23 00:11:35 -0700205}
206
207/* deleting the cache */
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900208static void
209delete_mmap_cache(struct tcb *tcp, const char *caller)
Luca Clementi327064b2013-07-23 00:11:35 -0700210{
211 unsigned int i;
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900212
213 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s",
214 "cache-delete",
215 tcp->mmap_cache_generation,
216 mmap_cache_generation,
217 tcp, tcp->mmap_cache, caller);
218
Luca Clementi327064b2013-07-23 00:11:35 -0700219 for (i = 0; i < tcp->mmap_cache_size; i++) {
220 free(tcp->mmap_cache[i].binary_filename);
221 tcp->mmap_cache[i].binary_filename = NULL;
222 }
223 free(tcp->mmap_cache);
224 tcp->mmap_cache = NULL;
225 tcp->mmap_cache_size = 0;
226}
227
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900228static bool
229rebuild_cache_if_invalid(struct tcb *tcp, const char *caller)
230{
231 if ((tcp->mmap_cache_generation != mmap_cache_generation)
232 && tcp->mmap_cache)
233 delete_mmap_cache(tcp, caller);
234
235 if (!tcp->mmap_cache)
236 build_mmap_cache(tcp);
237
238 if (!tcp->mmap_cache || !tcp->mmap_cache_size)
239 return false;
240 else
241 return true;
242}
243
244void
245unwind_cache_invalidate(struct tcb* tcp)
246{
247 mmap_cache_generation++;
248 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p", "increment",
249 tcp->mmap_cache_generation,
250 mmap_cache_generation,
251 tcp,
252 tcp->mmap_cache);
253}
254
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900255/*
256 * walking the stack
257 */
258static void
259stacktrace_walk(struct tcb *tcp,
260 call_action_fn call_action,
261 error_action_fn error_action,
262 void *data)
Luca Clementi327064b2013-07-23 00:11:35 -0700263{
264 unw_word_t ip;
265 unw_cursor_t cursor;
266 unw_word_t function_off_set;
267 int stack_depth = 0, ret_val;
268 /* these are used for the binary search through the mmap_chace */
269 unsigned int lower, upper, mid;
270 size_t symbol_name_size = 40;
271 char * symbol_name;
272 struct mmap_cache_t* cur_mmap_cache;
273 unsigned long true_offset;
274
275 if (!tcp->mmap_cache)
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900276 error_msg_and_die("bug: mmap_cache is NULL");
277 if (tcp->mmap_cache_size == 0)
278 error_msg_and_die("bug: mmap_cache is empty");
Luca Clementi327064b2013-07-23 00:11:35 -0700279
280 symbol_name = malloc(symbol_name_size);
281 if (!symbol_name)
282 die_out_of_memory();
283
284 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
285 perror_msg_and_die("Can't initiate libunwind");
286
287 do {
288 /* looping on the stack frame */
289 if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) {
290 perror_msg("Can't walk the stack of process %d", tcp->pid);
291 break;
292 }
293
294 lower = 0;
295 upper = tcp->mmap_cache_size - 1;
296
297 while (lower <= upper) {
298 /* find the mmap_cache and print the stack frame */
299 mid = (upper + lower) / 2;
300 cur_mmap_cache = &tcp->mmap_cache[mid];
301
302 if (ip >= cur_mmap_cache->start_addr &&
303 ip < cur_mmap_cache->end_addr) {
304 for (;;) {
305 symbol_name[0] = '\0';
306 ret_val = unw_get_proc_name(&cursor, symbol_name,
307 symbol_name_size, &function_off_set);
308 if (ret_val != -UNW_ENOMEM)
309 break;
310 symbol_name_size *= 2;
311 symbol_name = realloc(symbol_name, symbol_name_size);
312 if (!symbol_name)
313 die_out_of_memory();
314 }
315
316 true_offset = ip - cur_mmap_cache->start_addr +
317 cur_mmap_cache->mmap_offset;
318 if (symbol_name[0]) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900319 call_action(data,
320 cur_mmap_cache->binary_filename,
321 symbol_name,
322 function_off_set,
323 true_offset);
Luca Clementi327064b2013-07-23 00:11:35 -0700324 } else {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900325 call_action(data,
326 cur_mmap_cache->binary_filename,
327 symbol_name,
328 0,
329 true_offset);
Luca Clementi327064b2013-07-23 00:11:35 -0700330 }
Luca Clementi327064b2013-07-23 00:11:35 -0700331 break; /* stack frame printed */
332 }
333 else if (mid == 0) {
334 /*
335 * there is a bug in libunwind >= 1.0
336 * after a set_tid_address syscall
337 * unw_get_reg returns IP == 0
338 */
339 if(ip)
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900340 error_action(data,
341 "backtracing_error", 0);
Luca Clementi327064b2013-07-23 00:11:35 -0700342 goto ret;
343 }
344 else if (ip < cur_mmap_cache->start_addr)
Masatake YAMATOb4a2de82014-04-16 15:32:59 +0900345 upper = mid;
Luca Clementi327064b2013-07-23 00:11:35 -0700346 else
347 lower = mid + 1;
348
349 }
350 if (lower > upper) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900351 error_action(data,
352 "backtracing_error", ip);
Luca Clementi327064b2013-07-23 00:11:35 -0700353 goto ret;
354 }
355
356 ret_val = unw_step(&cursor);
357
358 if (++stack_depth > 255) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900359 error_action(data,
360 "too many stack frames", 0);
Luca Clementi327064b2013-07-23 00:11:35 -0700361 break;
362 }
363 } while (ret_val > 0);
364ret:
365 free(symbol_name);
366}
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900367
368/*
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900369 * printing an entry in stack to stream or buffer
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900370 */
371/*
372 * we want to keep the format used by backtrace_symbols from the glibc
373 *
374 * ./a.out() [0x40063d]
375 * ./a.out() [0x4006bb]
376 * ./a.out() [0x4006c6]
377 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
378 * ./a.out() [0x400569]
379 */
380#define STACK_ENTRY_SYMBOL_FMT \
381 " > %s(%s+0x%lx) [0x%lx]\n", \
382 binary_filename, \
383 symbol_name, \
384 function_off_set, \
385 true_offset
386#define STACK_ENTRY_NOSYMBOL_FMT \
387 " > %s() [0x%lx]\n", \
388 binary_filename, true_offset
389#define STACK_ENTRY_BUG_FMT \
390 " > BUG IN %s\n"
391#define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
392 " > %s [0x%lx]\n", error, true_offset
393#define STACK_ENTRY_ERROR_FMT \
394 " > %s\n", error
395
396static void
397print_call_cb(void *dummy,
398 char *binary_filename,
399 char *symbol_name,
400 unw_word_t function_off_set,
401 unsigned long true_offset)
402{
403 if (symbol_name)
404 tprintf(STACK_ENTRY_SYMBOL_FMT);
405 else if (binary_filename)
406 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
407 else
408 tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__);
409
410 line_ended();
411}
412
413static void
414print_error_cb(void *dummy,
415 const char *error,
416 unsigned long true_offset)
417{
418 if (true_offset)
419 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
420 else
421 tprintf(STACK_ENTRY_ERROR_FMT);
422
423 line_ended();
424}
425
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900426static char *
427sprint_call_or_error(char *binary_filename,
428 char *symbol_name,
429 unw_word_t function_off_set,
430 unsigned long true_offset,
431 const char *error)
432{
433 char *output_line = NULL;
434 int n;
435
436 if (symbol_name)
437 n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT);
438 else if (binary_filename)
439 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
440 else if (error)
441 n = true_offset
442 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
443 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
444 else
445 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __FUNCTION__);
446
447 if (n < 0)
448 error_msg_and_die("error in asprintf");
449
450 return output_line;
451}
452
453/*
454 * queue manipulators
455 */
456static void
457queue_put(struct queue_t *queue,
458 char *binary_filename,
459 char *symbol_name,
460 unw_word_t function_off_set,
461 unsigned long true_offset,
462 const char *error)
463{
464 struct call_t *call;
465
466 call = malloc(sizeof(*call));
467 if (!call)
468 die_out_of_memory();
469
470 call->output_line = sprint_call_or_error(binary_filename,
471 symbol_name,
472 function_off_set,
473 true_offset,
474 error);
475 call->next = NULL;
476
477 if (!queue->head) {
478 queue->head = call;
479 queue->tail = call;
480 } else {
481 queue->tail->next = call;
482 queue->tail = call;
483 }
484}
485
486static void
487queue_put_call(void *queue,
488 char *binary_filename,
489 char *symbol_name,
490 unw_word_t function_off_set,
491 unsigned long true_offset)
492{
493 queue_put(queue,
494 binary_filename,
495 symbol_name,
496 function_off_set,
497 true_offset,
498 NULL);
499}
500
501static void
502queue_put_error(void *queue,
503 const char *error,
504 unw_word_t ip)
505{
506 queue_put(queue, NULL, NULL, 0, ip, error);
507}
508
509static void
510queue_print(struct queue_t *queue)
511{
512 struct call_t *call, *tmp;
513
514 queue->tail = NULL;
515 call = queue->head;
516 queue->head = NULL;
517 while (call) {
518 tmp = call;
519 call = call->next;
520
521 tprints(tmp->output_line);
522 line_ended();
523
524 free(tmp->output_line);
525 tmp->output_line = NULL;
526 tmp->next = NULL;
527 free(tmp);
528 }
529}
530
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900531/*
532 * printing stack
533 */
534void
535unwind_print_stacktrace(struct tcb* tcp)
536{
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900537 if (tcp->queue->head) {
538 DPRINTF("tcp=%p, queue=%p", "queueprint", tcp, tcp->queue->head);
539 queue_print(tcp->queue);
540 }
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900541 else if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900542 DPRINTF("tcp=%p, queue=%p", "stackprint", tcp, tcp->queue->head);
543 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
544 }
545}
546
547/*
548 * capturing stack
549 */
550void
551unwind_capture_stacktrace(struct tcb *tcp)
552{
553 if (tcp->queue->head)
554 error_msg_and_die("bug: unprinted entries in queue");
555
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900556 if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
557 stacktrace_walk(tcp, queue_put_call, queue_put_error,
558 tcp->queue);
559 DPRINTF("tcp=%p, queue=%p", "captured", tcp, tcp->queue->head);
560 }
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900561}