2 * exceptions-ppc.c: exception support for PowerPC
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001 Ximian, Inc.
16 #include <mono/arch/ppc/ppc-codegen.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/threads.h>
20 #include <mono/metadata/debug-helpers.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/mono-debug.h>
27 typedef struct sigcontext MonoContext;
32 int sc_onstack; // sigstack state to restore
33 int sc_mask; // signal mask to restore
35 int sc_psw; // processor status word
36 int sc_sp; // stack pointer if sc_regs == NULL
37 void *sc_regs; // (kernel private) saved state
42 sigset_t uc_sigmask; // signal mask used by this context
43 stack_t uc_stack; // stack used by this context
44 struct ucontext *uc_link; // pointer to resuming context
45 size_t uc_mcsize; // size of the machine context passed in
46 mcontext_t uc_mcontext; // machine specific context
49 typedef struct ppc_exception_state {
50 unsigned long dar; // Fault registers for coredump
52 unsigned long exception;// number of powerpc exception taken
53 unsigned long pad0; // align to 16 bytes
55 unsigned long pad1[4]; // space in PCB "just in case"
56 } ppc_exception_state_t;
58 typedef struct ppc_vector_state {
59 unsigned long save_vr[32][4];
60 unsigned long save_vscr[4];
61 unsigned int save_pad5[4];
62 unsigned int save_vrvalid; // VRs that have been saved
63 unsigned int save_pad6[7];
66 typedef struct ppc_float_state {
69 unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish
70 unsigned int fpscr; // floating point status register
73 typedef struct ppc_thread_state {
74 unsigned int srr0; // Instruction address register (PC)
75 unsigned int srr1; // Machine state register (supervisor)
81 unsigned int cr; // Condition register
82 unsigned int xer; // User's integer exception register
83 unsigned int lr; // Link register
84 unsigned int ctr; // Count register
85 unsigned int mq; // MQ register (601 only)
87 unsigned int vrsave; // Vector Save Register
91 ppc_exception_state_t es;
92 ppc_thread_state_t ss;
94 ppc_vector_state_t vs;
97 typedef struct mcontext * mcontext_t;
103 /* we have the stack pointer, not the base pointer in sigcontext */
104 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (int)ip; } while (0);
105 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (int)bp; } while (0);
107 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
108 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_sp))
112 unsigned long unused1;
116 #else /* sigcontext is different on linux/ppc. See /usr/include/asm/ptrace.h. */
118 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->regs->nip = (unsigned long)ip; } while (0);
119 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs->gpr[1] = (unsigned long)bp; } while (0);
121 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->regs->nip))
122 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs->gpr[1]))
131 /* disbale this for now */
132 #undef MONO_USE_EXC_TABLES
134 #ifdef MONO_USE_EXC_TABLES
136 /*************************************/
137 /* STACK UNWINDING STUFF */
138 /*************************************/
140 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
143 #define DWARF_FRAME_REGISTERS 17
145 typedef struct frame_state
151 long reg_or_offset[DWARF_FRAME_REGISTERS+1];
152 unsigned short cfa_reg;
153 unsigned short retaddr_column;
154 char saved[DWARF_FRAME_REGISTERS+1];
160 get_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum)
162 switch (dwarf_regnum) {
180 g_assert_not_reached ();
187 set_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum, long value)
189 switch (dwarf_regnum) {
218 g_assert_not_reached ();
222 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
224 static framesf frame_state_for = NULL;
226 static gboolean inited = FALSE;
228 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
230 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
233 init_frame_state_for (void)
238 * There are two versions of __frame_state_for: one in libgcc.a and the
239 * other in glibc.so. We need the version from glibc.
240 * For more info, see this:
241 * http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html
243 if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
245 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
246 frame_state_for = NULL;
248 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
249 get_backtrace_symbols = NULL;
250 frame_state_for = NULL;
253 g_module_close (module);
261 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
263 /* mono_arch_has_unwind_info:
265 * Tests if a function has an DWARF exception table able to restore
266 * all caller saved registers.
269 mono_arch_has_unwind_info (MonoMethod *method)
272 struct frame_state state_in;
273 struct frame_state *res;
276 init_frame_state_for ();
278 if (!frame_state_for)
281 g_assert (method->addr);
283 memset (&state_in, 0, sizeof (state_in));
285 /* offset 10 is just a guess, but it works for all methods tested */
286 if ((res = frame_state_for ((char *)method->addr + 10, &state_in))) {
288 if (res->saved [X86_EBX] != 1 ||
289 res->saved [X86_EDI] != 1 ||
290 res->saved [X86_EBP] != 1 ||
291 res->saved [X86_ESI] != 1) {
306 void *return_address;
310 ppc_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, struct sigcontext *ctx,
311 struct sigcontext *new_ctx, MonoLMF *lmf, char **trace)
314 struct stack_frame *frame;
317 struct frame_state state_in;
318 struct frame_state *res;
324 init_frame_state_for ();
326 if (!frame_state_for)
329 frame = MONO_CONTEXT_GET_BP (ctx);
331 max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
335 memset (&state_in, 0, sizeof (state_in));
337 while ((gpointer)frame->next < (gpointer)max_stack) {
338 gpointer ip, addr = frame->return_address;
340 char *tmp, **symbols;
343 ip = MONO_CONTEXT_GET_IP (new_ctx);
344 symbols = get_backtrace_symbols (&ip, 1);
346 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
348 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
355 if ((res = frame_state_for (addr, &state_in))) {
358 cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
359 frame = (struct stack_frame *)((gint8*)cfa - 8);
360 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
361 int how = res->saved[i];
363 g_assert ((how == 0) || (how == 1));
366 val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
367 set_sigcontext_reg (new_ctx, i, val);
370 new_ctx->SC_ESP = (long)cfa;
372 if (res->saved [X86_EBX] == 1 &&
373 res->saved [X86_EDI] == 1 &&
374 res->saved [X86_EBP] == 1 &&
375 res->saved [X86_ESI] == 1 &&
376 (ji = mono_jit_info_table_find (domain, frame->return_address))) {
377 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
382 //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)));
384 MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
386 MONO_CONTEXT_SET_BP (new_ctx, frame);
388 /* stop if !frame or when we detect an unexpected managed frame */
389 if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
400 //g_assert_not_reached ();
413 * arch_get_restore_context:
415 * Returns a pointer to a method which restores a previously saved sigcontext.
418 arch_get_restore_context (void)
420 static guint8 *start = NULL;
427 /* restore_contect (struct sigcontext *ctx) */
428 /* we do not restore X86_EAX, X86_EDX */
430 start = code = g_malloc (1024);
433 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
435 /* get return address, stored in EDX */
436 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EIP), 4);
438 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
440 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
442 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
444 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
446 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
448 /* jump to the saved IP */
449 x86_jump_reg (code, X86_EDX);
456 * arch_get_call_filter:
458 * Returns a pointer to a method which calls an exception filter. We
459 * also use this function to call finally handlers (we pass NULL as
460 * @exc object in this case).
463 arch_get_call_filter (void)
465 static guint8 start [196];
466 static int inited = 0;
468 int alloc_size, pos, i;
474 /* call_filter (struct sigcontext *ctx, unsigned long eip, gpointer exc) */
477 ppc_mflr (code, ppc_r0);
478 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
479 alloc_size = PPC_MINIMAL_STACK_SIZE + (sizeof (gulong) * 14 + sizeof (gdouble) * 14);
480 // align to PPC_STACK_ALIGNMENT bytes
481 if (alloc_size & (PPC_STACK_ALIGNMENT - 1))
482 alloc_size += PPC_STACK_ALIGNMENT - (alloc_size & (PPC_STACK_ALIGNMENT - 1));
483 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
484 /* save all the regs on the stack */
485 pos = PPC_MINIMAL_STACK_SIZE;
486 ppc_stmw (code, ppc_r13, ppc_sp, pos);
487 pos += sizeof (gulong) * 19;
488 for (i = 14; i < 32; ++i) {
489 ppc_stfd (code, i, pos, ppc_sp);
490 pos += sizeof (gdouble);
492 /* FIXME: restore all the regs from ctx (in r3) */
493 /* call handler at eip (r4) and set the first arg with the exception (r5) */
494 ppc_mtctr (code, ppc_r4);
495 ppc_mr (code, ppc_r3, ppc_r5);
496 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
497 /* restore all the regs from the stack */
498 pos = PPC_MINIMAL_STACK_SIZE;
499 ppc_lmw (code, ppc_r13, ppc_sp, pos);
500 pos += sizeof (gulong) * 19;
501 for (i = 14; i < 32; ++i) {
502 ppc_lfd (code, i, pos, ppc_sp);
503 pos += sizeof (gdouble);
505 ppc_lwz (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
506 ppc_mtlr (code, ppc_r0);
507 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
509 g_assert ((code - start) < sizeof(start));
514 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
516 static void (*restore_context) (struct sigcontext *);
517 struct sigcontext ctx;
519 if (!restore_context)
520 restore_context = arch_get_restore_context ();
522 /* adjust eip so that it point into the call instruction */
525 MONO_CONTEXT_SET_BP (&ctx, esp);
526 MONO_CONTEXT_SET_IP (&ctx, eip);
538 mono_arch_handle_exception (&ctx, exc, FALSE);
539 restore_context (&ctx);
541 g_assert_not_reached ();
545 * arch_get_throw_exception_generic:
547 * Returns a function pointer which can be used to raise
548 * exceptions. The returned function has the following
549 * signature: void (*func) (MonoException *exc); or
550 * void (*func) (char *exc_name);
554 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name)
557 int alloc_size, pos, i;
561 ppc_mflr (code, ppc_r0);
562 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
563 alloc_size = PPC_MINIMAL_STACK_SIZE + (sizeof (gulong) * 14 + sizeof (gdouble) * 14);
564 // align to PPC_STACK_ALIGNMENT bytes
565 if (alloc_size & (PPC_STACK_ALIGNMENT - 1))
566 alloc_size += PPC_STACK_ALIGNMENT - (alloc_size & (PPC_STACK_ALIGNMENT - 1));
567 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
570 ppc_mr (code, ppc_r5, ppc_r3);
571 ppc_load (code, ppc_r3, mono_defaults.corlib);
572 ppc_load (code, ppc_r4, "System");
574 ppc_patch (code - 4, mono_exception_from_name);
576 /* save all the regs on the stack */
577 pos = PPC_MINIMAL_STACK_SIZE;
578 ppc_stmw (code, ppc_r13, ppc_sp, pos);
579 pos += sizeof (gulong) * 19;
580 for (i = 14; i < 32; ++i) {
581 ppc_stfd (code, i, pos, ppc_sp);
582 pos += sizeof (gdouble);
584 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
585 /* exc is already in place in r3 */
587 ppc_mflr (code, ppc_r4);
589 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
590 ppc_lwz (code, ppc_r5, 0, ppc_sp); /* caller sp */
591 /* pointer to the saved int regs */
592 pos = PPC_MINIMAL_STACK_SIZE;
593 ppc_addi (code, ppc_r6, ppc_sp, pos);
594 /* pointer to the saved fp regs */
595 pos += sizeof (gulong) * 19;
596 ppc_addi (code, ppc_r7, ppc_sp, pos);
598 ppc_patch (code - 4, throw_exception);
599 /* we should never reach this breakpoint */
601 g_assert ((code - start) < size);
606 * arch_get_throw_exception:
608 * Returns a function pointer which can be used to raise
609 * exceptions. The returned function has the following
610 * signature: void (*func) (MonoException *exc);
611 * For example to raise an arithmetic exception you can use:
613 * x86_push_imm (code, mono_get_exception_arithmetic ());
614 * x86_call_code (code, arch_get_throw_exception ());
618 mono_arch_get_throw_exception (void)
620 static guint8 start [128];
621 static int inited = 0;
625 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE);
631 * arch_get_throw_exception_by_name:
633 * Returns a function pointer which can be used to raise
634 * corlib exceptions. The returned function has the following
635 * signature: void (*func) (char *exc_name);
636 * For example to raise an arithmetic exception you can use:
638 * x86_push_imm (code, "ArithmeticException");
639 * x86_call_code (code, arch_get_throw_exception_by_name ());
643 mono_arch_get_throw_exception_by_name (void)
645 static guint8 start [160];
646 static int inited = 0;
650 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE);
656 glist_to_array (GList *list)
658 MonoDomain *domain = mono_domain_get ();
665 len = g_list_length (list);
666 res = mono_array_new (domain, mono_defaults.int_class, len);
668 for (i = 0; list; list = list->next, i++)
669 mono_array_set (res, gpointer, i, list->data);
674 /* mono_arch_find_jit_info:
676 * This function is used to gather information from @ctx. It return the
677 * MonoJitInfo of the corresponding function, unwinds one stack frame and
678 * stores the resulting context into @new_ctx. It also stores a string
679 * describing the stack location into @trace (if not NULL), and modifies
680 * the @lmf if necessary. @native_offset return the IP offset from the
681 * start of the function or -1 if that info is not available.
684 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoContext *ctx,
685 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
689 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
692 MonoPPCStackFrame *sframe;
694 ji = mono_jit_info_table_find (domain, ip);
706 char *source_location, *tmpaddr, *fname;
707 gint32 address, iloffset;
712 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
713 /* remove any unused lmf */
714 *lmf = (*lmf)->previous_lmf;
717 address = (char *)ip - (char *)ji->code_start;
720 *native_offset = address;
723 if (!ji->method->wrapper_type)
727 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
728 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
731 tmpaddr = g_strdup_printf ("<0x%05x>", address);
733 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
735 fname = mono_method_full_name (ji->method, TRUE);
738 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
740 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
743 g_free (source_location);
748 /* restore caller saved registers */
749 if (ji->used_regs & X86_EBX_MASK) {
750 new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
753 if (ji->used_regs & X86_EDI_MASK) {
754 new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
757 if (ji->used_regs & X86_ESI_MASK) {
758 new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
761 new_ctx->SC_ESP = ctx->SC_EBP;
762 /* we substract 1, so that the IP points into the call instruction */
763 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
764 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
766 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
767 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
768 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);
771 #ifdef MONO_USE_EXC_TABLES
772 } else if ((ji = ppc_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
784 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
786 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
789 memset (res, 0, sizeof (MonoJitInfo));
790 res->method = (*lmf)->method;
794 new_ctx->SC_ESI = (*lmf)->esi;
795 new_ctx->SC_EDI = (*lmf)->edi;
796 new_ctx->SC_EBX = (*lmf)->ebx;
797 new_ctx->SC_EBP = (*lmf)->ebp;
798 new_ctx->SC_EIP = (*lmf)->eip;
799 /* the lmf is always stored on the stack, so the following
800 * expression points to a stack location which can be used as ESP */
801 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
803 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
804 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
805 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);
806 *lmf = (*lmf)->previous_lmf;
816 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
818 MonoDomain *domain = mono_domain_get ();
820 MonoArray *ta = exc->trace_ips;
823 len = mono_array_length (ta);
825 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
827 for (i = skip; i < len; i++) {
829 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
830 gpointer ip = mono_array_get (ta, gpointer, i);
832 ji = mono_jit_info_table_find (domain, ip);
833 g_assert (ji != NULL);
835 sf->method = mono_method_get_object (domain, ji->method, NULL);
836 sf->native_offset = (char *)ip - (char *)ji->code_start;
838 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
840 if (need_file_info) {
843 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
845 sf->filename = filename? mono_string_new (domain, filename): NULL;
851 mono_array_set (res, gpointer, i, sf);
858 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
859 MonoDomain *domain = mono_domain_get ();
860 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
861 MonoLMF *lmf = jit_tls->lmf;
862 MonoJitInfo *ji, rji;
863 gint native_offset, il_offset;
865 MonoPPCStackFrame *sframe;
867 MonoContext ctx, new_ctx;
869 __asm__ volatile("lwz %0,0(r1)" : "=r" (sframe));
870 MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
871 MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
873 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
875 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
878 if (ji == (gpointer)-1)
881 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
883 if (func (ji->method, native_offset, il_offset, managed, user_data))
891 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
892 MonoReflectionMethod **method,
893 gint32 *iloffset, gint32 *native_offset,
894 MonoString **file, gint32 *line, gint32 *column)
896 MonoDomain *domain = mono_domain_get ();
897 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
898 MonoLMF *lmf = jit_tls->lmf;
899 MonoJitInfo *ji, rji;
900 MonoContext ctx, new_ctx;
902 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
903 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
908 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
912 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
915 /* skip all wrappers ??*/
916 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
917 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
918 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
925 *method = mono_method_get_object (domain, ji->method, NULL);
926 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
928 if (need_file_info) {
931 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
933 *file = filename? mono_string_new (domain, filename): NULL;
943 * arch_handle_exception:
944 * @ctx: saved processor state
945 * @obj: the exception object
946 * @test_only: only test if the exception is caught, but dont call handlers
951 mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
953 MonoDomain *domain = mono_domain_get ();
954 MonoJitInfo *ji, rji;
955 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
956 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
957 MonoLMF *lmf = jit_tls->lmf;
958 GList *trace_ips = NULL;
959 MonoException *mono_ex;
961 g_assert (ctx != NULL);
963 MonoException *ex = mono_get_exception_null_reference ();
964 ex->message = mono_string_new (domain,
965 "Object reference not set to an instance of an object");
966 obj = (MonoObject *)ex;
969 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
970 mono_ex = (MonoException*)obj;
971 mono_ex->stack_trace = NULL;
978 call_filter = arch_get_call_filter ();
980 g_assert (jit_tls->end_of_stack);
981 g_assert (jit_tls->abort_func);
984 MonoContext ctx_cp = *ctx;
985 if (mono_jit_trace_calls != NULL)
986 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
987 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
988 if (mono_break_on_exc)
990 mono_unhandled_exception (obj);
998 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, ctx, &new_ctx,
999 test_only ? &trace : NULL, &lmf, NULL, NULL);
1001 g_warning ("Exception inside function without unwind info");
1002 g_assert_not_reached ();
1005 if (ji != (gpointer)-1) {
1007 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1010 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1012 if (!mono_ex->stack_trace)
1013 strace = g_strdup ("");
1015 strace = mono_string_to_utf8 (mono_ex->stack_trace);
1017 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1020 mono_ex->stack_trace = mono_string_new (domain, tmp);
1025 if (ji->num_clauses) {
1028 g_assert (ji->clauses);
1030 for (i = 0; i < ji->num_clauses; i++) {
1031 MonoJitExceptionInfo *ei = &ji->clauses [i];
1033 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1034 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1036 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1037 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1038 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1039 call_filter (ctx, ei->data.filter, obj)))) {
1042 mono_ex->trace_ips = glist_to_array (trace_ips);
1043 g_list_free (trace_ips);
1047 if (mono_jit_trace_calls != NULL)
1048 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1049 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1050 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1055 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1056 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1057 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1058 if (mono_jit_trace_calls != NULL)
1059 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1060 call_filter (ctx, ei->handler_start, NULL);
1072 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1075 jit_tls->abort_func (obj);
1076 g_assert_not_reached ();
1079 mono_ex->trace_ips = glist_to_array (trace_ips);
1080 g_list_free (trace_ips);
1086 g_assert_not_reached ();
1090 mono_arch_has_unwind_info (gconstpointer addr)