2 * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
3 * Copyright (c) 2013-2018 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.
29 #include "mmap_cache.h"
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>
41 * Type used in stacktrace walker
43 typedef void (*call_action_fn)(void *data,
44 const char *binary_filename,
45 const char *symbol_name,
46 unw_word_t function_offset,
47 unsigned long true_offset);
48 typedef void (*error_action_fn)(void *data,
50 unsigned long true_offset);
53 * Type used in stacktrace capturing
65 static void queue_print(struct queue_t *queue);
67 static unw_addr_space_t libunwind_as;
69 static const char asprintf_error_str[] = "???";
74 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
76 error_msg_and_die("failed to create address space for stack tracing");
77 unw_set_caching_policy(libunwind_as, UNW_CACHE_GLOBAL);
82 unwind_tcb_init(struct tcb *tcp)
84 if (tcp->libunwind_ui)
87 tcp->libunwind_ui = _UPT_create(tcp->pid);
88 if (!tcp->libunwind_ui)
89 perror_msg_and_die("_UPT_create");
91 tcp->queue = xmalloc(sizeof(*tcp->queue));
92 tcp->queue->head = NULL;
93 tcp->queue->tail = NULL;
97 unwind_tcb_fin(struct tcb *tcp)
99 queue_print(tcp->queue);
103 _UPT_destroy(tcp->libunwind_ui);
104 tcp->libunwind_ui = NULL;
108 get_symbol_name(unw_cursor_t *cursor, char **name,
109 size_t *size, unw_word_t *offset)
112 int rc = unw_get_proc_name(cursor, *name, *size, offset);
115 if (rc != -UNW_ENOMEM) {
120 *name = xgrowarray(*name, size, 1);
125 print_stack_frame(struct tcb *tcp,
126 call_action_fn call_action,
127 error_action_fn error_action,
129 unw_cursor_t *cursor,
131 size_t *symbol_name_size)
134 struct mmap_cache_t *cur_mmap_cache;
136 if (unw_get_reg(cursor, UNW_REG_IP, &ip) < 0) {
137 perror_msg("Can't walk the stack of process %d", tcp->pid);
141 cur_mmap_cache = mmap_cache_search(tcp, ip);
143 /* ignore mappings that have no PROT_EXEC bit set */
144 && (cur_mmap_cache->protections & MMAP_CACHE_PROT_EXECUTABLE)) {
145 unsigned long true_offset;
146 unw_word_t function_offset;
148 get_symbol_name(cursor, symbol_name, symbol_name_size,
150 true_offset = ip - cur_mmap_cache->start_addr +
151 cur_mmap_cache->mmap_offset;
153 char *demangled_name =
154 cplus_demangle(*symbol_name,
155 DMGL_AUTO | DMGL_PARAMS);
158 cur_mmap_cache->binary_filename,
160 demangled_name ? demangled_name :
166 free(demangled_name);
173 * there is a bug in libunwind >= 1.0
174 * after a set_tid_address syscall
175 * unw_get_reg returns IP == 0
178 error_action(data, "unexpected_backtracing_error", ip);
186 stacktrace_walk(struct tcb *tcp,
187 call_action_fn call_action,
188 error_action_fn error_action,
192 size_t symbol_name_size = 40;
196 if (!tcp->mmap_cache)
197 error_msg_and_die("bug: mmap_cache is NULL");
198 if (tcp->mmap_cache_size == 0)
199 error_msg_and_die("bug: mmap_cache is empty");
201 symbol_name = xmalloc(symbol_name_size);
203 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
204 perror_msg_and_die("Can't initiate libunwind");
206 for (stack_depth = 0; stack_depth < 256; ++stack_depth) {
207 if (print_stack_frame(tcp, call_action, error_action, data,
208 &cursor, &symbol_name, &symbol_name_size) < 0)
210 if (unw_step(&cursor) <= 0)
213 if (stack_depth >= 256)
214 error_action(data, "too many stack frames", 0);
220 * printing an entry in stack to stream or buffer
223 * we want to keep the format used by backtrace_symbols from the glibc
225 * ./a.out() [0x40063d]
226 * ./a.out() [0x4006bb]
227 * ./a.out() [0x4006c6]
228 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
229 * ./a.out() [0x400569]
231 #define STACK_ENTRY_SYMBOL_FMT \
232 " > %s(%s+0x%lx) [0x%lx]\n", \
235 (unsigned long) function_offset, \
237 #define STACK_ENTRY_NOSYMBOL_FMT \
238 " > %s() [0x%lx]\n", \
239 binary_filename, true_offset
240 #define STACK_ENTRY_BUG_FMT \
242 #define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
243 " > %s [0x%lx]\n", error, true_offset
244 #define STACK_ENTRY_ERROR_FMT \
248 print_call_cb(void *dummy,
249 const char *binary_filename,
250 const char *symbol_name,
251 unw_word_t function_offset,
252 unsigned long true_offset)
254 if (symbol_name && (symbol_name[0] != '\0'))
255 tprintf(STACK_ENTRY_SYMBOL_FMT);
256 else if (binary_filename)
257 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
259 tprintf(STACK_ENTRY_BUG_FMT, __func__);
265 print_error_cb(void *dummy,
267 unsigned long true_offset)
270 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
272 tprintf(STACK_ENTRY_ERROR_FMT);
278 sprint_call_or_error(const char *binary_filename,
279 const char *symbol_name,
280 unw_word_t function_offset,
281 unsigned long true_offset,
284 char *output_line = NULL;
288 n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT);
289 else if (binary_filename)
290 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
293 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
294 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
296 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __func__);
299 perror_func_msg("asprintf");
300 output_line = (char *) asprintf_error_str;
310 queue_put(struct queue_t *queue,
311 const char *binary_filename,
312 const char *symbol_name,
313 unw_word_t function_offset,
314 unsigned long true_offset,
319 call = xmalloc(sizeof(*call));
320 call->output_line = sprint_call_or_error(binary_filename,
331 queue->tail->next = call;
337 queue_put_call(void *queue,
338 const char *binary_filename,
339 const char *symbol_name,
340 unw_word_t function_offset,
341 unsigned long true_offset)
352 queue_put_error(void *queue,
356 queue_put(queue, NULL, NULL, 0, ip, error);
360 queue_print(struct queue_t *queue)
362 struct call_t *call, *tmp;
371 tprints(tmp->output_line);
374 if (tmp->output_line != asprintf_error_str)
375 free(tmp->output_line);
377 tmp->output_line = NULL;
387 unwind_print_stacktrace(struct tcb *tcp)
389 #if SUPPORTED_PERSONALITIES > 1
390 if (tcp->currpers != DEFAULT_PERSONALITY) {
391 /* disable stack trace */
395 if (tcp->queue->head) {
396 debug_func_msg("head: tcp=%p, queue=%p", tcp, tcp->queue->head);
397 queue_print(tcp->queue);
398 } else switch (mmap_cache_rebuild_if_invalid(tcp, __func__)) {
399 case MMAP_CACHE_REBUILD_RENEWED:
400 unw_flush_cache(libunwind_as, 0, 0);
401 ATTRIBUTE_FALLTHROUGH;
402 case MMAP_CACHE_REBUILD_READY:
403 debug_func_msg("walk: tcp=%p, queue=%p", tcp, tcp->queue->head);
404 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
416 unwind_capture_stacktrace(struct tcb *tcp)
418 #if SUPPORTED_PERSONALITIES > 1
419 if (tcp->currpers != DEFAULT_PERSONALITY) {
420 /* disable stack trace */
424 if (tcp->queue->head)
425 error_msg_and_die("bug: unprinted entries in queue");
427 switch (mmap_cache_rebuild_if_invalid(tcp, __func__)) {
428 case MMAP_CACHE_REBUILD_RENEWED:
429 unw_flush_cache(libunwind_as, 0, 0);
430 ATTRIBUTE_FALLTHROUGH;
431 case MMAP_CACHE_REBUILD_READY:
432 stacktrace_walk(tcp, queue_put_call, queue_put_error,
434 debug_func_msg("tcp=%p, queue=%p", tcp, tcp->queue->head);