blob: 2e809266429eef0559c8d66f46378b517ea67534 [file] [log] [blame]
Luca Clementi327064b2013-07-23 00:11:35 -07001/*
2 * Copyright (c) 2013 Luca Clementi <luca.clementi@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
14 *
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.
25 */
26
27#include "defs.h"
28#include <limits.h>
29#include <libunwind-ptrace.h>
30
Dmitry V. Levinc903c822014-06-05 15:12:42 +000031#ifdef _LARGEFILE64_SOURCE
32# ifdef HAVE_FOPEN64
33# define fopen_for_input fopen64
34# else
35# define fopen_for_input fopen
36# endif
37#else
38# define fopen_for_input fopen
39#endif
40
Masatake YAMATO4e121e52014-04-16 15:33:05 +090041#define DPRINTF(F, A, ...) if (debug_flag) fprintf(stderr, " [unwind(" A ")] " F "\n", __VA_ARGS__)
42
Luca Clementi327064b2013-07-23 00:11:35 -070043/*
44 * Кeep a sorted array of cache entries,
45 * so that we can binary search through it.
46 */
47struct mmap_cache_t {
48 /**
49 * example entry:
Dmitry V. Levin9a349c72014-06-12 18:01:45 +040050 * 7fabbb09b000-7fabbb09f000 r-xp 00179000 fc:00 1180246 /lib/libc-2.11.1.so
Luca Clementi327064b2013-07-23 00:11:35 -070051 *
52 * start_addr is 0x7fabbb09b000
53 * end_addr is 0x7fabbb09f000
54 * mmap_offset is 0x179000
55 * binary_filename is "/lib/libc-2.11.1.so"
56 */
57 unsigned long start_addr;
58 unsigned long end_addr;
59 unsigned long mmap_offset;
Dmitry V. Levin806539c2014-06-05 22:37:09 +000060 char *binary_filename;
Luca Clementi327064b2013-07-23 00:11:35 -070061};
62
Masatake YAMATO2d534da2014-04-16 15:33:04 +090063/*
64 * Type used in stacktrace walker
65 */
66typedef void (*call_action_fn)(void *data,
Dmitry V. Levin806539c2014-06-05 22:37:09 +000067 const char *binary_filename,
68 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +000069 unw_word_t function_offset,
Masatake YAMATO2d534da2014-04-16 15:33:04 +090070 unsigned long true_offset);
71typedef void (*error_action_fn)(void *data,
72 const char *error,
73 unsigned long true_offset);
74
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090075/*
76 * Type used in stacktrace capturing
77 */
78struct call_t {
79 struct call_t* next;
80 char *output_line;
81};
82
83struct queue_t {
84 struct call_t *tail;
85 struct call_t *head;
86};
Masatake YAMATO9bc65612014-04-16 15:33:07 +090087
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090088static void queue_print(struct queue_t *queue);
Masatake YAMATO9bc65612014-04-16 15:33:07 +090089static void delete_mmap_cache(struct tcb *tcp, const char *caller);
Masatake YAMATO2d534da2014-04-16 15:33:04 +090090
Luca Clementi327064b2013-07-23 00:11:35 -070091static unw_addr_space_t libunwind_as;
Masatake YAMATO9bc65612014-04-16 15:33:07 +090092static unsigned int mmap_cache_generation;
Luca Clementi327064b2013-07-23 00:11:35 -070093
94void
Masatake YAMATO61413922014-04-16 15:33:02 +090095unwind_init(void)
Luca Clementi327064b2013-07-23 00:11:35 -070096{
97 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
98 if (!libunwind_as)
99 error_msg_and_die("failed to create address space for stack tracing");
Masatake YAMATOa0b4ee72014-04-16 15:33:10 +0900100 unw_set_caching_policy(libunwind_as, UNW_CACHE_GLOBAL);
Luca Clementi327064b2013-07-23 00:11:35 -0700101}
102
103void
Masatake YAMATO61413922014-04-16 15:33:02 +0900104unwind_tcb_init(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700105{
106 tcp->libunwind_ui = _UPT_create(tcp->pid);
107 if (!tcp->libunwind_ui)
108 die_out_of_memory();
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900109
110 tcp->queue = malloc(sizeof(*tcp->queue));
111 if (!tcp->queue)
112 die_out_of_memory();
113 tcp->queue->head = NULL;
114 tcp->queue->tail = NULL;
Luca Clementi327064b2013-07-23 00:11:35 -0700115}
116
117void
Masatake YAMATO61413922014-04-16 15:33:02 +0900118unwind_tcb_fin(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700119{
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900120 queue_print(tcp->queue);
121 free(tcp->queue);
122 tcp->queue = NULL;
123
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900124 delete_mmap_cache(tcp, __FUNCTION__);
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900125
Luca Clementi327064b2013-07-23 00:11:35 -0700126 _UPT_destroy(tcp->libunwind_ui);
127 tcp->libunwind_ui = NULL;
128}
129
130/*
131 * caching of /proc/ID/maps for each process to speed up stack tracing
132 *
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400133 * The cache must be refreshed after syscalls that affect memory mappings,
134 * e.g. mmap, mprotect, munmap, execve.
Luca Clementi327064b2013-07-23 00:11:35 -0700135 */
Masatake YAMATOb65042f2014-04-16 15:33:00 +0900136static void
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900137build_mmap_cache(struct tcb* tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700138{
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400139 FILE *fp;
140 struct mmap_cache_t *cache_head;
Luca Clementi327064b2013-07-23 00:11:35 -0700141 /* start with a small dynamically-allocated array and then expand it */
142 size_t cur_array_size = 10;
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400143 char filename[sizeof("/proc/4294967296/maps")];
144 char buffer[PATH_MAX + 80];
Luca Clementi327064b2013-07-23 00:11:35 -0700145
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400146 unw_flush_cache(libunwind_as, 0, 0);
Masatake YAMATOa0b4ee72014-04-16 15:33:10 +0900147
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400148 sprintf(filename, "/proc/%u/maps", tcp->pid);
Dmitry V. Levinc903c822014-06-05 15:12:42 +0000149 fp = fopen_for_input(filename, "r");
Luca Clementi327064b2013-07-23 00:11:35 -0700150 if (!fp) {
151 perror_msg("fopen: %s", filename);
152 return;
153 }
154
155 cache_head = calloc(cur_array_size, sizeof(*cache_head));
156 if (!cache_head)
157 die_out_of_memory();
158
159 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400160 struct mmap_cache_t *entry;
161 unsigned long start_addr, end_addr, mmap_offset;
162 char binary_path[PATH_MAX];
Luca Clementi327064b2013-07-23 00:11:35 -0700163
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400164 if (sscanf(buffer, "%lx-%lx %*c%*c%c%*c %lx %*x:%*x %*d %[^\n]",
165 &start_addr, &end_addr,
166 &mmap_offset, binary_path) != 4)
Luca Clementi327064b2013-07-23 00:11:35 -0700167 continue;
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400168
169 if (end_addr < start_addr) {
170 error_msg("%s: unrecognized file format", filename);
171 break;
Luca Clementi327064b2013-07-23 00:11:35 -0700172 }
173
Luca Clementi327064b2013-07-23 00:11:35 -0700174 /*
175 * sanity check to make sure that we're storing
176 * non-overlapping regions in ascending order
177 */
178 if (tcp->mmap_cache_size > 0) {
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400179 entry = &cache_head[tcp->mmap_cache_size - 1];
Luca Clementi327064b2013-07-23 00:11:35 -0700180
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400181 if (entry->start_addr == start_addr &&
182 entry->end_addr == end_addr) {
183 /* duplicate entry, e.g. [vsyscall] */
184 continue;
185 }
186 if (start_addr <= entry->start_addr ||
187 start_addr < entry->end_addr) {
188 error_msg("%s: overlapping memory region",
189 filename);
190 continue;
191 }
192 }
193
Luca Clementi327064b2013-07-23 00:11:35 -0700194 if (tcp->mmap_cache_size >= cur_array_size) {
195 cur_array_size *= 2;
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400196 cache_head = realloc(cache_head,
197 cur_array_size * sizeof(*cache_head));
Luca Clementi327064b2013-07-23 00:11:35 -0700198 if (!cache_head)
199 die_out_of_memory();
200 }
Dmitry V. Levin9a349c72014-06-12 18:01:45 +0400201
202 entry = &cache_head[tcp->mmap_cache_size];
203 entry->start_addr = start_addr;
204 entry->end_addr = end_addr;
205 entry->mmap_offset = mmap_offset;
206 entry->binary_filename = strdup(binary_path);
207 if (!entry->binary_filename)
208 die_out_of_memory();
209 tcp->mmap_cache_size++;
Luca Clementi327064b2013-07-23 00:11:35 -0700210 }
211 fclose(fp);
212 tcp->mmap_cache = cache_head;
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900213 tcp->mmap_cache_generation = mmap_cache_generation;
214
215 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p",
216 "cache-build",
217 tcp->mmap_cache_generation,
218 mmap_cache_generation,
219 tcp, tcp->mmap_cache);
Luca Clementi327064b2013-07-23 00:11:35 -0700220}
221
222/* deleting the cache */
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900223static void
224delete_mmap_cache(struct tcb *tcp, const char *caller)
Luca Clementi327064b2013-07-23 00:11:35 -0700225{
226 unsigned int i;
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900227
228 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s",
229 "cache-delete",
230 tcp->mmap_cache_generation,
231 mmap_cache_generation,
232 tcp, tcp->mmap_cache, caller);
233
Luca Clementi327064b2013-07-23 00:11:35 -0700234 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;
237 }
238 free(tcp->mmap_cache);
239 tcp->mmap_cache = NULL;
240 tcp->mmap_cache_size = 0;
241}
242
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900243static bool
244rebuild_cache_if_invalid(struct tcb *tcp, const char *caller)
245{
246 if ((tcp->mmap_cache_generation != mmap_cache_generation)
247 && tcp->mmap_cache)
248 delete_mmap_cache(tcp, caller);
249
250 if (!tcp->mmap_cache)
251 build_mmap_cache(tcp);
252
253 if (!tcp->mmap_cache || !tcp->mmap_cache_size)
254 return false;
255 else
256 return true;
257}
258
259void
260unwind_cache_invalidate(struct tcb* tcp)
261{
Luca Clementif1d73112014-06-09 22:05:38 -0700262#if SUPPORTED_PERSONALITIES > 1
263 if (tcp->currpers != DEFAULT_PERSONALITY) {
264 /* disable strack trace */
265 return;
266 }
267#endif
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900268 mmap_cache_generation++;
269 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p", "increment",
270 tcp->mmap_cache_generation,
271 mmap_cache_generation,
272 tcp,
273 tcp->mmap_cache);
274}
275
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000276static void
277get_symbol_name(unw_cursor_t *cursor, char **name,
278 size_t *size, unw_word_t *offset)
279{
280 for (;;) {
281 int rc = unw_get_proc_name(cursor, *name, *size, offset);
282 if (rc == 0)
283 break;
284 if (rc != -UNW_ENOMEM) {
285 **name = '\0';
286 *offset = 0;
287 break;
288 }
289 *size *= 2;
290 *name = realloc(*name, *size);
291 if (!*name)
292 die_out_of_memory();
293 }
294}
295
296static int
297print_stack_frame(struct tcb *tcp,
298 call_action_fn call_action,
299 error_action_fn error_action,
300 void *data,
301 unw_cursor_t *cursor,
302 char **symbol_name,
303 size_t *symbol_name_size)
304{
305 unw_word_t ip;
306 int lower = 0;
307 int upper = (int) tcp->mmap_cache_size - 1;
308
309 if (unw_get_reg(cursor, UNW_REG_IP, &ip) < 0) {
310 perror_msg("Can't walk the stack of process %d", tcp->pid);
311 return -1;
312 }
313
314 while (lower <= upper) {
315 struct mmap_cache_t *cur_mmap_cache;
316 int mid = (upper + lower) / 2;
317
318 cur_mmap_cache = &tcp->mmap_cache[mid];
319
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;
324
325 get_symbol_name(cursor, symbol_name, symbol_name_size,
326 &function_offset);
327 true_offset = ip - cur_mmap_cache->start_addr +
328 cur_mmap_cache->mmap_offset;
329 call_action(data,
330 cur_mmap_cache->binary_filename,
331 *symbol_name,
332 function_offset,
333 true_offset);
334 return 0;
335 }
336 else if (ip < cur_mmap_cache->start_addr)
337 upper = mid - 1;
338 else
339 lower = mid + 1;
340 }
341
342 /*
343 * there is a bug in libunwind >= 1.0
344 * after a set_tid_address syscall
345 * unw_get_reg returns IP == 0
346 */
347 if(ip)
348 error_action(data, "unexpected_backtracing_error", ip);
349 return -1;
350}
351
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900352/*
353 * walking the stack
354 */
355static void
356stacktrace_walk(struct tcb *tcp,
357 call_action_fn call_action,
358 error_action_fn error_action,
359 void *data)
Luca Clementi327064b2013-07-23 00:11:35 -0700360{
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000361 char *symbol_name;
Luca Clementi327064b2013-07-23 00:11:35 -0700362 size_t symbol_name_size = 40;
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000363 unw_cursor_t cursor;
364 int stack_depth;
Luca Clementi327064b2013-07-23 00:11:35 -0700365
366 if (!tcp->mmap_cache)
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900367 error_msg_and_die("bug: mmap_cache is NULL");
368 if (tcp->mmap_cache_size == 0)
369 error_msg_and_die("bug: mmap_cache is empty");
Luca Clementi327064b2013-07-23 00:11:35 -0700370
371 symbol_name = malloc(symbol_name_size);
372 if (!symbol_name)
373 die_out_of_memory();
374
375 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
376 perror_msg_and_die("Can't initiate libunwind");
377
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000378 for (stack_depth = 0; stack_depth < 256; ++stack_depth) {
379 if (print_stack_frame(tcp, call_action, error_action, data,
380 &cursor, &symbol_name, &symbol_name_size) < 0)
Luca Clementi327064b2013-07-23 00:11:35 -0700381 break;
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000382 if (unw_step(&cursor) <= 0)
Luca Clementi327064b2013-07-23 00:11:35 -0700383 break;
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000384 }
385 if (stack_depth >= 256)
386 error_action(data, "too many stack frames", 0);
387
Luca Clementi327064b2013-07-23 00:11:35 -0700388 free(symbol_name);
389}
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900390
391/*
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900392 * printing an entry in stack to stream or buffer
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900393 */
394/*
395 * we want to keep the format used by backtrace_symbols from the glibc
396 *
397 * ./a.out() [0x40063d]
398 * ./a.out() [0x4006bb]
399 * ./a.out() [0x4006c6]
400 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
401 * ./a.out() [0x400569]
402 */
403#define STACK_ENTRY_SYMBOL_FMT \
404 " > %s(%s+0x%lx) [0x%lx]\n", \
405 binary_filename, \
406 symbol_name, \
Dmitry V. Levin65557112014-06-05 21:44:40 +0000407 (unsigned long) function_offset, \
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900408 true_offset
409#define STACK_ENTRY_NOSYMBOL_FMT \
410 " > %s() [0x%lx]\n", \
411 binary_filename, true_offset
412#define STACK_ENTRY_BUG_FMT \
413 " > BUG IN %s\n"
414#define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
415 " > %s [0x%lx]\n", error, true_offset
416#define STACK_ENTRY_ERROR_FMT \
417 " > %s\n", error
418
419static void
420print_call_cb(void *dummy,
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000421 const char *binary_filename,
422 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000423 unw_word_t function_offset,
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900424 unsigned long true_offset)
425{
426 if (symbol_name)
427 tprintf(STACK_ENTRY_SYMBOL_FMT);
428 else if (binary_filename)
429 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
430 else
431 tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__);
432
433 line_ended();
434}
435
436static void
437print_error_cb(void *dummy,
438 const char *error,
439 unsigned long true_offset)
440{
441 if (true_offset)
442 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
443 else
444 tprintf(STACK_ENTRY_ERROR_FMT);
445
446 line_ended();
447}
448
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900449static char *
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000450sprint_call_or_error(const char *binary_filename,
451 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000452 unw_word_t function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900453 unsigned long true_offset,
454 const char *error)
455{
456 char *output_line = NULL;
457 int n;
458
459 if (symbol_name)
460 n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT);
461 else if (binary_filename)
462 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
463 else if (error)
464 n = true_offset
465 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
466 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
467 else
468 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __FUNCTION__);
469
470 if (n < 0)
471 error_msg_and_die("error in asprintf");
472
473 return output_line;
474}
475
476/*
477 * queue manipulators
478 */
479static void
480queue_put(struct queue_t *queue,
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000481 const char *binary_filename,
482 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000483 unw_word_t function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900484 unsigned long true_offset,
485 const char *error)
486{
487 struct call_t *call;
488
489 call = malloc(sizeof(*call));
490 if (!call)
491 die_out_of_memory();
492
493 call->output_line = sprint_call_or_error(binary_filename,
494 symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000495 function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900496 true_offset,
497 error);
498 call->next = NULL;
499
500 if (!queue->head) {
501 queue->head = call;
502 queue->tail = call;
503 } else {
504 queue->tail->next = call;
505 queue->tail = call;
506 }
507}
508
509static void
510queue_put_call(void *queue,
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000511 const char *binary_filename,
512 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000513 unw_word_t function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900514 unsigned long true_offset)
515{
516 queue_put(queue,
517 binary_filename,
518 symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000519 function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900520 true_offset,
521 NULL);
522}
523
524static void
525queue_put_error(void *queue,
526 const char *error,
Dmitry V. Levine4113972014-06-05 14:37:04 +0000527 unsigned long ip)
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900528{
529 queue_put(queue, NULL, NULL, 0, ip, error);
530}
531
532static void
533queue_print(struct queue_t *queue)
534{
535 struct call_t *call, *tmp;
536
537 queue->tail = NULL;
538 call = queue->head;
539 queue->head = NULL;
540 while (call) {
541 tmp = call;
542 call = call->next;
543
544 tprints(tmp->output_line);
545 line_ended();
546
547 free(tmp->output_line);
548 tmp->output_line = NULL;
549 tmp->next = NULL;
550 free(tmp);
551 }
552}
553
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900554/*
555 * printing stack
556 */
557void
558unwind_print_stacktrace(struct tcb* tcp)
559{
Luca Clementif1d73112014-06-09 22:05:38 -0700560#if SUPPORTED_PERSONALITIES > 1
561 if (tcp->currpers != DEFAULT_PERSONALITY) {
562 /* disable strack trace */
563 return;
564 }
565#endif
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900566 if (tcp->queue->head) {
567 DPRINTF("tcp=%p, queue=%p", "queueprint", tcp, tcp->queue->head);
568 queue_print(tcp->queue);
569 }
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900570 else if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900571 DPRINTF("tcp=%p, queue=%p", "stackprint", tcp, tcp->queue->head);
572 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
573 }
574}
575
576/*
577 * capturing stack
578 */
579void
580unwind_capture_stacktrace(struct tcb *tcp)
581{
Luca Clementif1d73112014-06-09 22:05:38 -0700582#if SUPPORTED_PERSONALITIES > 1
583 if (tcp->currpers != DEFAULT_PERSONALITY) {
584 /* disable strack trace */
585 return;
586 }
587#endif
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900588 if (tcp->queue->head)
589 error_msg_and_die("bug: unprinted entries in queue");
590
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900591 if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
592 stacktrace_walk(tcp, queue_put_call, queue_put_error,
593 tcp->queue);
594 DPRINTF("tcp=%p, queue=%p", "captured", tcp, tcp->queue->head);
595 }
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900596}