Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 1 | /* |
| 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. Levin | c903c82 | 2014-06-05 15:12:42 +0000 | [diff] [blame] | 31 | #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 YAMATO | 4e121e5 | 2014-04-16 15:33:05 +0900 | [diff] [blame] | 41 | #define DPRINTF(F, A, ...) if (debug_flag) fprintf(stderr, " [unwind(" A ")] " F "\n", __VA_ARGS__) |
| 42 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Кeep a sorted array of cache entries, |
| 45 | * so that we can binary search through it. |
| 46 | */ |
| 47 | struct 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. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 60 | char *binary_filename; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 63 | /* |
| 64 | * Type used in stacktrace walker |
| 65 | */ |
| 66 | typedef void (*call_action_fn)(void *data, |
Dmitry V. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 67 | const char *binary_filename, |
| 68 | const char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 69 | unw_word_t function_offset, |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 70 | unsigned long true_offset); |
| 71 | typedef void (*error_action_fn)(void *data, |
| 72 | const char *error, |
| 73 | unsigned long true_offset); |
| 74 | |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 75 | /* |
| 76 | * Type used in stacktrace capturing |
| 77 | */ |
| 78 | struct call_t { |
| 79 | struct call_t* next; |
| 80 | char *output_line; |
| 81 | }; |
| 82 | |
| 83 | struct queue_t { |
| 84 | struct call_t *tail; |
| 85 | struct call_t *head; |
| 86 | }; |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 87 | |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 88 | static void queue_print(struct queue_t *queue); |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 89 | static void delete_mmap_cache(struct tcb *tcp, const char *caller); |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 90 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 91 | static unw_addr_space_t libunwind_as; |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 92 | static unsigned int mmap_cache_generation; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 93 | |
| 94 | void |
Masatake YAMATO | 6141392 | 2014-04-16 15:33:02 +0900 | [diff] [blame] | 95 | unwind_init(void) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 96 | { |
| 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 YAMATO | a0b4ee7 | 2014-04-16 15:33:10 +0900 | [diff] [blame] | 100 | unw_set_caching_policy(libunwind_as, UNW_CACHE_GLOBAL); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
Masatake YAMATO | 6141392 | 2014-04-16 15:33:02 +0900 | [diff] [blame] | 104 | unwind_tcb_init(struct tcb *tcp) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 105 | { |
| 106 | tcp->libunwind_ui = _UPT_create(tcp->pid); |
| 107 | if (!tcp->libunwind_ui) |
| 108 | die_out_of_memory(); |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 109 | |
| 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
Masatake YAMATO | 6141392 | 2014-04-16 15:33:02 +0900 | [diff] [blame] | 118 | unwind_tcb_fin(struct tcb *tcp) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 119 | { |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 120 | queue_print(tcp->queue); |
| 121 | free(tcp->queue); |
| 122 | tcp->queue = NULL; |
| 123 | |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 124 | delete_mmap_cache(tcp, __FUNCTION__); |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 125 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 126 | _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 | * |
| 133 | * The cache must be refreshed after some syscall: mmap, mprotect, munmap, execve |
| 134 | */ |
Masatake YAMATO | b65042f | 2014-04-16 15:33:00 +0900 | [diff] [blame] | 135 | static void |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 136 | build_mmap_cache(struct tcb* tcp) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 137 | { |
| 138 | unsigned long start_addr, end_addr, mmap_offset; |
| 139 | char filename[sizeof ("/proc/0123456789/maps")]; |
| 140 | char buffer[PATH_MAX + 80]; |
| 141 | char binary_path[PATH_MAX]; |
| 142 | struct mmap_cache_t *cur_entry, *prev_entry; |
| 143 | /* start with a small dynamically-allocated array and then expand it */ |
| 144 | size_t cur_array_size = 10; |
| 145 | struct mmap_cache_t *cache_head; |
| 146 | FILE *fp; |
| 147 | |
Masatake YAMATO | a0b4ee7 | 2014-04-16 15:33:10 +0900 | [diff] [blame] | 148 | unw_flush_cache (libunwind_as, 0, 0); |
| 149 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 150 | sprintf(filename, "/proc/%d/maps", tcp->pid); |
Dmitry V. Levin | c903c82 | 2014-06-05 15:12:42 +0000 | [diff] [blame] | 151 | fp = fopen_for_input(filename, "r"); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 152 | if (!fp) { |
| 153 | perror_msg("fopen: %s", filename); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | cache_head = calloc(cur_array_size, sizeof(*cache_head)); |
| 158 | if (!cache_head) |
| 159 | die_out_of_memory(); |
| 160 | |
| 161 | while (fgets(buffer, sizeof(buffer), fp) != NULL) { |
| 162 | binary_path[0] = '\0'; // 'reset' it just to be paranoid |
| 163 | |
| 164 | sscanf(buffer, "%lx-%lx %*c%*c%*c%*c %lx %*x:%*x %*d %[^\n]", |
| 165 | &start_addr, &end_addr, &mmap_offset, binary_path); |
| 166 | |
| 167 | /* ignore special 'fake files' like "[vdso]", "[heap]", "[stack]", */ |
| 168 | if (binary_path[0] == '[') { |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | if (binary_path[0] == '\0') { |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | if (end_addr < start_addr) |
| 177 | perror_msg_and_die("%s: unrecognized maps file format", |
| 178 | filename); |
| 179 | |
| 180 | cur_entry = &cache_head[tcp->mmap_cache_size]; |
| 181 | cur_entry->start_addr = start_addr; |
| 182 | cur_entry->end_addr = end_addr; |
| 183 | cur_entry->mmap_offset = mmap_offset; |
| 184 | cur_entry->binary_filename = strdup(binary_path); |
| 185 | |
| 186 | /* |
| 187 | * sanity check to make sure that we're storing |
| 188 | * non-overlapping regions in ascending order |
| 189 | */ |
| 190 | if (tcp->mmap_cache_size > 0) { |
| 191 | prev_entry = &cache_head[tcp->mmap_cache_size - 1]; |
| 192 | if (prev_entry->start_addr >= cur_entry->start_addr) |
| 193 | perror_msg_and_die("Overlaying memory region in %s", |
| 194 | filename); |
| 195 | if (prev_entry->end_addr > cur_entry->start_addr) |
| 196 | perror_msg_and_die("Overlaying memory region in %s", |
| 197 | filename); |
| 198 | } |
| 199 | tcp->mmap_cache_size++; |
| 200 | |
| 201 | /* resize doubling its size */ |
| 202 | if (tcp->mmap_cache_size >= cur_array_size) { |
| 203 | cur_array_size *= 2; |
| 204 | cache_head = realloc(cache_head, cur_array_size * sizeof(*cache_head)); |
| 205 | if (!cache_head) |
| 206 | die_out_of_memory(); |
| 207 | } |
| 208 | } |
| 209 | fclose(fp); |
| 210 | tcp->mmap_cache = cache_head; |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 211 | tcp->mmap_cache_generation = mmap_cache_generation; |
| 212 | |
| 213 | DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p", |
| 214 | "cache-build", |
| 215 | tcp->mmap_cache_generation, |
| 216 | mmap_cache_generation, |
| 217 | tcp, tcp->mmap_cache); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* deleting the cache */ |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 221 | static void |
| 222 | delete_mmap_cache(struct tcb *tcp, const char *caller) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 223 | { |
| 224 | unsigned int i; |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 225 | |
| 226 | DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p, caller=%s", |
| 227 | "cache-delete", |
| 228 | tcp->mmap_cache_generation, |
| 229 | mmap_cache_generation, |
| 230 | tcp, tcp->mmap_cache, caller); |
| 231 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 232 | for (i = 0; i < tcp->mmap_cache_size; i++) { |
| 233 | free(tcp->mmap_cache[i].binary_filename); |
| 234 | tcp->mmap_cache[i].binary_filename = NULL; |
| 235 | } |
| 236 | free(tcp->mmap_cache); |
| 237 | tcp->mmap_cache = NULL; |
| 238 | tcp->mmap_cache_size = 0; |
| 239 | } |
| 240 | |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 241 | static bool |
| 242 | rebuild_cache_if_invalid(struct tcb *tcp, const char *caller) |
| 243 | { |
| 244 | if ((tcp->mmap_cache_generation != mmap_cache_generation) |
| 245 | && tcp->mmap_cache) |
| 246 | delete_mmap_cache(tcp, caller); |
| 247 | |
| 248 | if (!tcp->mmap_cache) |
| 249 | build_mmap_cache(tcp); |
| 250 | |
| 251 | if (!tcp->mmap_cache || !tcp->mmap_cache_size) |
| 252 | return false; |
| 253 | else |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | unwind_cache_invalidate(struct tcb* tcp) |
| 259 | { |
Luca Clementi | f1d7311 | 2014-06-09 22:05:38 -0700 | [diff] [blame] | 260 | #if SUPPORTED_PERSONALITIES > 1 |
| 261 | if (tcp->currpers != DEFAULT_PERSONALITY) { |
| 262 | /* disable strack trace */ |
| 263 | return; |
| 264 | } |
| 265 | #endif |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 266 | mmap_cache_generation++; |
| 267 | DPRINTF("tgen=%u, ggen=%u, tcp=%p, cache=%p", "increment", |
| 268 | tcp->mmap_cache_generation, |
| 269 | mmap_cache_generation, |
| 270 | tcp, |
| 271 | tcp->mmap_cache); |
| 272 | } |
| 273 | |
Dmitry V. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 274 | static void |
| 275 | get_symbol_name(unw_cursor_t *cursor, char **name, |
| 276 | size_t *size, unw_word_t *offset) |
| 277 | { |
| 278 | for (;;) { |
| 279 | int rc = unw_get_proc_name(cursor, *name, *size, offset); |
| 280 | if (rc == 0) |
| 281 | break; |
| 282 | if (rc != -UNW_ENOMEM) { |
| 283 | **name = '\0'; |
| 284 | *offset = 0; |
| 285 | break; |
| 286 | } |
| 287 | *size *= 2; |
| 288 | *name = realloc(*name, *size); |
| 289 | if (!*name) |
| 290 | die_out_of_memory(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static int |
| 295 | print_stack_frame(struct tcb *tcp, |
| 296 | call_action_fn call_action, |
| 297 | error_action_fn error_action, |
| 298 | void *data, |
| 299 | unw_cursor_t *cursor, |
| 300 | char **symbol_name, |
| 301 | size_t *symbol_name_size) |
| 302 | { |
| 303 | unw_word_t ip; |
| 304 | int lower = 0; |
| 305 | int upper = (int) tcp->mmap_cache_size - 1; |
| 306 | |
| 307 | if (unw_get_reg(cursor, UNW_REG_IP, &ip) < 0) { |
| 308 | perror_msg("Can't walk the stack of process %d", tcp->pid); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | while (lower <= upper) { |
| 313 | struct mmap_cache_t *cur_mmap_cache; |
| 314 | int mid = (upper + lower) / 2; |
| 315 | |
| 316 | cur_mmap_cache = &tcp->mmap_cache[mid]; |
| 317 | |
| 318 | if (ip >= cur_mmap_cache->start_addr && |
| 319 | ip < cur_mmap_cache->end_addr) { |
| 320 | unsigned long true_offset; |
| 321 | unw_word_t function_offset; |
| 322 | |
| 323 | get_symbol_name(cursor, symbol_name, symbol_name_size, |
| 324 | &function_offset); |
| 325 | true_offset = ip - cur_mmap_cache->start_addr + |
| 326 | cur_mmap_cache->mmap_offset; |
| 327 | call_action(data, |
| 328 | cur_mmap_cache->binary_filename, |
| 329 | *symbol_name, |
| 330 | function_offset, |
| 331 | true_offset); |
| 332 | return 0; |
| 333 | } |
| 334 | else if (ip < cur_mmap_cache->start_addr) |
| 335 | upper = mid - 1; |
| 336 | else |
| 337 | lower = mid + 1; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * there is a bug in libunwind >= 1.0 |
| 342 | * after a set_tid_address syscall |
| 343 | * unw_get_reg returns IP == 0 |
| 344 | */ |
| 345 | if(ip) |
| 346 | error_action(data, "unexpected_backtracing_error", ip); |
| 347 | return -1; |
| 348 | } |
| 349 | |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 350 | /* |
| 351 | * walking the stack |
| 352 | */ |
| 353 | static void |
| 354 | stacktrace_walk(struct tcb *tcp, |
| 355 | call_action_fn call_action, |
| 356 | error_action_fn error_action, |
| 357 | void *data) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 358 | { |
Dmitry V. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 359 | char *symbol_name; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 360 | size_t symbol_name_size = 40; |
Dmitry V. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 361 | unw_cursor_t cursor; |
| 362 | int stack_depth; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 363 | |
| 364 | if (!tcp->mmap_cache) |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 365 | error_msg_and_die("bug: mmap_cache is NULL"); |
| 366 | if (tcp->mmap_cache_size == 0) |
| 367 | error_msg_and_die("bug: mmap_cache is empty"); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 368 | |
| 369 | symbol_name = malloc(symbol_name_size); |
| 370 | if (!symbol_name) |
| 371 | die_out_of_memory(); |
| 372 | |
| 373 | if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0) |
| 374 | perror_msg_and_die("Can't initiate libunwind"); |
| 375 | |
Dmitry V. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 376 | for (stack_depth = 0; stack_depth < 256; ++stack_depth) { |
| 377 | if (print_stack_frame(tcp, call_action, error_action, data, |
| 378 | &cursor, &symbol_name, &symbol_name_size) < 0) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 379 | break; |
Dmitry V. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 380 | if (unw_step(&cursor) <= 0) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 381 | break; |
Dmitry V. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 382 | } |
| 383 | if (stack_depth >= 256) |
| 384 | error_action(data, "too many stack frames", 0); |
| 385 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 386 | free(symbol_name); |
| 387 | } |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 388 | |
| 389 | /* |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 390 | * printing an entry in stack to stream or buffer |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 391 | */ |
| 392 | /* |
| 393 | * we want to keep the format used by backtrace_symbols from the glibc |
| 394 | * |
| 395 | * ./a.out() [0x40063d] |
| 396 | * ./a.out() [0x4006bb] |
| 397 | * ./a.out() [0x4006c6] |
| 398 | * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d] |
| 399 | * ./a.out() [0x400569] |
| 400 | */ |
| 401 | #define STACK_ENTRY_SYMBOL_FMT \ |
| 402 | " > %s(%s+0x%lx) [0x%lx]\n", \ |
| 403 | binary_filename, \ |
| 404 | symbol_name, \ |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 405 | (unsigned long) function_offset, \ |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 406 | true_offset |
| 407 | #define STACK_ENTRY_NOSYMBOL_FMT \ |
| 408 | " > %s() [0x%lx]\n", \ |
| 409 | binary_filename, true_offset |
| 410 | #define STACK_ENTRY_BUG_FMT \ |
| 411 | " > BUG IN %s\n" |
| 412 | #define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \ |
| 413 | " > %s [0x%lx]\n", error, true_offset |
| 414 | #define STACK_ENTRY_ERROR_FMT \ |
| 415 | " > %s\n", error |
| 416 | |
| 417 | static void |
| 418 | print_call_cb(void *dummy, |
Dmitry V. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 419 | const char *binary_filename, |
| 420 | const char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 421 | unw_word_t function_offset, |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 422 | unsigned long true_offset) |
| 423 | { |
| 424 | if (symbol_name) |
| 425 | tprintf(STACK_ENTRY_SYMBOL_FMT); |
| 426 | else if (binary_filename) |
| 427 | tprintf(STACK_ENTRY_NOSYMBOL_FMT); |
| 428 | else |
| 429 | tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__); |
| 430 | |
| 431 | line_ended(); |
| 432 | } |
| 433 | |
| 434 | static void |
| 435 | print_error_cb(void *dummy, |
| 436 | const char *error, |
| 437 | unsigned long true_offset) |
| 438 | { |
| 439 | if (true_offset) |
| 440 | tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT); |
| 441 | else |
| 442 | tprintf(STACK_ENTRY_ERROR_FMT); |
| 443 | |
| 444 | line_ended(); |
| 445 | } |
| 446 | |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 447 | static char * |
Dmitry V. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 448 | sprint_call_or_error(const char *binary_filename, |
| 449 | const char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 450 | unw_word_t function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 451 | unsigned long true_offset, |
| 452 | const char *error) |
| 453 | { |
| 454 | char *output_line = NULL; |
| 455 | int n; |
| 456 | |
| 457 | if (symbol_name) |
| 458 | n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT); |
| 459 | else if (binary_filename) |
| 460 | n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT); |
| 461 | else if (error) |
| 462 | n = true_offset |
| 463 | ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT) |
| 464 | : asprintf(&output_line, STACK_ENTRY_ERROR_FMT); |
| 465 | else |
| 466 | n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __FUNCTION__); |
| 467 | |
| 468 | if (n < 0) |
| 469 | error_msg_and_die("error in asprintf"); |
| 470 | |
| 471 | return output_line; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * queue manipulators |
| 476 | */ |
| 477 | static void |
| 478 | queue_put(struct queue_t *queue, |
Dmitry V. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 479 | const char *binary_filename, |
| 480 | const char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 481 | unw_word_t function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 482 | unsigned long true_offset, |
| 483 | const char *error) |
| 484 | { |
| 485 | struct call_t *call; |
| 486 | |
| 487 | call = malloc(sizeof(*call)); |
| 488 | if (!call) |
| 489 | die_out_of_memory(); |
| 490 | |
| 491 | call->output_line = sprint_call_or_error(binary_filename, |
| 492 | symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 493 | function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 494 | true_offset, |
| 495 | error); |
| 496 | call->next = NULL; |
| 497 | |
| 498 | if (!queue->head) { |
| 499 | queue->head = call; |
| 500 | queue->tail = call; |
| 501 | } else { |
| 502 | queue->tail->next = call; |
| 503 | queue->tail = call; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | static void |
| 508 | queue_put_call(void *queue, |
Dmitry V. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 509 | const char *binary_filename, |
| 510 | const char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 511 | unw_word_t function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 512 | unsigned long true_offset) |
| 513 | { |
| 514 | queue_put(queue, |
| 515 | binary_filename, |
| 516 | symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 517 | function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 518 | true_offset, |
| 519 | NULL); |
| 520 | } |
| 521 | |
| 522 | static void |
| 523 | queue_put_error(void *queue, |
| 524 | const char *error, |
Dmitry V. Levin | e411397 | 2014-06-05 14:37:04 +0000 | [diff] [blame] | 525 | unsigned long ip) |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 526 | { |
| 527 | queue_put(queue, NULL, NULL, 0, ip, error); |
| 528 | } |
| 529 | |
| 530 | static void |
| 531 | queue_print(struct queue_t *queue) |
| 532 | { |
| 533 | struct call_t *call, *tmp; |
| 534 | |
| 535 | queue->tail = NULL; |
| 536 | call = queue->head; |
| 537 | queue->head = NULL; |
| 538 | while (call) { |
| 539 | tmp = call; |
| 540 | call = call->next; |
| 541 | |
| 542 | tprints(tmp->output_line); |
| 543 | line_ended(); |
| 544 | |
| 545 | free(tmp->output_line); |
| 546 | tmp->output_line = NULL; |
| 547 | tmp->next = NULL; |
| 548 | free(tmp); |
| 549 | } |
| 550 | } |
| 551 | |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 552 | /* |
| 553 | * printing stack |
| 554 | */ |
| 555 | void |
| 556 | unwind_print_stacktrace(struct tcb* tcp) |
| 557 | { |
Luca Clementi | f1d7311 | 2014-06-09 22:05:38 -0700 | [diff] [blame] | 558 | #if SUPPORTED_PERSONALITIES > 1 |
| 559 | if (tcp->currpers != DEFAULT_PERSONALITY) { |
| 560 | /* disable strack trace */ |
| 561 | return; |
| 562 | } |
| 563 | #endif |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 564 | if (tcp->queue->head) { |
| 565 | DPRINTF("tcp=%p, queue=%p", "queueprint", tcp, tcp->queue->head); |
| 566 | queue_print(tcp->queue); |
| 567 | } |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 568 | else if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) { |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 569 | DPRINTF("tcp=%p, queue=%p", "stackprint", tcp, tcp->queue->head); |
| 570 | stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * capturing stack |
| 576 | */ |
| 577 | void |
| 578 | unwind_capture_stacktrace(struct tcb *tcp) |
| 579 | { |
Luca Clementi | f1d7311 | 2014-06-09 22:05:38 -0700 | [diff] [blame] | 580 | #if SUPPORTED_PERSONALITIES > 1 |
| 581 | if (tcp->currpers != DEFAULT_PERSONALITY) { |
| 582 | /* disable strack trace */ |
| 583 | return; |
| 584 | } |
| 585 | #endif |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 586 | if (tcp->queue->head) |
| 587 | error_msg_and_die("bug: unprinted entries in queue"); |
| 588 | |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 589 | if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) { |
| 590 | stacktrace_walk(tcp, queue_put_call, queue_put_error, |
| 591 | tcp->queue); |
| 592 | DPRINTF("tcp=%p, queue=%p", "captured", tcp, tcp->queue->head); |
| 593 | } |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 594 | } |