static ssize_t coffeecatch_backtrace_signal(siginfo_t* si, void* sc, backtrace_frame_t* frames, size_t ignore_depth, size_t max_depth) { void *const libcorkscrew = dlopen("libcorkscrew.so", RTLD_LAZY | RTLD_LOCAL); if (libcorkscrew != NULL) { t_unwind_backtrace_signal_arch unwind_backtrace_signal_arch = (t_unwind_backtrace_signal_arch) dlsym(libcorkscrew, "unwind_backtrace_signal_arch"); t_acquire_my_map_info_list acquire_my_map_info_list = (t_acquire_my_map_info_list) dlsym(libcorkscrew, "acquire_my_map_info_list"); t_release_my_map_info_list release_my_map_info_list = (t_release_my_map_info_list) dlsym(libcorkscrew, "release_my_map_info_list"); if (unwind_backtrace_signal_arch != NULL && acquire_my_map_info_list != NULL && release_my_map_info_list != NULL) { map_info_t*const info = acquire_my_map_info_list(); const ssize_t size = unwind_backtrace_signal_arch(si, sc, info, frames, ignore_depth, max_depth); release_my_map_info_list(info); return size; } else { DEBUG(print("symbols not founs in libcorkscrew.so\n")); } dlclose(libcorkscrew); } else { DEBUG(print("libcorkscrew.so could not be loaded\n")); } return -1; }
void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames, backtrace_symbol_t* backtrace_symbols) { map_info_t* milist = acquire_my_map_info_list(); for (size_t i = 0; i < frames; i++) { const backtrace_frame_t* frame = &backtrace[i]; backtrace_symbol_t* symbol = &backtrace_symbols[i]; init_backtrace_symbol(symbol, frame->absolute_pc); const map_info_t* mi = find_map_info(milist, frame->absolute_pc); if (mi) { symbol->relative_pc = frame->absolute_pc - mi->start; if (mi->name[0]) { symbol->map_name = strdup(mi->name); } Dl_info info; if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) { symbol->relative_symbol_addr = (uintptr_t)info.dli_saddr - (uintptr_t)info.dli_fbase; symbol->symbol_name = strdup(info.dli_sname); symbol->demangled_name = demangle_symbol_name(symbol->symbol_name); } } } release_my_map_info_list(milist); }
ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { ALOGV("Unwinding current thread %d.", gettid()); map_info_t* milist = acquire_my_map_info_list(); backtrace_state_t state; state.backtrace = backtrace; state.ignore_depth = ignore_depth; state.max_depth = max_depth; state.ignored_frames = 0; state.returned_frames = 0; init_memory(&state.memory, milist); _Unwind_Reason_Code rc = _Unwind_Backtrace(unwind_backtrace_callback, &state); release_my_map_info_list(milist); if (state.returned_frames) { return state.returned_frames; } return rc == _URC_END_OF_STACK ? 0 : -1; }
ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) { if (tid == gettid()) { return unwind_backtrace(backtrace, ignore_depth + 1, max_depth); } ALOGV("Unwinding thread %d from thread %d.", tid, gettid()); // TODO: there's no tgkill(2) on Mac OS, so we'd either need the // mach_port_t or the pthread_t rather than the tid. #if defined(CORKSCREW_HAVE_ARCH) && !defined(__APPLE__) struct sigaction act; struct sigaction oact; memset(&act, 0, sizeof(act)); act.sa_sigaction = unwind_backtrace_thread_signal_handler; act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; sigemptyset(&act.sa_mask); pthread_mutex_lock(&g_unwind_signal_mutex); map_info_t* milist = acquire_my_map_info_list(); ssize_t frames = -1; if (!sigaction(SIGURG, &act, &oact)) { g_unwind_signal_state.map_info_list = milist; g_unwind_signal_state.backtrace = backtrace; g_unwind_signal_state.ignore_depth = ignore_depth; g_unwind_signal_state.max_depth = max_depth; g_unwind_signal_state.returned_frames = 0; android_atomic_release_store(tid, &g_unwind_signal_state.tid_state); // Signal the specific thread that we want to dump. int32_t tid_state = tid; if (tgkill(getpid(), tid, SIGURG)) { ALOGV("Failed to send SIGURG to thread %d.", tid); } else { // Wait for the other thread to start dumping the stack, or time out. int wait_millis = 250; for (;;) { tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state); if (tid_state != tid) { break; } if (wait_millis--) { ALOGV("Waiting for thread %d to start dumping the stack...", tid); usleep(1000); } else { ALOGV("Timed out waiting for thread %d to start dumping the stack.", tid); break; } } } // Try to cancel the dump if it has not started yet. if (tid_state == tid) { if (!android_atomic_acquire_cas(tid, STATE_CANCEL, &g_unwind_signal_state.tid_state)) { ALOGV("Canceled thread %d stack dump.", tid); tid_state = STATE_CANCEL; } else { tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state); } } // Wait indefinitely for the dump to finish or be canceled. // We cannot apply a timeout here because the other thread is accessing state that // is owned by this thread, such as milist. It should not take very // long to take the dump once started. while (tid_state == STATE_DUMPING) { ALOGV("Waiting for thread %d to finish dumping the stack...", tid); usleep(1000); tid_state = android_atomic_acquire_load(&g_unwind_signal_state.tid_state); } if (tid_state == STATE_DONE) { frames = g_unwind_signal_state.returned_frames; } sigaction(SIGURG, &oact, NULL); } release_my_map_info_list(milist); pthread_mutex_unlock(&g_unwind_signal_mutex); return frames; #else return -1; #endif }
static char * mono_extension_handle_native_sigsegv_libcorkscrew (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info) { #if defined (__arm__) || defined (__i386__) char *dl_err; get_backtrace_symbols_t get_backtrace_symbols; free_backtrace_symbols_t free_backtrace_symbols; unwind_backtrace_signal_arch_t unwind_backtrace_signal_arch; acquire_my_map_info_list_t acquire_my_map_info_list; release_my_map_info_list_t release_my_map_info_list; backtrace_frame_t frames [FRAMES_TO_UNWIND]; backtrace_symbol_t symbols [FRAMES_TO_UNWIND]; map_info_t *map_info; ssize_t frames_unwound; size_t i; MonoDl *dl = mono_dl_open ("libcorkscrew.so", MONO_DL_LAZY, &dl_err); if (!dl) return dl_err; LOAD_SYM (dl, dl_err, get_backtrace_symbols, get_backtrace_symbols); LOAD_SYM (dl, dl_err, free_backtrace_symbols, free_backtrace_symbols); LOAD_SYM (dl, dl_err, unwind_backtrace_signal_arch, unwind_backtrace_signal_arch); LOAD_SYM (dl, dl_err, acquire_my_map_info_list, acquire_my_map_info_list); LOAD_SYM (dl, dl_err, release_my_map_info_list, release_my_map_info_list); map_info = acquire_my_map_info_list (); frames_unwound = unwind_backtrace_signal_arch (info, ctx, map_info, frames, 0, FRAMES_TO_UNWIND); release_my_map_info_list (map_info); if (frames_unwound == -1) { mono_dl_close (dl); return g_strdup ("unwind_backtrace_signal_arch () returned -1"); } get_backtrace_symbols (frames, frames_unwound, symbols); for (i = 0; i < frames_unwound; i++) { backtrace_frame_t *frame = frames + i; backtrace_symbol_t *symbol = symbols + i; const char *name = symbol->demangled_name ? symbol->demangled_name : (symbol->symbol_name ? symbol->symbol_name : "???"); uintptr_t off = symbol->relative_pc - symbol->relative_symbol_addr; uintptr_t ip = frame->absolute_pc; mono_runtime_printf_err (" at %s+%zu [0x%zx]", name, off, ip); } free_backtrace_symbols (symbols, frames_unwound); mono_dl_close (dl); return NULL; #else return g_strdup ("libcorkscrew is only supported on 32-bit ARM/x86"); #endif }