blob: 53191be45d00814897fffcb0d56c94a13225734d [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
Masatake YAMATO4e121e52014-04-16 15:33:05 +090031#define DPRINTF(F, A, ...) if (debug_flag) fprintf(stderr, " [unwind(" A ")] " F "\n", __VA_ARGS__)
32
Luca Clementi327064b2013-07-23 00:11:35 -070033/*
34 * Кeep a sorted array of cache entries,
35 * so that we can binary search through it.
36 */
37struct mmap_cache_t {
38 /**
39 * example entry:
40 * 7fabbb09b000-7fabbb09f000 r--p 00179000 fc:00 1180246 /lib/libc-2.11.1.so
41 *
42 * start_addr is 0x7fabbb09b000
43 * end_addr is 0x7fabbb09f000
44 * mmap_offset is 0x179000
45 * binary_filename is "/lib/libc-2.11.1.so"
46 */
47 unsigned long start_addr;
48 unsigned long end_addr;
49 unsigned long mmap_offset;
50 char* binary_filename;
Masatake YAMATOb45b7fa2014-04-16 15:33:09 +090051 bool deleted;
Luca Clementi327064b2013-07-23 00:11:35 -070052};
53
Masatake YAMATO2d534da2014-04-16 15:33:04 +090054/*
55 * Type used in stacktrace walker
56 */
57typedef void (*call_action_fn)(void *data,
58 char *binary_filename,
59 char *symbol_name,
60 unw_word_t function_off_set,
61 unsigned long true_offset);
62typedef void (*error_action_fn)(void *data,
63 const char *error,
64 unsigned long true_offset);
65
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090066/*
67 * Type used in stacktrace capturing
68 */
69struct call_t {
70 struct call_t* next;
71 char *output_line;
72};
73
74struct queue_t {
75 struct call_t *tail;
76 struct call_t *head;
77};
Masatake YAMATO9bc65612014-04-16 15:33:07 +090078
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090079static void queue_print(struct queue_t *queue);
Masatake YAMATO9bc65612014-04-16 15:33:07 +090080static void delete_mmap_cache(struct tcb *tcp, const char *caller);
Masatake YAMATO2d534da2014-04-16 15:33:04 +090081
Luca Clementi327064b2013-07-23 00:11:35 -070082static unw_addr_space_t libunwind_as;
Masatake YAMATO9bc65612014-04-16 15:33:07 +090083static unsigned int mmap_cache_generation;
Luca Clementi327064b2013-07-23 00:11:35 -070084
85void
Masatake YAMATO61413922014-04-16 15:33:02 +090086unwind_init(void)
Luca Clementi327064b2013-07-23 00:11:35 -070087{
88 libunwind_as = unw_create_addr_space(&_UPT_accessors, 0);
89 if (!libunwind_as)
90 error_msg_and_die("failed to create address space for stack tracing");
91}
92
93void
Masatake YAMATO61413922014-04-16 15:33:02 +090094unwind_tcb_init(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -070095{
96 tcp->libunwind_ui = _UPT_create(tcp->pid);
97 if (!tcp->libunwind_ui)
98 die_out_of_memory();
Masatake YAMATOf8e39d72014-04-16 15:33:06 +090099
100 tcp->queue = malloc(sizeof(*tcp->queue));
101 if (!tcp->queue)
102 die_out_of_memory();
103 tcp->queue->head = NULL;
104 tcp->queue->tail = NULL;
Luca Clementi327064b2013-07-23 00:11:35 -0700105}
106
107void
Masatake YAMATO61413922014-04-16 15:33:02 +0900108unwind_tcb_fin(struct tcb *tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700109{
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900110 queue_print(tcp->queue);
111 free(tcp->queue);
112 tcp->queue = NULL;
113
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900114 delete_mmap_cache(tcp, __FUNCTION__);
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900115
Luca Clementi327064b2013-07-23 00:11:35 -0700116 _UPT_destroy(tcp->libunwind_ui);
117 tcp->libunwind_ui = NULL;
118}
119
120/*
121 * caching of /proc/ID/maps for each process to speed up stack tracing
122 *
123 * The cache must be refreshed after some syscall: mmap, mprotect, munmap, execve
124 */
Masatake YAMATOb65042f2014-04-16 15:33:00 +0900125static void
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900126build_mmap_cache(struct tcb* tcp)
Luca Clementi327064b2013-07-23 00:11:35 -0700127{
128 unsigned long start_addr, end_addr, mmap_offset;
129 char filename[sizeof ("/proc/0123456789/maps")];
130 char buffer[PATH_MAX + 80];
131 char binary_path[PATH_MAX];
132 struct mmap_cache_t *cur_entry, *prev_entry;
133 /* start with a small dynamically-allocated array and then expand it */
134 size_t cur_array_size = 10;
135 struct mmap_cache_t *cache_head;
136 FILE *fp;
137
Masatake YAMATOb45b7fa2014-04-16 15:33:09 +0900138 const char *deleted = " (deleted)";
139 size_t blen;
140 size_t dlen;
141
Luca Clementi327064b2013-07-23 00:11:35 -0700142 sprintf(filename, "/proc/%d/maps", tcp->pid);
143 fp = fopen(filename, "r");
144 if (!fp) {
145 perror_msg("fopen: %s", filename);
146 return;
147 }
148
149 cache_head = calloc(cur_array_size, sizeof(*cache_head));
150 if (!cache_head)
151 die_out_of_memory();
152
153 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
154 binary_path[0] = '\0'; // 'reset' it just to be paranoid
155
156 sscanf(buffer, "%lx-%lx %*c%*c%*c%*c %lx %*x:%*x %*d %[^\n]",
157 &start_addr, &end_addr, &mmap_offset, binary_path);
158
159 /* ignore special 'fake files' like "[vdso]", "[heap]", "[stack]", */
160 if (binary_path[0] == '[') {
161 continue;
162 }
163
164 if (binary_path[0] == '\0') {
165 continue;
166 }
167
168 if (end_addr < start_addr)
169 perror_msg_and_die("%s: unrecognized maps file format",
170 filename);
171
172 cur_entry = &cache_head[tcp->mmap_cache_size];
173 cur_entry->start_addr = start_addr;
174 cur_entry->end_addr = end_addr;
175 cur_entry->mmap_offset = mmap_offset;
176 cur_entry->binary_filename = strdup(binary_path);
177
Masatake YAMATOb45b7fa2014-04-16 15:33:09 +0900178 dlen = strlen(deleted);
179 blen = strlen(binary_path);
180 if (blen >= dlen && strcmp(binary_path + blen - dlen, deleted) == 0)
181 cur_entry->deleted = true;
182 else
183 cur_entry->deleted = false;
184
Luca Clementi327064b2013-07-23 00:11:35 -0700185 /*
186 * sanity check to make sure that we're storing
187 * non-overlapping regions in ascending order
188 */
189 if (tcp->mmap_cache_size > 0) {
190 prev_entry = &cache_head[tcp->mmap_cache_size - 1];
191 if (prev_entry->start_addr >= cur_entry->start_addr)
192 perror_msg_and_die("Overlaying memory region in %s",
193 filename);
194 if (prev_entry->end_addr > cur_entry->start_addr)
195 perror_msg_and_die("Overlaying memory region in %s",
196 filename);
197 }
198 tcp->mmap_cache_size++;
199
200 /* resize doubling its size */
201 if (tcp->mmap_cache_size >= cur_array_size) {
202 cur_array_size *= 2;
203 cache_head = realloc(cache_head, cur_array_size * sizeof(*cache_head));
204 if (!cache_head)
205 die_out_of_memory();
206 }
207 }
208 fclose(fp);
209 tcp->mmap_cache = cache_head;
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900210 tcp->mmap_cache_generation = mmap_cache_generation;
211
212 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p",
213 "cache-build",
214 tcp->mmap_cache_generation,
215 mmap_cache_generation,
216 tcp, tcp->mmap_cache);
Luca Clementi327064b2013-07-23 00:11:35 -0700217}
218
219/* deleting the cache */
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900220static void
221delete_mmap_cache(struct tcb *tcp, const char *caller)
Luca Clementi327064b2013-07-23 00:11:35 -0700222{
223 unsigned int i;
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900224
225 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s",
226 "cache-delete",
227 tcp->mmap_cache_generation,
228 mmap_cache_generation,
229 tcp, tcp->mmap_cache, caller);
230
Luca Clementi327064b2013-07-23 00:11:35 -0700231 for (i = 0; i < tcp->mmap_cache_size; i++) {
232 free(tcp->mmap_cache[i].binary_filename);
233 tcp->mmap_cache[i].binary_filename = NULL;
234 }
235 free(tcp->mmap_cache);
236 tcp->mmap_cache = NULL;
237 tcp->mmap_cache_size = 0;
238}
239
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900240static bool
241rebuild_cache_if_invalid(struct tcb *tcp, const char *caller)
242{
243 if ((tcp->mmap_cache_generation != mmap_cache_generation)
244 && tcp->mmap_cache)
245 delete_mmap_cache(tcp, caller);
246
247 if (!tcp->mmap_cache)
248 build_mmap_cache(tcp);
249
250 if (!tcp->mmap_cache || !tcp->mmap_cache_size)
251 return false;
252 else
253 return true;
254}
255
256void
257unwind_cache_invalidate(struct tcb* tcp)
258{
259 mmap_cache_generation++;
260 DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p", "increment",
261 tcp->mmap_cache_generation,
262 mmap_cache_generation,
263 tcp,
264 tcp->mmap_cache);
265}
266
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900267/*
268 * walking the stack
269 */
270static void
271stacktrace_walk(struct tcb *tcp,
272 call_action_fn call_action,
273 error_action_fn error_action,
274 void *data)
Luca Clementi327064b2013-07-23 00:11:35 -0700275{
276 unw_word_t ip;
277 unw_cursor_t cursor;
278 unw_word_t function_off_set;
279 int stack_depth = 0, ret_val;
280 /* these are used for the binary search through the mmap_chace */
281 unsigned int lower, upper, mid;
282 size_t symbol_name_size = 40;
283 char * symbol_name;
284 struct mmap_cache_t* cur_mmap_cache;
285 unsigned long true_offset;
Masatake YAMATOb45b7fa2014-04-16 15:33:09 +0900286 bool berror_expected = false;
Luca Clementi327064b2013-07-23 00:11:35 -0700287
288 if (!tcp->mmap_cache)
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900289 error_msg_and_die("bug: mmap_cache is NULL");
290 if (tcp->mmap_cache_size == 0)
291 error_msg_and_die("bug: mmap_cache is empty");
Luca Clementi327064b2013-07-23 00:11:35 -0700292
293 symbol_name = malloc(symbol_name_size);
294 if (!symbol_name)
295 die_out_of_memory();
296
297 if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0)
298 perror_msg_and_die("Can't initiate libunwind");
299
300 do {
301 /* looping on the stack frame */
302 if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) {
303 perror_msg("Can't walk the stack of process %d", tcp->pid);
304 break;
305 }
306
307 lower = 0;
308 upper = tcp->mmap_cache_size - 1;
309
310 while (lower <= upper) {
311 /* find the mmap_cache and print the stack frame */
312 mid = (upper + lower) / 2;
313 cur_mmap_cache = &tcp->mmap_cache[mid];
314
315 if (ip >= cur_mmap_cache->start_addr &&
316 ip < cur_mmap_cache->end_addr) {
317 for (;;) {
318 symbol_name[0] = '\0';
319 ret_val = unw_get_proc_name(&cursor, symbol_name,
320 symbol_name_size, &function_off_set);
321 if (ret_val != -UNW_ENOMEM)
322 break;
323 symbol_name_size *= 2;
324 symbol_name = realloc(symbol_name, symbol_name_size);
325 if (!symbol_name)
326 die_out_of_memory();
327 }
328
Masatake YAMATOb45b7fa2014-04-16 15:33:09 +0900329 if (cur_mmap_cache->deleted)
330 berror_expected = true;
331
Luca Clementi327064b2013-07-23 00:11:35 -0700332 true_offset = ip - cur_mmap_cache->start_addr +
333 cur_mmap_cache->mmap_offset;
334 if (symbol_name[0]) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900335 call_action(data,
336 cur_mmap_cache->binary_filename,
337 symbol_name,
338 function_off_set,
339 true_offset);
Luca Clementi327064b2013-07-23 00:11:35 -0700340 } else {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900341 call_action(data,
342 cur_mmap_cache->binary_filename,
343 symbol_name,
344 0,
345 true_offset);
Luca Clementi327064b2013-07-23 00:11:35 -0700346 }
Luca Clementi327064b2013-07-23 00:11:35 -0700347 break; /* stack frame printed */
348 }
349 else if (mid == 0) {
350 /*
351 * there is a bug in libunwind >= 1.0
352 * after a set_tid_address syscall
353 * unw_get_reg returns IP == 0
354 */
355 if(ip)
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900356 error_action(data,
357 "backtracing_error", 0);
Luca Clementi327064b2013-07-23 00:11:35 -0700358 goto ret;
359 }
360 else if (ip < cur_mmap_cache->start_addr)
Masatake YAMATOb4a2de82014-04-16 15:32:59 +0900361 upper = mid;
Luca Clementi327064b2013-07-23 00:11:35 -0700362 else
363 lower = mid + 1;
364
365 }
366 if (lower > upper) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900367 error_action(data,
Masatake YAMATOb45b7fa2014-04-16 15:33:09 +0900368 berror_expected
369 ?"expected_backtracing_error"
370 :"unexpected_backtracing_error",
371 ip);
Luca Clementi327064b2013-07-23 00:11:35 -0700372 goto ret;
373 }
374
375 ret_val = unw_step(&cursor);
376
377 if (++stack_depth > 255) {
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900378 error_action(data,
379 "too many stack frames", 0);
Luca Clementi327064b2013-07-23 00:11:35 -0700380 break;
381 }
382 } while (ret_val > 0);
383ret:
384 free(symbol_name);
385}
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900386
387/*
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900388 * printing an entry in stack to stream or buffer
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900389 */
390/*
391 * we want to keep the format used by backtrace_symbols from the glibc
392 *
393 * ./a.out() [0x40063d]
394 * ./a.out() [0x4006bb]
395 * ./a.out() [0x4006c6]
396 * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
397 * ./a.out() [0x400569]
398 */
399#define STACK_ENTRY_SYMBOL_FMT \
400 " > %s(%s+0x%lx) [0x%lx]\n", \
401 binary_filename, \
402 symbol_name, \
403 function_off_set, \
404 true_offset
405#define STACK_ENTRY_NOSYMBOL_FMT \
406 " > %s() [0x%lx]\n", \
407 binary_filename, true_offset
408#define STACK_ENTRY_BUG_FMT \
409 " > BUG IN %s\n"
410#define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \
411 " > %s [0x%lx]\n", error, true_offset
412#define STACK_ENTRY_ERROR_FMT \
413 " > %s\n", error
414
415static void
416print_call_cb(void *dummy,
417 char *binary_filename,
418 char *symbol_name,
419 unw_word_t function_off_set,
420 unsigned long true_offset)
421{
422 if (symbol_name)
423 tprintf(STACK_ENTRY_SYMBOL_FMT);
424 else if (binary_filename)
425 tprintf(STACK_ENTRY_NOSYMBOL_FMT);
426 else
427 tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__);
428
429 line_ended();
430}
431
432static void
433print_error_cb(void *dummy,
434 const char *error,
435 unsigned long true_offset)
436{
437 if (true_offset)
438 tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT);
439 else
440 tprintf(STACK_ENTRY_ERROR_FMT);
441
442 line_ended();
443}
444
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900445static char *
446sprint_call_or_error(char *binary_filename,
447 char *symbol_name,
448 unw_word_t function_off_set,
449 unsigned long true_offset,
450 const char *error)
451{
452 char *output_line = NULL;
453 int n;
454
455 if (symbol_name)
456 n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT);
457 else if (binary_filename)
458 n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT);
459 else if (error)
460 n = true_offset
461 ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT)
462 : asprintf(&output_line, STACK_ENTRY_ERROR_FMT);
463 else
464 n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __FUNCTION__);
465
466 if (n < 0)
467 error_msg_and_die("error in asprintf");
468
469 return output_line;
470}
471
472/*
473 * queue manipulators
474 */
475static void
476queue_put(struct queue_t *queue,
477 char *binary_filename,
478 char *symbol_name,
479 unw_word_t function_off_set,
480 unsigned long true_offset,
481 const char *error)
482{
483 struct call_t *call;
484
485 call = malloc(sizeof(*call));
486 if (!call)
487 die_out_of_memory();
488
489 call->output_line = sprint_call_or_error(binary_filename,
490 symbol_name,
491 function_off_set,
492 true_offset,
493 error);
494 call->next = NULL;
495
496 if (!queue->head) {
497 queue->head = call;
498 queue->tail = call;
499 } else {
500 queue->tail->next = call;
501 queue->tail = call;
502 }
503}
504
505static void
506queue_put_call(void *queue,
507 char *binary_filename,
508 char *symbol_name,
509 unw_word_t function_off_set,
510 unsigned long true_offset)
511{
512 queue_put(queue,
513 binary_filename,
514 symbol_name,
515 function_off_set,
516 true_offset,
517 NULL);
518}
519
520static void
521queue_put_error(void *queue,
522 const char *error,
523 unw_word_t ip)
524{
525 queue_put(queue, NULL, NULL, 0, ip, error);
526}
527
528static void
529queue_print(struct queue_t *queue)
530{
531 struct call_t *call, *tmp;
532
533 queue->tail = NULL;
534 call = queue->head;
535 queue->head = NULL;
536 while (call) {
537 tmp = call;
538 call = call->next;
539
540 tprints(tmp->output_line);
541 line_ended();
542
543 free(tmp->output_line);
544 tmp->output_line = NULL;
545 tmp->next = NULL;
546 free(tmp);
547 }
548}
549
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900550/*
551 * printing stack
552 */
553void
554unwind_print_stacktrace(struct tcb* tcp)
555{
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900556 if (tcp->queue->head) {
557 DPRINTF("tcp=%p, queue=%p", "queueprint", tcp, tcp->queue->head);
558 queue_print(tcp->queue);
559 }
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900560 else if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
Masatake YAMATOf8e39d72014-04-16 15:33:06 +0900561 DPRINTF("tcp=%p, queue=%p", "stackprint", tcp, tcp->queue->head);
562 stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL);
563 }
564}
565
566/*
567 * capturing stack
568 */
569void
570unwind_capture_stacktrace(struct tcb *tcp)
571{
572 if (tcp->queue->head)
573 error_msg_and_die("bug: unprinted entries in queue");
574
Masatake YAMATO9bc65612014-04-16 15:33:07 +0900575 if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) {
576 stacktrace_walk(tcp, queue_put_call, queue_put_error,
577 tcp->queue);
578 DPRINTF("tcp=%p, queue=%p", "captured", tcp, tcp->queue->head);
579 }
Masatake YAMATO2d534da2014-04-16 15:33:04 +0900580}