blob: c4a262cd7679b4534aa9bd8f5ea2beb4d85eeeb2 [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:
50 * 7fabbb09b000-7fabbb09f000 r--p 00179000 fc:00 1180246 /lib/libc-2.11.1.so
51 *
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;
Masatake YAMATOb45b7fa2014-04-16 15:33:09 +090061 bool deleted;
Luca Clementi327064b2013-07-23 00:11:35 -070062};
63
Masatake YAMATO2d534da2014-04-16 15:33:04 +090064/*
65 * Type used in stacktrace walker
66 */
67typedef void (*call_action_fn)(void *data,
Dmitry V. Levin806539c2014-06-05 22:37:09 +000068 const char *binary_filename,
69 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +000070 unw_word_t function_offset,
Masatake YAMATO2d534da2014-04-16 15:33:04 +090071 unsigned long true_offset);
72typedef void (*error_action_fn)(void *data,
73 const char *error,
74 unsigned long true_offset);
75
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090076/*
77 * Type used in stacktrace capturing
78 */
79struct call_t {
80 struct call_t* next;
81 char *output_line;
82};
83
84struct queue_t {
85 struct call_t *tail;
86 struct call_t *head;
87};
Masatake YAMATO9bc65612014-04-16 15:33:07 +090088
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090089static void queue_print(struct queue_t *queue);
Masatake YAMATO9bc65612014-04-16 15:33:07 +090090static void delete_mmap_cache(struct tcb *tcp, const char *caller);
Masatake YAMATO2d534da2014-04-16 15:33:04 +090091
Luca Clementi327064b2013-07-23 00:11:35 -070092static unw_addr_space_t libunwind_as;
Masatake YAMATO9bc65612014-04-16 15:33:07 +090093static unsigned int mmap_cache_generation;
Luca Clementi327064b2013-07-23 00:11:35 -070094
95void
Masatake YAMATO61413922014-04-16 15:33:02 +090096unwind_init(void)
Luca Clementi327064b2013-07-23 00:11:35 -070097{
98 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
99 if (!libunwind_as)
100 error_msg_and_die("failed to create address space for stack tracing");
Masatake YAMATOa0b4ee72014-04-16 15:33:10 +0900101 unw_set_caching_policy(libunwind_as, UNW_CACHE_GLOBAL);
Luca Clementi327064b2013-07-23 00:11:35 -0700102}
103
104void
Masatake YAMATO61413922014-04-16 15:33:02 +0900105unwind_tcb_init(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700106{
107 tcp->libunwind_ui = _UPT_create(tcp->pid);
108 if (!tcp->libunwind_ui)
109 die_out_of_memory();
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900110
111 tcp->queue = malloc(sizeof(*tcp->queue));
112 if (!tcp->queue)
113 die_out_of_memory();
114 tcp->queue->head = NULL;
115 tcp->queue->tail = NULL;
Luca Clementi327064b2013-07-23 00:11:35 -0700116}
117
118void
Masatake YAMATO61413922014-04-16 15:33:02 +0900119unwind_tcb_fin(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700120{
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900121 queue_print(tcp->queue);
122 free(tcp->queue);
123 tcp->queue = NULL;
124
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900125 delete_mmap_cache(tcp, __FUNCTION__);
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900126
Luca Clementi327064b2013-07-23 00:11:35 -0700127 _UPT_destroy(tcp->libunwind_ui);
128 tcp->libunwind_ui = NULL;
129}
130
131/*
132 * caching of /proc/ID/maps for each process to speed up stack tracing
133 *
134 * The cache must be refreshed after some syscall: mmap, mprotect, munmap, execve
135 */
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{
139 unsigned long start_addr, end_addr, mmap_offset;
140 char filename[sizeof ("/proc/0123456789/maps")];
141 char buffer[PATH_MAX + 80];
142 char binary_path[PATH_MAX];
143 struct mmap_cache_t *cur_entry, *prev_entry;
144 /* start with a small dynamically-allocated array and then expand it */
145 size_t cur_array_size = 10;
146 struct mmap_cache_t *cache_head;
147 FILE *fp;
148
Masatake YAMATOb45b7fa2014-04-16 15:33:09 +0900149 const char *deleted = " (deleted)";
150 size_t blen;
151 size_t dlen;
152
Masatake YAMATOa0b4ee72014-04-16 15:33:10 +0900153 unw_flush_cache (libunwind_as, 0, 0);
154
Luca Clementi327064b2013-07-23 00:11:35 -0700155 sprintf(filename, "/proc/%d/maps", tcp->pid);
Dmitry V. Levinc903c822014-06-05 15:12:42 +0000156 fp = fopen_for_input(filename, "r");
Luca Clementi327064b2013-07-23 00:11:35 -0700157 if (!fp) {
158 perror_msg("fopen: %s", filename);
159 return;
160 }
161
162 cache_head = calloc(cur_array_size, sizeof(*cache_head));
163 if (!cache_head)
164 die_out_of_memory();
165
166 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
167 binary_path[0] = '\0'; // 'reset' it just to be paranoid
168
169 sscanf(buffer, "%lx-%lx %*c%*c%*c%*c %lx %*x:%*x %*d %[^\n]",
170 &start_addr, &end_addr, &mmap_offset, binary_path);
171
172 /* ignore special 'fake files' like "[vdso]", "[heap]", "[stack]", */
173 if (binary_path[0] == '[') {
174 continue;
175 }
176
177 if (binary_path[0] == '\0') {
178 continue;
179 }
180
181 if (end_addr < start_addr)
182 perror_msg_and_die("%s: unrecognized maps file format",
183 filename);
184
185 cur_entry = &cache_head[tcp->mmap_cache_size];
186 cur_entry->start_addr = start_addr;
187 cur_entry->end_addr = end_addr;
188 cur_entry->mmap_offset = mmap_offset;
189 cur_entry->binary_filename = strdup(binary_path);
190
Masatake YAMATOb45b7fa2014-04-16 15:33:09 +0900191 dlen = strlen(deleted);
192 blen = strlen(binary_path);
193 if (blen >= dlen && strcmp(binary_path + blen - dlen, deleted) == 0)
194 cur_entry->deleted = true;
195 else
196 cur_entry->deleted = false;
197
Luca Clementi327064b2013-07-23 00:11:35 -0700198 /*
199 * sanity check to make sure that we're storing
200 * non-overlapping regions in ascending order
201 */
202 if (tcp->mmap_cache_size > 0) {
203 prev_entry = &cache_head[tcp->mmap_cache_size - 1];
204 if (prev_entry->start_addr >= cur_entry->start_addr)
205 perror_msg_and_die("Overlaying memory region in %s",
206 filename);
207 if (prev_entry->end_addr > cur_entry->start_addr)
208 perror_msg_and_die("Overlaying memory region in %s",
209 filename);
210 }
211 tcp->mmap_cache_size++;
212
213 /* resize doubling its size */
214 if (tcp->mmap_cache_size >= cur_array_size) {
215 cur_array_size *= 2;
216 cache_head = realloc(cache_head, cur_array_size * sizeof(*cache_head));
217 if (!cache_head)
218 die_out_of_memory();
219 }
220 }
221 fclose(fp);
222 tcp->mmap_cache = cache_head;
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900223 tcp->mmap_cache_generation = mmap_cache_generation;
224
225 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p",
226 "cache-build",
227 tcp->mmap_cache_generation,
228 mmap_cache_generation,
229 tcp, tcp->mmap_cache);
Luca Clementi327064b2013-07-23 00:11:35 -0700230}
231
232/* deleting the cache */
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900233static void
234delete_mmap_cache(struct tcb *tcp, const char *caller)
Luca Clementi327064b2013-07-23 00:11:35 -0700235{
236 unsigned int i;
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900237
238 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s",
239 "cache-delete",
240 tcp->mmap_cache_generation,
241 mmap_cache_generation,
242 tcp, tcp->mmap_cache, caller);
243
Luca Clementi327064b2013-07-23 00:11:35 -0700244 for (i = 0; i < tcp->mmap_cache_size; i++) {
245 free(tcp->mmap_cache[i].binary_filename);
246 tcp->mmap_cache[i].binary_filename = NULL;
247 }
248 free(tcp->mmap_cache);
249 tcp->mmap_cache = NULL;
250 tcp->mmap_cache_size = 0;
251}
252
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900253static bool
254rebuild_cache_if_invalid(struct tcb *tcp, const char *caller)
255{
256 if ((tcp->mmap_cache_generation != mmap_cache_generation)
257 && tcp->mmap_cache)
258 delete_mmap_cache(tcp, caller);
259
260 if (!tcp->mmap_cache)
261 build_mmap_cache(tcp);
262
263 if (!tcp->mmap_cache || !tcp->mmap_cache_size)
264 return false;
265 else
266 return true;
267}
268
269void
270unwind_cache_invalidate(struct tcb* tcp)
271{
Luca Clementif1d73112014-06-09 22:05:38 -0700272#if SUPPORTED_PERSONALITIES > 1
273 if (tcp->currpers != DEFAULT_PERSONALITY) {
274 /* disable strack trace */
275 return;
276 }
277#endif
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900278 mmap_cache_generation++;
279 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p", "increment",
280 tcp->mmap_cache_generation,
281 mmap_cache_generation,
282 tcp,
283 tcp->mmap_cache);
284}
285
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000286static void
287get_symbol_name(unw_cursor_t *cursor, char **name,
288 size_t *size, unw_word_t *offset)
289{
290 for (;;) {
291 int rc = unw_get_proc_name(cursor, *name, *size, offset);
292 if (rc == 0)
293 break;
294 if (rc != -UNW_ENOMEM) {
295 **name = '\0';
296 *offset = 0;
297 break;
298 }
299 *size *= 2;
300 *name = realloc(*name, *size);
301 if (!*name)
302 die_out_of_memory();
303 }
304}
305
306static int
307print_stack_frame(struct tcb *tcp,
308 call_action_fn call_action,
309 error_action_fn error_action,
310 void *data,
311 unw_cursor_t *cursor,
312 char **symbol_name,
313 size_t *symbol_name_size)
314{
315 unw_word_t ip;
316 int lower = 0;
317 int upper = (int) tcp->mmap_cache_size - 1;
318
319 if (unw_get_reg(cursor, UNW_REG_IP, &ip) < 0) {
320 perror_msg("Can't walk the stack of process %d", tcp->pid);
321 return -1;
322 }
323
324 while (lower <= upper) {
325 struct mmap_cache_t *cur_mmap_cache;
326 int mid = (upper + lower) / 2;
327
328 cur_mmap_cache = &tcp->mmap_cache[mid];
329
330 if (ip >= cur_mmap_cache->start_addr &&
331 ip < cur_mmap_cache->end_addr) {
332 unsigned long true_offset;
333 unw_word_t function_offset;
334
335 get_symbol_name(cursor, symbol_name, symbol_name_size,
336 &function_offset);
337 true_offset = ip - cur_mmap_cache->start_addr +
338 cur_mmap_cache->mmap_offset;
339 call_action(data,
340 cur_mmap_cache->binary_filename,
341 *symbol_name,
342 function_offset,
343 true_offset);
344 return 0;
345 }
346 else if (ip < cur_mmap_cache->start_addr)
347 upper = mid - 1;
348 else
349 lower = mid + 1;
350 }
351
352 /*
353 * there is a bug in libunwind >= 1.0
354 * after a set_tid_address syscall
355 * unw_get_reg returns IP == 0
356 */
357 if(ip)
358 error_action(data, "unexpected_backtracing_error", ip);
359 return -1;
360}
361
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900362/*
363 * walking the stack
364 */
365static void
366stacktrace_walk(struct tcb *tcp,
367 call_action_fn call_action,
368 error_action_fn error_action,
369 void *data)
Luca Clementi327064b2013-07-23 00:11:35 -0700370{
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000371 char *symbol_name;
Luca Clementi327064b2013-07-23 00:11:35 -0700372 size_t symbol_name_size = 40;
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000373 unw_cursor_t cursor;
374 int stack_depth;
Luca Clementi327064b2013-07-23 00:11:35 -0700375
376 if (!tcp->mmap_cache)
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900377 error_msg_and_die("bug: mmap_cache is NULL");
378 if (tcp->mmap_cache_size == 0)
379 error_msg_and_die("bug: mmap_cache is empty");
Luca Clementi327064b2013-07-23 00:11:35 -0700380
381 symbol_name = malloc(symbol_name_size);
382 if (!symbol_name)
383 die_out_of_memory();
384
385 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
386 perror_msg_and_die("Can't initiate libunwind");
387
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000388 for (stack_depth = 0; stack_depth < 256; ++stack_depth) {
389 if (print_stack_frame(tcp, call_action, error_action, data,
390 &cursor, &symbol_name, &symbol_name_size) < 0)
Luca Clementi327064b2013-07-23 00:11:35 -0700391 break;
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000392 if (unw_step(&cursor) <= 0)
Luca Clementi327064b2013-07-23 00:11:35 -0700393 break;
Dmitry V. Levin52840ed2014-06-05 22:28:57 +0000394 }
395 if (stack_depth >= 256)
396 error_action(data, "too many stack frames", 0);
397
Luca Clementi327064b2013-07-23 00:11:35 -0700398 free(symbol_name);
399}
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900400
401/*
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900402 * printing an entry in stack to stream or buffer
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900403 */
404/*
405 * we want to keep the format used by backtrace_symbols from the glibc
406 *
407 * ./a.out() [0x40063d]
408 * ./a.out() [0x4006bb]
409 * ./a.out() [0x4006c6]
410 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
411 * ./a.out() [0x400569]
412 */
413#define STACK_ENTRY_SYMBOL_FMT \
414 " > %s(%s+0x%lx) [0x%lx]\n", \
415 binary_filename, \
416 symbol_name, \
Dmitry V. Levin65557112014-06-05 21:44:40 +0000417 (unsigned long) function_offset, \
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900418 true_offset
419#define STACK_ENTRY_NOSYMBOL_FMT \
420 " > %s() [0x%lx]\n", \
421 binary_filename, true_offset
422#define STACK_ENTRY_BUG_FMT \
423 " > BUG IN %s\n"
424#define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
425 " > %s [0x%lx]\n", error, true_offset
426#define STACK_ENTRY_ERROR_FMT \
427 " > %s\n", error
428
429static void
430print_call_cb(void *dummy,
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000431 const char *binary_filename,
432 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000433 unw_word_t function_offset,
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900434 unsigned long true_offset)
435{
436 if (symbol_name)
437 tprintf(STACK_ENTRY_SYMBOL_FMT);
438 else if (binary_filename)
439 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
440 else
441 tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__);
442
443 line_ended();
444}
445
446static void
447print_error_cb(void *dummy,
448 const char *error,
449 unsigned long true_offset)
450{
451 if (true_offset)
452 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
453 else
454 tprintf(STACK_ENTRY_ERROR_FMT);
455
456 line_ended();
457}
458
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900459static char *
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000460sprint_call_or_error(const char *binary_filename,
461 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000462 unw_word_t function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900463 unsigned long true_offset,
464 const char *error)
465{
466 char *output_line = NULL;
467 int n;
468
469 if (symbol_name)
470 n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT);
471 else if (binary_filename)
472 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
473 else if (error)
474 n = true_offset
475 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
476 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
477 else
478 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __FUNCTION__);
479
480 if (n < 0)
481 error_msg_and_die("error in asprintf");
482
483 return output_line;
484}
485
486/*
487 * queue manipulators
488 */
489static void
490queue_put(struct queue_t *queue,
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000491 const char *binary_filename,
492 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000493 unw_word_t function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900494 unsigned long true_offset,
495 const char *error)
496{
497 struct call_t *call;
498
499 call = malloc(sizeof(*call));
500 if (!call)
501 die_out_of_memory();
502
503 call->output_line = sprint_call_or_error(binary_filename,
504 symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000505 function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900506 true_offset,
507 error);
508 call->next = NULL;
509
510 if (!queue->head) {
511 queue->head = call;
512 queue->tail = call;
513 } else {
514 queue->tail->next = call;
515 queue->tail = call;
516 }
517}
518
519static void
520queue_put_call(void *queue,
Dmitry V. Levin806539c2014-06-05 22:37:09 +0000521 const char *binary_filename,
522 const char *symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000523 unw_word_t function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900524 unsigned long true_offset)
525{
526 queue_put(queue,
527 binary_filename,
528 symbol_name,
Dmitry V. Levin65557112014-06-05 21:44:40 +0000529 function_offset,
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900530 true_offset,
531 NULL);
532}
533
534static void
535queue_put_error(void *queue,
536 const char *error,
Dmitry V. Levine4113972014-06-05 14:37:04 +0000537 unsigned long ip)
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900538{
539 queue_put(queue, NULL, NULL, 0, ip, error);
540}
541
542static void
543queue_print(struct queue_t *queue)
544{
545 struct call_t *call, *tmp;
546
547 queue->tail = NULL;
548 call = queue->head;
549 queue->head = NULL;
550 while (call) {
551 tmp = call;
552 call = call->next;
553
554 tprints(tmp->output_line);
555 line_ended();
556
557 free(tmp->output_line);
558 tmp->output_line = NULL;
559 tmp->next = NULL;
560 free(tmp);
561 }
562}
563
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900564/*
565 * printing stack
566 */
567void
568unwind_print_stacktrace(struct tcb* tcp)
569{
Luca Clementif1d73112014-06-09 22:05:38 -0700570#if SUPPORTED_PERSONALITIES > 1
571 if (tcp->currpers != DEFAULT_PERSONALITY) {
572 /* disable strack trace */
573 return;
574 }
575#endif
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900576 if (tcp->queue->head) {
577 DPRINTF("tcp=%p, queue=%p", "queueprint", tcp, tcp->queue->head);
578 queue_print(tcp->queue);
579 }
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900580 else if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900581 DPRINTF("tcp=%p, queue=%p", "stackprint", tcp, tcp->queue->head);
582 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
583 }
584}
585
586/*
587 * capturing stack
588 */
589void
590unwind_capture_stacktrace(struct tcb *tcp)
591{
Luca Clementif1d73112014-06-09 22:05:38 -0700592#if SUPPORTED_PERSONALITIES > 1
593 if (tcp->currpers != DEFAULT_PERSONALITY) {
594 /* disable strack trace */
595 return;
596 }
597#endif
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900598 if (tcp->queue->head)
599 error_msg_and_die("bug: unprinted entries in queue");
600
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900601 if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
602 stacktrace_walk(tcp, queue_put_call, queue_put_error,
603 tcp->queue);
604 DPRINTF("tcp=%p, queue=%p", "captured", tcp, tcp->queue->head);
605 }
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900606}