2 * mini-exceptions.c: generic exception support
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/threads.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/gc-internal.h>
21 #include <mono/metadata/mono-debug.h>
25 #define IS_ON_SIGALTSTACK(jit_tls) ((jit_tls) && ((guint8*)&(jit_tls) > (guint8*)(jit_tls)->signal_stack) && ((guint8*)&(jit_tls) < ((guint8*)(jit_tls)->signal_stack + (jit_tls)->signal_stack_size)))
28 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
30 MonoDomain *domain = mono_domain_get ();
32 MonoArray *ta = exc->trace_ips;
36 /* Exception is not thrown yet */
37 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
40 len = mono_array_length (ta);
42 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
44 for (i = skip; i < len; i++) {
46 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
47 gpointer ip = mono_array_get (ta, gpointer, i);
49 ji = mono_jit_info_table_find (domain, ip);
52 mono_array_set (res, gpointer, i, sf);
56 g_assert (ji != NULL);
58 sf->method = mono_method_get_object (domain, ji->method, NULL);
59 sf->native_offset = (char *)ip - (char *)ji->code_start;
61 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
66 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
68 sf->filename = filename? mono_string_new (domain, filename): NULL;
74 mono_array_set (res, gpointer, i, sf);
81 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
82 MonoDomain *domain = mono_domain_get ();
83 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
84 MonoLMF *lmf = jit_tls->lmf;
86 gint native_offset, il_offset;
89 MonoContext ctx, new_ctx;
91 mono_arch_flush_register_windows ();
93 MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
94 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
96 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
98 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
101 if (ji == (gpointer)-1)
104 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
106 if (func (ji->method, native_offset, il_offset, managed, user_data))
114 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
115 MonoReflectionMethod **method,
116 gint32 *iloffset, gint32 *native_offset,
117 MonoString **file, gint32 *line, gint32 *column)
119 MonoDomain *domain = mono_domain_get ();
120 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
121 MonoLMF *lmf = jit_tls->lmf;
122 MonoJitInfo *ji, rji;
123 MonoContext ctx, new_ctx;
125 mono_arch_flush_register_windows ();
127 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
128 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
133 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
137 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
140 /* skip all wrappers ??*/
141 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
142 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
143 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
150 *method = mono_method_get_object (domain, ji->method, NULL);
151 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
153 if (need_file_info) {
156 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
158 *file = filename? mono_string_new (domain, filename): NULL;
168 glist_to_array (GList *list)
170 MonoDomain *domain = mono_domain_get ();
177 len = g_list_length (list);
178 res = mono_array_new (domain, mono_defaults.int_class, len);
180 for (i = 0; list; list = list->next, i++)
181 mono_array_set (res, gpointer, i, list->data);
187 * mono_handle_exception:
188 * @ctx: saved processor state
189 * @obj: the exception object
190 * @test_only: only test if the exception is caught, but dont call handlers
194 mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only)
196 MonoDomain *domain = mono_domain_get ();
197 MonoJitInfo *ji, rji;
198 static int (*call_filter) (MonoContext *, gpointer) = NULL;
199 static void (*restore_context) (void *);
200 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
201 MonoLMF *lmf = jit_tls->lmf;
202 GList *trace_ips = NULL;
203 MonoException *mono_ex;
204 gboolean stack_overflow = FALSE;
205 MonoContext initial_ctx;
207 gboolean gc_disabled = FALSE;
208 MonoString *initial_stack_trace;
211 * This function might execute on an alternate signal stack, and Boehm GC
213 * Also, since the altstack is small, stack space intensive operations like
214 * JIT compilation should be avoided.
216 if (IS_ON_SIGALTSTACK (jit_tls)) {
218 * FIXME: disabling/enabling GC while already on a signal stack might
219 * not be safe either.
221 /* Have to reenable it later */
226 g_assert (ctx != NULL);
228 MonoException *ex = mono_get_exception_null_reference ();
229 ex->message = mono_string_new (domain, "Object reference not set to an instance of an object");
230 obj = (MonoObject *)ex;
234 * Allocate a new exception object instead of the preconstructed ones.
235 * We can't do this in sigsegv_signal_handler, since GC is not yet
238 if (obj == domain->stack_overflow_ex) {
239 obj = mono_get_exception_stack_overflow ();
241 else if (obj == domain->null_reference_ex) {
242 obj = mono_get_exception_null_reference ();
245 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
246 mono_ex = (MonoException*)obj;
247 initial_stack_trace = mono_ex->stack_trace;
252 if (obj == domain->stack_overflow_ex)
253 stack_overflow = TRUE;
256 call_filter = mono_arch_get_call_filter ();
258 if (!restore_context)
259 restore_context = mono_arch_get_restore_context ();
261 g_assert (jit_tls->end_of_stack);
262 g_assert (jit_tls->abort_func);
265 MonoContext ctx_cp = *ctx;
266 if (mono_jit_trace_calls != NULL)
267 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
268 if (!mono_handle_exception (&ctx_cp, obj, original_ip, TRUE)) {
269 if (mono_break_on_exc)
271 mono_unhandled_exception (obj);
273 if (mono_debugger_unhandled_exception (original_ip, obj)) {
275 * If this returns true, then we're running inside the
276 * Mono Debugger and the debugger wants us to restore the
277 * context and continue (normally, the debugger inserts
278 * a breakpoint on the `original_ip', so it regains control
279 * immediately after restoring the context).
281 MONO_CONTEXT_SET_IP (ctx, original_ip);
282 restore_context (ctx);
283 g_assert_not_reached ();
289 memset (&rji, 0, sizeof (rji));
294 gboolean need_trace = FALSE;
297 if (test_only && (frame_count < 1000))
300 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
301 need_trace ? &trace : NULL, &lmf, NULL, NULL);
303 g_warning ("Exception inside function without unwind info");
304 g_assert_not_reached ();
307 if (ji != (gpointer)-1) {
309 //printf ("M: %s %d %d.\n", mono_method_full_name (ji->method, TRUE), frame_count, test_only);
311 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
315 * Avoid overwriting the stack trace if the exception is
316 * rethrown. Also avoid giant stack traces during a stack
319 if (!initial_stack_trace && (frame_count < 1000)) {
320 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
322 if (!mono_ex->stack_trace)
323 strace = g_strdup ("");
325 strace = mono_string_to_utf8 (mono_ex->stack_trace);
327 tmp = g_strdup_printf ("%s%s\n", strace, trace);
330 mono_ex->stack_trace = mono_string_new (domain, tmp);
337 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
339 free_stack = 0xffffff;
342 * During stack overflow, wait till the unwinding frees some stack
343 * space before running handlers/finalizers.
345 if ((free_stack > (64 * 1024)) && ji->num_clauses) {
348 g_assert (ji->clauses);
350 for (i = 0; i < ji->num_clauses; i++) {
351 MonoJitExceptionInfo *ei = &ji->clauses [i];
352 gboolean filtered = FALSE;
354 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
355 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
358 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
359 /* store the exception object int cfg->excvar */
360 g_assert (ji->exvar_offset);
361 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
364 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
365 filtered = call_filter (ctx, ei->data.filter);
367 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
368 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) || filtered) {
371 mono_ex->trace_ips = glist_to_array (trace_ips);
372 g_list_free (trace_ips);
379 if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
380 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
381 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
389 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
390 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
391 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
392 if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
393 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
394 call_filter (ctx, ei->handler_start);
406 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
413 if (IS_ON_SIGALTSTACK (jit_tls)) {
414 /* Switch back to normal stack */
416 /* Free up some stack space */
417 MONO_CONTEXT_SET_SP (&initial_ctx, (guint32)(MONO_CONTEXT_GET_SP (&initial_ctx)) + (64 * 1024));
418 MONO_CONTEXT_SET_IP (&initial_ctx, (unsigned int)jit_tls->abort_func);
419 restore_context (&initial_ctx);
422 jit_tls->abort_func (obj);
423 g_assert_not_reached ();
426 mono_ex->trace_ips = glist_to_array (trace_ips);
427 g_list_free (trace_ips);
433 g_assert_not_reached ();