2 * exception.c: exception support
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #include <mono/arch/x86/x86-codegen.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/exception.h>
21 #include <mono/metadata/mono-debug.h>
30 /* use SIG* defines if possible */
35 /* sigcontext surrogate */
49 typedef void (* MonoW32ExceptionHandler) (int);
50 void win32_seh_init(void);
51 void win32_seh_cleanup(void);
52 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
66 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
68 static MonoW32ExceptionHandler fpe_handler;
69 static MonoW32ExceptionHandler ill_handler;
70 static MonoW32ExceptionHandler segv_handler;
72 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
74 #define W32_SEH_COPY_CONTEXT \
75 sctx->eax = ctx->Eax;\
76 sctx->ebx = ctx->Ebx;\
77 sctx->ecx = ctx->Ecx;\
78 sctx->edx = ctx->Edx;\
79 sctx->ebp = ctx->Ebp;\
80 sctx->esp = ctx->Esp;\
81 sctx->esi = ctx->Esi;\
82 sctx->edi = ctx->Edi;\
85 #define W32_SEH_HANDLE_EX(_ex) \
86 if (_ex##_handler) _ex##_handler((int)sctx)
89 * Unhandled Exception Filter
90 * Top-level per-process exception handler.
92 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
96 struct sigcontext* sctx;
99 res = EXCEPTION_CONTINUE_SEARCH;
101 er = ep->ExceptionRecord;
102 ctx = ep->ContextRecord;
103 sctx = g_malloc(sizeof(struct sigcontext));
106 switch (er->ExceptionCode) {
107 case EXCEPTION_ACCESS_VIOLATION:
108 W32_SEH_HANDLE_EX(segv);
110 case EXCEPTION_ILLEGAL_INSTRUCTION:
111 W32_SEH_HANDLE_EX(ill);
113 case EXCEPTION_INT_DIVIDE_BY_ZERO:
114 case EXCEPTION_INT_OVERFLOW:
115 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
116 case EXCEPTION_FLT_OVERFLOW:
117 case EXCEPTION_FLT_UNDERFLOW:
118 case EXCEPTION_FLT_INEXACT_RESULT:
119 W32_SEH_HANDLE_EX(fpe);
128 void win32_seh_init()
130 old_handler = SetUnhandledExceptionFilter(seh_handler);
133 void win32_seh_cleanup()
135 if (old_handler) SetUnhandledExceptionFilter(old_handler);
138 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
142 fpe_handler = handler;
145 ill_handler = handler;
148 segv_handler = handler;
155 #endif /* PLATFORM_WIN32 */
157 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
158 # define SC_EAX sc_eax
159 # define SC_EBX sc_ebx
160 # define SC_ECX sc_ecx
161 # define SC_EDX sc_edx
162 # define SC_EBP sc_ebp
163 # define SC_EIP sc_eip
164 # define SC_ESP sc_esp
165 # define SC_EDI sc_edi
166 # define SC_ESI sc_esi
179 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
181 typedef struct sigcontext MonoContext;
183 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->SC_EIP = (long)ip; } while (0);
184 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->SC_EBP = (long)bp; } while (0);
186 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->SC_EIP))
187 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->SC_EBP))
189 #ifdef MONO_USE_EXC_TABLES
191 /*************************************/
192 /* STACK UNWINDING STUFF */
193 /*************************************/
195 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
198 #define DWARF_FRAME_REGISTERS 17
200 typedef struct frame_state
206 long reg_or_offset[DWARF_FRAME_REGISTERS+1];
207 unsigned short cfa_reg;
208 unsigned short retaddr_column;
209 char saved[DWARF_FRAME_REGISTERS+1];
213 get_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum)
215 switch (dwarf_regnum) {
233 g_assert_not_reached ();
240 set_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum, long value)
242 switch (dwarf_regnum) {
271 g_assert_not_reached ();
275 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
277 static framesf frame_state_for = NULL;
279 static gboolean inited = FALSE;
281 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
283 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
286 init_frame_state_for (void)
291 * There are two versions of __frame_state_for: one in libgcc.a and the
292 * other in glibc.so. We need the version from glibc.
293 * For more info, see this:
294 * http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html
296 if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
298 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
299 frame_state_for = NULL;
301 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
302 get_backtrace_symbols = NULL;
303 frame_state_for = NULL;
306 g_module_close (module);
312 /* mono_arch_has_unwind_info:
314 * Tests if a function has an DWARF exception table able to restore
315 * all caller saved registers.
318 mono_arch_has_unwind_info (gconstpointer addr)
320 struct frame_state state_in;
321 struct frame_state *res;
324 init_frame_state_for ();
326 if (!frame_state_for)
331 memset (&state_in, 0, sizeof (state_in));
333 /* offset 10 is just a guess, but it works for all methods tested */
334 if ((res = frame_state_for ((char *)addr + 10, &state_in))) {
336 if (res->saved [X86_EBX] != 1 ||
337 res->saved [X86_EDI] != 1 ||
338 res->saved [X86_EBP] != 1 ||
339 res->saved [X86_ESI] != 1) {
351 void *return_address;
355 x86_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, struct sigcontext *ctx,
356 struct sigcontext *new_ctx, MonoLMF *lmf, char **trace)
358 struct stack_frame *frame;
361 struct frame_state state_in;
362 struct frame_state *res;
368 init_frame_state_for ();
370 if (!frame_state_for)
373 frame = MONO_CONTEXT_GET_BP (ctx);
375 max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
379 memset (&state_in, 0, sizeof (state_in));
381 while ((gpointer)frame->next < (gpointer)max_stack) {
382 gpointer ip, addr = frame->return_address;
384 char *tmp, **symbols;
387 ip = MONO_CONTEXT_GET_IP (new_ctx);
388 symbols = get_backtrace_symbols (&ip, 1);
390 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
392 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
399 if ((res = frame_state_for (addr, &state_in))) {
402 cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
403 frame = (struct stack_frame *)((gint8*)cfa - 8);
404 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
405 int how = res->saved[i];
407 g_assert ((how == 0) || (how == 1));
410 val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
411 set_sigcontext_reg (new_ctx, i, val);
414 new_ctx->SC_ESP = (long)cfa;
416 if (res->saved [X86_EBX] == 1 &&
417 res->saved [X86_EDI] == 1 &&
418 res->saved [X86_EBP] == 1 &&
419 res->saved [X86_ESI] == 1 &&
420 (ji = mono_jit_info_table_find (domain, frame->return_address))) {
421 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
426 //printf ("FRAME %p %p %p\n", frame, MONO_CONTEXT_GET_IP (new_ctx), mono_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (new_ctx)));
428 MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
430 MONO_CONTEXT_SET_BP (new_ctx, frame);
432 /* stop if !frame or when we detect an unexpected managed frame */
433 if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
444 //g_assert_not_reached ();
456 * arch_get_restore_context:
458 * Returns a pointer to a method which restores a previously saved sigcontext.
461 arch_get_restore_context (void)
463 static guint8 *start = NULL;
469 /* restore_contect (struct sigcontext *ctx) */
470 /* we do not restore X86_EAX, X86_EDX */
472 start = code = g_malloc (1024);
475 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
477 /* get return address, stored in EDX */
478 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EIP), 4);
480 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
482 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
484 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
486 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
488 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
490 /* jump to the saved IP */
491 x86_jump_reg (code, X86_EDX);
497 * arch_get_call_filter:
499 * Returns a pointer to a method which calls an exception filter. We
500 * also use this function to call finally handlers (we pass NULL as
501 * @exc object in this case).
504 arch_get_call_filter (void)
506 static guint8 start [64];
507 static int inited = 0;
514 /* call_filter (struct sigcontext *ctx, unsigned long eip) */
517 x86_push_reg (code, X86_EBP);
518 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
519 x86_push_reg (code, X86_EBX);
520 x86_push_reg (code, X86_EDI);
521 x86_push_reg (code, X86_ESI);
524 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
526 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
528 x86_push_reg (code, X86_EBP);
531 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
532 /* restore registers used by global register allocation (EBX & ESI) */
533 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
534 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
535 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
537 /* call the handler */
538 x86_call_reg (code, X86_ECX);
541 x86_pop_reg (code, X86_EBP);
543 /* restore saved regs */
544 x86_pop_reg (code, X86_ESI);
545 x86_pop_reg (code, X86_EDI);
546 x86_pop_reg (code, X86_EBX);
550 g_assert ((code - start) < 64);
555 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
556 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
557 unsigned long eip, unsigned long esp)
559 static void (*restore_context) (struct sigcontext *);
560 struct sigcontext ctx;
562 if (!restore_context)
563 restore_context = arch_get_restore_context ();
565 /* adjust eip so that it point into the call instruction */
578 mono_arch_handle_exception (&ctx, exc, FALSE);
579 restore_context (&ctx);
581 g_assert_not_reached ();
585 * arch_get_throw_exception:
587 * Returns a function pointer which can be used to raise
588 * exceptions. The returned function has the following
589 * signature: void (*func) (MonoException *exc);
590 * For example to raise an arithmetic exception you can use:
592 * x86_push_imm (code, mono_get_exception_arithmetic ());
593 * x86_call_code (code, arch_get_throw_exception ());
597 mono_arch_get_throw_exception (void)
599 static guint8 start [24];
600 static int inited = 0;
609 x86_push_reg (code, X86_ESP);
610 x86_push_membase (code, X86_ESP, 4); /* IP */
611 x86_push_membase (code, X86_ESP, 12); /* exception */
612 x86_push_reg (code, X86_EBP);
613 x86_push_reg (code, X86_EDI);
614 x86_push_reg (code, X86_ESI);
615 x86_push_reg (code, X86_EBX);
616 x86_push_reg (code, X86_EDX);
617 x86_push_reg (code, X86_ECX);
618 x86_push_reg (code, X86_EAX);
619 x86_call_code (code, throw_exception);
620 /* we should never reach this breakpoint */
621 x86_breakpoint (code);
623 g_assert ((code - start) < 24);
628 * arch_get_throw_exception_by_name:
630 * Returns a function pointer which can be used to raise
631 * corlib exceptions. The returned function has the following
632 * signature: void (*func) (char *exc_name);
633 * For example to raise an arithmetic exception you can use:
635 * x86_push_imm (code, "ArithmeticException");
636 * x86_call_code (code, arch_get_throw_exception_by_name ());
640 mono_arch_get_throw_exception_by_name (void)
642 static guint8 start [32];
643 static int inited = 0;
652 x86_push_membase (code, X86_ESP, 4); /* exception name */
653 x86_push_imm (code, "System");
654 x86_push_imm (code, mono_defaults.exception_class->image);
655 x86_call_code (code, mono_exception_from_name);
656 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
657 /* save the newly create object (overwrite exception name)*/
658 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
659 x86_jump_code (code, mono_arch_get_throw_exception ());
661 g_assert ((code - start) < 32);
667 glist_to_array (GList *list)
669 MonoDomain *domain = mono_domain_get ();
676 len = g_list_length (list);
677 res = mono_array_new (domain, mono_defaults.int_class, len);
679 for (i = 0; list; list = list->next, i++)
680 mono_array_set (res, gpointer, i, list->data);
685 /* mono_arch_find_jit_info:
687 * This function is used to gather information from @ctx. It return the
688 * MonoJitInfo of the corresponding function, unwinds one stack frame and
689 * stores the resulting context into @new_ctx. It also stores a string
690 * describing the stack location into @trace (if not NULL), and modifies
691 * the @lmf if necessary. @native_offset return the IP offset from the
692 * start of the function or -1 if that info is not available.
695 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoContext *ctx,
696 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
700 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
702 ji = mono_jit_info_table_find (domain, ip);
714 char *source_location, *tmpaddr, *fname;
715 gint32 address, iloffset;
720 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
721 /* remove any unused lmf */
722 *lmf = (*lmf)->previous_lmf;
725 address = (char *)ip - (char *)ji->code_start;
728 *native_offset = address;
731 if (!ji->method->wrapper_type)
735 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
736 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
739 tmpaddr = g_strdup_printf ("<0x%05x>", address);
741 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
743 fname = mono_method_full_name (ji->method, TRUE);
746 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
748 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
751 g_free (source_location);
756 /* restore caller saved registers */
757 if (ji->used_regs & X86_EBX_MASK) {
758 new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
761 if (ji->used_regs & X86_EDI_MASK) {
762 new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
765 if (ji->used_regs & X86_ESI_MASK) {
766 new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
769 new_ctx->SC_ESP = ctx->SC_EBP;
770 /* we substract 1, so that the IP points into the call instruction */
771 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
772 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
776 #ifdef MONO_USE_EXC_TABLES
777 } else if ((ji = x86_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
789 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
791 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
794 memset (res, 0, sizeof (MonoJitInfo));
795 res->method = (*lmf)->method;
798 new_ctx->SC_ESI = (*lmf)->esi;
799 new_ctx->SC_EDI = (*lmf)->edi;
800 new_ctx->SC_EBX = (*lmf)->ebx;
801 new_ctx->SC_EBP = (*lmf)->ebp;
802 new_ctx->SC_EIP = (*lmf)->eip;
803 /* the lmf is always stored on the stack, so the following
804 * expression points to a stack location which can be used as ESP */
805 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
807 *lmf = (*lmf)->previous_lmf;
817 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
819 MonoDomain *domain = mono_domain_get ();
821 MonoArray *ta = exc->trace_ips;
825 /* Exception is not thrown yet */
826 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
829 len = mono_array_length (ta);
831 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
833 for (i = skip; i < len; i++) {
835 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
836 gpointer ip = mono_array_get (ta, gpointer, i);
838 ji = mono_jit_info_table_find (domain, ip);
840 /* Unmanaged frame */
841 mono_array_set (res, gpointer, i, sf);
845 g_assert (ji != NULL);
847 sf->method = mono_method_get_object (domain, ji->method, NULL);
848 sf->native_offset = (char *)ip - (char *)ji->code_start;
850 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
852 if (need_file_info) {
855 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
857 sf->filename = filename? mono_string_new (domain, filename): NULL;
863 mono_array_set (res, gpointer, i, sf);
870 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
871 MonoDomain *domain = mono_domain_get ();
872 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
873 MonoLMF *lmf = jit_tls->lmf;
874 MonoJitInfo *ji, rji;
875 gint native_offset, il_offset;
878 MonoContext ctx, new_ctx;
880 MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
881 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
883 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
885 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
888 if (ji == (gpointer)-1)
891 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
893 if (func (ji->method, native_offset, il_offset, managed, user_data))
901 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
902 MonoReflectionMethod **method,
903 gint32 *iloffset, gint32 *native_offset,
904 MonoString **file, gint32 *line, gint32 *column)
906 MonoDomain *domain = mono_domain_get ();
907 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
908 MonoLMF *lmf = jit_tls->lmf;
909 MonoJitInfo *ji, rji;
910 MonoContext ctx, new_ctx;
912 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
913 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
918 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
922 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
925 /* skip all wrappers ??*/
926 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
927 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
928 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
935 *method = mono_method_get_object (domain, ji->method, NULL);
936 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
938 if (need_file_info) {
941 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
943 *file = filename? mono_string_new (domain, filename): NULL;
953 * arch_handle_exception:
954 * @ctx: saved processor state
955 * @obj: the exception object
956 * @test_only: only test if the exception is caught, but dont call handlers
961 mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
963 MonoDomain *domain = mono_domain_get ();
964 MonoJitInfo *ji, rji;
965 static int (*call_filter) (MonoContext *, gpointer) = NULL;
966 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
967 MonoLMF *lmf = jit_tls->lmf;
968 GList *trace_ips = NULL;
970 g_assert (ctx != NULL);
972 MonoException *ex = mono_get_exception_null_reference ();
973 ex->message = mono_string_new (domain,
974 "Object reference not set to an instance of an object");
975 obj = (MonoObject *)ex;
978 g_assert (mono_object_isinst (obj, mono_defaults.exception_class));
981 call_filter = arch_get_call_filter ();
983 g_assert (jit_tls->end_of_stack);
984 g_assert (jit_tls->abort_func);
987 MonoContext ctx_cp = *ctx;
988 if (mono_jit_trace_calls)
989 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
990 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
991 if (mono_break_on_exc)
993 mono_unhandled_exception (obj);
1001 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, ctx, &new_ctx,
1002 test_only ? &trace : NULL, &lmf, NULL, NULL);
1004 g_warning ("Exception inside function without unwind info");
1005 g_assert_not_reached ();
1008 if (ji != (gpointer)-1) {
1010 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE) {
1013 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1015 if (!((MonoException*)obj)->stack_trace)
1016 strace = g_strdup ("");
1018 strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
1020 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1023 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
1028 if (ji->num_clauses) {
1031 g_assert (ji->clauses);
1033 for (i = 0; i < ji->num_clauses; i++) {
1034 MonoJitExceptionInfo *ei = &ji->clauses [i];
1036 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1037 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1040 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
1041 /* store the exception object int cfg->excvar */
1042 g_assert (ji->exvar_offset);
1043 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1046 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1047 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1048 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1049 call_filter (ctx, ei->data.filter)))) {
1051 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
1052 g_list_free (trace_ips);
1056 if (mono_jit_trace_calls)
1057 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1058 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1063 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1064 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1065 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1066 if (mono_jit_trace_calls)
1067 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1068 call_filter (ctx, ei->handler_start);
1080 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1083 jit_tls->abort_func (obj);
1084 g_assert_not_reached ();
1086 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
1087 g_list_free (trace_ips);
1093 g_assert_not_reached ();