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>
22 #include <mono/metadata/mono-debug-debugger.h>
26 #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 #ifndef mono_find_jit_info
30 /* mono_find_jit_info:
32 * This function is used to gather information from @ctx. It return the
33 * MonoJitInfo of the corresponding function, unwinds one stack frame and
34 * stores the resulting context into @new_ctx. It also stores a string
35 * describing the stack location into @trace (if not NULL), and modifies
36 * the @lmf if necessary. @native_offset return the IP offset from the
37 * start of the function or -1 if that info is not available.
40 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
41 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
45 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
57 ji = mono_arch_find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, NULL, lmf, NULL, &managed2);
59 if (ji == (gpointer)-1)
62 if (managed2 || ji->method->wrapper_type) {
63 char *source_location, *tmpaddr, *fname;
64 gint32 address, iloffset;
66 address = (char *)ip - (char *)ji->code_start;
69 *native_offset = address;
72 if (!ji->method->wrapper_type)
76 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
77 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
80 tmpaddr = g_strdup_printf ("<0x%05x>", address);
82 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
84 fname = mono_method_full_name (ji->method, TRUE);
87 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
89 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
92 g_free (source_location);
98 char *fname = mono_method_full_name (res->method, TRUE);
99 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
107 #endif /* mono_find_jit_info */
110 ves_icall_System_Exception_get_trace (MonoException *ex)
112 MonoDomain *domain = mono_domain_get ();
114 MonoArray *ta = ex->trace_ips;
120 /* Exception is not thrown yet */
123 len = mono_array_length (ta);
124 trace_str = g_string_new ("");
125 for (i = 0; i < len; i++) {
127 gpointer ip = mono_array_get (ta, gpointer, i);
129 ji = mono_jit_info_table_find (domain, ip);
131 /* Unmanaged frame */
132 g_string_append_printf (trace_str, "in (unmanaged) %p\n", ip);
134 char *source_location, *fname;
135 gint32 address, iloffset;
137 address = (char *)ip - (char *)ji->code_start;
139 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, ex->object.vtable->domain);
140 iloffset = mono_debug_il_offset_from_address (ji->method, address, ex->object.vtable->domain);
143 sprintf (tmpaddr, "<0x%05x>", address);
145 sprintf (tmpaddr, "[0x%05x]", iloffset);
147 fname = mono_method_full_name (ji->method, TRUE);
150 g_string_append_printf (trace_str, "in %s (at %s) %s\n", tmpaddr, source_location, fname);
152 g_string_append_printf (trace_str, "in %s %s\n", tmpaddr, fname);
155 g_free (source_location);
159 res = mono_string_new (ex->object.vtable->domain, trace_str->str);
160 g_string_free (trace_str, TRUE);
166 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
168 MonoDomain *domain = mono_domain_get ();
170 MonoArray *ta = exc->trace_ips;
174 /* Exception is not thrown yet */
175 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
178 len = mono_array_length (ta);
180 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
182 for (i = skip; i < len; i++) {
184 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
185 gpointer ip = mono_array_get (ta, gpointer, i);
187 ji = mono_jit_info_table_find (domain, ip);
189 /* Unmanaged frame */
190 mono_array_set (res, gpointer, i, sf);
194 g_assert (ji != NULL);
196 if (ji->method->wrapper_type) {
200 s = mono_method_full_name (ji->method, TRUE);
201 sf->internal_method_name = mono_string_new (domain, s);
205 sf->method = mono_method_get_object (domain, ji->method, NULL);
206 sf->native_offset = (char *)ip - (char *)ji->code_start;
208 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
210 if (need_file_info) {
213 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
215 sf->filename = filename? mono_string_new (domain, filename): NULL;
221 mono_array_set (res, gpointer, i, sf);
229 * @domain: starting appdomain
230 * @jit_tls: JIT data for the thread
231 * @start_ctx: starting state of the stack frame
232 * @func: callback to call for each stack frame
233 * @user_data: data passed to the callback
235 * This function walks the stack of a thread, starting from the state
236 * represented by jit_tls and start_ctx. For each frame the callback
237 * function is called with the relevant info. The walk ends when no more
238 * managed stack frames are found or when the callback returns a TRUE value.
239 * Note that the function can be used to walk the stack of a thread
240 * different from the current.
243 mono_walk_stack (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start_ctx, MonoStackFrameWalk func, gpointer user_data)
245 MonoLMF *lmf = jit_tls->lmf;
246 MonoJitInfo *ji, rji;
249 MonoContext ctx, new_ctx;
253 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
255 * FIXME: mono_find_jit_info () will need to be able to return a different
256 * MonoDomain when apddomain transitions are found on the stack.
258 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
259 if (!ji || ji == (gpointer)-1)
262 if (func (domain, &new_ctx, ji, user_data))
269 #ifndef CUSTOM_STACK_WALK
272 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
273 MonoDomain *domain = mono_domain_get ();
274 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
275 MonoLMF *lmf = jit_tls->lmf;
276 MonoJitInfo *ji, rji;
277 gint native_offset, il_offset;
280 MonoContext ctx, new_ctx;
283 unsigned int stackptr, retaddr;
284 __asm mov stackptr, ebp;
285 __asm mov eax, DWORD PTR [ebp + 4];
286 __asm mov retaddr, eax;
287 MONO_CONTEXT_SET_IP (&ctx, retaddr);
288 /* FIXME: NOT WORKING -- THIS IS __builtin_frame_address (0) NOT (1) */
289 MONO_CONTEXT_SET_BP (&ctx, stackptr);
291 mono_arch_flush_register_windows ();
293 MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
294 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
296 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
298 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
301 if (ji == (gpointer)-1)
304 il_offset = do_il_offset ? mono_debug_il_offset_from_address (ji->method, native_offset, domain): -1;
306 if (func (ji->method, native_offset, il_offset, managed, user_data))
314 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
315 MonoReflectionMethod **method,
316 gint32 *iloffset, gint32 *native_offset,
317 MonoString **file, gint32 *line, gint32 *column)
319 MonoDomain *domain = mono_domain_get ();
320 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
321 MonoLMF *lmf = jit_tls->lmf;
322 MonoJitInfo *ji, rji;
323 MonoContext ctx, new_ctx;
324 unsigned int stackptr;
326 mono_arch_flush_register_windows ();
329 __asm mov stackptr, ebp;
331 stackptr = (unsigned int) __builtin_frame_address (0);
334 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
335 MONO_CONTEXT_SET_BP (&ctx, stackptr);
340 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
344 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
347 /* skip all wrappers ??*/
348 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
349 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
350 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
351 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
352 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
359 *method = mono_method_get_object (domain, ji->method, NULL);
360 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
362 if (need_file_info) {
365 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
367 *file = filename? mono_string_new (domain, filename): NULL;
376 #endif /* CUSTOM_STACK_WALK */
380 MonoSecurityFrame *frame;
381 } MonoFrameSecurityInfo;
384 callback_get_first_frame_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
386 MonoFrameSecurityInfo *si = (MonoFrameSecurityInfo*) data;
388 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
389 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
390 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
391 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
392 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
393 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
402 si->frame = mono_declsec_create_frame (domain, ji);
404 /* Stop - we only want the first frame (e.g. LinkDemand and InheritanceDemand) */
409 * ves_icall_System_Security_SecurityFrame_GetSecurityFrame:
410 * @skip: the number of stack frames to skip
412 * This function returns a the security informations of a single stack frame
413 * (after the skipped ones). This is required for [NonCas]LinkDemand[Choice]
414 * and [NonCas]InheritanceDemand[Choice] as only the caller security is
418 ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip)
420 MonoDomain *domain = mono_domain_get ();
421 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
422 MonoFrameSecurityInfo si;
426 /* seems that MSC doesn't like having __asm in macros */
427 unsigned int stackptr;
428 mono_arch_flush_register_windows ();
429 __asm mov stackptr, ebp;
430 MONO_CONTEXT_SET_IP (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
431 MONO_CONTEXT_SET_BP (&ctx, stackptr);
433 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
438 mono_walk_stack (domain, jit_tls, &ctx, callback_get_first_frame_security_info, (gpointer)&si);
440 return (si.skips == 0) ? si.frame : NULL;
450 callback_get_stack_frames_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
452 MonoSecurityStack *ss = (MonoSecurityStack*) data;
454 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
455 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
456 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
457 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
458 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
459 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
468 ss->stack = g_list_prepend (ss->stack, mono_declsec_create_frame (domain, ji));
470 /* continue down the stack */
475 glist_to_array (GList *list, MonoClass *eclass)
477 MonoDomain *domain = mono_domain_get ();
484 len = g_list_length (list);
485 res = mono_array_new (domain, eclass, len);
487 for (i = 0; list; list = list->next, i++)
488 mono_array_set (res, gpointer, i, list->data);
494 * ves_icall_System_Security_SecurityFrame_GetSecurityStack:
495 * @skip: the number of stack frames to skip
497 * This function returns an managed array of containing the security
498 * informations for each frame (after the skipped ones). This is used for
499 * [NonCas]Demand[Choice] where the complete evaluation of the stack is
503 ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
505 MonoDomain *domain = mono_domain_get ();
506 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
507 MonoSecurityStack ss;
512 /* seems that MSC doesn't like having __asm in macros */
513 unsigned int stackptr;
514 mono_arch_flush_register_windows ();
515 __asm mov stackptr, ebp;
516 MONO_CONTEXT_SET_IP (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
517 MONO_CONTEXT_SET_BP (&ctx, stackptr);
519 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
524 mono_walk_stack (domain, jit_tls, &ctx, callback_get_stack_frames_security_info, (gpointer)&ss);
526 stack = glist_to_array (ss.stack, mono_defaults.runtimesecurityframe_class);
528 g_list_free (ss.stack);
533 #ifndef CUSTOM_EXCEPTION_HANDLING
536 * mono_handle_exception:
537 * @ctx: saved processor state
538 * @obj: the exception object
539 * @test_only: only test if the exception is caught, but dont call handlers
543 mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only)
545 MonoDomain *domain = mono_domain_get ();
546 MonoJitInfo *ji, rji;
547 static int (*call_filter) (MonoContext *, gpointer) = NULL;
548 static void (*restore_context) (void *);
549 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
550 MonoLMF *lmf = jit_tls->lmf;
551 MonoArray *initial_trace_ips = NULL;
552 GList *trace_ips = NULL;
553 MonoException *mono_ex;
554 gboolean stack_overflow = FALSE;
555 MonoContext initial_ctx;
557 gboolean gc_disabled = FALSE;
558 gboolean has_dynamic_methods = FALSE;
561 * This function might execute on an alternate signal stack, and Boehm GC
563 * Also, since the altstack is small, stack space intensive operations like
564 * JIT compilation should be avoided.
566 if (IS_ON_SIGALTSTACK (jit_tls)) {
568 * FIXME: disabling/enabling GC while already on a signal stack might
569 * not be safe either.
571 /* Have to reenable it later */
576 g_assert (ctx != NULL);
578 MonoException *ex = mono_get_exception_null_reference ();
579 ex->message = mono_string_new (domain, "Object reference not set to an instance of an object");
580 obj = (MonoObject *)ex;
584 * Allocate a new exception object instead of the preconstructed ones.
585 * We can't do this in sigsegv_signal_handler, since GC is not yet
588 if (obj == domain->stack_overflow_ex) {
589 obj = mono_get_exception_stack_overflow ();
590 stack_overflow = TRUE;
592 else if (obj == domain->null_reference_ex) {
593 obj = mono_get_exception_null_reference ();
596 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
597 mono_ex = (MonoException*)obj;
598 initial_trace_ips = mono_ex->trace_ips;
604 call_filter = mono_arch_get_call_filter ();
606 if (!restore_context)
607 restore_context = mono_arch_get_restore_context ();
609 g_assert (jit_tls->end_of_stack);
610 g_assert (jit_tls->abort_func);
613 MonoContext ctx_cp = *ctx;
614 if (mono_jit_trace_calls != NULL)
615 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
616 if (!mono_handle_exception (&ctx_cp, obj, original_ip, TRUE)) {
617 if (mono_break_on_exc)
619 mono_unhandled_exception (obj);
621 if (mono_debugger_unhandled_exception (original_ip, MONO_CONTEXT_GET_SP (ctx), obj)) {
623 * If this returns true, then we're running inside the
624 * Mono Debugger and the debugger wants us to restore the
625 * context and continue (normally, the debugger inserts
626 * a breakpoint on the `original_ip', so it regains control
627 * immediately after restoring the context).
629 MONO_CONTEXT_SET_IP (ctx, original_ip);
630 restore_context (ctx);
631 g_assert_not_reached ();
637 memset (&rji, 0, sizeof (rji));
643 ji = mono_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
644 NULL, &lmf, NULL, NULL);
646 g_warning ("Exception inside function without unwind info");
647 g_assert_not_reached ();
650 if (ji != (gpointer)-1) {
652 //printf ("M: %s %d %d.\n", mono_method_full_name (ji->method, TRUE), frame_count, test_only);
654 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
656 * Avoid overwriting the stack trace if the exception is
657 * rethrown. Also avoid giant stack traces during a stack
660 if (!initial_trace_ips && (frame_count < 1000)) {
661 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
665 if (ji->method->dynamic)
666 has_dynamic_methods = TRUE;
669 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
671 free_stack = 0xffffff;
674 * During stack overflow, wait till the unwinding frees some stack
675 * space before running handlers/finalizers.
677 if ((free_stack > (64 * 1024)) && ji->num_clauses) {
680 for (i = 0; i < ji->num_clauses; i++) {
681 MonoJitExceptionInfo *ei = &ji->clauses [i];
682 gboolean filtered = FALSE;
685 if (ei->try_start < MONO_CONTEXT_GET_IP (ctx) &&
687 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
689 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
692 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
693 /* store the exception object in cfg->excvar */
694 g_assert (ei->exvar_offset);
695 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = obj;
698 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
699 mono_debugger_handle_exception (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), obj);
700 filtered = call_filter (ctx, ei->data.filter);
703 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
704 mono_object_isinst (obj, ei->data.catch_class)) || filtered) {
706 if (mono_ex && !initial_trace_ips) {
707 trace_ips = g_list_reverse (trace_ips);
708 mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
709 if (has_dynamic_methods)
710 /* These methods could go away anytime, so compute the stack trace now */
711 mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
713 g_list_free (trace_ips);
719 if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
720 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
721 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
722 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
729 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
730 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
731 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
732 if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
733 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
734 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
735 call_filter (ctx, ei->handler_start);
745 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
752 if (IS_ON_SIGALTSTACK (jit_tls)) {
753 /* Switch back to normal stack */
755 /* Free up some stack space */
756 MONO_CONTEXT_SET_SP (&initial_ctx, (gssize)(MONO_CONTEXT_GET_SP (&initial_ctx)) + (64 * 1024));
757 MONO_CONTEXT_SET_IP (&initial_ctx, (gssize)jit_tls->abort_func);
758 restore_context (&initial_ctx);
761 jit_tls->abort_func (obj);
762 g_assert_not_reached ();
764 if (mono_ex && !initial_trace_ips) {
765 trace_ips = g_list_reverse (trace_ips);
766 mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
767 if (has_dynamic_methods)
768 /* These methods could go away anytime, so compute the stack trace now */
769 mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
771 g_list_free (trace_ips);
777 g_assert_not_reached ();
779 #endif /* CUSTOM_EXECPTION_HANDLING */