1 /*------------------------------------------------------------------*/
3 /* Name - exceptions-s390.c */
5 /* Function - Exception support for S/390. */
7 /* Name - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
9 /* Date - January, 2004 */
11 /* Derivation - From exceptions-x86 & exceptions-ppc */
12 /* Paolo Molaro (lupus@ximian.com) */
13 /* Dietmar Maurer (dietmar@ximian.com) */
15 /* Copyright - 2001 Ximian, Inc. */
17 /*------------------------------------------------------------------*/
19 /*------------------------------------------------------------------*/
21 /*------------------------------------------------------------------*/
23 #define MONO_CONTEXT_SET_IP(ctx,ip) \
25 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip; \
26 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip; \
29 #define MONO_CONTEXT_SET_BP(ctx,bp) \
31 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp; \
32 (ctx)->uc_stack.ss_sp = (unsigned long)bp; \
35 #define MONO_CONTEXT_GET_IP(ctx) context_get_ip ((ctx))
36 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
38 /* disable this for now */
39 #undef MONO_USE_EXC_TABLES
41 #define S390_CALLFILTER_INTREGS S390_MINIMAL_STACK_SIZE
42 #define S390_CALLFILTER_FLTREGS S390_CALLFILTER_INTREGS+(16*sizeof(gulong))
43 #define S390_CALLFILTER_ACCREGS S390_CALLFILTER_FLTREGS+(16*sizeof(gdouble))
44 #define S390_CALLFILTER_SIZE (S390_CALLFILTER_ACCREGS+(16*sizeof(gulong)))
46 #define S390_THROWSTACK_ACCPRM S390_MINIMAL_STACK_SIZE
47 #define S390_THROWSTACK_FPCPRM S390_THROWSTACK_ACCPRM+sizeof(gpointer)
48 #define S390_THROWSTACK_INTREGS S390_THROWSTACK_FPCPRM+sizeof(gulong)
49 #define S390_THROWSTACK_FLTREGS S390_THROWSTACK_INTREGS+(16*sizeof(gulong))
50 #define S390_THROWSTACK_ACCREGS S390_THROWSTACK_FLTREGS+(16*sizeof(gdouble))
51 #define S390_THROWSTACK_SIZE (S390_THROWSTACK_ACCREGS+(16*sizeof(gulong)))
53 /*========================= End of Defines =========================*/
55 /*------------------------------------------------------------------*/
57 /*------------------------------------------------------------------*/
65 #include <mono/arch/s390/s390-codegen.h>
66 #include <mono/metadata/appdomain.h>
67 #include <mono/metadata/tabledefs.h>
68 #include <mono/metadata/threads.h>
69 #include <mono/metadata/debug-helpers.h>
70 #include <mono/metadata/exception.h>
71 #include <mono/metadata/mono-debug.h>
74 #include "mini-s390.h"
76 /*========================= End of Includes ========================*/
78 /*------------------------------------------------------------------*/
80 /*------------------------------------------------------------------*/
90 #ifdef MONO_USE_EXC_TABLES
92 /*************************************/
93 /* STACK UNWINDING STUFF */
94 /*************************************/
96 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
99 #define DWARF_FRAME_REGISTERS 17
101 typedef struct frame_state
107 long reg_or_offset[DWARF_FRAME_REGISTERS+1];
108 unsigned short cfa_reg;
109 unsigned short retaddr_column;
110 char saved[DWARF_FRAME_REGISTERS+1];
113 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
117 /*========================= End of Typedefs ========================*/
119 /*------------------------------------------------------------------*/
120 /* P r o t o t y p e s */
121 /*------------------------------------------------------------------*/
123 gboolean mono_arch_handle_exception (void *ctx,
127 /*========================= End of Prototypes ======================*/
129 /*------------------------------------------------------------------*/
130 /* G l o b a l V a r i a b l e s */
131 /*------------------------------------------------------------------*/
133 #ifdef MONO_USE_EXC_TABLES
135 static framesf frame_state_for = NULL;
137 static gboolean inited = FALSE;
139 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
141 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
145 /*====================== End of Global Variables ===================*/
147 /*------------------------------------------------------------------*/
149 /* Name - context_get_ip */
151 /* Function - Extract the current instruction address from the */
154 /*------------------------------------------------------------------*/
156 static inline gpointer
157 context_get_ip (MonoContext *ctx)
161 ip = (gpointer) ((gint32) (ctx->uc_mcontext.psw.addr) & 0x7fffffff);
165 /*========================= End of Function ========================*/
167 #ifdef MONO_USE_EXC_TABLES
169 /*------------------------------------------------------------------*/
171 /* Name - init_frame_state_for */
173 /* Function - Load the __frame_state_for from libc. */
174 /* There are two versions of __frame_state_for: one */
175 /* in libgcc.a and the other in glibc.so. We need */
176 /* the version from glibc. For more information, see:*/
177 /* http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html */
179 /*------------------------------------------------------------------*/
182 init_frame_state_for (void)
186 if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
188 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
189 frame_state_for = NULL;
191 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
192 get_backtrace_symbols = NULL;
193 frame_state_for = NULL;
196 g_module_close (module);
202 /*========================= End of Function ========================*/
206 /*------------------------------------------------------------------*/
208 /* Name - mono_arch_has_unwind_info */
210 /* Function - Tests if a function has a DWARF exception table */
211 /* that is able to restore all caller saved registers*/
213 /*------------------------------------------------------------------*/
216 mono_arch_has_unwind_info (gconstpointer addr)
219 struct frame_state state_in;
220 struct frame_state *res;
223 init_frame_state_for ();
225 if (!frame_state_for)
228 g_assert (method->addr);
230 memset (&state_in, 0, sizeof (state_in));
232 /* offset 10 is just a guess, but it works for all methods tested */
233 if ((res = frame_state_for ((char *)method->addr + 10, &state_in))) {
235 if (res->saved [X86_EBX] != 1 ||
236 res->saved [X86_EDI] != 1 ||
237 res->saved [X86_EBP] != 1 ||
238 res->saved [X86_ESI] != 1) {
250 /*========================= End of Function ========================*/
252 /*------------------------------------------------------------------*/
254 /* Name - s390_unwind_native_frame */
256 /* Function - Use the context to unwind a stack frame. */
258 /*------------------------------------------------------------------*/
261 s390_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
262 struct sigcontext *ctx, struct sigcontext *new_ctx,
263 MonoLMF *lmf, char **trace)
266 struct stack_frame *frame;
269 struct frame_state state_in;
270 struct frame_state *res;
276 init_frame_state_for ();
278 if (!frame_state_for)
281 frame = MONO_CONTEXT_GET_BP (ctx);
283 max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
287 memset (&state_in, 0, sizeof (state_in));
289 while ((gpointer)frame->next < (gpointer)max_stack) {
290 gpointer ip, addr = frame->return_address;
292 char *tmp, **symbols;
295 ip = MONO_CONTEXT_GET_IP (new_ctx);
296 symbols = get_backtrace_symbols (&ip, 1);
298 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
300 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
307 if ((res = frame_state_for (addr, &state_in))) {
310 cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
311 frame = (struct stack_frame *)((gint8*)cfa - 8);
312 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
313 int how = res->saved[i];
315 g_assert ((how == 0) || (how == 1));
318 val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
319 set_sigcontext_reg (new_ctx, i, val);
322 new_ctx->SC_ESP = (long)cfa;
324 if (res->saved [X86_EBX] == 1 &&
325 res->saved [X86_EDI] == 1 &&
326 res->saved [X86_EBP] == 1 &&
327 res->saved [X86_ESI] == 1 &&
328 (ji = mono_jit_info_table_find (domain, frame->return_address))) {
329 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
335 MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
337 MONO_CONTEXT_SET_BP (new_ctx, frame);
339 /* stop if !frame or when we detect an unexpected managed frame */
340 if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
358 /*========================= End of Function ========================*/
360 /*------------------------------------------------------------------*/
362 /* Name - arch_get_call_filter */
364 /* Function - Return a pointer to a method which calls an */
365 /* exception filter. We also use this function to */
366 /* call finally handlers (we pass NULL as @exc */
367 /* object in this case). */
369 /*------------------------------------------------------------------*/
372 arch_get_call_filter (void)
374 static guint8 start [512];
375 static int inited = 0;
377 int alloc_size, pos, i;
383 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
386 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
387 s390_lr (code, s390_r14, STK_BASE);
388 alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
389 s390_ahi (code, STK_BASE, -alloc_size);
390 s390_st (code, s390_r14, 0, STK_BASE, 0);
392 /*------------------------------------------------------*/
393 /* save general registers on stack */
394 /*------------------------------------------------------*/
395 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
397 /*------------------------------------------------------*/
398 /* save floating point registers on stack */
399 /*------------------------------------------------------*/
400 // pos = S390_CALLFILTER_FLTREGS;
401 // for (i = 0; i < 16; ++i) {
402 // s390_std (code, i, 0, STK_BASE, pos);
403 // pos += sizeof (gdouble);
406 /*------------------------------------------------------*/
407 /* save access registers on stack */
408 /*------------------------------------------------------*/
409 // s390_stam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
411 /*------------------------------------------------------*/
413 /*------------------------------------------------------*/
414 s390_lr (code, s390_r13, s390_r2);
416 /*------------------------------------------------------*/
417 /* Get A(Handler Entry Point) */
418 /*------------------------------------------------------*/
419 s390_lr (code, s390_r0, s390_r3);
421 /*------------------------------------------------------*/
422 /* Set parameter register with Exception */
423 /*------------------------------------------------------*/
424 s390_lr (code, s390_r2, s390_r4);
426 /*------------------------------------------------------*/
427 /* Load all registers with values from the context */
428 /*------------------------------------------------------*/
429 s390_lm (code, s390_r3, s390_r12, s390_r13, G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[3]));
430 pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
431 for (i = 0; i < 16; ++i) {
432 s390_ld (code, i, 0, s390_r13, pos);
433 pos += sizeof(gdouble);
436 /*------------------------------------------------------*/
437 /* Point at the copied stack frame and call the filter */
438 /*------------------------------------------------------*/
439 s390_lr (code, s390_r1, s390_r0);
440 s390_basr (code, s390_r14, s390_r1);
442 /*------------------------------------------------------*/
443 /* Save return value */
444 /*------------------------------------------------------*/
445 s390_lr (code, s390_r14, s390_r2);
447 /*------------------------------------------------------*/
448 /* Restore all the regs from the stack */
449 /*------------------------------------------------------*/
450 s390_lm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
451 // pos = S390_CALLFILTER_FLTREGS;
452 // for (i = 0; i < 16; ++i) {
453 // s390_ld (code, i, 0, STK_BASE, pos);
454 // pos += sizeof (gdouble);
457 s390_lr (code, s390_r2, s390_r14);
458 // s390_lam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
459 s390_ahi (code, s390_r15, alloc_size);
460 s390_lm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
461 s390_br (code, s390_r14);
463 g_assert ((code - start) < sizeof(start));
467 /*========================= End of Function ========================*/
469 /*------------------------------------------------------------------*/
471 /* Name - throw_exception. */
473 /* Function - Raise an exception based on the parameters passed.*/
475 /*------------------------------------------------------------------*/
478 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
479 gulong *int_regs, gdouble *fp_regs, gulong *acc_regs, guint fpc)
481 static void (*restore_context) (MonoContext *);
485 memset(&ctx, 0, sizeof(ctx));
489 /* adjust eip so that it point into the call instruction */
492 for (iReg = 0; iReg < 16; iReg++) {
493 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
494 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
495 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
498 ctx.uc_mcontext.fpregs.fpc = fpc;
500 MONO_CONTEXT_SET_BP (&ctx, sp);
501 MONO_CONTEXT_SET_IP (&ctx, ip);
503 mono_arch_handle_exception (&ctx, exc, FALSE);
506 g_assert_not_reached ();
509 /*========================= End of Function ========================*/
511 /*------------------------------------------------------------------*/
513 /* Name - arch_get_throw_exception_generic */
515 /* Function - Return a function pointer which can be used to */
516 /* raise exceptions. The returned function has the */
517 /* following signature: */
518 /* void (*func) (MonoException *exc); or, */
519 /* void (*func) (char *exc_name); */
521 /*------------------------------------------------------------------*/
524 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name)
527 int alloc_size, pos, i, offset;
531 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
532 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
533 s390_lr (code, s390_r14, STK_BASE);
534 s390_ahi (code, STK_BASE, -alloc_size);
535 s390_st (code, s390_r14, 0, STK_BASE, 0);
537 s390_lr (code, s390_r4, s390_r2);
538 s390_bras (code, s390_r13, 6);
539 s390_word (code, mono_defaults.corlib);
540 s390_word (code, "System");
541 s390_l (code, s390_r2, 0, s390_r13, 0);
542 s390_l (code, s390_r3, 0, s390_r13, 4);
543 offset = (guint32) S390_RELATIVE(mono_exception_from_name, code);
544 s390_brasl(code, s390_r14, offset);
546 /*------------------------------------------------------*/
547 /* save the general registers on the stack */
548 /*------------------------------------------------------*/
549 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
551 s390_lr (code, s390_r1, STK_BASE);
552 s390_ahi (code, s390_r1, alloc_size);
553 /*------------------------------------------------------*/
554 /* save the return address in the parameter register */
555 /*------------------------------------------------------*/
556 s390_l (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
558 /*------------------------------------------------------*/
559 /* save the floating point registers */
560 /*------------------------------------------------------*/
561 pos = S390_THROWSTACK_FLTREGS;
562 for (i = 0; i < 16; ++i) {
563 s390_std (code, i, 0,STK_BASE, pos);
564 pos += sizeof (gdouble);
566 /*------------------------------------------------------*/
567 /* save the access registers */
568 /*------------------------------------------------------*/
569 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
571 /*------------------------------------------------------*/
572 /* call throw_exception (exc, ip, sp, gr, fr, ar) */
573 /* exc is already in place in r2 */
574 /*------------------------------------------------------*/
575 s390_lr (code, s390_r4, s390_r1); /* caller sp */
576 /*------------------------------------------------------*/
577 /* pointer to the saved int regs */
578 /*------------------------------------------------------*/
579 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
580 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
581 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
582 s390_st (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
583 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM);
584 offset = (guint32) S390_RELATIVE(throw_exception, code);
585 s390_brasl(code, s390_r14, offset);
586 /* we should never reach this breakpoint */
588 g_assert ((code - start) < size);
592 /*========================= End of Function ========================*/
594 /*------------------------------------------------------------------*/
596 /* Name - arch_get_throw_exception */
598 /* Function - Return a function pointer which can be used to */
599 /* raise exceptions. The returned function has the */
600 /* following signature: */
601 /* void (*func) (MonoException *exc); */
603 /*------------------------------------------------------------------*/
606 mono_arch_get_throw_exception (void)
608 static guint8 start [256];
609 static int inited = 0;
613 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE);
618 /*========================= End of Function ========================*/
620 /*------------------------------------------------------------------*/
622 /* Name - arch_get_throw_exception_by_name */
624 /* Function - Return a function pointer which can be used to */
625 /* raise corlib exceptions. The return function has */
626 /* the following signature: */
627 /* void (*func) (char *exc_name); */
629 /*------------------------------------------------------------------*/
632 mono_arch_get_throw_exception_by_name (void)
634 static guint8 start [160];
635 static int inited = 0;
639 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE);
644 /*========================= End of Function ========================*/
646 /*------------------------------------------------------------------*/
648 /* Name - glist_to_array */
650 /* Function - Convert a list to a mono array. */
652 /*------------------------------------------------------------------*/
655 glist_to_array (GList *list)
657 MonoDomain *domain = mono_domain_get ();
664 len = g_list_length (list);
665 res = mono_array_new (domain, mono_defaults.int_class, len);
667 for (i = 0; list; list = list->next, i++)
668 mono_array_set (res, gpointer, i, list->data);
673 /*========================= End of Function ========================*/
675 /*------------------------------------------------------------------*/
677 /* Name - mono_arch_find_jit_info */
679 /* Function - This function is used to gather informatoin from */
680 /* @ctx. It returns the MonoJitInfo of the corres- */
681 /* ponding function, unwinds one stack frame and */
682 /* stores the resulting context into @new_ctx. It */
683 /* also stores a string describing the stack location*/
684 /* into @trace (if not NULL), and modifies the @lmf */
685 /* if necessary. @native_offset returns the IP off- */
686 /* set from the start of the function or -1 if that */
687 /* informatoin is not available. */
689 /*------------------------------------------------------------------*/
692 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
693 MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
694 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
695 int *native_offset, gboolean *managed)
698 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
701 MonoS390StackFrame *sframe;
704 (ip > prev_ji->code_start &&
705 ((guint8 *) ip < ((guint8 *) prev_ji->code_start) + prev_ji->code_size)))
708 ji = mono_jit_info_table_find (domain, ip);
720 char *source_location, *tmpaddr, *fname;
721 gint32 address, iloffset;
725 // memcpy(new_ctx, ctx, sizeof(*new_ctx));
727 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
728 /* remove any unused lmf */
729 *lmf = (*lmf)->previous_lmf;
732 address = (char *)ip - (char *)ji->code_start;
735 *native_offset = address;
738 if (!ji->method->wrapper_type)
742 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
743 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
746 tmpaddr = g_strdup_printf ("<0x%08x>", address);
748 tmpaddr = g_strdup_printf ("[0x%08x]", iloffset);
750 fname = mono_method_full_name (ji->method, TRUE);
753 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
755 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
758 g_free (source_location);
761 sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_BP (ctx);
762 MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
763 sframe = (MonoS390StackFrame *) sframe->prev;
764 MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
765 memcpy (&new_ctx->uc_mcontext.gregs[6], sframe->regs, (8*sizeof(gint32)));
768 #ifdef MONO_USE_EXC_TABLES
769 } else if ((ji = s390_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
781 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
783 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
786 memset (res, 0, sizeof (MonoJitInfo));
787 res->method = (*lmf)->method;
791 MONO_CONTEXT_SET_BP (ctx, (*lmf)->ebp);
792 MONO_CONTEXT_SET_IP (ctx, (*lmf)->eip);
794 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
795 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
797 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
798 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
799 *lmf = (*lmf)->previous_lmf;
808 /*========================= End of Function ========================*/
810 /*------------------------------------------------------------------*/
812 /* Name - ves_icall_get_trace */
816 /*------------------------------------------------------------------*/
819 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
821 MonoDomain *domain = mono_domain_get ();
823 MonoArray *ta = exc->trace_ips;
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,
833 len > skip ? len - skip : 0);
835 for (i = skip; i < len; i++) {
837 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
838 gpointer ip = mono_array_get (ta, gpointer, i);
840 ji = mono_jit_info_table_find (domain, ip);
842 mono_array_set (res, gpointer, i, sf);
846 sf->method = mono_method_get_object (domain, ji->method, NULL);
847 sf->native_offset = (char *)ip - (char *)ji->code_start;
849 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
851 if (need_file_info) {
854 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
856 sf->filename = filename? mono_string_new (domain, filename): NULL;
862 mono_array_set (res, gpointer, i, sf);
868 /*========================= End of Function ========================*/
870 /*------------------------------------------------------------------*/
872 /* Name - mono_jit_walk_stack */
876 /*------------------------------------------------------------------*/
879 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
880 MonoDomain *domain = mono_domain_get ();
881 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
882 MonoLMF *lmf = jit_tls->lmf;
883 MonoJitInfo *ji, rji;
884 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, NULL,
894 &ctx, &new_ctx, NULL, &lmf,
895 &native_offset, &managed);
898 if (ji == (gpointer)-1)
901 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
903 if (func (ji->method, native_offset, il_offset, managed, user_data))
910 /*========================= End of Function ========================*/
912 /*------------------------------------------------------------------*/
914 /* Name - ves_icall_get_frame_info */
918 /*------------------------------------------------------------------*/
921 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
922 MonoReflectionMethod **method,
923 gint32 *iloffset, gint32 *native_offset,
924 MonoString **file, gint32 *line, gint32 *column)
926 MonoDomain *domain = mono_domain_get ();
927 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
928 MonoLMF *lmf = jit_tls->lmf;
929 MonoJitInfo *ji, rji;
930 MonoContext ctx, new_ctx;
932 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
933 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
938 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL,
939 &ctx, &new_ctx, NULL, &lmf,
940 native_offset, NULL);
944 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
947 /* skip all wrappers ??*/
948 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
949 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
950 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
957 *method = mono_method_get_object (domain, ji->method, NULL);
958 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
960 if (need_file_info) {
963 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
965 *file = filename? mono_string_new (domain, filename): NULL;
974 /*========================= End of Function ========================*/
976 /*------------------------------------------------------------------*/
978 /* Name - mono_arch_handle_exception */
980 /* Function - Handle an exception raised by the JIT code. */
982 /* Parameters - ctx - Saved processor state */
983 /* obj - The exception object */
984 /* test_only - Only test if the exception is caught, */
985 /* but don't call handlers */
987 /*------------------------------------------------------------------*/
990 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
992 MonoContext *ctx = uc;
993 MonoDomain *domain = mono_domain_get ();
994 MonoJitInfo *ji, rji;
995 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
996 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
997 MonoLMF *lmf = jit_tls->lmf;
998 GList *trace_ips = NULL;
999 MonoException *mono_ex;
1001 g_assert (ctx != NULL);
1002 memset(&rji, 0, sizeof(rji));
1004 MonoException *ex = mono_get_exception_null_reference ();
1005 ex->message = mono_string_new (domain,
1006 "Object reference not set to an instance of an object");
1007 obj = (MonoObject *)ex;
1010 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1011 mono_ex = (MonoException*)obj;
1012 mono_ex->stack_trace = NULL;
1019 call_filter = arch_get_call_filter ();
1021 g_assert (jit_tls->end_of_stack);
1022 g_assert (jit_tls->abort_func);
1025 MonoContext ctx_cp = *ctx;
1026 if (mono_jit_trace_calls != NULL)
1027 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
1028 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
1029 if (mono_break_on_exc)
1031 mono_unhandled_exception (obj);
1036 MonoContext new_ctx;
1039 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
1040 test_only ? &trace : NULL, &lmf, NULL, NULL);
1042 g_warning ("Exception inside function without unwind info");
1043 g_assert_not_reached ();
1046 if (ji != (gpointer)-1) {
1048 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1051 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1053 if (!mono_ex->stack_trace)
1054 strace = g_strdup ("");
1056 strace = mono_string_to_utf8 (mono_ex->stack_trace);
1058 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1061 mono_ex->stack_trace = mono_string_new (domain, tmp);
1066 if (ji->num_clauses) {
1069 g_assert (ji->clauses);
1071 for (i = 0; i < ji->num_clauses; i++) {
1072 MonoJitExceptionInfo *ei = &ji->clauses [i];
1074 if (ei->try_start < MONO_CONTEXT_GET_IP (ctx) &&
1075 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1077 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1078 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1079 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1080 call_filter (ctx, ei->data.filter, obj)))) {
1083 mono_ex->trace_ips = glist_to_array (trace_ips);
1084 g_list_free (trace_ips);
1088 // memcpy(ctx, &new_ctx, sizeof(new_ctx));
1089 if (mono_jit_trace_calls != NULL)
1090 g_print ("EXCEPTION: catch found at clause %d of %s - caught at %p with sp %p\n",
1091 i, mono_method_full_name (ji->method, TRUE),
1093 MONO_CONTEXT_GET_BP(ctx));
1094 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1095 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1100 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1101 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1102 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1103 if (mono_jit_trace_calls != NULL)
1104 g_print ("EXCEPTION: finally clause %d of %s handled at: %p using sp: %p\n",
1105 i, mono_method_full_name (ji->method, TRUE),
1107 MONO_CONTEXT_GET_BP(ctx));
1108 call_filter (ctx, ei->handler_start, NULL);
1120 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1123 jit_tls->abort_func (obj);
1124 g_assert_not_reached ();
1127 mono_ex->trace_ips = glist_to_array (trace_ips);
1128 g_list_free (trace_ips);
1134 g_assert_not_reached ();
1137 /*========================= End of Function ========================*/
1139 /*------------------------------------------------------------------*/
1141 /* Name - mono_arch_ip_from_context */
1143 /* Function - Return the instruction pointer from the context. */
1145 /* Parameters - sigctx - Saved processor state */
1147 /*------------------------------------------------------------------*/
1150 mono_arch_ip_from_context (void *sigctx)
1152 return context_get_ip (sigctx);