2 * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include <libunwind-ptrace.h>
31 #ifdef _LARGEFILE64_SOURCE
33 # define fopen_for_input fopen64
35 # define fopen_for_input fopen
38 # define fopen_for_input fopen
41 #define DPRINTF(F, A, ...) if (debug_flag) fprintf(stderr, " [unwind(" A ")] " F "\n", __VA_ARGS__)
44 * Кeep a sorted array of cache entries,
45 * so that we can binary search through it.
50 * 7fabbb09b000-7fabbb09f000 r-xp 00179000 fc:00 1180246 /lib/libc-2.11.1.so
52 * start_addr is 0x7fabbb09b000
53 * end_addr is 0x7fabbb09f000
54 * mmap_offset is 0x179000
55 * binary_filename is "/lib/libc-2.11.1.so"
57 unsigned long start_addr;
58 unsigned long end_addr;
59 unsigned long mmap_offset;
60 char *binary_filename;
64 * Type used in stacktrace walker
66 typedef void (*call_action_fn)(void *data,
67 const char *binary_filename,
68 const char *symbol_name,
69 unw_word_t function_offset,
70 unsigned long true_offset);
71 typedef void (*error_action_fn)(void *data,
73 unsigned long true_offset);
76 * Type used in stacktrace capturing
88 static void queue_print(struct queue_t *queue);
89 static void delete_mmap_cache(struct tcb *tcp, const char *caller);
91 static unw_addr_space_t libunwind_as;
92 static unsigned int mmap_cache_generation;
97 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
99 error_msg_and_die("failed to create address space for stack tracing");
100 unw_set_caching_policy(libunwind_as, UNW_CACHE_GLOBAL);
104 unwind_tcb_init(struct tcb *tcp)
106 tcp->libunwind_ui = _UPT_create(tcp->pid);
107 if (!tcp->libunwind_ui)
110 tcp->queue = malloc(sizeof(*tcp->queue));
113 tcp->queue->head = NULL;
114 tcp->queue->tail = NULL;
118 unwind_tcb_fin(struct tcb *tcp)
120 queue_print(tcp->queue);
124 delete_mmap_cache(tcp, __FUNCTION__);
126 _UPT_destroy(tcp->libunwind_ui);
127 tcp->libunwind_ui = NULL;
131 * caching of /proc/ID/maps for each process to speed up stack tracing
133 * The cache must be refreshed after syscalls that affect memory mappings,
134 * e.g. mmap, mprotect, munmap, execve.
137 build_mmap_cache(struct tcb* tcp)
140 struct mmap_cache_t *cache_head;
141 /* start with a small dynamically-allocated array and then expand it */
142 size_t cur_array_size = 10;
143 char filename[sizeof("/proc/4294967296/maps")];
144 char buffer[PATH_MAX + 80];
146 unw_flush_cache(libunwind_as, 0, 0);
148 sprintf(filename, "/proc/%u/maps", tcp->pid);
149 fp = fopen_for_input(filename, "r");
151 perror_msg("fopen: %s", filename);
155 cache_head = calloc(cur_array_size, sizeof(*cache_head));
159 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
160 struct mmap_cache_t *entry;
161 unsigned long start_addr, end_addr, mmap_offset;
163 char binary_path[PATH_MAX];
165 if (sscanf(buffer, "%lx-%lx %*c%*c%c%*c %lx %*x:%*x %*d %[^\n]",
166 &start_addr, &end_addr, &exec_bit,
167 &mmap_offset, binary_path) != 5)
170 /* ignore mappings that have no PROT_EXEC bit set */
174 if (end_addr < start_addr) {
175 error_msg("%s: unrecognized file format", filename);
180 * sanity check to make sure that we're storing
181 * non-overlapping regions in ascending order
183 if (tcp->mmap_cache_size > 0) {
184 entry = &cache_head[tcp->mmap_cache_size - 1];
185 if (entry->start_addr == start_addr &&
186 entry->end_addr == end_addr) {
187 /* duplicate entry, e.g. [vsyscall] */
190 if (start_addr <= entry->start_addr ||
191 start_addr < entry->end_addr) {
192 error_msg("%s: overlapping memory region",
198 if (tcp->mmap_cache_size >= cur_array_size) {
200 cache_head = realloc(cache_head,
201 cur_array_size * sizeof(*cache_head));
206 entry = &cache_head[tcp->mmap_cache_size];
207 entry->start_addr = start_addr;
208 entry->end_addr = end_addr;
209 entry->mmap_offset = mmap_offset;
210 entry->binary_filename = strdup(binary_path);
211 if (!entry->binary_filename)
213 tcp->mmap_cache_size++;
216 tcp->mmap_cache = cache_head;
217 tcp->mmap_cache_generation = mmap_cache_generation;
219 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p",
221 tcp->mmap_cache_generation,
222 mmap_cache_generation,
223 tcp, tcp->mmap_cache);
226 /* deleting the cache */
228 delete_mmap_cache(struct tcb *tcp, const char *caller)
232 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s",
234 tcp->mmap_cache_generation,
235 mmap_cache_generation,
236 tcp, tcp->mmap_cache, caller);
238 for (i = 0; i < tcp->mmap_cache_size; i++) {
239 free(tcp->mmap_cache[i].binary_filename);
240 tcp->mmap_cache[i].binary_filename = NULL;
242 free(tcp->mmap_cache);
243 tcp->mmap_cache = NULL;
244 tcp->mmap_cache_size = 0;
248 rebuild_cache_if_invalid(struct tcb *tcp, const char *caller)
250 if ((tcp->mmap_cache_generation != mmap_cache_generation)
252 delete_mmap_cache(tcp, caller);
254 if (!tcp->mmap_cache)
255 build_mmap_cache(tcp);
257 if (!tcp->mmap_cache || !tcp->mmap_cache_size)
264 unwind_cache_invalidate(struct tcb* tcp)
266 #if SUPPORTED_PERSONALITIES > 1
267 if (tcp->currpers != DEFAULT_PERSONALITY) {
268 /* disable strack trace */
272 mmap_cache_generation++;
273 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p", "increment",
274 tcp->mmap_cache_generation,
275 mmap_cache_generation,
281 get_symbol_name(unw_cursor_t *cursor, char **name,
282 size_t *size, unw_word_t *offset)
285 int rc = unw_get_proc_name(cursor, *name, *size, offset);
288 if (rc != -UNW_ENOMEM) {
294 *name = realloc(*name, *size);
301 print_stack_frame(struct tcb *tcp,
302 call_action_fn call_action,
303 error_action_fn error_action,
305 unw_cursor_t *cursor,
307 size_t *symbol_name_size)
311 int upper = (int) tcp->mmap_cache_size - 1;
313 if (unw_get_reg(cursor, UNW_REG_IP, &ip) < 0) {
314 perror_msg("Can't walk the stack of process %d", tcp->pid);
318 while (lower <= upper) {
319 struct mmap_cache_t *cur_mmap_cache;
320 int mid = (upper + lower) / 2;
322 cur_mmap_cache = &tcp->mmap_cache[mid];
324 if (ip >= cur_mmap_cache->start_addr &&
325 ip < cur_mmap_cache->end_addr) {
326 unsigned long true_offset;
327 unw_word_t function_offset;
329 get_symbol_name(cursor, symbol_name, symbol_name_size,
331 true_offset = ip - cur_mmap_cache->start_addr +
332 cur_mmap_cache->mmap_offset;
334 cur_mmap_cache->binary_filename,
340 else if (ip < cur_mmap_cache->start_addr)
347 * there is a bug in libunwind >= 1.0
348 * after a set_tid_address syscall
349 * unw_get_reg returns IP == 0
352 error_action(data, "unexpected_backtracing_error", ip);
360 stacktrace_walk(struct tcb *tcp,
361 call_action_fn call_action,
362 error_action_fn error_action,
366 size_t symbol_name_size = 40;
370 if (!tcp->mmap_cache)
371 error_msg_and_die("bug: mmap_cache is NULL");
372 if (tcp->mmap_cache_size == 0)
373 error_msg_and_die("bug: mmap_cache is empty");
375 symbol_name = malloc(symbol_name_size);
379 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
380 perror_msg_and_die("Can't initiate libunwind");
382 for (stack_depth = 0; stack_depth < 256; ++stack_depth) {
383 if (print_stack_frame(tcp, call_action, error_action, data,
384 &cursor, &symbol_name, &symbol_name_size) < 0)
386 if (unw_step(&cursor) <= 0)
389 if (stack_depth >= 256)
390 error_action(data, "too many stack frames", 0);
396 * printing an entry in stack to stream or buffer
399 * we want to keep the format used by backtrace_symbols from the glibc
401 * ./a.out() [0x40063d]
402 * ./a.out() [0x4006bb]
403 * ./a.out() [0x4006c6]
404 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
405 * ./a.out() [0x400569]
407 #define STACK_ENTRY_SYMBOL_FMT \
408 " > %s(%s+0x%lx) [0x%lx]\n", \
411 (unsigned long) function_offset, \
413 #define STACK_ENTRY_NOSYMBOL_FMT \
414 " > %s() [0x%lx]\n", \
415 binary_filename, true_offset
416 #define STACK_ENTRY_BUG_FMT \
418 #define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
419 " > %s [0x%lx]\n", error, true_offset
420 #define STACK_ENTRY_ERROR_FMT \
424 print_call_cb(void *dummy,
425 const char *binary_filename,
426 const char *symbol_name,
427 unw_word_t function_offset,
428 unsigned long true_offset)
430 if (symbol_name && (symbol_name[0] != '\0'))
431 tprintf(STACK_ENTRY_SYMBOL_FMT);
432 else if (binary_filename)
433 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
435 tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__);
441 print_error_cb(void *dummy,
443 unsigned long true_offset)
446 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
448 tprintf(STACK_ENTRY_ERROR_FMT);
454 sprint_call_or_error(const char *binary_filename,
455 const char *symbol_name,
456 unw_word_t function_offset,
457 unsigned long true_offset,
460 char *output_line = NULL;
464 n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT);
465 else if (binary_filename)
466 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
469 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
470 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
472 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __FUNCTION__);
475 error_msg_and_die("error in asprintf");
484 queue_put(struct queue_t *queue,
485 const char *binary_filename,
486 const char *symbol_name,
487 unw_word_t function_offset,
488 unsigned long true_offset,
493 call = malloc(sizeof(*call));
497 call->output_line = sprint_call_or_error(binary_filename,
508 queue->tail->next = call;
514 queue_put_call(void *queue,
515 const char *binary_filename,
516 const char *symbol_name,
517 unw_word_t function_offset,
518 unsigned long true_offset)
529 queue_put_error(void *queue,
533 queue_put(queue, NULL, NULL, 0, ip, error);
537 queue_print(struct queue_t *queue)
539 struct call_t *call, *tmp;
548 tprints(tmp->output_line);
551 free(tmp->output_line);
552 tmp->output_line = NULL;
562 unwind_print_stacktrace(struct tcb* tcp)
564 #if SUPPORTED_PERSONALITIES > 1
565 if (tcp->currpers != DEFAULT_PERSONALITY) {
566 /* disable strack trace */
570 if (tcp->queue->head) {
571 DPRINTF("tcp=%p, queue=%p", "queueprint", tcp, tcp->queue->head);
572 queue_print(tcp->queue);
574 else if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
575 DPRINTF("tcp=%p, queue=%p", "stackprint", tcp, tcp->queue->head);
576 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
584 unwind_capture_stacktrace(struct tcb *tcp)
586 #if SUPPORTED_PERSONALITIES > 1
587 if (tcp->currpers != DEFAULT_PERSONALITY) {
588 /* disable strack trace */
592 if (tcp->queue->head)
593 error_msg_and_die("bug: unprinted entries in queue");
595 if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
596 stacktrace_walk(tcp, queue_put_call, queue_put_error,
598 DPRINTF("tcp=%p, queue=%p", "captured", tcp, tcp->queue->head);