[MACH] Add retry logic to all mach api calls in the case of interruption.
[mono.git] / mono / mini / mini-exceptions-native-unwinder.c
1 /*
2  * mini-exceptions-native-unwinder.c: libcorkscrew-based native unwinder
3  *
4  * Authors:
5  *   Alex Rønne Petersen (alexrp@xamarin.com)
6  *
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.
9  */
10 #include <config.h>
11
12 #include <mono/utils/mono-logger-internals.h>
13
14 /*
15  * Attempt to handle native SIGSEGVs with libunwind or libcorkscrew.
16  */
17
18 #ifdef HAVE_SIGNAL_H
19 #include <signal.h>
20 #endif
21
22 #include <mono/utils/mono-signal-handler.h>
23 #include "mini.h"
24
25 #if defined (PLATFORM_ANDROID)
26
27 #include <signal.h>
28 #include <sys/types.h>
29 #include <mono/utils/mono-dl.h>
30
31 #define UNW_LOCAL_ONLY
32 #undef _U /* ctype.h apparently defines this and it screws up the libunwind headers. */
33 #include "../../external/android-libunwind/include/libunwind.h"
34 #define _U 0x01
35
36 #define FUNC_NAME_LENGTH 512
37 #define FRAMES_TO_UNWIND 256
38
39 /* Expand the SYM argument. */
40 #define LOAD_SYM(DL, ERR, SYM, VAR) _LOAD_SYM(DL, ERR, SYM, VAR)
41 #define _LOAD_SYM(DL, ERR, SYM, VAR) \
42         do { \
43                 if ((ERR = mono_dl_symbol (DL, #SYM, (void **) &VAR))) { \
44                         mono_dl_close (DL); \
45                         return ERR; \
46                 } \
47         } while (0)
48
49 typedef int (*unw_init_local_t) (unw_cursor_t *, unw_context_t *);
50 typedef int (*unw_get_reg_t) (unw_cursor_t *, int, unw_word_t *);
51 typedef int (*unw_get_proc_name_t) (unw_cursor_t *, char *, size_t, unw_word_t *);
52 typedef int (*unw_step_t) (unw_cursor_t *);
53
54 static char *
55 mono_extension_handle_native_sigsegv_libunwind (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
56 {
57         char *dl_err;
58         int unw_err;
59
60         unw_init_local_t unw_init_local_fn;
61         unw_get_reg_t unw_get_reg_fn;
62         unw_get_proc_name_t unw_get_proc_name_fn;
63         unw_step_t unw_step_fn;
64
65         unw_cursor_t cursor;
66
67         size_t frames = 0;
68
69         MonoDl *dl = mono_dl_open ("libunwind.so", MONO_DL_LAZY, &dl_err);
70
71         if (!dl)
72                 return dl_err;
73
74         LOAD_SYM (dl, dl_err, UNW_OBJ (init_local), unw_init_local_fn);
75         LOAD_SYM (dl, dl_err, UNW_OBJ (get_reg), unw_get_reg_fn);
76         LOAD_SYM (dl, dl_err, UNW_OBJ (get_proc_name), unw_get_proc_name_fn);
77         LOAD_SYM (dl, dl_err, UNW_OBJ (step), unw_step_fn);
78
79         if ((unw_err = unw_init_local_fn (&cursor, ctx))) {
80                 mono_dl_close (dl);
81
82                 return g_strdup_printf ("unw_init_local () returned %d", unw_err);
83         }
84
85         do {
86                 int reg_err;
87
88                 unw_word_t ip, off;
89                 char name [FUNC_NAME_LENGTH];
90
91                 if ((reg_err = unw_get_reg_fn (&cursor, UNW_REG_IP, &ip))) {
92                         mono_runtime_printf_err ("unw_get_reg (UNW_REG_IP) returned %d", reg_err);
93                         break;
94                 }
95
96                 reg_err = unw_get_proc_name_fn (&cursor, name, FUNC_NAME_LENGTH, &off);
97
98                 if (reg_err == -UNW_ENOINFO)
99                         strcpy (name, "???");
100
101                 mono_runtime_printf_err (" at %s+%zu [0x%zx]", name, off, ip);
102
103                 unw_err = unw_step_fn (&cursor);
104                 frames++;
105         } while (unw_err > 0 && frames < FRAMES_TO_UNWIND);
106
107         if (unw_err < 0)
108                 mono_runtime_printf_err ("unw_step () returned %d", unw_err);
109
110         mono_dl_close (dl);
111
112         return NULL;
113 }
114
115 /*
116  * This code is based on the AOSP header system/core/include/corkscrew/backtrace.h.
117  *
118  * This is copied here because libcorkscrew is not a stable library and the header (and
119  * other headers that it depends on) will eventually go away.
120  *
121  * We can probably remove this one day when libunwind becomes the norm.
122  */
123
124 typedef struct {
125         uintptr_t absolute_pc;
126         uintptr_t stack_top;
127         size_t stack_size;
128 } backtrace_frame_t;
129
130 typedef struct {
131         uintptr_t relative_pc;
132         uintptr_t relative_symbol_addr;
133         char *map_name;
134         char *symbol_name;
135         char *demangled_name;
136 } backtrace_symbol_t;
137
138 typedef void (*get_backtrace_symbols_t) (const backtrace_frame_t *backtrace, size_t frames, backtrace_symbol_t *backtrace_symbols);
139 typedef void (*free_backtrace_symbols_t) (backtrace_symbol_t *backtrace_symbols, size_t frames);
140
141 enum {
142         MAX_BACKTRACE_LINE_LENGTH = 800,
143 };
144
145 /* Internals that we're exploiting to work in a signal handler. Only works on ARM/x86. */
146
147 typedef struct map_info_t map_info_t;
148
149 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);
150 typedef map_info_t *(*acquire_my_map_info_list_t) (void);
151 typedef void (*release_my_map_info_list_t) (map_info_t *milist);
152
153 static char *
154 mono_extension_handle_native_sigsegv_libcorkscrew (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
155 {
156 #if defined (__arm__) || defined (__i386__)
157         char *dl_err;
158
159         get_backtrace_symbols_t get_backtrace_symbols;
160         free_backtrace_symbols_t free_backtrace_symbols;
161         unwind_backtrace_signal_arch_t unwind_backtrace_signal_arch;
162         acquire_my_map_info_list_t acquire_my_map_info_list;
163         release_my_map_info_list_t release_my_map_info_list;
164
165         backtrace_frame_t frames [FRAMES_TO_UNWIND];
166         backtrace_symbol_t symbols [FRAMES_TO_UNWIND];
167
168         map_info_t *map_info;
169         ssize_t frames_unwound;
170         size_t i;
171
172         MonoDl *dl = mono_dl_open ("libcorkscrew.so", MONO_DL_LAZY, &dl_err);
173
174         if (!dl)
175                 return dl_err;
176
177         LOAD_SYM (dl, dl_err, get_backtrace_symbols, get_backtrace_symbols);
178         LOAD_SYM (dl, dl_err, free_backtrace_symbols, free_backtrace_symbols);
179         LOAD_SYM (dl, dl_err, unwind_backtrace_signal_arch, unwind_backtrace_signal_arch);
180         LOAD_SYM (dl, dl_err, acquire_my_map_info_list, acquire_my_map_info_list);
181         LOAD_SYM (dl, dl_err, release_my_map_info_list, release_my_map_info_list);
182
183         map_info = acquire_my_map_info_list ();
184         frames_unwound = unwind_backtrace_signal_arch (info, ctx, map_info, frames, 0, FRAMES_TO_UNWIND);
185         release_my_map_info_list (map_info);
186
187         if (frames_unwound == -1) {
188                 mono_dl_close (dl);
189
190                 return g_strdup ("unwind_backtrace_signal_arch () returned -1");
191         }
192
193         get_backtrace_symbols (frames, frames_unwound, symbols);
194
195         for (i = 0; i < frames_unwound; i++) {
196                 backtrace_frame_t *frame = frames + i;
197                 backtrace_symbol_t *symbol = symbols + i;
198
199                 const char *name = symbol->demangled_name ? symbol->demangled_name : (symbol->symbol_name ? symbol->symbol_name : "???");
200                 uintptr_t off = symbol->relative_pc - symbol->relative_symbol_addr;
201                 uintptr_t ip = frame->absolute_pc;
202
203                 mono_runtime_printf_err ("  at %s+%zu [0x%zx]", name, off, ip);
204         }
205
206         free_backtrace_symbols (symbols, frames_unwound);
207
208         mono_dl_close (dl);
209
210         return NULL;
211 #else
212         return g_strdup ("libcorkscrew is only supported on 32-bit ARM/x86");
213 #endif
214 }
215
216 void
217 mono_exception_native_unwind (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
218 {
219         char *unwind_err, *corkscrew_err;
220
221         mono_runtime_printf_err ("\nAttempting native Android stacktrace:\n");
222
223         unwind_err = mono_extension_handle_native_sigsegv_libunwind (ctx, info);
224
225         if (unwind_err) {
226                 corkscrew_err = mono_extension_handle_native_sigsegv_libcorkscrew (ctx, info);
227
228                 if (corkscrew_err) {
229                         mono_runtime_printf_err ("\tCould not unwind with `libunwind.so`: %s", unwind_err);
230                         mono_runtime_printf_err ("\tCould not unwind with `libcorkscrew.so`: %s", corkscrew_err);
231                         mono_runtime_printf_err ("\n\tNo options left to get a native stacktrace :-(");
232
233                         g_free (corkscrew_err);
234                 }
235
236                 g_free (unwind_err);
237         }
238 }
239
240 #else
241
242 void
243 mono_exception_native_unwind (void *ctx, MONO_SIG_HANDLER_INFO_TYPE *info)
244 {
245 }
246
247 #endif