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: |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 50 | * 7fabbb09b000-7fabbb09f000 r-xp 00179000 fc:00 1180246 /lib/libc-2.11.1.so |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 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 | * |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 133 | * The cache must be refreshed after syscalls that affect memory mappings, |
| 134 | * e.g. mmap, mprotect, munmap, execve. |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 135 | */ |
Masatake YAMATO | b65042f | 2014-04-16 15:33:00 +0900 | [diff] [blame] | 136 | static void |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 137 | build_mmap_cache(struct tcb* tcp) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 138 | { |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 139 | FILE *fp; |
| 140 | struct mmap_cache_t *cache_head; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 141 | /* start with a small dynamically-allocated array and then expand it */ |
| 142 | size_t cur_array_size = 10; |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 143 | char filename[sizeof("/proc/4294967296/maps")]; |
| 144 | char buffer[PATH_MAX + 80]; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 145 | |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 146 | unw_flush_cache(libunwind_as, 0, 0); |
Masatake YAMATO | a0b4ee7 | 2014-04-16 15:33:10 +0900 | [diff] [blame] | 147 | |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 148 | sprintf(filename, "/proc/%u/maps", tcp->pid); |
Dmitry V. Levin | c903c82 | 2014-06-05 15:12:42 +0000 | [diff] [blame] | 149 | fp = fopen_for_input(filename, "r"); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 150 | 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. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 160 | struct mmap_cache_t *entry; |
| 161 | unsigned long start_addr, end_addr, mmap_offset; |
| 162 | char binary_path[PATH_MAX]; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 163 | |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 164 | 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 167 | continue; |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 168 | |
| 169 | if (end_addr < start_addr) { |
| 170 | error_msg("%s: unrecognized file format", filename); |
| 171 | break; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 174 | /* |
| 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. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 179 | entry = &cache_head[tcp->mmap_cache_size - 1]; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 180 | |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 181 | 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 194 | if (tcp->mmap_cache_size >= cur_array_size) { |
| 195 | cur_array_size *= 2; |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 196 | cache_head = realloc(cache_head, |
| 197 | cur_array_size * sizeof(*cache_head)); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 198 | if (!cache_head) |
| 199 | die_out_of_memory(); |
| 200 | } |
Dmitry V. Levin | 9a349c7 | 2014-06-12 18:01:45 +0400 | [diff] [blame^] | 201 | |
| 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 210 | } |
| 211 | fclose(fp); |
| 212 | tcp->mmap_cache = cache_head; |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 213 | 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* deleting the cache */ |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 223 | static void |
| 224 | delete_mmap_cache(struct tcb *tcp, const char *caller) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 225 | { |
| 226 | unsigned int i; |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 227 | |
| 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 234 | 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 YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 243 | static bool |
| 244 | rebuild_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 | |
| 259 | void |
| 260 | unwind_cache_invalidate(struct tcb* tcp) |
| 261 | { |
Luca Clementi | f1d7311 | 2014-06-09 22:05:38 -0700 | [diff] [blame] | 262 | #if SUPPORTED_PERSONALITIES > 1 |
| 263 | if (tcp->currpers != DEFAULT_PERSONALITY) { |
| 264 | /* disable strack trace */ |
| 265 | return; |
| 266 | } |
| 267 | #endif |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 268 | 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. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 276 | static void |
| 277 | get_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 | |
| 296 | static int |
| 297 | print_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 YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 352 | /* |
| 353 | * walking the stack |
| 354 | */ |
| 355 | static void |
| 356 | stacktrace_walk(struct tcb *tcp, |
| 357 | call_action_fn call_action, |
| 358 | error_action_fn error_action, |
| 359 | void *data) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 360 | { |
Dmitry V. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 361 | char *symbol_name; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 362 | size_t symbol_name_size = 40; |
Dmitry V. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 363 | unw_cursor_t cursor; |
| 364 | int stack_depth; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 365 | |
| 366 | if (!tcp->mmap_cache) |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 367 | 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 370 | |
| 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. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 378 | 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 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 | if (unw_step(&cursor) <= 0) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 383 | break; |
Dmitry V. Levin | 52840ed | 2014-06-05 22:28:57 +0000 | [diff] [blame] | 384 | } |
| 385 | if (stack_depth >= 256) |
| 386 | error_action(data, "too many stack frames", 0); |
| 387 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 388 | free(symbol_name); |
| 389 | } |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 390 | |
| 391 | /* |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 392 | * printing an entry in stack to stream or buffer |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 393 | */ |
| 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. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 407 | (unsigned long) function_offset, \ |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 408 | 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 | |
| 419 | static void |
| 420 | print_call_cb(void *dummy, |
Dmitry V. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 421 | const char *binary_filename, |
| 422 | const char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 423 | unw_word_t function_offset, |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 424 | 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 | |
| 436 | static void |
| 437 | print_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 YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 449 | static char * |
Dmitry V. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 450 | sprint_call_or_error(const char *binary_filename, |
| 451 | const char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 452 | unw_word_t function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 453 | 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 | */ |
| 479 | static void |
| 480 | queue_put(struct queue_t *queue, |
Dmitry V. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 481 | const char *binary_filename, |
| 482 | const char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 483 | unw_word_t function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 484 | 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. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 495 | function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 496 | 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 | |
| 509 | static void |
| 510 | queue_put_call(void *queue, |
Dmitry V. Levin | 806539c | 2014-06-05 22:37:09 +0000 | [diff] [blame] | 511 | const char *binary_filename, |
| 512 | const char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 513 | unw_word_t function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 514 | unsigned long true_offset) |
| 515 | { |
| 516 | queue_put(queue, |
| 517 | binary_filename, |
| 518 | symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 519 | function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 520 | true_offset, |
| 521 | NULL); |
| 522 | } |
| 523 | |
| 524 | static void |
| 525 | queue_put_error(void *queue, |
| 526 | const char *error, |
Dmitry V. Levin | e411397 | 2014-06-05 14:37:04 +0000 | [diff] [blame] | 527 | unsigned long ip) |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 528 | { |
| 529 | queue_put(queue, NULL, NULL, 0, ip, error); |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | queue_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 YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 554 | /* |
| 555 | * printing stack |
| 556 | */ |
| 557 | void |
| 558 | unwind_print_stacktrace(struct tcb* tcp) |
| 559 | { |
Luca Clementi | f1d7311 | 2014-06-09 22:05:38 -0700 | [diff] [blame] | 560 | #if SUPPORTED_PERSONALITIES > 1 |
| 561 | if (tcp->currpers != DEFAULT_PERSONALITY) { |
| 562 | /* disable strack trace */ |
| 563 | return; |
| 564 | } |
| 565 | #endif |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 566 | if (tcp->queue->head) { |
| 567 | DPRINTF("tcp=%p, queue=%p", "queueprint", tcp, tcp->queue->head); |
| 568 | queue_print(tcp->queue); |
| 569 | } |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 570 | else if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) { |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 571 | 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 | */ |
| 579 | void |
| 580 | unwind_capture_stacktrace(struct tcb *tcp) |
| 581 | { |
Luca Clementi | f1d7311 | 2014-06-09 22:05:38 -0700 | [diff] [blame] | 582 | #if SUPPORTED_PERSONALITIES > 1 |
| 583 | if (tcp->currpers != DEFAULT_PERSONALITY) { |
| 584 | /* disable strack trace */ |
| 585 | return; |
| 586 | } |
| 587 | #endif |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 588 | if (tcp->queue->head) |
| 589 | error_msg_and_die("bug: unprinted entries in queue"); |
| 590 | |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 591 | 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 YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 596 | } |