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; |
| 60 | char* binary_filename; |
Masatake YAMATO | b45b7fa | 2014-04-16 15:33:09 +0900 | [diff] [blame] | 61 | bool deleted; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 64 | /* |
| 65 | * Type used in stacktrace walker |
| 66 | */ |
| 67 | typedef void (*call_action_fn)(void *data, |
| 68 | char *binary_filename, |
| 69 | char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 70 | unw_word_t function_offset, |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 71 | unsigned long true_offset); |
| 72 | typedef void (*error_action_fn)(void *data, |
| 73 | const char *error, |
| 74 | unsigned long true_offset); |
| 75 | |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 76 | /* |
| 77 | * Type used in stacktrace capturing |
| 78 | */ |
| 79 | struct call_t { |
| 80 | struct call_t* next; |
| 81 | char *output_line; |
| 82 | }; |
| 83 | |
| 84 | struct queue_t { |
| 85 | struct call_t *tail; |
| 86 | struct call_t *head; |
| 87 | }; |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 88 | |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 89 | static void queue_print(struct queue_t *queue); |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 90 | static void delete_mmap_cache(struct tcb *tcp, const char *caller); |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 91 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 92 | static unw_addr_space_t libunwind_as; |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 93 | static unsigned int mmap_cache_generation; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 94 | |
| 95 | void |
Masatake YAMATO | 6141392 | 2014-04-16 15:33:02 +0900 | [diff] [blame] | 96 | unwind_init(void) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 97 | { |
| 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 YAMATO | a0b4ee7 | 2014-04-16 15:33:10 +0900 | [diff] [blame] | 101 | unw_set_caching_policy(libunwind_as, UNW_CACHE_GLOBAL); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
Masatake YAMATO | 6141392 | 2014-04-16 15:33:02 +0900 | [diff] [blame] | 105 | unwind_tcb_init(struct tcb *tcp) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 106 | { |
| 107 | tcp->libunwind_ui = _UPT_create(tcp->pid); |
| 108 | if (!tcp->libunwind_ui) |
| 109 | die_out_of_memory(); |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 110 | |
| 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
Masatake YAMATO | 6141392 | 2014-04-16 15:33:02 +0900 | [diff] [blame] | 119 | unwind_tcb_fin(struct tcb *tcp) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 120 | { |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 121 | queue_print(tcp->queue); |
| 122 | free(tcp->queue); |
| 123 | tcp->queue = NULL; |
| 124 | |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 125 | delete_mmap_cache(tcp, __FUNCTION__); |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 126 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 127 | _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 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 | { |
| 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 YAMATO | b45b7fa | 2014-04-16 15:33:09 +0900 | [diff] [blame] | 149 | const char *deleted = " (deleted)"; |
| 150 | size_t blen; |
| 151 | size_t dlen; |
| 152 | |
Masatake YAMATO | a0b4ee7 | 2014-04-16 15:33:10 +0900 | [diff] [blame] | 153 | unw_flush_cache (libunwind_as, 0, 0); |
| 154 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 155 | sprintf(filename, "/proc/%d/maps", tcp->pid); |
Dmitry V. Levin | c903c82 | 2014-06-05 15:12:42 +0000 | [diff] [blame] | 156 | fp = fopen_for_input(filename, "r"); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 157 | 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 YAMATO | b45b7fa | 2014-04-16 15:33:09 +0900 | [diff] [blame] | 191 | 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 198 | /* |
| 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 YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 223 | 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* deleting the cache */ |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 233 | static void |
| 234 | delete_mmap_cache(struct tcb *tcp, const char *caller) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 235 | { |
| 236 | unsigned int i; |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 237 | |
| 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 Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 244 | 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 YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 253 | static bool |
| 254 | rebuild_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 | |
| 269 | void |
| 270 | unwind_cache_invalidate(struct tcb* tcp) |
| 271 | { |
Luca Clementi | f1d7311 | 2014-06-09 22:05:38 -0700 | [diff] [blame^] | 272 | #if SUPPORTED_PERSONALITIES > 1 |
| 273 | if (tcp->currpers != DEFAULT_PERSONALITY) { |
| 274 | /* disable strack trace */ |
| 275 | return; |
| 276 | } |
| 277 | #endif |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 278 | 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 | |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 286 | /* |
| 287 | * walking the stack |
| 288 | */ |
| 289 | static void |
| 290 | stacktrace_walk(struct tcb *tcp, |
| 291 | call_action_fn call_action, |
| 292 | error_action_fn error_action, |
| 293 | void *data) |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 294 | { |
| 295 | unw_word_t ip; |
| 296 | unw_cursor_t cursor; |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 297 | unw_word_t function_offset; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 298 | int stack_depth = 0, ret_val; |
| 299 | /* these are used for the binary search through the mmap_chace */ |
Dmitry V. Levin | c12eb11 | 2014-06-05 21:40:43 +0000 | [diff] [blame] | 300 | int lower, upper, mid; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 301 | size_t symbol_name_size = 40; |
| 302 | char * symbol_name; |
| 303 | struct mmap_cache_t* cur_mmap_cache; |
| 304 | unsigned long true_offset; |
Masatake YAMATO | b45b7fa | 2014-04-16 15:33:09 +0900 | [diff] [blame] | 305 | bool berror_expected = false; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 306 | |
| 307 | if (!tcp->mmap_cache) |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 308 | error_msg_and_die("bug: mmap_cache is NULL"); |
| 309 | if (tcp->mmap_cache_size == 0) |
| 310 | error_msg_and_die("bug: mmap_cache is empty"); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 311 | |
| 312 | symbol_name = malloc(symbol_name_size); |
| 313 | if (!symbol_name) |
| 314 | die_out_of_memory(); |
| 315 | |
| 316 | if (unw_init_remote(&cursor, libunwind_as, tcp->libunwind_ui) < 0) |
| 317 | perror_msg_and_die("Can't initiate libunwind"); |
| 318 | |
| 319 | do { |
| 320 | /* looping on the stack frame */ |
| 321 | if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) { |
| 322 | perror_msg("Can't walk the stack of process %d", tcp->pid); |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | lower = 0; |
Dmitry V. Levin | c12eb11 | 2014-06-05 21:40:43 +0000 | [diff] [blame] | 327 | upper = (int) tcp->mmap_cache_size - 1; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 328 | |
| 329 | while (lower <= upper) { |
| 330 | /* find the mmap_cache and print the stack frame */ |
| 331 | mid = (upper + lower) / 2; |
| 332 | cur_mmap_cache = &tcp->mmap_cache[mid]; |
| 333 | |
| 334 | if (ip >= cur_mmap_cache->start_addr && |
| 335 | ip < cur_mmap_cache->end_addr) { |
| 336 | for (;;) { |
| 337 | symbol_name[0] = '\0'; |
| 338 | ret_val = unw_get_proc_name(&cursor, symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 339 | symbol_name_size, &function_offset); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 340 | if (ret_val != -UNW_ENOMEM) |
| 341 | break; |
| 342 | symbol_name_size *= 2; |
| 343 | symbol_name = realloc(symbol_name, symbol_name_size); |
| 344 | if (!symbol_name) |
| 345 | die_out_of_memory(); |
| 346 | } |
| 347 | |
Masatake YAMATO | b45b7fa | 2014-04-16 15:33:09 +0900 | [diff] [blame] | 348 | if (cur_mmap_cache->deleted) |
| 349 | berror_expected = true; |
| 350 | |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 351 | true_offset = ip - cur_mmap_cache->start_addr + |
| 352 | cur_mmap_cache->mmap_offset; |
| 353 | if (symbol_name[0]) { |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 354 | call_action(data, |
| 355 | cur_mmap_cache->binary_filename, |
| 356 | symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 357 | function_offset, |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 358 | true_offset); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 359 | } else { |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 360 | call_action(data, |
| 361 | cur_mmap_cache->binary_filename, |
| 362 | symbol_name, |
| 363 | 0, |
| 364 | true_offset); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 365 | } |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 366 | break; /* stack frame printed */ |
| 367 | } |
| 368 | else if (mid == 0) { |
| 369 | /* |
| 370 | * there is a bug in libunwind >= 1.0 |
| 371 | * after a set_tid_address syscall |
| 372 | * unw_get_reg returns IP == 0 |
| 373 | */ |
| 374 | if(ip) |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 375 | error_action(data, |
| 376 | "backtracing_error", 0); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 377 | goto ret; |
| 378 | } |
| 379 | else if (ip < cur_mmap_cache->start_addr) |
Dmitry V. Levin | c12eb11 | 2014-06-05 21:40:43 +0000 | [diff] [blame] | 380 | upper = mid - 1; |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 381 | else |
| 382 | lower = mid + 1; |
| 383 | |
| 384 | } |
| 385 | if (lower > upper) { |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 386 | error_action(data, |
Masatake YAMATO | b45b7fa | 2014-04-16 15:33:09 +0900 | [diff] [blame] | 387 | berror_expected |
| 388 | ?"expected_backtracing_error" |
| 389 | :"unexpected_backtracing_error", |
| 390 | ip); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 391 | goto ret; |
| 392 | } |
| 393 | |
| 394 | ret_val = unw_step(&cursor); |
| 395 | |
| 396 | if (++stack_depth > 255) { |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 397 | error_action(data, |
| 398 | "too many stack frames", 0); |
Luca Clementi | 327064b | 2013-07-23 00:11:35 -0700 | [diff] [blame] | 399 | break; |
| 400 | } |
| 401 | } while (ret_val > 0); |
| 402 | ret: |
| 403 | free(symbol_name); |
| 404 | } |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 405 | |
| 406 | /* |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 407 | * printing an entry in stack to stream or buffer |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 408 | */ |
| 409 | /* |
| 410 | * we want to keep the format used by backtrace_symbols from the glibc |
| 411 | * |
| 412 | * ./a.out() [0x40063d] |
| 413 | * ./a.out() [0x4006bb] |
| 414 | * ./a.out() [0x4006c6] |
| 415 | * /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d] |
| 416 | * ./a.out() [0x400569] |
| 417 | */ |
| 418 | #define STACK_ENTRY_SYMBOL_FMT \ |
| 419 | " > %s(%s+0x%lx) [0x%lx]\n", \ |
| 420 | binary_filename, \ |
| 421 | symbol_name, \ |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 422 | (unsigned long) function_offset, \ |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 423 | true_offset |
| 424 | #define STACK_ENTRY_NOSYMBOL_FMT \ |
| 425 | " > %s() [0x%lx]\n", \ |
| 426 | binary_filename, true_offset |
| 427 | #define STACK_ENTRY_BUG_FMT \ |
| 428 | " > BUG IN %s\n" |
| 429 | #define STACK_ENTRY_ERROR_WITH_OFFSET_FMT \ |
| 430 | " > %s [0x%lx]\n", error, true_offset |
| 431 | #define STACK_ENTRY_ERROR_FMT \ |
| 432 | " > %s\n", error |
| 433 | |
| 434 | static void |
| 435 | print_call_cb(void *dummy, |
| 436 | char *binary_filename, |
| 437 | char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 438 | unw_word_t function_offset, |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 439 | unsigned long true_offset) |
| 440 | { |
| 441 | if (symbol_name) |
| 442 | tprintf(STACK_ENTRY_SYMBOL_FMT); |
| 443 | else if (binary_filename) |
| 444 | tprintf(STACK_ENTRY_NOSYMBOL_FMT); |
| 445 | else |
| 446 | tprintf(STACK_ENTRY_BUG_FMT, __FUNCTION__); |
| 447 | |
| 448 | line_ended(); |
| 449 | } |
| 450 | |
| 451 | static void |
| 452 | print_error_cb(void *dummy, |
| 453 | const char *error, |
| 454 | unsigned long true_offset) |
| 455 | { |
| 456 | if (true_offset) |
| 457 | tprintf(STACK_ENTRY_ERROR_WITH_OFFSET_FMT); |
| 458 | else |
| 459 | tprintf(STACK_ENTRY_ERROR_FMT); |
| 460 | |
| 461 | line_ended(); |
| 462 | } |
| 463 | |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 464 | static char * |
| 465 | sprint_call_or_error(char *binary_filename, |
| 466 | char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 467 | unw_word_t function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 468 | unsigned long true_offset, |
| 469 | const char *error) |
| 470 | { |
| 471 | char *output_line = NULL; |
| 472 | int n; |
| 473 | |
| 474 | if (symbol_name) |
| 475 | n = asprintf(&output_line, STACK_ENTRY_SYMBOL_FMT); |
| 476 | else if (binary_filename) |
| 477 | n = asprintf(&output_line, STACK_ENTRY_NOSYMBOL_FMT); |
| 478 | else if (error) |
| 479 | n = true_offset |
| 480 | ? asprintf(&output_line, STACK_ENTRY_ERROR_WITH_OFFSET_FMT) |
| 481 | : asprintf(&output_line, STACK_ENTRY_ERROR_FMT); |
| 482 | else |
| 483 | n = asprintf(&output_line, STACK_ENTRY_BUG_FMT, __FUNCTION__); |
| 484 | |
| 485 | if (n < 0) |
| 486 | error_msg_and_die("error in asprintf"); |
| 487 | |
| 488 | return output_line; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * queue manipulators |
| 493 | */ |
| 494 | static void |
| 495 | queue_put(struct queue_t *queue, |
| 496 | char *binary_filename, |
| 497 | char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 498 | unw_word_t function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 499 | unsigned long true_offset, |
| 500 | const char *error) |
| 501 | { |
| 502 | struct call_t *call; |
| 503 | |
| 504 | call = malloc(sizeof(*call)); |
| 505 | if (!call) |
| 506 | die_out_of_memory(); |
| 507 | |
| 508 | call->output_line = sprint_call_or_error(binary_filename, |
| 509 | symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 510 | function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 511 | true_offset, |
| 512 | error); |
| 513 | call->next = NULL; |
| 514 | |
| 515 | if (!queue->head) { |
| 516 | queue->head = call; |
| 517 | queue->tail = call; |
| 518 | } else { |
| 519 | queue->tail->next = call; |
| 520 | queue->tail = call; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | static void |
| 525 | queue_put_call(void *queue, |
| 526 | char *binary_filename, |
| 527 | char *symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 528 | unw_word_t function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 529 | unsigned long true_offset) |
| 530 | { |
| 531 | queue_put(queue, |
| 532 | binary_filename, |
| 533 | symbol_name, |
Dmitry V. Levin | 6555711 | 2014-06-05 21:44:40 +0000 | [diff] [blame] | 534 | function_offset, |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 535 | true_offset, |
| 536 | NULL); |
| 537 | } |
| 538 | |
| 539 | static void |
| 540 | queue_put_error(void *queue, |
| 541 | const char *error, |
Dmitry V. Levin | e411397 | 2014-06-05 14:37:04 +0000 | [diff] [blame] | 542 | unsigned long ip) |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 543 | { |
| 544 | queue_put(queue, NULL, NULL, 0, ip, error); |
| 545 | } |
| 546 | |
| 547 | static void |
| 548 | queue_print(struct queue_t *queue) |
| 549 | { |
| 550 | struct call_t *call, *tmp; |
| 551 | |
| 552 | queue->tail = NULL; |
| 553 | call = queue->head; |
| 554 | queue->head = NULL; |
| 555 | while (call) { |
| 556 | tmp = call; |
| 557 | call = call->next; |
| 558 | |
| 559 | tprints(tmp->output_line); |
| 560 | line_ended(); |
| 561 | |
| 562 | free(tmp->output_line); |
| 563 | tmp->output_line = NULL; |
| 564 | tmp->next = NULL; |
| 565 | free(tmp); |
| 566 | } |
| 567 | } |
| 568 | |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 569 | /* |
| 570 | * printing stack |
| 571 | */ |
| 572 | void |
| 573 | unwind_print_stacktrace(struct tcb* tcp) |
| 574 | { |
Luca Clementi | f1d7311 | 2014-06-09 22:05:38 -0700 | [diff] [blame^] | 575 | #if SUPPORTED_PERSONALITIES > 1 |
| 576 | if (tcp->currpers != DEFAULT_PERSONALITY) { |
| 577 | /* disable strack trace */ |
| 578 | return; |
| 579 | } |
| 580 | #endif |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 581 | if (tcp->queue->head) { |
| 582 | DPRINTF("tcp=%p, queue=%p", "queueprint", tcp, tcp->queue->head); |
| 583 | queue_print(tcp->queue); |
| 584 | } |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 585 | else if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) { |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 586 | DPRINTF("tcp=%p, queue=%p", "stackprint", tcp, tcp->queue->head); |
| 587 | stacktrace_walk(tcp, print_call_cb, print_error_cb, NULL); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * capturing stack |
| 593 | */ |
| 594 | void |
| 595 | unwind_capture_stacktrace(struct tcb *tcp) |
| 596 | { |
Luca Clementi | f1d7311 | 2014-06-09 22:05:38 -0700 | [diff] [blame^] | 597 | #if SUPPORTED_PERSONALITIES > 1 |
| 598 | if (tcp->currpers != DEFAULT_PERSONALITY) { |
| 599 | /* disable strack trace */ |
| 600 | return; |
| 601 | } |
| 602 | #endif |
Masatake YAMATO | f8e39d7 | 2014-04-16 15:33:06 +0900 | [diff] [blame] | 603 | if (tcp->queue->head) |
| 604 | error_msg_and_die("bug: unprinted entries in queue"); |
| 605 | |
Masatake YAMATO | 9bc6561 | 2014-04-16 15:33:07 +0900 | [diff] [blame] | 606 | if (rebuild_cache_if_invalid(tcp, __FUNCTION__)) { |
| 607 | stacktrace_walk(tcp, queue_put_call, queue_put_error, |
| 608 | tcp->queue); |
| 609 | DPRINTF("tcp=%p, queue=%p", "captured", tcp, tcp->queue->head); |
| 610 | } |
Masatake YAMATO | 2d534da | 2014-04-16 15:33:04 +0900 | [diff] [blame] | 611 | } |