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, gpointer exc) */
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);
530 x86_push_membase (code, X86_EBP, 16);
532 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
533 /* restore registers used by global register allocation (EBX & ESI) */
534 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
535 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
536 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
537 /* save the ESP - this is used by endfinally */
538 x86_mov_membase_reg (code, X86_EBP, mono_exc_esp_offset, X86_ESP, 4);
539 /* call the handler */
540 x86_call_reg (code, X86_ECX);
541 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
543 x86_pop_reg (code, X86_EBP);
544 /* restore saved regs */
545 x86_pop_reg (code, X86_ESI);
546 x86_pop_reg (code, X86_EDI);
547 x86_pop_reg (code, X86_EBX);
551 g_assert ((code - start) < 64);
556 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
557 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
558 unsigned long eip, unsigned long esp)
560 static void (*restore_context) (struct sigcontext *);
561 struct sigcontext ctx;
563 if (!restore_context)
564 restore_context = arch_get_restore_context ();
566 /* adjust eip so that it point into the call instruction */
579 mono_arch_handle_exception (&ctx, exc, FALSE);
580 restore_context (&ctx);
582 g_assert_not_reached ();
586 * arch_get_throw_exception:
588 * Returns a function pointer which can be used to raise
589 * exceptions. The returned function has the following
590 * signature: void (*func) (MonoException *exc);
591 * For example to raise an arithmetic exception you can use:
593 * x86_push_imm (code, mono_get_exception_arithmetic ());
594 * x86_call_code (code, arch_get_throw_exception ());
598 mono_arch_get_throw_exception (void)
600 static guint8 start [24];
601 static int inited = 0;
610 x86_push_reg (code, X86_ESP);
611 x86_push_membase (code, X86_ESP, 4); /* IP */
612 x86_push_membase (code, X86_ESP, 12); /* exception */
613 x86_push_reg (code, X86_EBP);
614 x86_push_reg (code, X86_EDI);
615 x86_push_reg (code, X86_ESI);
616 x86_push_reg (code, X86_EBX);
617 x86_push_reg (code, X86_EDX);
618 x86_push_reg (code, X86_ECX);
619 x86_push_reg (code, X86_EAX);
620 x86_call_code (code, throw_exception);
621 /* we should never reach this breakpoint */
622 x86_breakpoint (code);
624 g_assert ((code - start) < 24);
629 * arch_get_throw_exception_by_name:
631 * Returns a function pointer which can be used to raise
632 * corlib exceptions. The returned function has the following
633 * signature: void (*func) (char *exc_name);
634 * For example to raise an arithmetic exception you can use:
636 * x86_push_imm (code, "ArithmeticException");
637 * x86_call_code (code, arch_get_throw_exception_by_name ());
641 mono_arch_get_throw_exception_by_name (void)
643 static guint8 start [32];
644 static int inited = 0;
653 x86_push_membase (code, X86_ESP, 4); /* exception name */
654 x86_push_imm (code, "System");
655 x86_push_imm (code, mono_defaults.exception_class->image);
656 x86_call_code (code, mono_exception_from_name);
657 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
658 /* save the newly create object (overwrite exception name)*/
659 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
660 x86_jump_code (code, mono_arch_get_throw_exception ());
662 g_assert ((code - start) < 32);
668 glist_to_array (GList *list)
670 MonoDomain *domain = mono_domain_get ();
677 len = g_list_length (list);
678 res = mono_array_new (domain, mono_defaults.int_class, len);
680 for (i = 0; list; list = list->next, i++)
681 mono_array_set (res, gpointer, i, list->data);
686 /* mono_arch_find_jit_info:
688 * This function is used to gather information from @ctx. It return the
689 * MonoJitInfo of the corresponding function, unwinds one stack frame and
690 * stores the resulting context into @new_ctx. It also stores a string
691 * describing the stack location into @trace (if not NULL), and modifies
692 * the @lmf if necessary. @native_offset return the IP offset from the
693 * start of the function or -1 if that info is not available.
696 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoContext *ctx,
697 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
701 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
703 ji = mono_jit_info_table_find (domain, ip);
715 char *source_location, *tmpaddr, *fname;
716 gint32 address, iloffset;
721 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
722 /* remove any unused lmf */
723 *lmf = (*lmf)->previous_lmf;
726 address = (char *)ip - (char *)ji->code_start;
729 *native_offset = address;
732 if (!ji->method->wrapper_type)
736 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
737 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
740 tmpaddr = g_strdup_printf ("<0x%05x>", address);
742 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
744 fname = mono_method_full_name (ji->method, TRUE);
747 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
749 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
752 g_free (source_location);
757 /* restore caller saved registers */
758 if (ji->used_regs & X86_EBX_MASK) {
759 new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
762 if (ji->used_regs & X86_EDI_MASK) {
763 new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
766 if (ji->used_regs & X86_ESI_MASK) {
767 new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
770 new_ctx->SC_ESP = ctx->SC_EBP;
771 /* we substract 1, so that the IP points into the call instruction */
772 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
773 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
777 #ifdef MONO_USE_EXC_TABLES
778 } else if ((ji = x86_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
790 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
792 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
795 memset (res, 0, sizeof (MonoJitInfo));
796 res->method = (*lmf)->method;
799 new_ctx->SC_ESI = (*lmf)->esi;
800 new_ctx->SC_EDI = (*lmf)->edi;
801 new_ctx->SC_EBX = (*lmf)->ebx;
802 new_ctx->SC_EBP = (*lmf)->ebp;
803 new_ctx->SC_EIP = (*lmf)->eip;
804 /* the lmf is always stored on the stack, so the following
805 * expression points to a stack location which can be used as ESP */
806 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
808 *lmf = (*lmf)->previous_lmf;
818 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
820 MonoDomain *domain = mono_domain_get ();
822 MonoArray *ta = exc->trace_ips;
826 /* Exception is not thrown yet */
827 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
830 len = mono_array_length (ta);
832 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
834 for (i = skip; i < len; i++) {
836 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
837 gpointer ip = mono_array_get (ta, gpointer, i);
839 ji = mono_jit_info_table_find (domain, ip);
841 /* Unmanaged frame */
842 mono_array_set (res, gpointer, i, sf);
846 g_assert (ji != NULL);
848 sf->method = mono_method_get_object (domain, ji->method, NULL);
849 sf->native_offset = (char *)ip - (char *)ji->code_start;
851 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
853 if (need_file_info) {
856 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
858 sf->filename = filename? mono_string_new (domain, filename): NULL;
864 mono_array_set (res, gpointer, i, sf);
871 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
872 MonoDomain *domain = mono_domain_get ();
873 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
874 MonoLMF *lmf = jit_tls->lmf;
875 MonoJitInfo *ji, rji;
876 gint native_offset, il_offset;
879 MonoContext ctx, new_ctx;
881 MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
882 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
884 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
886 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
889 if (ji == (gpointer)-1)
892 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
894 if (func (ji->method, native_offset, il_offset, managed, user_data))
902 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
903 MonoReflectionMethod **method,
904 gint32 *iloffset, gint32 *native_offset,
905 MonoString **file, gint32 *line, gint32 *column)
907 MonoDomain *domain = mono_domain_get ();
908 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
909 MonoLMF *lmf = jit_tls->lmf;
910 MonoJitInfo *ji, rji;
911 MonoContext ctx, new_ctx;
913 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
914 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
919 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
923 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
926 /* skip all wrappers ??*/
927 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
928 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
929 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
936 *method = mono_method_get_object (domain, ji->method, NULL);
937 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
939 if (need_file_info) {
942 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
944 *file = filename? mono_string_new (domain, filename): NULL;
954 * arch_handle_exception:
955 * @ctx: saved processor state
956 * @obj: the exception object
957 * @test_only: only test if the exception is caught, but dont call handlers
962 mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
964 MonoDomain *domain = mono_domain_get ();
965 MonoJitInfo *ji, rji;
966 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
967 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
968 MonoLMF *lmf = jit_tls->lmf;
969 GList *trace_ips = NULL;
971 g_assert (ctx != NULL);
973 MonoException *ex = mono_get_exception_null_reference ();
974 ex->message = mono_string_new (domain,
975 "Object reference not set to an instance of an object");
976 obj = (MonoObject *)ex;
979 g_assert (mono_object_isinst (obj, mono_defaults.exception_class));
982 call_filter = arch_get_call_filter ();
984 g_assert (jit_tls->end_of_stack);
985 g_assert (jit_tls->abort_func);
988 MonoContext ctx_cp = *ctx;
989 if (mono_jit_trace_calls)
990 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
991 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
992 if (mono_break_on_exc)
994 mono_unhandled_exception (obj);
1002 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, ctx, &new_ctx,
1003 test_only ? &trace : NULL, &lmf, NULL, NULL);
1005 g_warning ("Exception inside function without unwind info");
1006 g_assert_not_reached ();
1009 if (ji != (gpointer)-1) {
1011 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE) {
1014 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1016 if (!((MonoException*)obj)->stack_trace)
1017 strace = g_strdup ("");
1019 strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
1021 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1024 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
1029 if (ji->num_clauses) {
1032 g_assert (ji->clauses);
1034 for (i = 0; i < ji->num_clauses; i++) {
1035 MonoJitExceptionInfo *ei = &ji->clauses [i];
1037 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1038 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1040 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1041 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1042 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1043 call_filter (ctx, ei->data.filter, obj)))) {
1045 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
1046 g_list_free (trace_ips);
1049 if (mono_jit_trace_calls)
1050 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1051 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1052 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1056 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1057 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1058 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1059 if (mono_jit_trace_calls)
1060 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1061 call_filter (ctx, ei->handler_start, NULL);
1073 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1076 jit_tls->abort_func (obj);
1077 g_assert_not_reached ();
1079 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
1080 g_list_free (trace_ips);
1086 g_assert_not_reached ();