Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "Corkscrew" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include "backtrace-arch.h" |
| 21 | #include "backtrace-helper.h" |
| 22 | #include "ptrace-arch.h" |
| 23 | #include <corkscrew/map_info.h> |
| 24 | #include <corkscrew/symbol_table.h> |
| 25 | #include <corkscrew/ptrace.h> |
| 26 | #include <corkscrew/demangle.h> |
| 27 | |
| 28 | #include <unistd.h> |
| 29 | #include <signal.h> |
| 30 | #include <pthread.h> |
| 31 | #include <unwind.h> |
| 32 | #include <sys/exec_elf.h> |
| 33 | #include <cutils/log.h> |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 34 | #include <cutils/atomic.h> |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 35 | |
| 36 | #if HAVE_DLADDR |
| 37 | #include <dlfcn.h> |
| 38 | #endif |
| 39 | |
| 40 | typedef struct { |
| 41 | backtrace_frame_t* backtrace; |
| 42 | size_t ignore_depth; |
| 43 | size_t max_depth; |
| 44 | size_t ignored_frames; |
| 45 | size_t returned_frames; |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 46 | memory_t memory; |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 47 | } backtrace_state_t; |
| 48 | |
| 49 | static _Unwind_Reason_Code unwind_backtrace_callback(struct _Unwind_Context* context, void* arg) { |
| 50 | backtrace_state_t* state = (backtrace_state_t*)arg; |
| 51 | uintptr_t pc = _Unwind_GetIP(context); |
| 52 | if (pc) { |
| 53 | // TODO: Get information about the stack layout from the _Unwind_Context. |
| 54 | // This will require a new architecture-specific function to query |
| 55 | // the appropriate registers. Current callers of unwind_backtrace |
| 56 | // don't need this information, so we won't bother collecting it just yet. |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 57 | add_backtrace_entry(rewind_pc_arch(&state->memory, pc), state->backtrace, |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 58 | state->ignore_depth, state->max_depth, |
| 59 | &state->ignored_frames, &state->returned_frames); |
| 60 | } |
| 61 | return state->returned_frames < state->max_depth ? _URC_NO_REASON : _URC_END_OF_STACK; |
| 62 | } |
| 63 | |
| 64 | ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 65 | ALOGV("Unwinding current thread %d.", gettid()); |
| 66 | |
| 67 | map_info_t* milist = acquire_my_map_info_list(); |
| 68 | |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 69 | backtrace_state_t state; |
| 70 | state.backtrace = backtrace; |
| 71 | state.ignore_depth = ignore_depth; |
| 72 | state.max_depth = max_depth; |
| 73 | state.ignored_frames = 0; |
| 74 | state.returned_frames = 0; |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 75 | init_memory(&state.memory, milist); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 76 | |
| 77 | _Unwind_Reason_Code rc =_Unwind_Backtrace(unwind_backtrace_callback, &state); |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 78 | |
| 79 | release_my_map_info_list(milist); |
| 80 | |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 81 | if (state.returned_frames) { |
| 82 | return state.returned_frames; |
| 83 | } |
| 84 | return rc == _URC_END_OF_STACK ? 0 : -1; |
| 85 | } |
| 86 | |
| 87 | #ifdef CORKSCREW_HAVE_ARCH |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 88 | static const int32_t STATE_DUMPING = -1; |
| 89 | static const int32_t STATE_DONE = -2; |
| 90 | static const int32_t STATE_CANCEL = -3; |
| 91 | |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 92 | static pthread_mutex_t g_unwind_signal_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 93 | static volatile struct { |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 94 | int32_t tid_state; |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 95 | const map_info_t* map_info_list; |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 96 | backtrace_frame_t* backtrace; |
| 97 | size_t ignore_depth; |
| 98 | size_t max_depth; |
| 99 | size_t returned_frames; |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 100 | } g_unwind_signal_state; |
| 101 | |
| 102 | static void unwind_backtrace_thread_signal_handler(int n, siginfo_t* siginfo, void* sigcontext) { |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 103 | if (!android_atomic_acquire_cas(gettid(), STATE_DUMPING, &g_unwind_signal_state.tid_state)) { |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 104 | g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch( |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 105 | siginfo, sigcontext, |
| 106 | g_unwind_signal_state.map_info_list, |
| 107 | g_unwind_signal_state.backtrace, |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 108 | g_unwind_signal_state.ignore_depth, |
| 109 | g_unwind_signal_state.max_depth); |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 110 | android_atomic_release_store(STATE_DONE, &g_unwind_signal_state.tid_state); |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 111 | } else { |
| 112 | ALOGV("Received spurious SIGURG on thread %d that was intended for thread %d.", |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 113 | gettid(), android_atomic_acquire_load(&g_unwind_signal_state.tid_state)); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | #endif |
| 117 | |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 118 | extern int tgkill(int tgid, int tid, int sig); |
| 119 | |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 120 | ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace, |
| 121 | size_t ignore_depth, size_t max_depth) { |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 122 | if (tid == gettid()) { |
| 123 | return unwind_backtrace(backtrace, ignore_depth + 1, max_depth); |
| 124 | } |
| 125 | |
| 126 | ALOGV("Unwinding thread %d from thread %d.", tid, gettid()); |
| 127 | |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 128 | #ifdef CORKSCREW_HAVE_ARCH |
| 129 | struct sigaction act; |
| 130 | struct sigaction oact; |
| 131 | memset(&act, 0, sizeof(act)); |
| 132 | act.sa_sigaction = unwind_backtrace_thread_signal_handler; |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 133 | act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 134 | sigemptyset(&act.sa_mask); |
| 135 | |
| 136 | pthread_mutex_lock(&g_unwind_signal_mutex); |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 137 | map_info_t* milist = acquire_my_map_info_list(); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 138 | |
| 139 | ssize_t frames = -1; |
| 140 | if (!sigaction(SIGURG, &act, &oact)) { |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 141 | g_unwind_signal_state.map_info_list = milist; |
| 142 | g_unwind_signal_state.backtrace = backtrace; |
| 143 | g_unwind_signal_state.ignore_depth = ignore_depth; |
| 144 | g_unwind_signal_state.max_depth = max_depth; |
| 145 | g_unwind_signal_state.returned_frames = 0; |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 146 | android_atomic_release_store(tid, &g_unwind_signal_state.tid_state); |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 147 | |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 148 | // Signal the specific thread that we want to dump. |
| 149 | int32_t tid_state = tid; |
| 150 | if (tgkill(getpid(), tid, SIGURG)) { |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 151 | ALOGV("Failed to send SIGURG to thread %d.", tid); |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 152 | } else { |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 153 | // Wait for the other thread to start dumping the stack, or time out. |
| 154 | int wait_millis = 250; |
| 155 | for (;;) { |
| 156 | tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state); |
| 157 | if (tid_state != tid) { |
| 158 | break; |
| 159 | } |
| 160 | if (wait_millis--) { |
| 161 | ALOGV("Waiting for thread %d to start dumping the stack...", tid); |
| 162 | usleep(1000); |
| 163 | } else { |
| 164 | ALOGV("Timed out waiting for thread %d to start dumping the stack.", tid); |
| 165 | break; |
| 166 | } |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 167 | } |
Jeff Brown | 6775456 | 2011-11-18 15:34:35 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Try to cancel the dump if it has not started yet. |
| 171 | if (tid_state == tid) { |
| 172 | if (!android_atomic_acquire_cas(tid, STATE_CANCEL, &g_unwind_signal_state.tid_state)) { |
| 173 | ALOGV("Canceled thread %d stack dump.", tid); |
| 174 | tid_state = STATE_CANCEL; |
| 175 | } else { |
| 176 | tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Wait indefinitely for the dump to finish or be canceled. |
| 181 | // We cannot apply a timeout here because the other thread is accessing state that |
| 182 | // is owned by this thread, such as milist. It should not take very |
| 183 | // long to take the dump once started. |
| 184 | while (tid_state == STATE_DUMPING) { |
| 185 | ALOGV("Waiting for thread %d to finish dumping the stack...", tid); |
| 186 | usleep(1000); |
| 187 | tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state); |
| 188 | } |
| 189 | |
| 190 | if (tid_state == STATE_DONE) { |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 191 | frames = g_unwind_signal_state.returned_frames; |
| 192 | } |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 193 | |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 194 | sigaction(SIGURG, &oact, NULL); |
| 195 | } |
| 196 | |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 197 | release_my_map_info_list(milist); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 198 | pthread_mutex_unlock(&g_unwind_signal_mutex); |
| 199 | return frames; |
| 200 | #else |
| 201 | return -1; |
| 202 | #endif |
| 203 | } |
| 204 | |
| 205 | ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context, |
| 206 | backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { |
| 207 | #ifdef CORKSCREW_HAVE_ARCH |
| 208 | return unwind_backtrace_ptrace_arch(tid, context, backtrace, ignore_depth, max_depth); |
| 209 | #else |
| 210 | return -1; |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | static void init_backtrace_symbol(backtrace_symbol_t* symbol, uintptr_t pc) { |
| 215 | symbol->relative_pc = pc; |
Jeff Brown | 19b39f3 | 2011-11-21 21:10:00 -0800 | [diff] [blame] | 216 | symbol->relative_symbol_addr = 0; |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 217 | symbol->map_name = NULL; |
Jeff Brown | 19b39f3 | 2011-11-21 21:10:00 -0800 | [diff] [blame] | 218 | symbol->symbol_name = NULL; |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 219 | symbol->demangled_name = NULL; |
| 220 | } |
| 221 | |
| 222 | void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames, |
| 223 | backtrace_symbol_t* backtrace_symbols) { |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 224 | map_info_t* milist = acquire_my_map_info_list(); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 225 | for (size_t i = 0; i < frames; i++) { |
| 226 | const backtrace_frame_t* frame = &backtrace[i]; |
| 227 | backtrace_symbol_t* symbol = &backtrace_symbols[i]; |
| 228 | init_backtrace_symbol(symbol, frame->absolute_pc); |
| 229 | |
| 230 | const map_info_t* mi = find_map_info(milist, frame->absolute_pc); |
| 231 | if (mi) { |
| 232 | symbol->relative_pc = frame->absolute_pc - mi->start; |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 233 | if (mi->name[0]) { |
| 234 | symbol->map_name = strdup(mi->name); |
| 235 | } |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 236 | #if HAVE_DLADDR |
| 237 | Dl_info info; |
| 238 | if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) { |
Jeff Brown | 19b39f3 | 2011-11-21 21:10:00 -0800 | [diff] [blame] | 239 | symbol->relative_symbol_addr = (uintptr_t)info.dli_saddr |
| 240 | - (uintptr_t)info.dli_fbase; |
| 241 | symbol->symbol_name = strdup(info.dli_sname); |
| 242 | symbol->demangled_name = demangle_symbol_name(symbol->symbol_name); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 243 | } |
| 244 | #endif |
| 245 | } |
| 246 | } |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 247 | release_my_map_info_list(milist); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void get_backtrace_symbols_ptrace(const ptrace_context_t* context, |
| 251 | const backtrace_frame_t* backtrace, size_t frames, |
| 252 | backtrace_symbol_t* backtrace_symbols) { |
| 253 | for (size_t i = 0; i < frames; i++) { |
| 254 | const backtrace_frame_t* frame = &backtrace[i]; |
| 255 | backtrace_symbol_t* symbol = &backtrace_symbols[i]; |
| 256 | init_backtrace_symbol(symbol, frame->absolute_pc); |
| 257 | |
| 258 | const map_info_t* mi; |
| 259 | const symbol_t* s; |
| 260 | find_symbol_ptrace(context, frame->absolute_pc, &mi, &s); |
| 261 | if (mi) { |
| 262 | symbol->relative_pc = frame->absolute_pc - mi->start; |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 263 | if (mi->name[0]) { |
| 264 | symbol->map_name = strdup(mi->name); |
| 265 | } |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 266 | } |
| 267 | if (s) { |
Jeff Brown | 19b39f3 | 2011-11-21 21:10:00 -0800 | [diff] [blame] | 268 | symbol->relative_symbol_addr = s->start; |
| 269 | symbol->symbol_name = strdup(s->name); |
| 270 | symbol->demangled_name = demangle_symbol_name(symbol->symbol_name); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames) { |
| 276 | for (size_t i = 0; i < frames; i++) { |
| 277 | backtrace_symbol_t* symbol = &backtrace_symbols[i]; |
Jeff Brown | f0c5872 | 2011-11-03 17:58:44 -0700 | [diff] [blame] | 278 | free(symbol->map_name); |
Jeff Brown | 19b39f3 | 2011-11-21 21:10:00 -0800 | [diff] [blame] | 279 | free(symbol->symbol_name); |
Jeff Brown | 501edd2 | 2011-10-19 20:35:35 -0700 | [diff] [blame] | 280 | free(symbol->demangled_name); |
| 281 | init_backtrace_symbol(symbol, 0); |
| 282 | } |
| 283 | } |
Jeff Brown | 19b39f3 | 2011-11-21 21:10:00 -0800 | [diff] [blame] | 284 | |
| 285 | void format_backtrace_line(unsigned frameNumber, const backtrace_frame_t* frame, |
| 286 | const backtrace_symbol_t* symbol, char* buffer, size_t bufferSize) { |
| 287 | const char* mapName = symbol->map_name ? symbol->map_name : "<unknown>"; |
| 288 | const char* symbolName = symbol->demangled_name ? symbol->demangled_name : symbol->symbol_name; |
| 289 | size_t fieldWidth = (bufferSize - 80) / 2; |
| 290 | if (symbolName) { |
| 291 | uint32_t pc_offset = symbol->relative_pc - symbol->relative_symbol_addr; |
| 292 | if (pc_offset) { |
| 293 | snprintf(buffer, bufferSize, "#%02d pc %08x %.*s (%.*s+%u)", |
| 294 | frameNumber, symbol->relative_pc, fieldWidth, mapName, |
| 295 | fieldWidth, symbolName, pc_offset); |
| 296 | } else { |
| 297 | snprintf(buffer, bufferSize, "#%02d pc %08x %.*s (%.*s)", |
| 298 | frameNumber, symbol->relative_pc, fieldWidth, mapName, |
| 299 | fieldWidth, symbolName); |
| 300 | } |
| 301 | } else { |
| 302 | snprintf(buffer, bufferSize, "#%02d pc %08x %.*s", |
| 303 | frameNumber, symbol->relative_pc, fieldWidth, mapName); |
| 304 | } |
| 305 | } |