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