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.
32 # if defined HAVE_DEMANGLE_H
33 # include <demangle.h>
34 # elif defined HAVE_LIBIBERTY_DEMANGLE_H
35 # include <libiberty/demangle.h>
40 * Type used in stacktrace capturing
47 struct unwind_queue_t {
52 static void queue_print(struct unwind_queue_t *queue);
54 static const char asprintf_error_str[] = "???";
64 unwind_tcb_init(struct tcb *tcp)
66 if (tcp->unwind_queue)
69 tcp->unwind_queue = xmalloc(sizeof(*tcp->unwind_queue));
70 tcp->unwind_queue->head = NULL;
71 tcp->unwind_queue->tail = NULL;
73 tcp->unwind_ctx = unwinder.tcb_init(tcp);
77 unwind_tcb_fin(struct tcb *tcp)
79 if (!tcp->unwind_queue)
82 queue_print(tcp->unwind_queue);
83 free(tcp->unwind_queue);
84 tcp->unwind_queue = NULL;
86 unwinder.tcb_fin(tcp);
87 tcp->unwind_ctx = NULL;
91 * printing an entry in stack to stream or buffer
94 * we want to keep the format used by backtrace_symbols from the glibc
96 * ./a.out() [0x40063d]
97 * ./a.out() [0x4006bb]
98 * ./a.out() [0x4006c6]
99 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
100 * ./a.out() [0x400569]
102 #define STACK_ENTRY_SYMBOL_FMT(SYM) \
103 " > %s(%s+0x%lx) [0x%lx]\n", \
106 (unsigned long) function_offset, \
108 #define STACK_ENTRY_NOSYMBOL_FMT \
109 " > %s() [0x%lx]\n", \
110 binary_filename, true_offset
111 #define STACK_ENTRY_BUG_FMT \
113 #define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
114 " > %s [0x%lx]\n", error, true_offset
115 #define STACK_ENTRY_ERROR_FMT \
119 print_call_cb(void *dummy,
120 const char *binary_filename,
121 const char *symbol_name,
122 unwind_function_offset_t function_offset,
123 unsigned long true_offset)
125 if (symbol_name && (symbol_name[0] != '\0')) {
127 char *demangled_name =
128 cplus_demangle(symbol_name,
129 DMGL_AUTO | DMGL_PARAMS);
131 tprintf(STACK_ENTRY_SYMBOL_FMT(
133 demangled_name ? demangled_name :
137 free(demangled_name);
140 else if (binary_filename)
141 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
143 tprintf(STACK_ENTRY_BUG_FMT, __func__);
149 print_error_cb(void *dummy,
151 unsigned long true_offset)
154 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
156 tprintf(STACK_ENTRY_ERROR_FMT);
162 sprint_call_or_error(const char *binary_filename,
163 const char *symbol_name,
164 unwind_function_offset_t function_offset,
165 unsigned long true_offset,
168 char *output_line = NULL;
173 char *demangled_name =
174 cplus_demangle(symbol_name,
175 DMGL_AUTO | DMGL_PARAMS);
177 n = asprintf(&output_line,
178 STACK_ENTRY_SYMBOL_FMT(
180 demangled_name ? demangled_name :
184 free(demangled_name);
187 else if (binary_filename)
188 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
191 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
192 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
194 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __func__);
197 perror_func_msg("asprintf");
198 output_line = (char *) asprintf_error_str;
208 queue_put(struct unwind_queue_t *queue,
209 const char *binary_filename,
210 const char *symbol_name,
211 unwind_function_offset_t function_offset,
212 unsigned long true_offset,
217 call = xmalloc(sizeof(*call));
218 call->output_line = sprint_call_or_error(binary_filename,
229 queue->tail->next = call;
235 queue_put_call(void *queue,
236 const char *binary_filename,
237 const char *symbol_name,
238 unwind_function_offset_t function_offset,
239 unsigned long true_offset)
250 queue_put_error(void *queue,
254 queue_put(queue, NULL, NULL, 0, ip, error);
258 queue_print(struct unwind_queue_t *queue)
260 struct call_t *call, *tmp;
269 tprints(tmp->output_line);
272 if (tmp->output_line != asprintf_error_str)
273 free(tmp->output_line);
275 tmp->output_line = NULL;
285 unwind_tcb_print(struct tcb *tcp)
287 #if SUPPORTED_PERSONALITIES > 1
288 if (tcp->currpers != DEFAULT_PERSONALITY) {
289 /* disable stack trace */
293 if (tcp->unwind_queue->head) {
294 debug_func_msg("head: tcp=%p, queue=%p",
295 tcp, tcp->unwind_queue->head);
296 queue_print(tcp->unwind_queue);
298 unwinder.tcb_walk(tcp, print_call_cb, print_error_cb, NULL);
305 unwind_tcb_capture(struct tcb *tcp)
307 #if SUPPORTED_PERSONALITIES > 1
308 if (tcp->currpers != DEFAULT_PERSONALITY) {
309 /* disable stack trace */
313 if (tcp->unwind_queue->head)
314 error_msg_and_die("bug: unprinted entries in queue");
316 debug_func_msg("walk: tcp=%p, queue=%p",
317 tcp, tcp->unwind_queue->head);
318 unwinder.tcb_walk(tcp, queue_put_call, queue_put_error,