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_HANDLE_EX(_ex) \
75 if (_ex##_handler) _ex##_handler((int)sctx)
78 * Unhandled Exception Filter
79 * Top-level per-process exception handler.
81 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
85 struct sigcontext* sctx;
88 res = EXCEPTION_CONTINUE_EXECUTION;
90 er = ep->ExceptionRecord;
91 ctx = ep->ContextRecord;
92 sctx = g_malloc(sizeof(struct sigcontext));
94 /* Copy Win32 context to UNIX style context */
100 sctx->esp = ctx->Esp;
101 sctx->esi = ctx->Esi;
102 sctx->edi = ctx->Edi;
103 sctx->eip = ctx->Eip;
105 switch (er->ExceptionCode) {
106 case EXCEPTION_ACCESS_VIOLATION:
107 W32_SEH_HANDLE_EX(segv);
109 case EXCEPTION_ILLEGAL_INSTRUCTION:
110 W32_SEH_HANDLE_EX(ill);
112 case EXCEPTION_INT_DIVIDE_BY_ZERO:
113 case EXCEPTION_INT_OVERFLOW:
114 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
115 case EXCEPTION_FLT_OVERFLOW:
116 case EXCEPTION_FLT_UNDERFLOW:
117 case EXCEPTION_FLT_INEXACT_RESULT:
118 W32_SEH_HANDLE_EX(fpe);
124 /* Copy context back */
125 ctx->Eax = sctx->eax;
126 ctx->Ebx = sctx->ebx;
127 ctx->Ecx = sctx->ecx;
128 ctx->Edx = sctx->edx;
129 ctx->Ebp = sctx->ebp;
130 ctx->Esp = sctx->esp;
131 ctx->Esi = sctx->esi;
132 ctx->Edi = sctx->edi;
133 ctx->Eip = sctx->eip;
138 void win32_seh_init()
140 old_handler = SetUnhandledExceptionFilter(seh_handler);
143 void win32_seh_cleanup()
145 if (old_handler) SetUnhandledExceptionFilter(old_handler);
148 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
152 fpe_handler = handler;
155 ill_handler = handler;
158 segv_handler = handler;
165 #endif /* PLATFORM_WIN32 */
167 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
168 # define SC_EAX sc_eax
169 # define SC_EBX sc_ebx
170 # define SC_ECX sc_ecx
171 # define SC_EDX sc_edx
172 # define SC_EBP sc_ebp
173 # define SC_EIP sc_eip
174 # define SC_ESP sc_esp
175 # define SC_EDI sc_edi
176 # define SC_ESI sc_esi
189 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
191 typedef struct sigcontext MonoContext;
193 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->SC_EIP = (long)ip; } while (0);
194 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->SC_EBP = (long)bp; } while (0);
196 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->SC_EIP))
197 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->SC_EBP))
199 #ifdef MONO_USE_EXC_TABLES
201 /*************************************/
202 /* STACK UNWINDING STUFF */
203 /*************************************/
205 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
208 #define DWARF_FRAME_REGISTERS 17
210 typedef struct frame_state
216 long reg_or_offset[DWARF_FRAME_REGISTERS+1];
217 unsigned short cfa_reg;
218 unsigned short retaddr_column;
219 char saved[DWARF_FRAME_REGISTERS+1];
223 get_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum)
225 switch (dwarf_regnum) {
243 g_assert_not_reached ();
250 set_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum, long value)
252 switch (dwarf_regnum) {
281 g_assert_not_reached ();
285 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
287 static framesf frame_state_for = NULL;
289 static gboolean inited = FALSE;
291 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
293 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
296 init_frame_state_for (void)
301 * There are two versions of __frame_state_for: one in libgcc.a and the
302 * other in glibc.so. We need the version from glibc.
303 * For more info, see this:
304 * http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html
306 if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
308 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
309 frame_state_for = NULL;
311 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
312 get_backtrace_symbols = NULL;
313 frame_state_for = NULL;
316 g_module_close (module);
322 /* mono_arch_has_unwind_info:
324 * Tests if a function has an DWARF exception table able to restore
325 * all caller saved registers.
328 mono_arch_has_unwind_info (gconstpointer addr)
330 struct frame_state state_in;
331 struct frame_state *res;
334 init_frame_state_for ();
336 if (!frame_state_for)
341 memset (&state_in, 0, sizeof (state_in));
343 /* offset 10 is just a guess, but it works for all methods tested */
344 if ((res = frame_state_for ((char *)addr + 10, &state_in))) {
346 if (res->saved [X86_EBX] == 1 &&
347 res->saved [X86_EDI] == 1 &&
348 res->saved [X86_EBP] == 1 &&
349 res->saved [X86_ESI] == 1)
359 void *return_address;
363 x86_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, struct sigcontext *ctx,
364 struct sigcontext *new_ctx, MonoLMF *lmf, char **trace)
366 struct stack_frame *frame;
369 struct frame_state state_in;
370 struct frame_state *res;
376 init_frame_state_for ();
378 if (!frame_state_for)
381 frame = MONO_CONTEXT_GET_BP (ctx);
383 max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
387 memset (&state_in, 0, sizeof (state_in));
389 while ((gpointer)frame->next < (gpointer)max_stack) {
390 gpointer ip, addr = frame->return_address;
392 char *tmp, **symbols;
395 ip = MONO_CONTEXT_GET_IP (new_ctx);
396 symbols = get_backtrace_symbols (&ip, 1);
398 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
400 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
407 if ((res = frame_state_for (addr, &state_in))) {
410 cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
411 frame = (struct stack_frame *)((gint8*)cfa - 8);
412 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
413 int how = res->saved[i];
415 g_assert ((how == 0) || (how == 1));
418 val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
419 set_sigcontext_reg (new_ctx, i, val);
422 new_ctx->SC_ESP = (long)cfa;
424 if (res->saved [X86_EBX] == 1 &&
425 res->saved [X86_EDI] == 1 &&
426 res->saved [X86_EBP] == 1 &&
427 res->saved [X86_ESI] == 1 &&
428 (ji = mono_jit_info_table_find (domain, frame->return_address))) {
429 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
434 //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)));
436 MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
438 MONO_CONTEXT_SET_BP (new_ctx, frame);
440 /* stop if !frame or when we detect an unexpected managed frame */
441 if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
452 //g_assert_not_reached ();
464 * arch_get_restore_context:
466 * Returns a pointer to a method which restores a previously saved sigcontext.
469 arch_get_restore_context (void)
471 static guint8 *start = NULL;
477 /* restore_contect (struct sigcontext *ctx) */
478 /* we do not restore X86_EAX, X86_EDX */
480 start = code = g_malloc (1024);
483 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
485 /* get return address, stored in EDX */
486 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EIP), 4);
488 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
490 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
492 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
494 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
496 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
498 /* jump to the saved IP */
499 x86_jump_reg (code, X86_EDX);
505 * arch_get_call_filter:
507 * Returns a pointer to a method which calls an exception filter. We
508 * also use this function to call finally handlers (we pass NULL as
509 * @exc object in this case).
512 arch_get_call_filter (void)
514 static guint8 start [64];
515 static int inited = 0;
522 /* call_filter (struct sigcontext *ctx, unsigned long eip) */
525 x86_push_reg (code, X86_EBP);
526 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
527 x86_push_reg (code, X86_EBX);
528 x86_push_reg (code, X86_EDI);
529 x86_push_reg (code, X86_ESI);
532 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
534 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
536 x86_push_reg (code, X86_EBP);
539 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
540 /* restore registers used by global register allocation (EBX & ESI) */
541 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
542 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
543 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
545 /* call the handler */
546 x86_call_reg (code, X86_ECX);
549 x86_pop_reg (code, X86_EBP);
551 /* restore saved regs */
552 x86_pop_reg (code, X86_ESI);
553 x86_pop_reg (code, X86_EDI);
554 x86_pop_reg (code, X86_EBX);
558 g_assert ((code - start) < 64);
563 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
564 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
565 unsigned long eip, unsigned long esp)
567 static void (*restore_context) (struct sigcontext *);
568 struct sigcontext ctx;
570 if (!restore_context)
571 restore_context = arch_get_restore_context ();
573 /* adjust eip so that it point into the call instruction */
586 mono_arch_handle_exception (&ctx, exc, FALSE);
587 restore_context (&ctx);
589 g_assert_not_reached ();
593 * arch_get_throw_exception:
595 * Returns a function pointer which can be used to raise
596 * exceptions. The returned function has the following
597 * signature: void (*func) (MonoException *exc);
598 * For example to raise an arithmetic exception you can use:
600 * x86_push_imm (code, mono_get_exception_arithmetic ());
601 * x86_call_code (code, arch_get_throw_exception ());
605 mono_arch_get_throw_exception (void)
607 static guint8 start [24];
608 static int inited = 0;
617 x86_push_reg (code, X86_ESP);
618 x86_push_membase (code, X86_ESP, 4); /* IP */
619 x86_push_membase (code, X86_ESP, 12); /* exception */
620 x86_push_reg (code, X86_EBP);
621 x86_push_reg (code, X86_EDI);
622 x86_push_reg (code, X86_ESI);
623 x86_push_reg (code, X86_EBX);
624 x86_push_reg (code, X86_EDX);
625 x86_push_reg (code, X86_ECX);
626 x86_push_reg (code, X86_EAX);
627 x86_call_code (code, throw_exception);
628 /* we should never reach this breakpoint */
629 x86_breakpoint (code);
631 g_assert ((code - start) < 24);
636 * arch_get_throw_exception_by_name:
638 * Returns a function pointer which can be used to raise
639 * corlib exceptions. The returned function has the following
640 * signature: void (*func) (char *exc_name);
641 * For example to raise an arithmetic exception you can use:
643 * x86_push_imm (code, "ArithmeticException");
644 * x86_call_code (code, arch_get_throw_exception_by_name ());
648 mono_arch_get_throw_exception_by_name (void)
650 static guint8 start [32];
651 static int inited = 0;
660 x86_push_membase (code, X86_ESP, 4); /* exception name */
661 x86_push_imm (code, "System");
662 x86_push_imm (code, mono_defaults.exception_class->image);
663 x86_call_code (code, mono_exception_from_name);
664 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
665 /* save the newly create object (overwrite exception name)*/
666 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
667 x86_jump_code (code, mono_arch_get_throw_exception ());
669 g_assert ((code - start) < 32);
675 glist_to_array (GList *list)
677 MonoDomain *domain = mono_domain_get ();
684 len = g_list_length (list);
685 res = mono_array_new (domain, mono_defaults.int_class, len);
687 for (i = 0; list; list = list->next, i++)
688 mono_array_set (res, gpointer, i, list->data);
693 /* mono_arch_find_jit_info:
695 * This function is used to gather information from @ctx. It return the
696 * MonoJitInfo of the corresponding function, unwinds one stack frame and
697 * stores the resulting context into @new_ctx. It also stores a string
698 * describing the stack location into @trace (if not NULL), and modifies
699 * the @lmf if necessary. @native_offset return the IP offset from the
700 * start of the function or -1 if that info is not available.
703 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoContext *ctx,
704 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
708 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
710 ji = mono_jit_info_table_find (domain, ip);
722 char *source_location, *tmpaddr, *fname;
723 gint32 address, iloffset;
728 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
729 /* remove any unused lmf */
730 *lmf = (*lmf)->previous_lmf;
733 address = (char *)ip - (char *)ji->code_start;
736 *native_offset = address;
739 if (!ji->method->wrapper_type)
743 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
744 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
747 tmpaddr = g_strdup_printf ("<0x%05x>", address);
749 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
751 fname = mono_method_full_name (ji->method, TRUE);
754 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
756 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
759 g_free (source_location);
764 /* restore caller saved registers */
765 if (ji->used_regs & X86_EBX_MASK) {
766 new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
769 if (ji->used_regs & X86_EDI_MASK) {
770 new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
773 if (ji->used_regs & X86_ESI_MASK) {
774 new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
777 new_ctx->SC_ESP = ctx->SC_EBP;
778 /* we substract 1, so that the IP points into the call instruction */
779 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
780 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
784 #ifdef MONO_USE_EXC_TABLES
785 } else if ((ji = x86_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
797 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
799 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
802 memset (res, 0, sizeof (MonoJitInfo));
803 res->method = (*lmf)->method;
806 new_ctx->SC_ESI = (*lmf)->esi;
807 new_ctx->SC_EDI = (*lmf)->edi;
808 new_ctx->SC_EBX = (*lmf)->ebx;
809 new_ctx->SC_EBP = (*lmf)->ebp;
810 new_ctx->SC_EIP = (*lmf)->eip;
811 /* the lmf is always stored on the stack, so the following
812 * expression points to a stack location which can be used as ESP */
813 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
815 *lmf = (*lmf)->previous_lmf;
825 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
827 MonoDomain *domain = mono_domain_get ();
829 MonoArray *ta = exc->trace_ips;
833 /* Exception is not thrown yet */
834 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
837 len = mono_array_length (ta);
839 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
841 for (i = skip; i < len; i++) {
843 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
844 gpointer ip = mono_array_get (ta, gpointer, i);
846 ji = mono_jit_info_table_find (domain, ip);
848 /* Unmanaged frame */
849 mono_array_set (res, gpointer, i, sf);
853 g_assert (ji != NULL);
855 sf->method = mono_method_get_object (domain, ji->method, NULL);
856 sf->native_offset = (char *)ip - (char *)ji->code_start;
858 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
860 if (need_file_info) {
863 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
865 sf->filename = filename? mono_string_new (domain, filename): NULL;
871 mono_array_set (res, gpointer, i, sf);
878 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
879 MonoDomain *domain = mono_domain_get ();
880 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
881 MonoLMF *lmf = jit_tls->lmf;
882 MonoJitInfo *ji, rji;
883 gint native_offset, il_offset;
886 MonoContext ctx, new_ctx;
888 MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
889 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
891 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
893 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
896 if (ji == (gpointer)-1)
899 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
901 if (func (ji->method, native_offset, il_offset, managed, user_data))
909 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
910 MonoReflectionMethod **method,
911 gint32 *iloffset, gint32 *native_offset,
912 MonoString **file, gint32 *line, gint32 *column)
914 MonoDomain *domain = mono_domain_get ();
915 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
916 MonoLMF *lmf = jit_tls->lmf;
917 MonoJitInfo *ji, rji;
918 MonoContext ctx, new_ctx;
920 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
921 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
926 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
930 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
933 /* skip all wrappers ??*/
934 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
935 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
936 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
943 *method = mono_method_get_object (domain, ji->method, NULL);
944 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
946 if (need_file_info) {
949 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
951 *file = filename? mono_string_new (domain, filename): NULL;
961 * arch_handle_exception:
962 * @ctx: saved processor state
963 * @obj: the exception object
964 * @test_only: only test if the exception is caught, but dont call handlers
969 mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
971 MonoDomain *domain = mono_domain_get ();
972 MonoJitInfo *ji, rji;
973 static int (*call_filter) (MonoContext *, gpointer) = NULL;
974 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
975 MonoLMF *lmf = jit_tls->lmf;
976 GList *trace_ips = NULL;
978 g_assert (ctx != NULL);
980 MonoException *ex = mono_get_exception_null_reference ();
981 ex->message = mono_string_new (domain,
982 "Object reference not set to an instance of an object");
983 obj = (MonoObject *)ex;
986 g_assert (mono_object_isinst (obj, mono_defaults.exception_class));
989 call_filter = arch_get_call_filter ();
991 g_assert (jit_tls->end_of_stack);
992 g_assert (jit_tls->abort_func);
995 MonoContext ctx_cp = *ctx;
996 if (mono_jit_trace_calls)
997 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
998 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
999 if (mono_break_on_exc)
1001 mono_unhandled_exception (obj);
1006 MonoContext new_ctx;
1009 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, ctx, &new_ctx,
1010 test_only ? &trace : NULL, &lmf, NULL, NULL);
1012 g_warning ("Exception inside function without unwind info");
1013 g_assert_not_reached ();
1016 if (ji != (gpointer)-1) {
1018 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE) {
1021 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1023 if (!((MonoException*)obj)->stack_trace)
1024 strace = g_strdup ("");
1026 strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
1028 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1031 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
1036 if (ji->num_clauses) {
1039 g_assert (ji->clauses);
1041 for (i = 0; i < ji->num_clauses; i++) {
1042 MonoJitExceptionInfo *ei = &ji->clauses [i];
1044 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1045 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1048 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
1049 /* store the exception object int cfg->excvar */
1050 g_assert (ji->exvar_offset);
1051 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1054 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1055 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1056 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1057 call_filter (ctx, ei->data.filter)))) {
1059 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
1060 g_list_free (trace_ips);
1064 if (mono_jit_trace_calls)
1065 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1066 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1071 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1072 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1073 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1074 if (mono_jit_trace_calls)
1075 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1076 call_filter (ctx, ei->handler_start);
1088 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1091 jit_tls->abort_func (obj);
1092 g_assert_not_reached ();
1094 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
1095 g_list_free (trace_ips);
1101 g_assert_not_reached ();