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 #define DPRINTF(F, A, ...) if (debug_flag) fprintf(stderr, " [unwind(" A ")] " F "\n", __VA_ARGS__)
34 * Кeep a sorted array of cache entries,
35 * so that we can binary search through it.
40 * 7fabbb09b000-7fabbb09f000 r--p 00179000 fc:00 1180246 /lib/libc-2.11.1.so
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"
47 unsigned long start_addr;
48 unsigned long end_addr;
49 unsigned long mmap_offset;
50 char* binary_filename;
55 * Type used in stacktrace walker
57 typedef void (*call_action_fn)(void *data,
58 char *binary_filename,
60 unw_word_t function_off_set,
61 unsigned long true_offset);
62 typedef void (*error_action_fn)(void *data,
64 unsigned long true_offset);
67 * Type used in stacktrace capturing
79 static void queue_print(struct queue_t *queue);
80 static void delete_mmap_cache(struct tcb *tcp, const char *caller);
82 static unw_addr_space_t libunwind_as;
83 static unsigned int mmap_cache_generation;
88 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
90 error_msg_and_die("failed to create address space for stack tracing");
91 unw_set_caching_policy(libunwind_as, UNW_CACHE_GLOBAL);
95 unwind_tcb_init(struct tcb *tcp)
97 tcp->libunwind_ui = _UPT_create(tcp->pid);
98 if (!tcp->libunwind_ui)
101 tcp->queue = malloc(sizeof(*tcp->queue));
104 tcp->queue->head = NULL;
105 tcp->queue->tail = NULL;
109 unwind_tcb_fin(struct tcb *tcp)
111 queue_print(tcp->queue);
115 delete_mmap_cache(tcp, __FUNCTION__);
117 _UPT_destroy(tcp->libunwind_ui);
118 tcp->libunwind_ui = NULL;
122 * caching of /proc/ID/maps for each process to speed up stack tracing
124 * The cache must be refreshed after some syscall: mmap, mprotect, munmap, execve
127 build_mmap_cache(struct tcb* tcp)
129 unsigned long start_addr, end_addr, mmap_offset;
130 char filename[sizeof ("/proc/0123456789/maps")];
131 char buffer[PATH_MAX + 80];
132 char binary_path[PATH_MAX];
133 struct mmap_cache_t *cur_entry, *prev_entry;
134 /* start with a small dynamically-allocated array and then expand it */
135 size_t cur_array_size = 10;
136 struct mmap_cache_t *cache_head;
139 const char *deleted = " (deleted)";
143 unw_flush_cache (libunwind_as, 0, 0);
145 sprintf(filename, "/proc/%d/maps", tcp->pid);
146 fp = fopen(filename, "r");
148 perror_msg("fopen: %s", filename);
152 cache_head = calloc(cur_array_size, sizeof(*cache_head));
156 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
157 binary_path[0] = '\0'; // 'reset' it just to be paranoid
159 sscanf(buffer, "%lx-%lx %*c%*c%*c%*c %lx %*x:%*x %*d %[^\n]",
160 &start_addr, &end_addr, &mmap_offset, binary_path);
162 /* ignore special 'fake files' like "[vdso]", "[heap]", "[stack]", */
163 if (binary_path[0] == '[') {
167 if (binary_path[0] == '\0') {
171 if (end_addr < start_addr)
172 perror_msg_and_die("%s: unrecognized maps file format",
175 cur_entry = &cache_head[tcp->mmap_cache_size];
176 cur_entry->start_addr = start_addr;
177 cur_entry->end_addr = end_addr;
178 cur_entry->mmap_offset = mmap_offset;
179 cur_entry->binary_filename = strdup(binary_path);
181 dlen = strlen(deleted);
182 blen = strlen(binary_path);
183 if (blen >= dlen && strcmp(binary_path + blen - dlen, deleted) == 0)
184 cur_entry->deleted = true;
186 cur_entry->deleted = false;
189 * sanity check to make sure that we're storing
190 * non-overlapping regions in ascending order
192 if (tcp->mmap_cache_size > 0) {
193 prev_entry = &cache_head[tcp->mmap_cache_size - 1];
194 if (prev_entry->start_addr >= cur_entry->start_addr)
195 perror_msg_and_die("Overlaying memory region in %s",
197 if (prev_entry->end_addr > cur_entry->start_addr)
198 perror_msg_and_die("Overlaying memory region in %s",
201 tcp->mmap_cache_size++;
203 /* resize doubling its size */
204 if (tcp->mmap_cache_size >= cur_array_size) {
206 cache_head = realloc(cache_head, cur_array_size * sizeof(*cache_head));
212 tcp->mmap_cache = cache_head;
213 tcp->mmap_cache_generation = mmap_cache_generation;
215 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p",
217 tcp->mmap_cache_generation,
218 mmap_cache_generation,
219 tcp, tcp->mmap_cache);
222 /* deleting the cache */
224 delete_mmap_cache(struct tcb *tcp, const char *caller)
228 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s",
230 tcp->mmap_cache_generation,
231 mmap_cache_generation,
232 tcp, tcp->mmap_cache, caller);
234 for (i = 0; i < tcp->mmap_cache_size; i++) {
235 free(tcp->mmap_cache[i].binary_filename);
236 tcp->mmap_cache[i].binary_filename = NULL;
238 free(tcp->mmap_cache);
239 tcp->mmap_cache = NULL;
240 tcp->mmap_cache_size = 0;
244 rebuild_cache_if_invalid(struct tcb *tcp, const char *caller)
246 if ((tcp->mmap_cache_generation != mmap_cache_generation)
248 delete_mmap_cache(tcp, caller);
250 if (!tcp->mmap_cache)
251 build_mmap_cache(tcp);
253 if (!tcp->mmap_cache || !tcp->mmap_cache_size)
260 unwind_cache_invalidate(struct tcb* tcp)
262 mmap_cache_generation++;
263 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p", "increment",
264 tcp->mmap_cache_generation,
265 mmap_cache_generation,
274 stacktrace_walk(struct tcb *tcp,
275 call_action_fn call_action,
276 error_action_fn error_action,
281 unw_word_t function_off_set;
282 int stack_depth = 0, ret_val;
283 /* these are used for the binary search through the mmap_chace */
284 unsigned int lower, upper, mid;
285 size_t symbol_name_size = 40;
287 struct mmap_cache_t* cur_mmap_cache;
288 unsigned long true_offset;
289 bool berror_expected = false;
291 if (!tcp->mmap_cache)
292 error_msg_and_die("bug: mmap_cache is NULL");
293 if (tcp->mmap_cache_size == 0)
294 error_msg_and_die("bug: mmap_cache is empty");
296 symbol_name = malloc(symbol_name_size);
300 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
301 perror_msg_and_die("Can't initiate libunwind");
304 /* looping on the stack frame */
305 if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) {
306 perror_msg("Can't walk the stack of process %d", tcp->pid);
311 upper = tcp->mmap_cache_size - 1;
313 while (lower <= upper) {
314 /* find the mmap_cache and print the stack frame */
315 mid = (upper + lower) / 2;
316 cur_mmap_cache = &tcp->mmap_cache[mid];
318 if (ip >= cur_mmap_cache->start_addr &&
319 ip < cur_mmap_cache->end_addr) {
321 symbol_name[0] = '\0';
322 ret_val = unw_get_proc_name(&cursor, symbol_name,
323 symbol_name_size, &function_off_set);
324 if (ret_val != -UNW_ENOMEM)
326 symbol_name_size *= 2;
327 symbol_name = realloc(symbol_name, symbol_name_size);
332 if (cur_mmap_cache->deleted)
333 berror_expected = true;
335 true_offset = ip - cur_mmap_cache->start_addr +
336 cur_mmap_cache->mmap_offset;
337 if (symbol_name[0]) {
339 cur_mmap_cache->binary_filename,
345 cur_mmap_cache->binary_filename,
350 break; /* stack frame printed */
354 * there is a bug in libunwind >= 1.0
355 * after a set_tid_address syscall
356 * unw_get_reg returns IP == 0
360 "backtracing_error", 0);
363 else if (ip < cur_mmap_cache->start_addr)
372 ?"expected_backtracing_error"
373 :"unexpected_backtracing_error",
378 ret_val = unw_step(&cursor);
380 if (++stack_depth > 255) {
382 "too many stack frames", 0);
385 } while (ret_val > 0);
391 * printing an entry in stack to stream or buffer
394 * we want to keep the format used by backtrace_symbols from the glibc
396 * ./a.out() [0x40063d]
397 * ./a.out() [0x4006bb]
398 * ./a.out() [0x4006c6]
399 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
400 * ./a.out() [0x400569]
402 #define STACK_ENTRY_SYMBOL_FMT \
403 " > %s(%s+0x%lx) [0x%lx]\n", \
408 #define STACK_ENTRY_NOSYMBOL_FMT \
409 " > %s() [0x%lx]\n", \
410 binary_filename, true_offset
411 #define STACK_ENTRY_BUG_FMT \
413 #define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
414 " > %s [0x%lx]\n", error, true_offset
415 #define STACK_ENTRY_ERROR_FMT \
419 print_call_cb(void *dummy,
420 char *binary_filename,
422 unw_word_t function_off_set,
423 unsigned long true_offset)
426 tprintf(STACK_ENTRY_SYMBOL_FMT);
427 else if (binary_filename)
428 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
430 tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__);
436 print_error_cb(void *dummy,
438 unsigned long true_offset)
441 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
443 tprintf(STACK_ENTRY_ERROR_FMT);
449 sprint_call_or_error(char *binary_filename,
451 unw_word_t function_off_set,
452 unsigned long true_offset,
455 char *output_line = NULL;
459 n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT);
460 else if (binary_filename)
461 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
464 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
465 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
467 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __FUNCTION__);
470 error_msg_and_die("error in asprintf");
479 queue_put(struct queue_t *queue,
480 char *binary_filename,
482 unw_word_t function_off_set,
483 unsigned long true_offset,
488 call = malloc(sizeof(*call));
492 call->output_line = sprint_call_or_error(binary_filename,
503 queue->tail->next = call;
509 queue_put_call(void *queue,
510 char *binary_filename,
512 unw_word_t function_off_set,
513 unsigned long true_offset)
524 queue_put_error(void *queue,
528 queue_put(queue, NULL, NULL, 0, ip, error);
532 queue_print(struct queue_t *queue)
534 struct call_t *call, *tmp;
543 tprints(tmp->output_line);
546 free(tmp->output_line);
547 tmp->output_line = NULL;
557 unwind_print_stacktrace(struct tcb* tcp)
559 if (tcp->queue->head) {
560 DPRINTF("tcp=%p, queue=%p", "queueprint", tcp, tcp->queue->head);
561 queue_print(tcp->queue);
563 else if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
564 DPRINTF("tcp=%p, queue=%p", "stackprint", tcp, tcp->queue->head);
565 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
573 unwind_capture_stacktrace(struct tcb *tcp)
575 if (tcp->queue->head)
576 error_msg_and_die("bug: unprinted entries in queue");
578 if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
579 stacktrace_walk(tcp, queue_put_call, queue_put_error,
581 DPRINTF("tcp=%p, queue=%p", "captured", tcp, tcp->queue->head);