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 /*------------------------------------------------------------------*/
96 #ifdef MONO_USE_EXC_TABLES
98 /*************************************/
99 /* STACK UNWINDING STUFF */
100 /*************************************/
102 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
105 #define DWARF_FRAME_REGISTERS 17
107 typedef struct frame_state
113 long reg_or_offset[DWARF_FRAME_REGISTERS+1];
114 unsigned short cfa_reg;
115 unsigned short retaddr_column;
116 char saved[DWARF_FRAME_REGISTERS+1];
119 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
123 /*========================= End of Typedefs ========================*/
125 /*------------------------------------------------------------------*/
126 /* P r o t o t y p e s */
127 /*------------------------------------------------------------------*/
129 gboolean mono_arch_handle_exception (void *ctx,
133 /*========================= End of Prototypes ======================*/
135 /*------------------------------------------------------------------*/
136 /* G l o b a l V a r i a b l e s */
137 /*------------------------------------------------------------------*/
139 #ifdef MONO_USE_EXC_TABLES
141 static framesf frame_state_for = NULL;
143 static gboolean inited = FALSE;
145 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
147 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
151 /*====================== End of Global Variables ===================*/
153 /*------------------------------------------------------------------*/
155 /* Name - context_get_ip */
157 /* Function - Extract the current instruction address from the */
160 /*------------------------------------------------------------------*/
162 static inline gpointer
163 context_get_ip (MonoContext *ctx)
167 ip = (gpointer) ((gint32) (ctx->uc_mcontext.psw.addr) & 0x7fffffff);
171 /*========================= End of Function ========================*/
173 #ifdef MONO_USE_EXC_TABLES
175 /*------------------------------------------------------------------*/
177 /* Name - init_frame_state_for */
179 /* Function - Load the __frame_state_for from libc. */
180 /* There are two versions of __frame_state_for: one */
181 /* in libgcc.a and the other in glibc.so. We need */
182 /* the version from glibc. For more information, see:*/
183 /* http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html */
185 /*------------------------------------------------------------------*/
188 init_frame_state_for (void)
192 if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
194 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
195 frame_state_for = NULL;
197 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
198 get_backtrace_symbols = NULL;
199 frame_state_for = NULL;
202 g_module_close (module);
208 /*========================= End of Function ========================*/
212 /*------------------------------------------------------------------*/
214 /* Name - mono_arch_has_unwind_info */
216 /* Function - Tests if a function has a DWARF exception table */
217 /* that is able to restore all caller saved registers*/
219 /*------------------------------------------------------------------*/
222 mono_arch_has_unwind_info (gconstpointer addr)
225 struct frame_state state_in;
226 struct frame_state *res;
229 init_frame_state_for ();
231 if (!frame_state_for)
234 g_assert (method->addr);
236 memset (&state_in, 0, sizeof (state_in));
238 /* offset 10 is just a guess, but it works for all methods tested */
239 if ((res = frame_state_for ((char *)method->addr + 10, &state_in))) {
241 if (res->saved [X86_EBX] != 1 ||
242 res->saved [X86_EDI] != 1 ||
243 res->saved [X86_EBP] != 1 ||
244 res->saved [X86_ESI] != 1) {
256 /*========================= End of Function ========================*/
258 /*------------------------------------------------------------------*/
260 /* Name - s390_unwind_native_frame */
262 /* Function - Use the context to unwind a stack frame. */
264 /*------------------------------------------------------------------*/
267 s390_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
268 struct sigcontext *ctx, struct sigcontext *new_ctx,
269 MonoLMF *lmf, char **trace)
272 struct stack_frame *frame;
275 struct frame_state state_in;
276 struct frame_state *res;
282 init_frame_state_for ();
284 if (!frame_state_for)
287 frame = MONO_CONTEXT_GET_BP (ctx);
289 max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
293 memset (&state_in, 0, sizeof (state_in));
295 while ((gpointer)frame->next < (gpointer)max_stack) {
296 gpointer ip, addr = frame->return_address;
298 char *tmp, **symbols;
301 ip = MONO_CONTEXT_GET_IP (new_ctx);
302 symbols = get_backtrace_symbols (&ip, 1);
304 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
306 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
313 if ((res = frame_state_for (addr, &state_in))) {
316 cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
317 frame = (struct stack_frame *)((gint8*)cfa - 8);
318 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
319 int how = res->saved[i];
321 g_assert ((how == 0) || (how == 1));
324 val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
325 set_sigcontext_reg (new_ctx, i, val);
328 new_ctx->SC_ESP = (long)cfa;
330 if (res->saved [X86_EBX] == 1 &&
331 res->saved [X86_EDI] == 1 &&
332 res->saved [X86_EBP] == 1 &&
333 res->saved [X86_ESI] == 1 &&
334 (ji = mono_jit_info_table_find (domain, frame->return_address))) {
335 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
340 // printf ("FRAME %p %p %p\n",
341 // frame, MONO_CONTEXT_GET_IP (new_ctx),
342 // mono_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (new_ctx)));
344 MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
346 MONO_CONTEXT_SET_BP (new_ctx, frame);
348 /* stop if !frame or when we detect an unexpected managed frame */
349 if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
367 /*========================= End of Function ========================*/
369 /*------------------------------------------------------------------*/
371 /* Name - arch_get_call_filter */
373 /* Function - Return a pointer to a method which calls an */
374 /* exception filter. We also use this function to */
375 /* call finally handlers (we pass NULL as @exc */
376 /* object in this case). */
378 /*------------------------------------------------------------------*/
381 arch_get_call_filter (void)
383 static guint8 start [256];
384 static int inited = 0;
386 int alloc_size, pos, i;
392 /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
395 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
396 s390_lr (code, s390_r14, STK_BASE);
397 alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
398 s390_ahi (code, STK_BASE, -alloc_size);
399 s390_st (code, s390_r14, 0, STK_BASE, 0);
401 /*------------------------------------------------------*/
402 /* save general registers on stack */
403 /*------------------------------------------------------*/
404 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
406 /*------------------------------------------------------*/
407 /* save floating point registers on stack */
408 /*------------------------------------------------------*/
409 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
410 pos = S390_CALLFILTER_FLTREGS;
411 for (i = 0; i < 16; ++i) {
412 s390_std (code, i, 0, STK_BASE, pos);
413 pos += sizeof (gdouble);
416 /*------------------------------------------------------*/
417 /* save access registers on stack */
418 /*------------------------------------------------------*/
419 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_CALLFILTER_ACCREGS);
421 s390_lr (code, s390_r13, s390_r2);
422 s390_lr (code, s390_r14, s390_r3);
423 s390_lr (code, s390_r2, s390_r4);
425 s390_lm (code, s390_r0, s390_r1, s390_r13, G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs));
426 s390_lm (code, s390_r3, s390_r12, s390_r13, G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[2]));
427 pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
428 for (i = 0; i < 16; ++i) {
429 s390_ld (code, i, 0, s390_r13, pos);
430 pos += sizeof(gdouble);
433 s390_basr (code, s390_r14, s390_r14);
435 /* restore all the regs from the stack */
436 s390_lm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
437 pos = S390_CALLFILTER_FLTREGS;
438 for (i = 0; i < 16; ++i) {
439 s390_ld (code, i, 0, STK_BASE, pos);
440 pos += sizeof (gdouble);
443 s390_lam (code, s390_r0, s390_r15, STK_BASE, S390_CALLFILTER_ACCREGS);
444 s390_ahi (code, s390_r15, alloc_size);
445 s390_lm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
446 s390_br (code, s390_r14);
448 g_assert ((code - start) < sizeof(start));
452 /*========================= End of Function ========================*/
454 /*------------------------------------------------------------------*/
456 /* Name - arch_get_restore_context */
458 /* Function - Return a pointer to a method which restores a */
459 /* previously saved context. */
461 /*------------------------------------------------------------------*/
464 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp,
465 gulong *int_regs, gdouble *fp_regs, gulong *acc_regs, guint fpc)
467 static void (*restore_context) (MonoContext *);
471 memset(&ctx, 0, sizeof(ctx));
475 /* adjust eip so that it point into the call instruction */
478 for (iReg = 0; iReg < 16; iReg++) {
479 ctx.uc_mcontext.gregs[iReg] = int_regs[iReg];
480 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
481 ctx.uc_mcontext.aregs[iReg] = acc_regs[iReg];
484 ctx.uc_mcontext.fpregs.fpc = fpc;
486 MONO_CONTEXT_SET_BP (&ctx, sp);
487 MONO_CONTEXT_SET_IP (&ctx, ip);
489 mono_arch_handle_exception (&ctx, exc, FALSE);
492 g_assert_not_reached ();
495 /*========================= End of Function ========================*/
497 /*------------------------------------------------------------------*/
499 /* Name - arch_get_throw_exception_generic */
501 /* Function - Return a function pointer which can be used to */
502 /* raise exceptions. The returned function has the */
503 /* following signature: */
504 /* void (*func) (MonoException *exc); or, */
505 /* void (*func) (char *exc_name); */
507 /*------------------------------------------------------------------*/
510 mono_arch_get_throw_exception_generic (guint8 *start, int size, int by_name)
513 int alloc_size, pos, i, offset;
517 s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
518 alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
519 s390_lr (code, s390_r14, STK_BASE);
520 s390_ahi (code, STK_BASE, -alloc_size);
521 s390_st (code, s390_r14, 0, STK_BASE, 0);
523 s390_lr (code, s390_r4, s390_r2);
524 s390_bras (code, s390_r13, 6);
525 s390_word (code, mono_defaults.corlib);
526 s390_word (code, "System");
527 s390_l (code, s390_r2, 0, s390_r13, 0);
528 s390_l (code, s390_r3, 0, s390_r13, 4);
529 offset = (guint32) S390_RELATIVE(mono_exception_from_name, code);
530 s390_brasl(code, s390_r14, offset);
532 /*------------------------------------------------------*/
533 /* save the general registers on the stack */
534 /*------------------------------------------------------*/
535 s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
537 s390_lr (code, s390_r1, STK_BASE);
538 s390_ahi (code, s390_r1, alloc_size);
539 /*------------------------------------------------------*/
540 /* save the return address in the parameter register */
541 /*------------------------------------------------------*/
542 s390_l (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
544 /*------------------------------------------------------*/
545 /* save the floating point registers */
546 /*------------------------------------------------------*/
547 pos = S390_THROWSTACK_FLTREGS;
548 for (i = 0; i < 16; ++i) {
549 s390_std (code, i, 0,STK_BASE, pos);
550 pos += sizeof (gdouble);
552 /*------------------------------------------------------*/
553 /* save the access registers */
554 /*------------------------------------------------------*/
555 s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
557 /*------------------------------------------------------*/
558 /* call throw_exception (exc, ip, sp, gr, fr, ar) */
559 /* exc is already in place in r2 */
560 /*------------------------------------------------------*/
561 s390_lr (code, s390_r4, s390_r1); /* caller sp */
562 /*------------------------------------------------------*/
563 /* pointer to the saved int regs */
564 /*------------------------------------------------------*/
565 s390_la (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
566 s390_la (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
567 s390_la (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
568 s390_st (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
569 s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM);
570 offset = (guint32) S390_RELATIVE(throw_exception, code);
571 s390_brasl(code, s390_r14, offset);
572 /* we should never reach this breakpoint */
574 g_assert ((code - start) < size);
578 /*========================= End of Function ========================*/
580 /*------------------------------------------------------------------*/
582 /* Name - arch_get_throw_exception */
584 /* Function - Return a function pointer which can be used to */
585 /* raise exceptions. The returned function has the */
586 /* following signature: */
587 /* void (*func) (MonoException *exc); */
589 /*------------------------------------------------------------------*/
592 mono_arch_get_throw_exception (void)
594 static guint8 start [128];
595 static int inited = 0;
599 mono_arch_get_throw_exception_generic (start, sizeof (start), FALSE);
604 /*========================= End of Function ========================*/
606 /*------------------------------------------------------------------*/
608 /* Name - arch_get_throw_exception_by_name */
610 /* Function - Return a function pointer which can be used to */
611 /* raise corlib exceptions. The return function has */
612 /* the following signature: */
613 /* void (*func) (char *exc_name); */
615 /*------------------------------------------------------------------*/
618 mono_arch_get_throw_exception_by_name (void)
620 static guint8 start [160];
621 static int inited = 0;
625 mono_arch_get_throw_exception_generic (start, sizeof (start), TRUE);
630 /*========================= End of Function ========================*/
632 /*------------------------------------------------------------------*/
634 /* Name - glist_to_array */
636 /* Function - Convert a list to a mono array. */
638 /*------------------------------------------------------------------*/
641 glist_to_array (GList *list)
643 MonoDomain *domain = mono_domain_get ();
650 len = g_list_length (list);
651 res = mono_array_new (domain, mono_defaults.int_class, len);
653 for (i = 0; list; list = list->next, i++)
654 mono_array_set (res, gpointer, i, list->data);
659 /*========================= End of Function ========================*/
661 /*------------------------------------------------------------------*/
663 /* Name - mono_arch_find_jit_info */
665 /* Function - This function is used to gather informatoin from */
666 /* @ctx. It returns the MonoJitInfo of the corres- */
667 /* ponding function, unwinds one stack frame and */
668 /* stores the resulting context into @new_ctx. It */
669 /* also stores a string describing the stack location*/
670 /* into @trace (if not NULL), and modifies the @lmf */
671 /* if necessary. @native_offset returns the IP off- */
672 /* set from the start of the function or -1 if that */
673 /* informatoin is not available. */
675 /*------------------------------------------------------------------*/
678 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls,
679 MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
680 MonoContext *new_ctx, char **trace, MonoLMF **lmf,
681 int *native_offset, gboolean *managed)
684 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
687 MonoS390StackFrame *sframe;
690 (ip > prev_ji->code_start &&
691 ((guint8 *) ip < ((guint8 *) prev_ji->code_start) + prev_ji->code_size)))
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);
746 sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_BP (ctx);
747 MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
748 MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
751 #ifdef MONO_USE_EXC_TABLES
752 } else if ((ji = s390_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
764 *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
766 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
769 memset (res, 0, sizeof (MonoJitInfo));
770 res->method = (*lmf)->method;
774 MONO_CONTEXT_SET_BP (ctx, (*lmf)->ebp);
775 MONO_CONTEXT_SET_IP (ctx, (*lmf)->eip);
777 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
778 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
779 *lmf = (*lmf)->previous_lmf;
788 /*========================= End of Function ========================*/
790 /*------------------------------------------------------------------*/
792 /* Name - ves_icall_get_trace */
796 /*------------------------------------------------------------------*/
799 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
801 MonoDomain *domain = mono_domain_get ();
803 MonoArray *ta = exc->trace_ips;
806 len = mono_array_length (ta);
808 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
810 for (i = skip; i < len; i++) {
812 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
813 gpointer ip = mono_array_get (ta, gpointer, i);
815 ji = mono_jit_info_table_find (domain, ip);
816 g_assert (ji != NULL);
818 sf->method = mono_method_get_object (domain, ji->method, NULL);
819 sf->native_offset = (char *)ip - (char *)ji->code_start;
821 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
823 if (need_file_info) {
826 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
828 sf->filename = filename? mono_string_new (domain, filename): NULL;
834 mono_array_set (res, gpointer, i, sf);
840 /*========================= End of Function ========================*/
842 /*------------------------------------------------------------------*/
844 /* Name - mono_jit_walk_stack */
848 /*------------------------------------------------------------------*/
851 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
852 MonoDomain *domain = mono_domain_get ();
853 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
854 MonoLMF *lmf = jit_tls->lmf;
855 MonoJitInfo *ji, rji;
856 gint native_offset, il_offset;
858 MonoContext ctx, new_ctx;
860 MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
861 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
863 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
865 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL,
866 &ctx, &new_ctx, NULL, &lmf,
867 &native_offset, &managed);
870 if (ji == (gpointer)-1)
873 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
875 if (func (ji->method, native_offset, il_offset, managed, user_data))
882 /*========================= End of Function ========================*/
884 /*------------------------------------------------------------------*/
886 /* Name - ves_icall_get_frame_info */
890 /*------------------------------------------------------------------*/
893 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
894 MonoReflectionMethod **method,
895 gint32 *iloffset, gint32 *native_offset,
896 MonoString **file, gint32 *line, gint32 *column)
898 MonoDomain *domain = mono_domain_get ();
899 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
900 MonoLMF *lmf = jit_tls->lmf;
901 MonoJitInfo *ji, rji;
902 MonoContext ctx, new_ctx;
904 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
905 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
910 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL,
911 &ctx, &new_ctx, NULL, &lmf,
912 native_offset, NULL);
916 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
919 /* skip all wrappers ??*/
920 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
921 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
922 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
929 *method = mono_method_get_object (domain, ji->method, NULL);
930 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
932 if (need_file_info) {
935 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
937 *file = filename? mono_string_new (domain, filename): NULL;
946 /*========================= End of Function ========================*/
948 /*------------------------------------------------------------------*/
950 /* Name - mono_arch_handle_exception */
952 /* Function - Handle an exception raised by the JIT code. */
954 /* Parameters - ctx - Saved processor state */
955 /* obj - The exception object */
956 /* test_only - Only test if the exception is caught, */
957 /* but don't call handlers */
959 /*------------------------------------------------------------------*/
962 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
964 MonoContext *ctx = uc;
965 MonoDomain *domain = mono_domain_get ();
966 MonoJitInfo *ji, rji;
967 static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
968 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
969 MonoLMF *lmf = jit_tls->lmf;
970 GList *trace_ips = NULL;
971 MonoException *mono_ex;
973 g_assert (ctx != NULL);
974 memset(&rji, 0, sizeof(rji));
976 MonoException *ex = mono_get_exception_null_reference ();
977 ex->message = mono_string_new (domain,
978 "Object reference not set to an instance of an object");
979 obj = (MonoObject *)ex;
982 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
983 mono_ex = (MonoException*)obj;
984 mono_ex->stack_trace = NULL;
991 call_filter = arch_get_call_filter ();
993 g_assert (jit_tls->end_of_stack);
994 g_assert (jit_tls->abort_func);
997 MonoContext ctx_cp = *ctx;
998 if (mono_jit_trace_calls != NULL)
999 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
1000 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
1001 if (mono_break_on_exc)
1003 mono_unhandled_exception (obj);
1008 MonoContext new_ctx;
1011 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
1012 test_only ? &trace : NULL, &lmf, NULL, NULL);
1014 g_warning ("Exception inside function without unwind info");
1015 g_assert_not_reached ();
1018 if (ji != (gpointer)-1) {
1020 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
1023 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
1025 if (!mono_ex->stack_trace)
1026 strace = g_strdup ("");
1028 strace = mono_string_to_utf8 (mono_ex->stack_trace);
1030 tmp = g_strdup_printf ("%s%s\n", strace, trace);
1034 mono_ex->stack_trace = mono_string_new (domain, tmp);
1039 if (ji->num_clauses) {
1042 g_assert (ji->clauses);
1044 for (i = 0; i < ji->num_clauses; i++) {
1045 MonoJitExceptionInfo *ei = &ji->clauses [i];
1047 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1048 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
1050 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
1051 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
1052 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
1053 call_filter (ctx, ei->data.filter, obj)))) {
1056 mono_ex->trace_ips = glist_to_array (trace_ips);
1057 g_list_free (trace_ips);
1061 if (mono_jit_trace_calls != NULL)
1062 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1063 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1064 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1069 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
1070 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1071 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1072 if (mono_jit_trace_calls != NULL)
1073 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1074 call_filter (ctx, ei->handler_start, NULL);
1086 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1089 jit_tls->abort_func (obj);
1090 g_assert_not_reached ();
1093 mono_ex->trace_ips = glist_to_array (trace_ips);
1094 g_list_free (trace_ips);
1100 g_assert_not_reached ();
1103 /*========================= End of Function ========================*/
1106 mono_arch_ip_from_context (void *sigctx)
1108 return context_get_ip (sigctx);