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.
17 #include <mono/arch/ppc/ppc-codegen.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/mono-debug.h>
28 static gboolean arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only);
33 int sc_onstack; // sigstack state to restore
34 int sc_mask; // signal mask to restore
36 int sc_psw; // processor status word
37 int sc_sp; // stack pointer if sc_regs == NULL
38 void *sc_regs; // (kernel private) saved state
43 sigset_t uc_sigmask; // signal mask used by this context
44 stack_t uc_stack; // stack used by this context
45 struct ucontext *uc_link; // pointer to resuming context
46 size_t uc_mcsize; // size of the machine context passed in
47 mcontext_t uc_mcontext; // machine specific context
50 typedef struct ppc_exception_state {
51 unsigned long dar; // Fault registers for coredump
53 unsigned long exception;// number of powerpc exception taken
54 unsigned long pad0; // align to 16 bytes
56 unsigned long pad1[4]; // space in PCB "just in case"
57 } ppc_exception_state_t;
59 typedef struct ppc_vector_state {
60 unsigned long save_vr[32][4];
61 unsigned long save_vscr[4];
62 unsigned int save_pad5[4];
63 unsigned int save_vrvalid; // VRs that have been saved
64 unsigned int save_pad6[7];
67 typedef struct ppc_float_state {
70 unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish
71 unsigned int fpscr; // floating point status register
74 typedef struct ppc_thread_state {
75 unsigned int srr0; // Instruction address register (PC)
76 unsigned int srr1; // Machine state register (supervisor)
82 unsigned int cr; // Condition register
83 unsigned int xer; // User's integer exception register
84 unsigned int lr; // Link register
85 unsigned int ctr; // Count register
86 unsigned int mq; // MQ register (601 only)
88 unsigned int vrsave; // Vector Save Register
92 ppc_exception_state_t es;
93 ppc_thread_state_t ss;
95 ppc_vector_state_t vs;
98 typedef struct mcontext * mcontext_t;
100 Linux/PPC instead has:
102 unsigned long _unused[4];
104 unsigned long handler;
105 unsigned long oldmask;
106 struct pt_regs *regs;
109 unsigned long gpr[32];
112 unsigned long orig_gpr3; // Used for restarting system calls
117 unsigned long mq; // 601 only (not used at present)
118 // Used on APUS to hold IPL value.
119 unsigned long trap; // Reason for being here
120 // N.B. for critical exceptions on 4xx, the dar and dsisr
121 // fields are overloaded to hold srr0 and srr1.
122 unsigned long dar; // Fault registers
123 unsigned long dsisr; // on 4xx/Book-E used for ESR
124 unsigned long result; // Result of a system call
127 elf_gregset_t mc_gregs;
128 elf_fpregset_t mc_fregs;
129 unsigned long mc_pad[2];
130 elf_vrregset_t mc_vregs __attribute__((__aligned__(16)));
134 unsigned long uc_flags;
135 struct ucontext *uc_link;
138 struct mcontext *uc_regs; // points to uc_mcontext field
140 // glibc has 1024-bit signal masks, ours are 64-bit
143 struct mcontext uc_mcontext;
146 #define ELF_NGREG 48 // includes nip, msr, lr, etc.
147 #define ELF_NFPREG 33 // includes fpscr
150 typedef unsigned long elf_greg_t;
151 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
153 // Floating point registers
154 typedef double elf_fpreg_t;
155 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
160 /* we have the stack pointer, not the base pointer in sigcontext */
161 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (int)ip; } while (0);
162 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (int)bp; } while (0);
164 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
165 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_sp))
171 unsigned long unused1;
184 /* disbale this for now */
185 #undef MONO_USE_EXC_TABLES
187 #ifdef MONO_USE_EXC_TABLES
189 /*************************************/
190 /* STACK UNWINDING STUFF */
191 /*************************************/
193 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
196 #define DWARF_FRAME_REGISTERS 17
198 typedef struct frame_state
204 long reg_or_offset[DWARF_FRAME_REGISTERS+1];
205 unsigned short cfa_reg;
206 unsigned short retaddr_column;
207 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);
314 /* mono_arch_has_unwind_info:
316 * Tests if a function has an DWARF exception table able to restore
317 * all caller saved registers.
320 mono_arch_has_unwind_info (MonoMethod *method)
323 struct frame_state state_in;
324 struct frame_state *res;
327 init_frame_state_for ();
329 if (!frame_state_for)
332 g_assert (method->addr);
334 memset (&state_in, 0, sizeof (state_in));
336 /* offset 10 is just a guess, but it works for all methods tested */
337 if ((res = frame_state_for ((char *)method->addr + 10, &state_in))) {
339 if (res->saved [X86_EBX] != 1 ||
340 res->saved [X86_EDI] != 1 ||
341 res->saved [X86_EBP] != 1 ||
342 res->saved [X86_ESI] != 1) {
357 void *return_address;
361 ppc_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *ctx,
362 MonoContext *new_ctx, MonoLMF *lmf, char **trace)
365 struct stack_frame *frame;
368 struct frame_state state_in;
369 struct frame_state *res;
375 init_frame_state_for ();
377 if (!frame_state_for)
380 frame = MONO_CONTEXT_GET_BP (ctx);
382 max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
386 memset (&state_in, 0, sizeof (state_in));
388 while ((gpointer)frame->next < (gpointer)max_stack) {
389 gpointer ip, addr = frame->return_address;
391 char *tmp, **symbols;
394 ip = MONO_CONTEXT_GET_IP (new_ctx);
395 symbols = get_backtrace_symbols (&ip, 1);
397 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
399 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
406 if ((res = frame_state_for (addr, &state_in))) {
409 cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
410 frame = (struct stack_frame *)((gint8*)cfa - 8);
411 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
412 int how = res->saved[i];
414 g_assert ((how == 0) || (how == 1));
417 val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
418 set_sigcontext_reg (new_ctx, i, val);
421 new_ctx->SC_ESP = (long)cfa;
423 if (res->saved [X86_EBX] == 1 &&
424 res->saved [X86_EDI] == 1 &&
425 res->saved [X86_EBP] == 1 &&
426 res->saved [X86_ESI] == 1 &&
427 (ji = mono_jit_info_table_find (domain, frame->return_address))) {
428 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
433 //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)));
435 MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
437 MONO_CONTEXT_SET_BP (new_ctx, frame);
439 /* stop if !frame or when we detect an unexpected managed frame */
440 if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
451 //g_assert_not_reached ();
463 /* FIXME: restore the rest of the registers */
464 #define restore_regs_from_context(ctx_reg,ip_reg,tmp_reg) do { \
465 ppc_lwz (code, ip_reg, G_STRUCT_OFFSET (MonoContext, sc_ir), ctx_reg); \
466 ppc_lwz (code, ppc_sp, G_STRUCT_OFFSET (MonoContext, sc_sp), ctx_reg); \
467 ppc_lmw (code, ppc_r13, ctx_reg, G_STRUCT_OFFSET (MonoContext, regs)); \
471 #define setup_context(ctx)
474 * arch_get_restore_context:
476 * Returns a pointer to a method which restores a previously saved sigcontext.
477 * The first argument in r3 is the pointer to the context.
480 arch_get_restore_context (void)
483 static guint8 start [64];
484 static int inited = 0;
491 restore_regs_from_context (ppc_r3, ppc_r4, ppc_r5);
493 /* jump to the saved IP */
494 ppc_mtctr (code, ppc_r4);
495 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
499 g_assert ((code - start) < sizeof(start));
504 * arch_get_call_filter:
506 * Returns a pointer to a method which calls an exception filter. We
507 * also use this function to call finally handlers (we pass NULL as
508 * @exc object in this case).
511 arch_get_call_filter (void)
513 static guint8 start [256];
514 static int inited = 0;
516 int alloc_size, pos, i;
522 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
525 ppc_mflr (code, ppc_r0);
526 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
527 alloc_size = 32 + PPC_MINIMAL_STACK_SIZE + (sizeof (gulong) * 19 + sizeof (gdouble) * 18);
528 // align to PPC_STACK_ALIGNMENT bytes
529 if (alloc_size & (PPC_STACK_ALIGNMENT - 1))
530 alloc_size += PPC_STACK_ALIGNMENT - (alloc_size & (PPC_STACK_ALIGNMENT - 1));
531 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
532 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
533 /* save all the regs on the stack */
534 pos = 32 + PPC_MINIMAL_STACK_SIZE;
535 ppc_stmw (code, ppc_r13, ppc_sp, pos);
536 pos += sizeof (gulong) * 19;
537 for (i = 14; i < 32; ++i) {
538 ppc_stfd (code, i, pos, ppc_sp);
539 pos += sizeof (gdouble);
541 /* restore all the regs from ctx (in r3) */
542 /* FIXME: calling the filter code must not restore the stack pointer */
543 restore_regs_from_context (ppc_r3, ppc_r6, ppc_r7);
544 /* call handler at eip (r4) and set the first arg with the exception (r5) */
545 ppc_mtctr (code, ppc_r4);
546 ppc_mr (code, ppc_r3, ppc_r5);
547 ppc_bcctrl (code, PPC_BR_ALWAYS, 0);
548 /* restore all the regs from the stack */
549 pos = 32 + PPC_MINIMAL_STACK_SIZE;
550 ppc_lmw (code, ppc_r13, ppc_sp, pos);
551 pos += sizeof (gulong) * 19;
552 for (i = 14; i < 32; ++i) {
553 ppc_lfd (code, i, pos, ppc_sp);
554 pos += sizeof (gdouble);
556 ppc_lwz (code, ppc_r0, alloc_size + PPC_RET_ADDR_OFFSET, ppc_sp);
557 ppc_mtlr (code, ppc_r0);
558 ppc_addic (code, ppc_sp, ppc_sp, alloc_size);
561 g_assert ((code - start) < sizeof(start));
566 throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, gulong *int_regs, gdouble *fp_regs)
568 static void (*restore_context) (MonoContext *);
571 if (!restore_context)
572 restore_context = arch_get_restore_context ();
574 /* adjust eip so that it point into the call instruction */
577 setup_context (&ctx);
579 /*printf ("stack in throw: %p\n", esp);*/
580 MONO_CONTEXT_SET_BP (&ctx, esp);
581 MONO_CONTEXT_SET_IP (&ctx, eip);
582 memcpy (&ctx.regs, int_regs, sizeof (gulong) * 19);
584 arch_handle_exception (&ctx, exc, FALSE);
585 restore_context (&ctx);
587 g_assert_not_reached ();
591 * arch_get_throw_exception_generic:
593 * Returns a function pointer which can be used to raise
594 * exceptions. The returned function has the following
595 * signature: void (*func) (MonoException *exc); or
596 * void (*func) (char *exc_name);
600 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name)
603 int alloc_size, pos, i;
607 ppc_mflr (code, ppc_r0);
608 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
609 alloc_size = 32 + PPC_MINIMAL_STACK_SIZE + (sizeof (gulong) * 19 + sizeof (gdouble) * 18);
610 // align to PPC_STACK_ALIGNMENT bytes
611 if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
612 alloc_size += PPC_STACK_ALIGNMENT - 1;
613 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
615 /*g_print ("alloc size = %d\n", alloc_size);*/
616 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
617 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
620 ppc_mr (code, ppc_r5, ppc_r3);
621 ppc_load (code, ppc_r3, mono_defaults.corlib);
622 ppc_load (code, ppc_r4, "System");
624 ppc_patch (code - 4, mono_exception_from_name);
626 /* save all the regs on the stack */
627 pos = 32 + PPC_MINIMAL_STACK_SIZE;
628 ppc_stmw (code, ppc_r13, ppc_sp, pos);
629 pos += sizeof (gulong) * 19;
630 /* align for doubles */
633 for (i = 14; i < 32; ++i) {
634 ppc_stfd (code, i, pos, ppc_sp);
635 pos += sizeof (gdouble);
637 /* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
639 ppc_lwz (code, ppc_r5, 0, ppc_sp);
640 /* exc is already in place in r3 */
642 ppc_lwz (code, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_r5);
644 ppc_mr (code, ppc_r4, ppc_r0); /* caller ip */
645 /* pointer to the saved int regs */
646 pos = 32 + PPC_MINIMAL_STACK_SIZE;
647 ppc_addi (code, ppc_r6, ppc_sp, pos);
648 /* pointer to the saved fp regs */
649 pos += sizeof (gulong) * 19;
650 ppc_addi (code, ppc_r7, ppc_sp, pos);
652 ppc_patch (code - 4, throw_exception);
653 /* we should never reach this breakpoint */
655 g_assert ((code - start) < size);
660 * arch_get_throw_exception:
662 * Returns a function pointer which can be used to raise
663 * exceptions. The returned function has the following
664 * signature: void (*func) (MonoException *exc);
665 * For example to raise an arithmetic exception you can use:
667 * x86_push_imm (code, mono_get_exception_arithmetic ());
668 * x86_call_code (code, arch_get_throw_exception ());
672 mono_arch_get_throw_exception (void)
674 static guint8 start [128];
675 static int inited = 0;
679 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE);
685 * arch_get_throw_exception_by_name:
687 * Returns a function pointer which can be used to raise
688 * corlib exceptions. The returned function has the following
689 * signature: void (*func) (char *exc_name);
690 * For example to raise an arithmetic exception you can use:
692 * x86_push_imm (code, "ArithmeticException");
693 * x86_call_code (code, arch_get_throw_exception_by_name ());
697 mono_arch_get_throw_exception_by_name (void)
699 static guint8 start [160];
700 static int inited = 0;
704 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE);
710 glist_to_array (GList *list)
712 MonoDomain *domain = mono_domain_get ();
719 len = g_list_length (list);
720 res = mono_array_new (domain, mono_defaults.int_class, len);
722 for (i = 0; list; list = list->next, i++)
723 mono_array_set (res, gpointer, i, list->data);
728 /* mono_arch_find_jit_info:
730 * This function is used to gather information from @ctx. It return the
731 * MonoJitInfo of the corresponding function, unwinds one stack frame and
732 * stores the resulting context into @new_ctx. It also stores a string
733 * describing the stack location into @trace (if not NULL), and modifies
734 * the @lmf if necessary. @native_offset return the IP offset from the
735 * start of the function or -1 if that info is not available.
738 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji,
739 MonoContext *ctx, MonoContext *new_ctx, char **trace, MonoLMF **lmf,
740 int *native_offset, gboolean *managed)
743 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
746 MonoPPCStackFrame *sframe;
748 /* Avoid costly table lookup during stack overflow */
749 if (prev_ji && (ip > prev_ji->code_start && ((guint8*)ip < ((guint8*)prev_ji->code_start) + prev_ji->code_size)))
752 ji = mono_jit_info_table_find (domain, ip);
764 char *source_location, *tmpaddr, *fname;
765 gint32 address, iloffset;
770 setup_context (new_ctx);
772 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
773 /* remove any unused lmf */
774 *lmf = (*lmf)->previous_lmf;
777 address = (char *)ip - (char *)ji->code_start;
780 *native_offset = address;
783 if (!ji->method->wrapper_type)
787 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
788 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
791 tmpaddr = g_strdup_printf ("<0x%05x>", address);
793 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
795 fname = mono_method_full_name (ji->method, TRUE);
798 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
800 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
803 g_free (source_location);
808 /* restore caller saved registers */
809 if (ji->used_regs & X86_EBX_MASK) {
810 new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
813 if (ji->used_regs & X86_EDI_MASK) {
814 new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
817 if (ji->used_regs & X86_ESI_MASK) {
818 new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
821 new_ctx->SC_ESP = ctx->SC_EBP;
822 /* we substract 1, so that the IP points into the call instruction */
823 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
824 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
826 sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
827 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
829 /* keep updated with emit_prolog in mini-ppc.c */
831 for (i = 13; i < 32; ++i) {
832 if (ji->used_regs & (1 << i)) {
833 offset += sizeof (gulong);
834 new_ctx->regs [i] = *(gulong*)((char*)sframe->sp - offset);
838 /* the calling IP is in the parent frame */
839 sframe = (MonoPPCStackFrame*)sframe->sp;
840 /* we substract 4, so that the IP points into the call instruction */
841 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr - 4);
844 #ifdef MONO_USE_EXC_TABLES
845 } else if ((ji = ppc_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
852 setup_context (new_ctx);
858 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
860 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
863 memset (res, 0, sizeof (MonoJitInfo));
864 res->method = (*lmf)->method;
868 new_ctx->SC_ESI = (*lmf)->esi;
869 new_ctx->SC_EDI = (*lmf)->edi;
870 new_ctx->SC_EBX = (*lmf)->ebx;
871 new_ctx->SC_EBP = (*lmf)->ebp;
872 new_ctx->SC_EIP = (*lmf)->eip;
873 /* the lmf is always stored on the stack, so the following
874 * expression points to a stack location which can be used as ESP */
875 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
877 /*sframe = (MonoPPCStackFrame*)MONO_CONTEXT_GET_BP (ctx);
878 MONO_CONTEXT_SET_BP (new_ctx, sframe->sp);
879 MONO_CONTEXT_SET_IP (new_ctx, sframe->lr);*/
880 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
881 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
882 memcpy (&new_ctx->regs, (*lmf)->iregs, sizeof (gulong) * 19);
883 *lmf = (*lmf)->previous_lmf;
893 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
895 MonoDomain *domain = mono_domain_get ();
897 MonoArray *ta = exc->trace_ips;
900 len = mono_array_length (ta);
902 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
904 for (i = skip; i < len; i++) {
906 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
907 gpointer ip = mono_array_get (ta, gpointer, i);
909 ji = mono_jit_info_table_find (domain, ip);
910 g_assert (ji != NULL);
912 sf->method = mono_method_get_object (domain, ji->method, NULL);
913 sf->native_offset = (char *)ip - (char *)ji->code_start;
915 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
917 if (need_file_info) {
920 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
922 sf->filename = filename? mono_string_new (domain, filename): NULL;
928 mono_array_set (res, gpointer, i, sf);
935 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
936 MonoDomain *domain = mono_domain_get ();
937 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
938 MonoLMF *lmf = jit_tls->lmf;
939 MonoJitInfo *ji, rji;
940 gint native_offset, il_offset;
942 MonoPPCStackFrame *sframe;
944 MonoContext ctx, new_ctx;
946 setup_context (&ctx);
947 setup_context (&new_ctx);
950 __asm__ volatile("lwz %0,0(r1)" : "=r" (sframe));
952 __asm__ volatile("lwz %0,0(1)" : "=r" (sframe));
954 MONO_CONTEXT_SET_IP (&ctx, sframe->lr);
955 MONO_CONTEXT_SET_BP (&ctx, sframe->sp);
957 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
959 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
962 if (ji == (gpointer)-1)
965 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
967 if (func (ji->method, native_offset, il_offset, managed, user_data))
971 setup_context (&ctx);
976 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
977 MonoReflectionMethod **method,
978 gint32 *iloffset, gint32 *native_offset,
979 MonoString **file, gint32 *line, gint32 *column)
981 MonoDomain *domain = mono_domain_get ();
982 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
983 MonoLMF *lmf = jit_tls->lmf;
984 MonoJitInfo *ji, rji;
985 MonoContext ctx, new_ctx;
987 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
988 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
993 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
997 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
1000 /* skip all wrappers ??*/
1001 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
1002 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
1003 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
1008 } while (skip >= 0);
1010 *method = mono_method_get_object (domain, ji->method, NULL);
1011 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
1013 if (need_file_info) {
1016 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
1018 *file = filename? mono_string_new (domain, filename): NULL;
1028 * This is the function called from the signal handler
1032 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
1034 struct ucontext *uc = ctx;
1038 mctx.sc_ir = uc->uc_mcontext->ss.srr0;
1039 mctx.sc_sp = uc->uc_mcontext->ss.r1;
1040 memcpy (&mctx.regs, &uc->uc_mcontext->ss.r13, sizeof (gulong) * 19);
1041 memcpy (&mctx.fregs, &uc->uc_mcontext->fs.fpregs [14], sizeof (double) * 20);
1043 result = arch_handle_exception (&mctx, obj, test_only);
1044 /* restore the context so that returning from the signal handler will invoke
1047 uc->uc_mcontext->ss.srr0 = mctx.sc_ir;
1048 uc->uc_mcontext->ss.r1 = mctx.sc_sp;
1049 memcpy (&uc->uc_mcontext->ss.r13, &mctx.regs, sizeof (gulong) * 19);
1050 memcpy (&uc->uc_mcontext->fs.fpregs [14], &mctx.fregs, sizeof (double) * 20);
1056 mono_arch_handle_exception (void *ctx, gpointer obj, gboolean test_only)
1058 struct ucontext *uc = ctx;
1062 mctx.sc_ir = uc->uc_mcontext.uc_regs->gregs [PT_NIP];
1063 mctx.sc_sp = uc->uc_mcontext.uc_regs->gregs [PT_R1];
1064 memcpy (&mctx.regs, &uc->uc_mcontext.uc_regs->gregs [PT_R13], sizeof (gulong) * 19);
1065 memcpy (&mctx.fregs, &uc->uc_mcontext.uc_regs->fpregs.fpregs [14], sizeof (double) * 20);
1067 result = arch_handle_exception (&mctx, obj, test_only);
1068 /* restore the context so that returning from the signal handler will invoke
1071 uc->uc_mcontext.uc_regs->gregs [PT_NIP] = mctx.sc_ir;
1072 uc->uc_mcontext.uc_regs->gregs [PT_R1] = mctx.sc_sp;
1073 memcpy (&uc->uc_mcontext.uc_regs->gregs [PT_R13], &mctx.regs, sizeof (gulong) * 19);
1074 memcpy (&uc->uc_mcontext.uc_regs->fpregs.fpregs [14], &mctx.fregs, sizeof (double) * 20);
1080 * arch_handle_exception:
1081 * @ctx: saved processor state
1082 * @obj: the exception object
1083 * @test_only: only test if the exception is caught, but dont call handlers
1088 arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
1090 MonoDomain *domain = mono_domain_get ();
1091 MonoJitInfo *ji, rji;
1092 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
1093 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
1094 MonoLMF *lmf = jit_tls->lmf;
1095 GList *trace_ips = NULL;
1096 MonoException *mono_ex;
1097 MonoString *initial_stack_trace;
1099 g_assert (ctx != NULL);
1101 MonoException *ex = mono_get_exception_null_reference ();
1102 ex->message = mono_string_new (domain,
1103 "Object reference not set to an instance of an object");
1104 obj = (MonoObject *)ex;
1107 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1108 mono_ex = (MonoException*)obj;
1109 initial_stack_trace = mono_ex->stack_trace;
1116 call_filter = arch_get_call_filter ();
1118 g_assert (jit_tls->end_of_stack);
1119 g_assert (jit_tls->abort_func);
1122 MonoContext ctx_cp = *ctx;
1123 setup_context (&ctx_cp);
1124 if (mono_jit_trace_calls != NULL)
1125 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
1126 if (!arch_handle_exception (&ctx_cp, obj, TRUE)) {
1127 if (mono_break_on_exc)
1129 mono_unhandled_exception (obj);
1134 MonoContext new_ctx;
1137 setup_context (&new_ctx);
1138 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
1139 test_only ? &trace : NULL, &lmf, NULL, NULL);
1141 g_warning ("Exception inside function without unwind info");
1142 g_assert_not_reached ();
1145 if (ji != (gpointer)-1) {
1147 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1150 if (!initial_stack_trace) {
1151 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1153 if (!mono_ex->stack_trace)
1154 strace = g_strdup ("");
1156 strace = mono_string_to_utf8 (mono_ex->stack_trace);
1158 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1161 mono_ex->stack_trace = mono_string_new (domain, tmp);
1167 if (ji->num_clauses) {
1170 g_assert (ji->clauses);
1172 for (i = 0; i < ji->num_clauses; i++) {
1173 MonoJitExceptionInfo *ei = &ji->clauses [i];
1175 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1176 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1178 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1179 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1180 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1181 call_filter (ctx, ei->data.filter, obj)))) {
1184 mono_ex->trace_ips = glist_to_array (trace_ips);
1185 g_list_free (trace_ips);
1189 if (mono_jit_trace_calls != NULL)
1190 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1191 /*printf ("stack for catch: %p\n", MONO_CONTEXT_GET_BP (ctx));*/
1192 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1193 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1198 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1199 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1200 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1201 if (mono_jit_trace_calls != NULL)
1202 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1203 call_filter (ctx, ei->handler_start, NULL);
1214 setup_context (ctx);
1216 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1219 jit_tls->abort_func (obj);
1220 g_assert_not_reached ();
1223 mono_ex->trace_ips = glist_to_array (trace_ips);
1224 g_list_free (trace_ips);
1230 g_assert_not_reached ();
1234 mono_arch_has_unwind_info (gconstpointer addr)