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 /*------------------------------------------------------------------*/
64 #include <mono/arch/s390/s390-codegen.h>
65 #include <mono/metadata/appdomain.h>
66 #include <mono/metadata/tabledefs.h>
67 #include <mono/metadata/threads.h>
68 #include <mono/metadata/debug-helpers.h>
69 #include <mono/metadata/exception.h>
70 #include <mono/metadata/mono-debug.h>
73 #include "mini-s390.h"
75 /*========================= End of Includes ========================*/
77 /*------------------------------------------------------------------*/
79 /*------------------------------------------------------------------*/
81 typedef struct ucontext MonoContext;
89 #ifdef MONO_USE_EXC_TABLES
91 /*************************************/
92 /* STACK UNWINDING STUFF */
93 /*************************************/
95 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
98 #define DWARF_FRAME_REGISTERS 17
100 typedef struct frame_state
106 long reg_or_offset[DWARF_FRAME_REGISTERS+1];
107 unsigned short cfa_reg;
108 unsigned short retaddr_column;
109 char saved[DWARF_FRAME_REGISTERS+1];
112 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
116 /*========================= End of Typedefs ========================*/
118 /*------------------------------------------------------------------*/
119 /* P r o t o t y p e s */
120 /*------------------------------------------------------------------*/
122 gboolean mono_arch_handle_exception (MonoContext *ctx,
126 /*========================= End of Prototypes ======================*/
128 /*------------------------------------------------------------------*/
129 /* G l o b a l V a r i a b l e s */
130 /*------------------------------------------------------------------*/
132 #ifdef MONO_USE_EXC_TABLES
134 static framesf frame_state_for = NULL;
136 static gboolean inited = FALSE;
138 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
140 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
144 /*====================== End of Global Variables ===================*/
146 /*------------------------------------------------------------------*/
148 /* Name - context_get_ip */
150 /* Function - Extract the current instruction address from the */
153 /*------------------------------------------------------------------*/
155 static inline gpointer
156 context_get_ip (MonoContext *ctx)
160 ip = (gpointer) ((gint32) (ctx->uc_mcontext.psw.addr) & 0x7fffffff);
164 /*========================= End of Function ========================*/
166 #ifdef MONO_USE_EXC_TABLES
168 /*------------------------------------------------------------------*/
170 /* Name - init_frame_state_for */
172 /* Function - Load the __frame_state_for from libc. */
173 /* There are two versions of __frame_state_for: one */
174 /* in libgcc.a and the other in glibc.so. We need */
175 /* the version from glibc. For more information, see:*/
176 /* http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html */
178 /*------------------------------------------------------------------*/
181 init_frame_state_for (void)
185 if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
187 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
188 frame_state_for = NULL;
190 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
191 get_backtrace_symbols = NULL;
192 frame_state_for = NULL;
195 g_module_close (module);
201 /*========================= End of Function ========================*/
205 /*------------------------------------------------------------------*/
207 /* Name - mono_arch_has_unwind_info */
209 /* Function - Tests if a function has a DWARF exception table */
210 /* that is able to restore all caller saved registers*/
212 /*------------------------------------------------------------------*/
215 mono_arch_has_unwind_info (gconstpointer addr)
218 struct frame_state state_in;
219 struct frame_state *res;
222 init_frame_state_for ();
224 if (!frame_state_for)
227 g_assert (method->addr);
229 memset (&state_in, 0, sizeof (state_in));
231 /* offset 10 is just a guess, but it works for all methods tested */
232 if ((res = frame_state_for ((char *)method->addr + 10, &state_in))) {
234 if (res->saved [X86_EBX] != 1 ||
235 res->saved [X86_EDI] != 1 ||
236 res->saved [X86_EBP] != 1 ||
237 res->saved [X86_ESI] != 1) {
249 /*========================= End of Function ========================*/
251 /*------------------------------------------------------------------*/
253 /* Name - s390_unwind_native_frame */
255 /* Function - Use the context to unwind a stack frame. */
257 /*------------------------------------------------------------------*/
260 s390_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
261 struct sigcontext *ctx, struct sigcontext *new_ctx,
262 MonoLMF *lmf, char **trace)
265 struct stack_frame *frame;
268 struct frame_state state_in;
269 struct frame_state *res;
275 init_frame_state_for ();
277 if (!frame_state_for)
280 frame = MONO_CONTEXT_GET_BP (ctx);
282 max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
286 memset (&state_in, 0, sizeof (state_in));
288 while ((gpointer)frame->next < (gpointer)max_stack) {
289 gpointer ip, addr = frame->return_address;
291 char *tmp, **symbols;
294 ip = MONO_CONTEXT_GET_IP (new_ctx);
295 symbols = get_backtrace_symbols (&ip, 1);
297 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
299 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
306 if ((res = frame_state_for (addr, &state_in))) {
309 cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
310 frame = (struct stack_frame *)((gint8*)cfa - 8);
311 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
312 int how = res->saved[i];
314 g_assert ((how == 0) || (how == 1));
317 val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
318 set_sigcontext_reg (new_ctx, i, val);
321 new_ctx->SC_ESP = (long)cfa;
323 if (res->saved [X86_EBX] == 1 &&
324 res->saved [X86_EDI] == 1 &&
325 res->saved [X86_EBP] == 1 &&
326 res->saved [X86_ESI] == 1 &&
327 (ji = mono_jit_info_table_find (domain, frame->return_address))) {
328 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
333 // printf ("FRAME %p %p %p\n",
334 // frame, MONO_CONTEXT_GET_IP (new_ctx),
335 // mono_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (new_ctx)));
337 MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
339 MONO_CONTEXT_SET_BP (new_ctx, frame);
341 /* stop if !frame or when we detect an unexpected managed frame */
342 if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
360 /*========================= End of Function ========================*/
362 /*------------------------------------------------------------------*/
364 /* Name - arch_get_call_filter */
366 /* Function - Return a pointer to a method which calls an */
367 /* exception filter. We also use this function to */
368 /* call finally handlers (we pass NULL as @exc */
369 /* object in this case). */
371 /*------------------------------------------------------------------*/
374 arch_get_call_filter (void)
376 static guint8 start [256];
377 static int inited = 0;
379 int alloc_size, pos, i;
385 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
388 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
389 s390_lr (code, s390_r14, STK_BASE);
390 alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
391 s390_ahi (code, STK_BASE, -alloc_size);
392 s390_st (code, s390_r14, 0, STK_BASE, 0);
394 /*------------------------------------------------------*/
395 /* save general registers on stack */
396 /*------------------------------------------------------*/
397 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
399 /*------------------------------------------------------*/
400 /* save floating point registers on stack */
401 /*------------------------------------------------------*/
402 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
403 pos = S390_CALLFILTER_FLTREGS;
404 for (i = 0; i < 16; ++i) {
405 s390_std (code, i, 0, STK_BASE, pos);
406 pos += sizeof (gdouble);
409 /*------------------------------------------------------*/
410 /* save access registers on stack */
411 /*------------------------------------------------------*/
412 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_CALLFILTER_ACCREGS);
414 s390_lr (code, s390_r13, s390_r2);
415 s390_lr (code, s390_r14, s390_r3);
416 s390_lr (code, s390_r2, s390_r4);
418 s390_lm (code, s390_r0, s390_r1, s390_r13, G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs));
419 s390_lm (code, s390_r3, s390_r12, s390_r13, G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[2]));
420 pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
421 for (i = 0; i < 16; ++i) {
422 s390_ld (code, i, 0, s390_r13, pos);
423 pos += sizeof(gdouble);
426 s390_basr (code, s390_r14, s390_r14);
428 /* restore all the regs from the stack */
429 s390_lm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
430 pos = S390_CALLFILTER_FLTREGS;
431 for (i = 0; i < 16; ++i) {
432 s390_ld (code, i, 0, STK_BASE, pos);
433 pos += sizeof (gdouble);
436 s390_lam (code, s390_r0, s390_r15, STK_BASE, S390_CALLFILTER_ACCREGS);
437 s390_ahi (code, s390_r15, alloc_size);
438 s390_lm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
439 s390_br (code, s390_r14);
441 g_assert ((code - start) < sizeof(start));
445 /*========================= End of Function ========================*/
447 /*------------------------------------------------------------------*/
449 /* Name - arch_get_restore_context */
451 /* Function - Return a pointer to a method which restores a */
452 /* previously saved context. */
454 /*------------------------------------------------------------------*/
457 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
458 gulong *int_regs, gdouble *fp_regs, gulong *acc_regs, guint fpc)
460 static void (*restore_context) (MonoContext *);
464 memset(&ctx, 0, sizeof(ctx));
468 /* adjust eip so that it point into the call instruction */
471 for (iReg = 0; iReg < 16; iReg++) {
472 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
473 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
474 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
477 ctx.uc_mcontext.fpregs.fpc = fpc;
479 MONO_CONTEXT_SET_BP (&ctx, sp);
480 MONO_CONTEXT_SET_IP (&ctx, ip);
482 mono_arch_handle_exception (&ctx, exc, FALSE);
485 g_assert_not_reached ();
488 /*========================= End of Function ========================*/
490 /*------------------------------------------------------------------*/
492 /* Name - arch_get_throw_exception_generic */
494 /* Function - Return a function pointer which can be used to */
495 /* raise exceptions. The returned function has the */
496 /* following signature: */
497 /* void (*func) (MonoException *exc); or, */
498 /* void (*func) (char *exc_name); */
500 /*------------------------------------------------------------------*/
503 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name)
506 int alloc_size, pos, i, offset;
510 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
511 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
512 s390_lr (code, s390_r14, STK_BASE);
513 s390_ahi (code, STK_BASE, -alloc_size);
514 s390_st (code, s390_r14, 0, STK_BASE, 0);
516 s390_lr (code, s390_r4, s390_r2);
517 s390_bras (code, s390_r13, 6);
518 s390_word (code, mono_defaults.corlib);
519 s390_word (code, "System");
520 s390_l (code, s390_r2, 0, s390_r13, 0);
521 s390_l (code, s390_r3, 0, s390_r13, 4);
522 offset = (guint32) S390_RELATIVE(mono_exception_from_name, code);
523 s390_brasl(code, s390_r14, offset);
525 /*------------------------------------------------------*/
526 /* save the general registers on the stack */
527 /*------------------------------------------------------*/
528 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
530 s390_lr (code, s390_r1, STK_BASE);
531 s390_ahi (code, s390_r1, alloc_size);
532 /*------------------------------------------------------*/
533 /* save the return address in the parameter register */
534 /*------------------------------------------------------*/
535 s390_l (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
537 /*------------------------------------------------------*/
538 /* save the floating point registers */
539 /*------------------------------------------------------*/
540 pos = S390_THROWSTACK_FLTREGS;
541 for (i = 0; i < 16; ++i) {
542 s390_std (code, i, 0,STK_BASE, pos);
543 pos += sizeof (gdouble);
545 /*------------------------------------------------------*/
546 /* save the access registers */
547 /*------------------------------------------------------*/
548 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
550 /*------------------------------------------------------*/
551 /* call throw_exception (exc, ip, sp, gr, fr, ar) */
552 /* exc is already in place in r2 */
553 /*------------------------------------------------------*/
554 s390_lr (code, s390_r4, s390_r1); /* caller sp */
555 /*------------------------------------------------------*/
556 /* pointer to the saved int regs */
557 /*------------------------------------------------------*/
558 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
559 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
560 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
561 s390_st (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
562 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM);
563 offset = (guint32) S390_RELATIVE(throw_exception, code);
564 s390_brasl(code, s390_r14, offset);
565 /* we should never reach this breakpoint */
567 g_assert ((code - start) < size);
571 /*========================= End of Function ========================*/
573 /*------------------------------------------------------------------*/
575 /* Name - arch_get_throw_exception */
577 /* Function - Return a function pointer which can be used to */
578 /* raise exceptions. The returned function has the */
579 /* following signature: */
580 /* void (*func) (MonoException *exc); */
582 /*------------------------------------------------------------------*/
585 mono_arch_get_throw_exception (void)
587 static guint8 start [128];
588 static int inited = 0;
592 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE);
597 /*========================= End of Function ========================*/
599 /*------------------------------------------------------------------*/
601 /* Name - arch_get_throw_exception_by_name */
603 /* Function - Return a function pointer which can be used to */
604 /* raise corlib exceptions. The return function has */
605 /* the following signature: */
606 /* void (*func) (char *exc_name); */
608 /*------------------------------------------------------------------*/
611 mono_arch_get_throw_exception_by_name (void)
613 static guint8 start [160];
614 static int inited = 0;
618 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE);
623 /*========================= End of Function ========================*/
625 /*------------------------------------------------------------------*/
627 /* Name - glist_to_array */
629 /* Function - Convert a list to a mono array. */
631 /*------------------------------------------------------------------*/
634 glist_to_array (GList *list)
636 MonoDomain *domain = mono_domain_get ();
643 len = g_list_length (list);
644 res = mono_array_new (domain, mono_defaults.int_class, len);
646 for (i = 0; list; list = list->next, i++)
647 mono_array_set (res, gpointer, i, list->data);
652 /*========================= End of Function ========================*/
654 /*------------------------------------------------------------------*/
656 /* Name - mono_arch_find_jit_info */
658 /* Function - This function is used to gather informatoin from */
659 /* @ctx. It returns the MonoJitInfo of the corres- */
660 /* ponding function, unwinds one stack frame and */
661 /* stores the resulting context into @new_ctx. It */
662 /* also stores a string describing the stack location*/
663 /* into @trace (if not NULL), and modifies the @lmf */
664 /* if necessary. @native_offset returns the IP off- */
665 /* set from the start of the function or -1 if that */
666 /* informatoin is not available. */
668 /*------------------------------------------------------------------*/
671 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
672 MonoJitInfo *res, MonoContext *ctx,
673 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
674 int *native_offset, gboolean *managed)
677 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
681 ji = mono_jit_info_table_find (domain, ip);
693 char *source_location, *tmpaddr, *fname;
694 gint32 address, iloffset;
699 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
700 /* remove any unused lmf */
701 *lmf = (*lmf)->previous_lmf;
704 address = (char *)ip - (char *)ji->code_start;
707 *native_offset = address;
710 if (!ji->method->wrapper_type)
714 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
715 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
718 tmpaddr = g_strdup_printf ("<0x%05x>", address);
720 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
722 fname = mono_method_full_name (ji->method, TRUE);
725 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
727 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
730 g_free (source_location);
735 /* restore caller saved registers */
736 if (ji->used_regs & X86_EBX_MASK) {
737 new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
740 if (ji->used_regs & X86_EDI_MASK) {
741 new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
744 if (ji->used_regs & X86_ESI_MASK) {
745 new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
748 new_ctx->SC_ESP = ctx->SC_EBP;
749 /* we substract 1, so that the IP points into the call instruction */
750 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
751 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
753 MONO_CONTEXT_SET_BP (new_ctx, MONO_CONTEXT_GET_BP (ctx));
754 MONO_CONTEXT_SET_IP (new_ctx, MONO_CONTEXT_GET_IP (ctx));
757 #ifdef MONO_USE_EXC_TABLES
758 } else if ((ji = s390_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
770 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
772 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
775 memset (res, 0, sizeof (MonoJitInfo));
776 res->method = (*lmf)->method;
780 new_ctx->SC_ESI = (*lmf)->esi;
781 new_ctx->SC_EDI = (*lmf)->edi;
782 new_ctx->SC_EBX = (*lmf)->ebx;
783 new_ctx->SC_EBP = (*lmf)->ebp;
784 new_ctx->SC_EIP = (*lmf)->eip;
785 /* the lmf is always stored on the stack, so the following
786 * expression points to a stack location which can be used as ESP */
787 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
789 MONO_CONTEXT_SET_BP (new_ctx, MONO_CONTEXT_GET_BP (ctx));
790 MONO_CONTEXT_SET_IP (new_ctx, MONO_CONTEXT_GET_IP (ctx));
791 *lmf = (*lmf)->previous_lmf;
800 /*========================= End of Function ========================*/
802 /*------------------------------------------------------------------*/
804 /* Name - ves_icall_get_trace */
808 /*------------------------------------------------------------------*/
811 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
813 MonoDomain *domain = mono_domain_get ();
815 MonoArray *ta = exc->trace_ips;
818 len = mono_array_length (ta);
820 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
822 for (i = skip; i < len; i++) {
824 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
825 gpointer ip = mono_array_get (ta, gpointer, i);
827 ji = mono_jit_info_table_find (domain, ip);
828 g_assert (ji != NULL);
830 sf->method = mono_method_get_object (domain, ji->method, NULL);
831 sf->native_offset = (char *)ip - (char *)ji->code_start;
833 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
835 if (need_file_info) {
838 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
840 sf->filename = filename? mono_string_new (domain, filename): NULL;
846 mono_array_set (res, gpointer, i, sf);
852 /*========================= End of Function ========================*/
854 /*------------------------------------------------------------------*/
856 /* Name - mono_jit_walk_stack */
860 /*------------------------------------------------------------------*/
863 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
864 MonoDomain *domain = mono_domain_get ();
865 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
866 MonoLMF *lmf = jit_tls->lmf;
867 MonoJitInfo *ji, rji;
868 gint native_offset, il_offset;
870 MonoContext ctx, new_ctx;
872 MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
873 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
875 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
877 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
880 if (ji == (gpointer)-1)
883 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
885 if (func (ji->method, native_offset, il_offset, managed, user_data))
892 /*========================= End of Function ========================*/
894 /*------------------------------------------------------------------*/
896 /* Name - ves_icall_get_frame_info */
900 /*------------------------------------------------------------------*/
903 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
904 MonoReflectionMethod **method,
905 gint32 *iloffset, gint32 *native_offset,
906 MonoString **file, gint32 *line, gint32 *column)
908 MonoDomain *domain = mono_domain_get ();
909 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
910 MonoLMF *lmf = jit_tls->lmf;
911 MonoJitInfo *ji, rji;
912 MonoContext ctx, new_ctx;
914 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
915 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
920 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
924 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
927 /* skip all wrappers ??*/
928 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
929 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
930 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
937 *method = mono_method_get_object (domain, ji->method, NULL);
938 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
940 if (need_file_info) {
943 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
945 *file = filename? mono_string_new (domain, filename): NULL;
954 /*========================= End of Function ========================*/
956 /*------------------------------------------------------------------*/
958 /* Name - mono_arch_handle_exception */
960 /* Function - Handle an exception raised by the JIT code. */
962 /* Parameters - ctx - Saved processor state */
963 /* obj - The exception object */
964 /* test_only - Only test if the exception is caught, */
965 /* but don't call handlers */
967 /*------------------------------------------------------------------*/
970 mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
972 MonoDomain *domain = mono_domain_get ();
973 MonoJitInfo *ji, rji;
974 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
975 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
976 MonoLMF *lmf = jit_tls->lmf;
977 GList *trace_ips = NULL;
978 MonoException *mono_ex;
980 g_assert (ctx != NULL);
982 MonoException *ex = mono_get_exception_null_reference ();
983 ex->message = mono_string_new (domain,
984 "Object reference not set to an instance of an object");
985 obj = (MonoObject *)ex;
988 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
989 mono_ex = (MonoException*)obj;
990 mono_ex->stack_trace = NULL;
997 call_filter = arch_get_call_filter ();
999 g_assert (jit_tls->end_of_stack);
1000 g_assert (jit_tls->abort_func);
1003 MonoContext ctx_cp = *ctx;
1004 if (mono_jit_trace_calls != NULL)
1005 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
1006 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
1007 if (mono_break_on_exc)
1009 mono_unhandled_exception (obj);
1014 MonoContext new_ctx;
1017 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, ctx, &new_ctx,
1018 test_only ? &trace : NULL, &lmf, NULL, NULL);
1020 g_warning ("Exception inside function without unwind info");
1021 g_assert_not_reached ();
1024 if (ji != (gpointer)-1) {
1026 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1029 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1031 if (!mono_ex->stack_trace)
1032 strace = g_strdup ("");
1034 strace = mono_string_to_utf8 (mono_ex->stack_trace);
1036 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1039 mono_ex->stack_trace = mono_string_new (domain, tmp);
1044 if (ji->num_clauses) {
1047 g_assert (ji->clauses);
1049 for (i = 0; i < ji->num_clauses; i++) {
1050 MonoJitExceptionInfo *ei = &ji->clauses [i];
1052 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1053 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1055 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1056 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1057 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1058 call_filter (ctx, ei->data.filter, obj)))) {
1061 mono_ex->trace_ips = glist_to_array (trace_ips);
1062 g_list_free (trace_ips);
1066 if (mono_jit_trace_calls != NULL)
1067 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1068 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1069 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1074 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1075 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1076 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1077 if (mono_jit_trace_calls != NULL)
1078 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1079 call_filter (ctx, ei->handler_start, NULL);
1091 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1094 jit_tls->abort_func (obj);
1095 g_assert_not_reached ();
1098 mono_ex->trace_ips = glist_to_array (trace_ips);
1099 g_list_free (trace_ips);
1105 g_assert_not_reached ();
1108 /*========================= End of Function ========================*/