blob: e4c6623c25fa376581a6cfa75be1bbfc49c37cf4 [file] [log] [blame]
Luca Clementi327064b2013-07-23 00:11:35 -07001/*
2 * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
Elliott Hughesb7556142018-02-20 17:03:16 -08003 * Copyright (c) 2013-2018 The strace developers.
Luca Clementi327064b2013-07-23 00:11:35 -07004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
15 *
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.
26 */
27
28#include "defs.h"
Elliott Hughes28e98bc2018-06-14 16:59:04 -070029#include "unwind.h"
Luca Clementi327064b2013-07-23 00:11:35 -070030
Elliott Hughesb7556142018-02-20 17:03:16 -080031#ifdef USE_DEMANGLE
32# if defined HAVE_DEMANGLE_H
33# include <demangle.h>
34# elif defined HAVE_LIBIBERTY_DEMANGLE_H
35# include <libiberty/demangle.h>
36# endif
37#endif
38
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090039/*
40 * Type used in stacktrace capturing
41 */
42struct call_t {
Elliott Hughesdc75b012017-07-05 13:54:44 -070043 struct call_t *next;
44 char *output_line;
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090045};
46
Elliott Hughes28e98bc2018-06-14 16:59:04 -070047struct unwind_queue_t {
Elliott Hughesdc75b012017-07-05 13:54:44 -070048 struct call_t *tail;
49 struct call_t *head;
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090050};
Masatake YAMATO9bc65612014-04-16 15:33:07 +090051
Elliott Hughes28e98bc2018-06-14 16:59:04 -070052static void queue_print(struct unwind_queue_t *queue);
Luca Clementi327064b2013-07-23 00:11:35 -070053
Elliott Hughesb7556142018-02-20 17:03:16 -080054static const char asprintf_error_str[] = "???";
55
Luca Clementi327064b2013-07-23 00:11:35 -070056void
Masatake YAMATO61413922014-04-16 15:33:02 +090057unwind_init(void)
Luca Clementi327064b2013-07-23 00:11:35 -070058{
Elliott Hughes28e98bc2018-06-14 16:59:04 -070059 if (unwinder.init)
60 unwinder.init();
Luca Clementi327064b2013-07-23 00:11:35 -070061}
62
63void
Masatake YAMATO61413922014-04-16 15:33:02 +090064unwind_tcb_init(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -070065{
Elliott Hughes28e98bc2018-06-14 16:59:04 -070066 if (tcp->unwind_queue)
Elliott Hughesd35df492017-02-15 15:19:05 -080067 return;
68
Elliott Hughes28e98bc2018-06-14 16:59:04 -070069 tcp->unwind_queue = xmalloc(sizeof(*tcp->unwind_queue));
70 tcp->unwind_queue->head = NULL;
71 tcp->unwind_queue->tail = NULL;
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090072
Elliott Hughes28e98bc2018-06-14 16:59:04 -070073 tcp->unwind_ctx = unwinder.tcb_init(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -070074}
75
76void
Masatake YAMATO61413922014-04-16 15:33:02 +090077unwind_tcb_fin(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -070078{
Elliott Hughes03a418e2018-06-15 13:11:40 -070079 if (!tcp->unwind_queue)
80 return;
81
Elliott Hughes28e98bc2018-06-14 16:59:04 -070082 queue_print(tcp->unwind_queue);
83 free(tcp->unwind_queue);
84 tcp->unwind_queue = NULL;
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090085
Elliott Hughes28e98bc2018-06-14 16:59:04 -070086 unwinder.tcb_fin(tcp);
87 tcp->unwind_ctx = NULL;
Luca Clementi327064b2013-07-23 00:11:35 -070088}
Masatake YAMATO2d534da2014-04-16 15:33:04 +090089
90/*
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090091 * printing an entry in stack to stream or buffer
Masatake YAMATO2d534da2014-04-16 15:33:04 +090092 */
93/*
94 * we want to keep the format used by backtrace_symbols from the glibc
95 *
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]
101 */
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700102#define STACK_ENTRY_SYMBOL_FMT(SYM) \
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900103 " > %s(%s+0x%lx) [0x%lx]\n", \
104 binary_filename, \
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700105 (SYM), \
Dmitry V. Levin65557112014-06-05 21:44:40 +0000106 (unsigned long) function_offset, \
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900107 true_offset
108#define STACK_ENTRY_NOSYMBOL_FMT \
109 " > %s() [0x%lx]\n", \
110 binary_filename, true_offset
111#define STACK_ENTRY_BUG_FMT \
112 " > BUG IN %s\n"
113#define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
114 " > %s [0x%lx]\n", error, true_offset
115#define STACK_ENTRY_ERROR_FMT \
116 " > %s\n", error
117
118static void
119print_call_cb(void *dummy,
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000120 const char *binary_filename,
121 const char *symbol_name,
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700122 unwind_function_offset_t function_offset,
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900123 unsigned long true_offset)
124{
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700125 if (symbol_name && (symbol_name[0] != '\0')) {
126#ifdef USE_DEMANGLE
127 char *demangled_name =
128 cplus_demangle(symbol_name,
129 DMGL_AUTO | DMGL_PARAMS);
130#endif
131 tprintf(STACK_ENTRY_SYMBOL_FMT(
132#ifdef USE_DEMANGLE
133 demangled_name ? demangled_name :
134#endif
135 symbol_name));
136#ifdef USE_DEMANGLE
137 free(demangled_name);
138#endif
139 }
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900140 else if (binary_filename)
141 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
142 else
Elliott Hughesdc75b012017-07-05 13:54:44 -0700143 tprintf(STACK_ENTRY_BUG_FMT, __func__);
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900144
145 line_ended();
146}
147
148static void
149print_error_cb(void *dummy,
150 const char *error,
151 unsigned long true_offset)
152{
153 if (true_offset)
154 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
155 else
156 tprintf(STACK_ENTRY_ERROR_FMT);
157
158 line_ended();
159}
160
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900161static char *
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000162sprint_call_or_error(const char *binary_filename,
163 const char *symbol_name,
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700164 unwind_function_offset_t function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900165 unsigned long true_offset,
166 const char *error)
167{
Elliott Hughesdc75b012017-07-05 13:54:44 -0700168 char *output_line = NULL;
169 int n;
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900170
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700171 if (symbol_name) {
172#ifdef USE_DEMANGLE
173 char *demangled_name =
174 cplus_demangle(symbol_name,
175 DMGL_AUTO | DMGL_PARAMS);
176#endif
177 n = asprintf(&output_line,
178 STACK_ENTRY_SYMBOL_FMT(
179#ifdef USE_DEMANGLE
180 demangled_name ? demangled_name :
181#endif
182 symbol_name));
183#ifdef USE_DEMANGLE
184 free(demangled_name);
185#endif
186 }
Elliott Hughesdc75b012017-07-05 13:54:44 -0700187 else if (binary_filename)
188 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
189 else if (error)
190 n = true_offset
191 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
192 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
193 else
194 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __func__);
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900195
Elliott Hughesb7556142018-02-20 17:03:16 -0800196 if (n < 0) {
197 perror_func_msg("asprintf");
198 output_line = (char *) asprintf_error_str;
199 }
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900200
Elliott Hughesdc75b012017-07-05 13:54:44 -0700201 return output_line;
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900202}
203
204/*
205 * queue manipulators
206 */
207static void
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700208queue_put(struct unwind_queue_t *queue,
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000209 const char *binary_filename,
210 const char *symbol_name,
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700211 unwind_function_offset_t function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900212 unsigned long true_offset,
213 const char *error)
214{
215 struct call_t *call;
216
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000217 call = xmalloc(sizeof(*call));
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900218 call->output_line = sprint_call_or_error(binary_filename,
219 symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000220 function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900221 true_offset,
222 error);
223 call->next = NULL;
224
225 if (!queue->head) {
226 queue->head = call;
227 queue->tail = call;
228 } else {
229 queue->tail->next = call;
230 queue->tail = call;
231 }
232}
233
234static void
235queue_put_call(void *queue,
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000236 const char *binary_filename,
237 const char *symbol_name,
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700238 unwind_function_offset_t function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900239 unsigned long true_offset)
240{
241 queue_put(queue,
242 binary_filename,
243 symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000244 function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900245 true_offset,
246 NULL);
247}
248
249static void
250queue_put_error(void *queue,
251 const char *error,
Dmitry V. Levine4113972014-06-05 14:37:04 +0000252 unsigned long ip)
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900253{
254 queue_put(queue, NULL, NULL, 0, ip, error);
255}
256
257static void
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700258queue_print(struct unwind_queue_t *queue)
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900259{
260 struct call_t *call, *tmp;
261
262 queue->tail = NULL;
263 call = queue->head;
264 queue->head = NULL;
265 while (call) {
266 tmp = call;
267 call = call->next;
268
269 tprints(tmp->output_line);
270 line_ended();
271
Elliott Hughesb7556142018-02-20 17:03:16 -0800272 if (tmp->output_line != asprintf_error_str)
273 free(tmp->output_line);
274
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900275 tmp->output_line = NULL;
276 tmp->next = NULL;
277 free(tmp);
278 }
279}
280
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900281/*
282 * printing stack
283 */
284void
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700285unwind_tcb_print(struct tcb *tcp)
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900286{
Luca Clementif1d73112014-06-09 22:05:38 -0700287#if SUPPORTED_PERSONALITIES > 1
288 if (tcp->currpers != DEFAULT_PERSONALITY) {
Elliott Hughesb7556142018-02-20 17:03:16 -0800289 /* disable stack trace */
Luca Clementif1d73112014-06-09 22:05:38 -0700290 return;
291 }
292#endif
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700293 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);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700297 } else
298 unwinder.tcb_walk(tcp, print_call_cb, print_error_cb, NULL);
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900299}
300
301/*
302 * capturing stack
303 */
304void
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700305unwind_tcb_capture(struct tcb *tcp)
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900306{
Luca Clementif1d73112014-06-09 22:05:38 -0700307#if SUPPORTED_PERSONALITIES > 1
308 if (tcp->currpers != DEFAULT_PERSONALITY) {
Elliott Hughesb7556142018-02-20 17:03:16 -0800309 /* disable stack trace */
Luca Clementif1d73112014-06-09 22:05:38 -0700310 return;
311 }
312#endif
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700313 if (tcp->unwind_queue->head)
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900314 error_msg_and_die("bug: unprinted entries in queue");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700315 else {
316 debug_func_msg("walk: tcp=%p, queue=%p",
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700317 tcp, tcp->unwind_queue->head);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700318 unwinder.tcb_walk(tcp, queue_put_call, queue_put_error,
319 tcp->unwind_queue);
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900320 }
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900321}