2 * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
3 * Copyright (c) 2013-2017 The strace developers.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <libunwind-ptrace.h>
33 # if defined HAVE_DEMANGLE_H
34 # include <demangle.h>
35 # elif defined HAVE_LIBIBERTY_DEMANGLE_H
36 # include <libiberty/demangle.h>
42 #ifdef _LARGEFILE64_SOURCE
44 # define fopen_for_input fopen64
46 # define fopen_for_input fopen
49 # define fopen_for_input fopen
53 * Keep a sorted array of cache entries,
54 * so that we can binary search through it.
59 * 7fabbb09b000-7fabbb09f000 r-xp 00179000 fc:00 1180246 /lib/libc-2.11.1.so
61 * start_addr is 0x7fabbb09b000
62 * end_addr is 0x7fabbb09f000
63 * mmap_offset is 0x179000
64 * binary_filename is "/lib/libc-2.11.1.so"
66 unsigned long start_addr;
67 unsigned long end_addr;
68 unsigned long mmap_offset;
69 char *binary_filename;
73 * Type used in stacktrace walker
75 typedef void (*call_action_fn)(void *data,
76 const char *binary_filename,
77 const char *symbol_name,
78 unw_word_t function_offset,
79 unsigned long true_offset);
80 typedef void (*error_action_fn)(void *data,
82 unsigned long true_offset);
85 * Type used in stacktrace capturing
97 static void queue_print(struct queue_t *queue);
98 static void delete_mmap_cache(struct tcb *tcp, const char *caller);
100 static unw_addr_space_t libunwind_as;
101 static unsigned int mmap_cache_generation;
103 static const char asprintf_error_str[] = "???";
108 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
110 error_msg_and_die("failed to create address space for stack tracing");
111 unw_set_caching_policy(libunwind_as, UNW_CACHE_GLOBAL);
115 unwind_tcb_init(struct tcb *tcp)
117 if (tcp->libunwind_ui)
120 tcp->libunwind_ui = _UPT_create(tcp->pid);
121 if (!tcp->libunwind_ui)
122 perror_msg_and_die("_UPT_create");
124 tcp->queue = xmalloc(sizeof(*tcp->queue));
125 tcp->queue->head = NULL;
126 tcp->queue->tail = NULL;
130 unwind_tcb_fin(struct tcb *tcp)
132 queue_print(tcp->queue);
136 delete_mmap_cache(tcp, __func__);
138 _UPT_destroy(tcp->libunwind_ui);
139 tcp->libunwind_ui = NULL;
143 * caching of /proc/ID/maps for each process to speed up stack tracing
145 * The cache must be refreshed after syscalls that affect memory mappings,
146 * e.g. mmap, mprotect, munmap, execve.
149 build_mmap_cache(struct tcb *tcp)
152 struct mmap_cache_t *cache_head = NULL;
153 size_t cur_array_size = 0;
154 char filename[sizeof("/proc/4294967296/maps")];
155 char buffer[PATH_MAX + 80];
157 unw_flush_cache(libunwind_as, 0, 0);
159 xsprintf(filename, "/proc/%u/maps", tcp->pid);
160 fp = fopen_for_input(filename, "r");
162 perror_msg("fopen: %s", filename);
166 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
167 struct mmap_cache_t *entry;
168 unsigned long start_addr, end_addr, mmap_offset;
170 char binary_path[sizeof(buffer)];
172 if (sscanf(buffer, "%lx-%lx %*c%*c%c%*c %lx %*x:%*x %*d %[^\n]",
173 &start_addr, &end_addr, &exec_bit,
174 &mmap_offset, binary_path) != 5)
177 /* ignore mappings that have no PROT_EXEC bit set */
181 if (end_addr < start_addr) {
182 error_msg("%s: unrecognized file format", filename);
187 * sanity check to make sure that we're storing
188 * non-overlapping regions in ascending order
190 if (tcp->mmap_cache_size > 0) {
191 entry = &cache_head[tcp->mmap_cache_size - 1];
192 if (entry->start_addr == start_addr &&
193 entry->end_addr == end_addr) {
194 /* duplicate entry, e.g. [vsyscall] */
197 if (start_addr <= entry->start_addr ||
198 start_addr < entry->end_addr) {
199 debug_msg("%s: overlapping memory region: "
200 "\"%s\" [%08lx-%08lx] overlaps with "
201 "\"%s\" [%08lx-%08lx]",
202 filename, binary_path, start_addr,
203 end_addr, entry->binary_filename,
204 entry->start_addr, entry->end_addr);
209 if (tcp->mmap_cache_size >= cur_array_size)
210 cache_head = xgrowarray(cache_head, &cur_array_size,
211 sizeof(*cache_head));
213 entry = &cache_head[tcp->mmap_cache_size];
214 entry->start_addr = start_addr;
215 entry->end_addr = end_addr;
216 entry->mmap_offset = mmap_offset;
217 entry->binary_filename = xstrdup(binary_path);
218 tcp->mmap_cache_size++;
221 tcp->mmap_cache = cache_head;
222 tcp->mmap_cache_generation = mmap_cache_generation;
224 debug_func_msg("tgen=%u, ggen=%u, tcp=%p, cache=%p",
225 tcp->mmap_cache_generation,
226 mmap_cache_generation,
227 tcp, tcp->mmap_cache);
230 /* deleting the cache */
232 delete_mmap_cache(struct tcb *tcp, const char *caller)
236 debug_func_msg("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s",
237 tcp->mmap_cache_generation,
238 mmap_cache_generation,
239 tcp, tcp->mmap_cache, caller);
241 for (i = 0; i < tcp->mmap_cache_size; i++) {
242 free(tcp->mmap_cache[i].binary_filename);
243 tcp->mmap_cache[i].binary_filename = NULL;
245 free(tcp->mmap_cache);
246 tcp->mmap_cache = NULL;
247 tcp->mmap_cache_size = 0;
251 rebuild_cache_if_invalid(struct tcb *tcp, const char *caller)
253 if ((tcp->mmap_cache_generation != mmap_cache_generation)
255 delete_mmap_cache(tcp, caller);
257 if (!tcp->mmap_cache)
258 build_mmap_cache(tcp);
260 return 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 stack trace */
272 mmap_cache_generation++;
273 debug_func_msg("tgen=%u, ggen=%u, tcp=%p, cache=%p",
274 tcp->mmap_cache_generation,
275 mmap_cache_generation,
276 tcp, tcp->mmap_cache);
280 get_symbol_name(unw_cursor_t *cursor, char **name,
281 size_t *size, unw_word_t *offset)
284 int rc = unw_get_proc_name(cursor, *name, *size, offset);
287 if (rc != -UNW_ENOMEM) {
292 *name = xgrowarray(*name, size, 1);
297 print_stack_frame(struct tcb *tcp,
298 call_action_fn call_action,
299 error_action_fn error_action,
301 unw_cursor_t *cursor,
303 size_t *symbol_name_size)
307 int upper = (int) tcp->mmap_cache_size - 1;
309 if (unw_get_reg(cursor, UNW_REG_IP, &ip) < 0) {
310 perror_msg("Can't walk the stack of process %d", tcp->pid);
314 while (lower <= upper) {
315 struct mmap_cache_t *cur_mmap_cache;
316 int mid = (upper + lower) / 2;
318 cur_mmap_cache = &tcp->mmap_cache[mid];
320 if (ip >= cur_mmap_cache->start_addr &&
321 ip < cur_mmap_cache->end_addr) {
322 unsigned long true_offset;
323 unw_word_t function_offset;
325 get_symbol_name(cursor, symbol_name, symbol_name_size,
327 true_offset = ip - cur_mmap_cache->start_addr +
328 cur_mmap_cache->mmap_offset;
331 char *demangled_name =
332 cplus_demangle(*symbol_name,
333 DMGL_AUTO | DMGL_PARAMS);
337 cur_mmap_cache->binary_filename,
339 demangled_name ? demangled_name :
345 free(demangled_name);
349 } else if (ip < cur_mmap_cache->start_addr)
356 * there is a bug in libunwind >= 1.0
357 * after a set_tid_address syscall
358 * unw_get_reg returns IP == 0
361 error_action(data, "unexpected_backtracing_error", ip);
369 stacktrace_walk(struct tcb *tcp,
370 call_action_fn call_action,
371 error_action_fn error_action,
375 size_t symbol_name_size = 40;
379 if (!tcp->mmap_cache)
380 error_msg_and_die("bug: mmap_cache is NULL");
381 if (tcp->mmap_cache_size == 0)
382 error_msg_and_die("bug: mmap_cache is empty");
384 symbol_name = xmalloc(symbol_name_size);
386 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
387 perror_msg_and_die("Can't initiate libunwind");
389 for (stack_depth = 0; stack_depth < 256; ++stack_depth) {
390 if (print_stack_frame(tcp, call_action, error_action, data,
391 &cursor, &symbol_name, &symbol_name_size) < 0)
393 if (unw_step(&cursor) <= 0)
396 if (stack_depth >= 256)
397 error_action(data, "too many stack frames", 0);
403 * printing an entry in stack to stream or buffer
406 * we want to keep the format used by backtrace_symbols from the glibc
408 * ./a.out() [0x40063d]
409 * ./a.out() [0x4006bb]
410 * ./a.out() [0x4006c6]
411 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
412 * ./a.out() [0x400569]
414 #define STACK_ENTRY_SYMBOL_FMT \
415 " > %s(%s+0x%lx) [0x%lx]\n", \
418 (unsigned long) function_offset, \
420 #define STACK_ENTRY_NOSYMBOL_FMT \
421 " > %s() [0x%lx]\n", \
422 binary_filename, true_offset
423 #define STACK_ENTRY_BUG_FMT \
425 #define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
426 " > %s [0x%lx]\n", error, true_offset
427 #define STACK_ENTRY_ERROR_FMT \
431 print_call_cb(void *dummy,
432 const char *binary_filename,
433 const char *symbol_name,
434 unw_word_t function_offset,
435 unsigned long true_offset)
437 if (symbol_name && (symbol_name[0] != '\0'))
438 tprintf(STACK_ENTRY_SYMBOL_FMT);
439 else if (binary_filename)
440 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
442 tprintf(STACK_ENTRY_BUG_FMT, __func__);
448 print_error_cb(void *dummy,
450 unsigned long true_offset)
453 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
455 tprintf(STACK_ENTRY_ERROR_FMT);
461 sprint_call_or_error(const char *binary_filename,
462 const char *symbol_name,
463 unw_word_t function_offset,
464 unsigned long true_offset,
467 char *output_line = NULL;
471 n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT);
472 else if (binary_filename)
473 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
476 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
477 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
479 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __func__);
482 perror_func_msg("asprintf");
483 output_line = (char *) asprintf_error_str;
493 queue_put(struct queue_t *queue,
494 const char *binary_filename,
495 const char *symbol_name,
496 unw_word_t function_offset,
497 unsigned long true_offset,
502 call = xmalloc(sizeof(*call));
503 call->output_line = sprint_call_or_error(binary_filename,
514 queue->tail->next = call;
520 queue_put_call(void *queue,
521 const char *binary_filename,
522 const char *symbol_name,
523 unw_word_t function_offset,
524 unsigned long true_offset)
535 queue_put_error(void *queue,
539 queue_put(queue, NULL, NULL, 0, ip, error);
543 queue_print(struct queue_t *queue)
545 struct call_t *call, *tmp;
554 tprints(tmp->output_line);
557 if (tmp->output_line != asprintf_error_str)
558 free(tmp->output_line);
560 tmp->output_line = NULL;
570 unwind_print_stacktrace(struct tcb *tcp)
572 #if SUPPORTED_PERSONALITIES > 1
573 if (tcp->currpers != DEFAULT_PERSONALITY) {
574 /* disable stack trace */
578 if (tcp->queue->head) {
579 debug_func_msg("head: tcp=%p, queue=%p", tcp, tcp->queue->head);
580 queue_print(tcp->queue);
581 } else if (rebuild_cache_if_invalid(tcp, __func__)) {
582 debug_func_msg("walk: tcp=%p, queue=%p", tcp, tcp->queue->head);
583 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
591 unwind_capture_stacktrace(struct tcb *tcp)
593 #if SUPPORTED_PERSONALITIES > 1
594 if (tcp->currpers != DEFAULT_PERSONALITY) {
595 /* disable stack trace */
599 if (tcp->queue->head)
600 error_msg_and_die("bug: unprinted entries in queue");
602 if (rebuild_cache_if_invalid(tcp, __func__)) {
603 stacktrace_walk(tcp, queue_put_call, queue_put_error,
605 debug_func_msg("tcp=%p, queue=%p", tcp, tcp->queue->head);