2 * mini-exceptions-native-unwinder.c: libcorkscrew-based native unwinder
5 * Alex Rønne Petersen (alexrp@xamarin.com)
7 * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 * Attempt to handle native SIGSEGVs with libunwind or libcorkscrew.
20 #include <mono/utils/mono-signal-handler.h>
23 #if defined (PLATFORM_ANDROID)
26 #include <sys/types.h>
27 #include <mono/utils/mono-dl.h>
29 #define UNW_LOCAL_ONLY
30 #undef _U /* ctype.h apparently defines this and it screws up the libunwind headers. */
31 #include "android-libunwind/libunwind.h"
34 #define FUNC_NAME_LENGTH 512
35 #define FRAMES_TO_UNWIND 256
37 /* Expand the SYM argument. */
38 #define LOAD_SYM(DL, ERR, SYM, VAR) _LOAD_SYM(DL, ERR, SYM, VAR)
39 #define _LOAD_SYM(DL, ERR, SYM, VAR) \
41 if ((ERR = mono_dl_symbol (DL, #SYM, (void **) &VAR))) { \
47 typedef int (*unw_init_local_t) (unw_cursor_t *, unw_context_t *);
48 typedef int (*unw_get_reg_t) (unw_cursor_t *, int, unw_word_t *);
49 typedef int (*unw_get_proc_name_t) (unw_cursor_t *, char *, size_t, unw_word_t *);
50 typedef int (*unw_step_t) (unw_cursor_t *);
53 mono_extension_handle_native_sigsegv_libunwind (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
58 unw_init_local_t unw_init_local_fn;
59 unw_get_reg_t unw_get_reg_fn;
60 unw_get_proc_name_t unw_get_proc_name_fn;
61 unw_step_t unw_step_fn;
67 MonoDl *dl = mono_dl_open ("libunwind.so", MONO_DL_LAZY, &dl_err);
72 LOAD_SYM (dl, dl_err, UNW_OBJ (init_local), unw_init_local_fn);
73 LOAD_SYM (dl, dl_err, UNW_OBJ (get_reg), unw_get_reg_fn);
74 LOAD_SYM (dl, dl_err, UNW_OBJ (get_proc_name), unw_get_proc_name_fn);
75 LOAD_SYM (dl, dl_err, UNW_OBJ (step), unw_step_fn);
77 if ((unw_err = unw_init_local_fn (&cursor, ctx))) {
80 return g_strdup_printf ("unw_init_local () returned %d", unw_err);
87 char name [FUNC_NAME_LENGTH];
89 if ((reg_err = unw_get_reg_fn (&cursor, UNW_REG_IP, &ip))) {
90 mono_runtime_printf_err ("unw_get_reg (UNW_REG_IP) returned %d", reg_err);
94 reg_err = unw_get_proc_name_fn (&cursor, name, FUNC_NAME_LENGTH, &off);
96 if (reg_err == -UNW_ENOINFO)
99 mono_runtime_printf_err (" at %s+%zu [0x%zx]", name, off, ip);
101 unw_err = unw_step_fn (&cursor);
103 } while (unw_err > 0 && frames < FRAMES_TO_UNWIND);
106 mono_runtime_printf_err ("unw_step () returned %d", unw_err);
114 * This code is based on the AOSP header system/core/include/corkscrew/backtrace.h.
116 * This is copied here because libcorkscrew is not a stable library and the header (and
117 * other headers that it depends on) will eventually go away.
119 * We can probably remove this one day when libunwind becomes the norm.
123 uintptr_t absolute_pc;
129 uintptr_t relative_pc;
130 uintptr_t relative_symbol_addr;
133 char *demangled_name;
134 } backtrace_symbol_t;
136 typedef void (*get_backtrace_symbols_t) (const backtrace_frame_t *backtrace, size_t frames, backtrace_symbol_t *backtrace_symbols);
137 typedef void (*free_backtrace_symbols_t) (backtrace_symbol_t *backtrace_symbols, size_t frames);
140 MAX_BACKTRACE_LINE_LENGTH = 800,
143 /* Internals that we're exploiting to work in a signal handler. Only works on ARM/x86. */
145 typedef struct map_info_t map_info_t;
147 typedef ssize_t (*unwind_backtrace_signal_arch_t) (siginfo_t *si, void *sc, const map_info_t *lst, backtrace_frame_t *bt, size_t ignore_depth, size_t max_depth);
148 typedef map_info_t *(*acquire_my_map_info_list_t) (void);
149 typedef void (*release_my_map_info_list_t) (map_info_t *milist);
152 mono_extension_handle_native_sigsegv_libcorkscrew (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
154 #if defined (__arm__) || defined (__i386__)
157 get_backtrace_symbols_t get_backtrace_symbols;
158 free_backtrace_symbols_t free_backtrace_symbols;
159 unwind_backtrace_signal_arch_t unwind_backtrace_signal_arch;
160 acquire_my_map_info_list_t acquire_my_map_info_list;
161 release_my_map_info_list_t release_my_map_info_list;
163 backtrace_frame_t frames [FRAMES_TO_UNWIND];
164 backtrace_symbol_t symbols [FRAMES_TO_UNWIND];
166 map_info_t *map_info;
167 ssize_t frames_unwound;
170 MonoDl *dl = mono_dl_open ("libcorkscrew.so", MONO_DL_LAZY, &dl_err);
175 LOAD_SYM (dl, dl_err, get_backtrace_symbols, get_backtrace_symbols);
176 LOAD_SYM (dl, dl_err, free_backtrace_symbols, free_backtrace_symbols);
177 LOAD_SYM (dl, dl_err, unwind_backtrace_signal_arch, unwind_backtrace_signal_arch);
178 LOAD_SYM (dl, dl_err, acquire_my_map_info_list, acquire_my_map_info_list);
179 LOAD_SYM (dl, dl_err, release_my_map_info_list, release_my_map_info_list);
181 map_info = acquire_my_map_info_list ();
182 frames_unwound = unwind_backtrace_signal_arch (info, ctx, map_info, frames, 0, FRAMES_TO_UNWIND);
183 release_my_map_info_list (map_info);
185 if (frames_unwound == -1) {
188 return g_strdup ("unwind_backtrace_signal_arch () returned -1");
191 get_backtrace_symbols (frames, frames_unwound, symbols);
193 for (i = 0; i < frames_unwound; i++) {
194 backtrace_frame_t *frame = frames + i;
195 backtrace_symbol_t *symbol = symbols + i;
197 const char *name = symbol->demangled_name ? symbol->demangled_name : (symbol->symbol_name ? symbol->symbol_name : "???");
198 uintptr_t off = symbol->relative_pc - symbol->relative_symbol_addr;
199 uintptr_t ip = frame->absolute_pc;
201 mono_runtime_printf_err (" at %s+%zu [0x%zx]", name, off, ip);
204 free_backtrace_symbols (symbols, frames_unwound);
210 return g_strdup ("libcorkscrew is only supported on 32-bit ARM/x86");
215 mono_exception_native_unwind (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
217 char *unwind_err, *corkscrew_err;
219 mono_runtime_printf_err ("\nAttempting native Android stacktrace:\n");
221 unwind_err = mono_extension_handle_native_sigsegv_libunwind (ctx, info);
224 corkscrew_err = mono_extension_handle_native_sigsegv_libcorkscrew (ctx, info);
227 mono_runtime_printf_err ("\tCould not unwind with `libunwind.so`: %s", unwind_err);
228 mono_runtime_printf_err ("\tCould not unwind with `libcorkscrew.so`: %s", corkscrew_err);
229 mono_runtime_printf_err ("\n\tNo options left to get a native stacktrace :-(");
231 g_free (corkscrew_err);
241 mono_exception_native_unwind (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)