20d7d78e96bb6905b73df0801a082985ead042ff
[mono.git] / mono / mini / exceptions-s390.c
1 /*------------------------------------------------------------------*/
2 /*                                                                  */
3 /* Name        - exceptions-s390.c                                  */
4 /*                                                                  */
5 /* Function    - Exception support for S/390.                       */
6 /*                                                                  */
7 /* Name        - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
8 /*                                                                  */
9 /* Date        - January, 2004                                      */
10 /*                                                                  */
11 /* Derivation  - From exceptions-x86 & exceptions-ppc               */
12 /*               Paolo Molaro (lupus@ximian.com)                    */
13 /*               Dietmar Maurer (dietmar@ximian.com)                */
14 /*                                                                  */
15 /* Copyright   - 2001 Ximian, Inc.                                  */
16 /*                                                                  */
17 /*------------------------------------------------------------------*/
18
19 /*------------------------------------------------------------------*/
20 /*                 D e f i n e s                                    */
21 /*------------------------------------------------------------------*/
22
23 #define MONO_CONTEXT_SET_IP(ctx,ip)                                     \
24         do {                                                            \
25                 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip;       \
26                 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip;        \
27         } while (0); 
28
29 #define MONO_CONTEXT_SET_BP(ctx,bp)                                     \
30         do {                                                            \
31                 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp;       \
32                 (ctx)->uc_stack.ss_sp        = (unsigned long)bp;       \
33         } while (0); 
34
35 #define MONO_CONTEXT_GET_IP(ctx) context_get_ip ((ctx))
36 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
37
38 #define S390_CALLFILTER_INTREGS         S390_MINIMAL_STACK_SIZE
39 #define S390_CALLFILTER_FLTREGS         S390_CALLFILTER_INTREGS+(16*sizeof(gulong))
40 #define S390_CALLFILTER_ACCREGS         S390_CALLFILTER_FLTREGS+(16*sizeof(gdouble))
41 #define S390_CALLFILTER_SIZE            (S390_CALLFILTER_ACCREGS+(16*sizeof(gulong)))
42
43 #define S390_THROWSTACK_ACCPRM          S390_MINIMAL_STACK_SIZE
44 #define S390_THROWSTACK_FPCPRM          S390_THROWSTACK_ACCPRM+sizeof(gpointer)
45 #define S390_THROWSTACK_RETHROW         S390_THROWSTACK_FPCPRM+sizeof(gulong)
46 #define S390_THROWSTACK_INTREGS         S390_THROWSTACK_RETHROW+sizeof(gboolean)
47 #define S390_THROWSTACK_FLTREGS         S390_THROWSTACK_INTREGS+(16*sizeof(gulong))
48 #define S390_THROWSTACK_ACCREGS         S390_THROWSTACK_FLTREGS+(16*sizeof(gdouble))
49 #define S390_THROWSTACK_SIZE            (S390_THROWSTACK_ACCREGS+(16*sizeof(gulong)))
50
51 /*========================= End of Defines =========================*/
52
53 /*------------------------------------------------------------------*/
54 /*                 I n c l u d e s                                  */
55 /*------------------------------------------------------------------*/
56
57 #include <config.h>
58 #include <glib.h>
59 #include <signal.h>
60 #include <string.h>
61 #include <ucontext.h>
62
63 #include <mono/arch/s390/s390-codegen.h>
64 #include <mono/metadata/appdomain.h>
65 #include <mono/metadata/tabledefs.h>
66 #include <mono/metadata/threads.h>
67 #include <mono/metadata/debug-helpers.h>
68 #include <mono/metadata/exception.h>
69 #include <mono/metadata/mono-debug.h>
70
71 #include "mini.h"
72 #include "mini-s390.h"
73
74 /*========================= End of Includes ========================*/
75
76 /*------------------------------------------------------------------*/
77 /*                 T y p e d e f s                                  */
78 /*------------------------------------------------------------------*/
79
80 typedef struct
81 {
82   void *prev;
83   void *unused[5];
84   void *regs[8];
85   void *return_address;
86 } MonoS390StackFrame;
87
88 /*========================= End of Typedefs ========================*/
89
90 /*------------------------------------------------------------------*/
91 /*                   P r o t o t y p e s                            */
92 /*------------------------------------------------------------------*/
93
94 gboolean mono_arch_handle_exception (void     *ctx,
95                                      gpointer obj, 
96                                      gboolean test_only);
97
98 /*========================= End of Prototypes ======================*/
99
100 /*------------------------------------------------------------------*/
101 /*                 G l o b a l   V a r i a b l e s                  */
102 /*------------------------------------------------------------------*/
103
104 /*====================== End of Global Variables ===================*/
105
106 /*------------------------------------------------------------------*/
107 /*                                                                  */
108 /* Name         - context_get_ip                                    */
109 /*                                                                  */
110 /* Function     - Extract the current instruction address from the  */
111 /*                context.                                          */
112 /*                                                                  */
113 /*------------------------------------------------------------------*/
114
115 static inline gpointer 
116 context_get_ip (MonoContext *ctx) 
117 {
118         gpointer ip;
119
120         ip = (gpointer) ((gint32) (ctx->uc_mcontext.psw.addr) & 0x7fffffff);
121         return ip;
122 }
123
124 /*========================= End of Function ========================*/
125
126 /*------------------------------------------------------------------*/
127 /*                                                                  */
128 /* Name         - mono_arch_has_unwind_info                         */
129 /*                                                                  */
130 /* Function     - Tests if a function has a DWARF exception table   */
131 /*                that is able to restore all caller saved registers*/
132 /*                                                                  */
133 /*------------------------------------------------------------------*/
134
135 gboolean
136 mono_arch_has_unwind_info (gconstpointer addr)
137 {
138         return FALSE;
139 }
140
141 /*========================= End of Function ========================*/
142
143 /*------------------------------------------------------------------*/
144 /*                                                                  */
145 /* Name         - arch_get_call_filter                              */
146 /*                                                                  */
147 /* Function     - Return a pointer to a method which calls an       */
148 /*                exception filter. We also use this function to    */
149 /*                call finally handlers (we pass NULL as @exc       */
150 /*                object in this case).                             */
151 /*                                                                  */
152 /*------------------------------------------------------------------*/
153
154 static gpointer
155 arch_get_call_filter (void)
156 {
157         static guint8 start [512];
158         static int inited = 0;
159         guint8 *code;
160         int alloc_size, pos, i;
161
162         if (inited)
163                 return start;
164
165         inited = 1;
166         /* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
167         code = start;
168
169         s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
170         s390_lr  (code, s390_r14, STK_BASE);
171         alloc_size = S390_ALIGN(S390_CALLFILTER_SIZE, S390_STACK_ALIGNMENT);
172         s390_ahi (code, STK_BASE, -alloc_size);
173         s390_st  (code, s390_r14, 0, STK_BASE, 0);
174
175         /*------------------------------------------------------*/
176         /* save general registers on stack                      */
177         /*------------------------------------------------------*/
178         s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
179
180         /*------------------------------------------------------*/
181         /* save floating point registers on stack               */
182         /*------------------------------------------------------*/
183 //      pos = S390_CALLFILTER_FLTREGS;
184 //      for (i = 0; i < 16; ++i) {
185 //              s390_std (code, i, 0, STK_BASE, pos);
186 //              pos += sizeof (gdouble);
187 //      }
188
189         /*------------------------------------------------------*/
190         /* save access registers on stack                       */
191         /*------------------------------------------------------*/
192 //      s390_stam (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
193
194         /*------------------------------------------------------*/
195         /* Get A(Context)                                       */
196         /*------------------------------------------------------*/
197         s390_lr   (code, s390_r13, s390_r2);
198
199         /*------------------------------------------------------*/
200         /* Get A(Handler Entry Point)                           */
201         /*------------------------------------------------------*/
202         s390_lr   (code, s390_r0, s390_r3);
203
204         /*------------------------------------------------------*/
205         /* Set parameter register with Exception                */
206         /*------------------------------------------------------*/
207         s390_lr   (code, s390_r2, s390_r4);
208
209         /*------------------------------------------------------*/
210         /* Load all registers with values from the context      */
211         /*------------------------------------------------------*/
212         s390_lm   (code, s390_r3, s390_r12, s390_r13, G_STRUCT_OFFSET(MonoContext, uc_mcontext.gregs[3]));
213         pos = G_STRUCT_OFFSET(MonoContext, uc_mcontext.fpregs.fprs[0]);
214         for (i = 0; i < 16; ++i) {
215                 s390_ld  (code, i, 0, s390_r13, pos);
216                 pos += sizeof(gdouble);
217         }
218         
219         /*------------------------------------------------------*/
220         /* Point at the copied stack frame and call the filter  */
221         /*------------------------------------------------------*/
222         s390_lr   (code, s390_r1, s390_r0);
223         s390_basr (code, s390_r14, s390_r1);
224
225         /*------------------------------------------------------*/
226         /* Save return value                                    */
227         /*------------------------------------------------------*/
228         s390_lr   (code, s390_r14, s390_r2);
229
230         /*------------------------------------------------------*/
231         /* Restore all the regs from the stack                  */
232         /*------------------------------------------------------*/
233         s390_lm (code, s390_r0, s390_r13, STK_BASE, S390_CALLFILTER_INTREGS);
234 //      pos = S390_CALLFILTER_FLTREGS;
235 //      for (i = 0; i < 16; ++i) {
236 //              s390_ld (code, i, 0, STK_BASE, pos);
237 //              pos += sizeof (gdouble);
238 //      }
239
240         s390_lr   (code, s390_r2, s390_r14);
241 //      s390_lam  (code, s390_a0, s390_a15, STK_BASE, S390_CALLFILTER_ACCREGS);
242         s390_ahi  (code, s390_r15, alloc_size);
243         s390_lm   (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
244         s390_br   (code, s390_r14);
245
246         g_assert ((code - start) < sizeof(start));
247         return start;
248 }
249
250 /*========================= End of Function ========================*/
251
252 /*------------------------------------------------------------------*/
253 /*                                                                  */
254 /* Name         - throw_exception.                                  */
255 /*                                                                  */
256 /* Function     - Raise an exception based on the parameters passed.*/
257 /*                                                                  */
258 /*------------------------------------------------------------------*/
259
260 static void
261 throw_exception (MonoObject *exc, unsigned long ip, unsigned long sp, 
262                  gulong *int_regs, gdouble *fp_regs, gulong *acc_regs, 
263                  guint fpc, gboolean rethrow)
264 {
265         static void (*restore_context) (MonoContext *);
266         MonoContext ctx;
267         int iReg;
268         
269         memset(&ctx, 0, sizeof(ctx));
270
271         getcontext(&ctx);
272
273         /* adjust eip so that it point into the call instruction */
274         ip -= 6;
275
276         for (iReg = 0; iReg < 16; iReg++) {
277                 ctx.uc_mcontext.gregs[iReg]         = int_regs[iReg];
278                 ctx.uc_mcontext.fpregs.fprs[iReg].d = fp_regs[iReg];
279                 ctx.uc_mcontext.aregs[iReg]         = acc_regs[iReg];
280         }
281
282         ctx.uc_mcontext.fpregs.fpc = fpc;
283
284         MONO_CONTEXT_SET_BP (&ctx, sp);
285         MONO_CONTEXT_SET_IP (&ctx, ip);
286         
287         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
288                 MonoException *mono_ex = (MonoException*)exc;
289                 if (!rethrow)
290                         mono_ex->stack_trace = NULL;
291         }
292         mono_arch_handle_exception (&ctx, exc, FALSE);
293         setcontext(&ctx);
294
295         g_assert_not_reached ();
296 }
297
298 /*========================= End of Function ========================*/
299
300 /*------------------------------------------------------------------*/
301 /*                                                                  */
302 /* Name         - get_throw_exception_generic                       */
303 /*                                                                  */
304 /* Function     - Return a function pointer which can be used to    */
305 /*                raise exceptions. The returned function has the   */
306 /*                following signature:                              */
307 /*                void (*func) (MonoException *exc); or,            */
308 /*                void (*func) (char *exc_name);                    */
309 /*                                                                  */
310 /*------------------------------------------------------------------*/
311
312 static gpointer 
313 get_throw_exception_generic (guint8 *start, int size, 
314                              int by_name, gboolean rethrow)
315 {
316         guint8 *code;
317         int alloc_size, pos, i, offset;
318
319         code = start;
320
321         s390_stm (code, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
322         alloc_size = S390_ALIGN(S390_THROWSTACK_SIZE, S390_STACK_ALIGNMENT);
323         s390_lr   (code, s390_r14, STK_BASE);
324         s390_ahi  (code, STK_BASE, -alloc_size);
325         s390_st   (code, s390_r14, 0, STK_BASE, 0);
326         if (by_name) {
327                 s390_lr   (code, s390_r4, s390_r2);
328                 s390_bras (code, s390_r13, 6);
329                 s390_word (code, mono_defaults.corlib);
330                 s390_word (code, "System");
331                 s390_l    (code, s390_r2, 0, s390_r13, 0);
332                 s390_l    (code, s390_r3, 0, s390_r13, 4);
333                 offset = (guint32) S390_RELATIVE(mono_exception_from_name, code);
334                 s390_brasl(code, s390_r14, offset);
335         }
336         /*------------------------------------------------------*/
337         /* save the general registers on the stack              */
338         /*------------------------------------------------------*/
339         s390_stm (code, s390_r0, s390_r13, STK_BASE, S390_THROWSTACK_INTREGS);
340
341         s390_lr  (code, s390_r1, STK_BASE);
342         s390_ahi (code, s390_r1, alloc_size);
343         /*------------------------------------------------------*/
344         /* save the return address in the parameter register    */
345         /*------------------------------------------------------*/
346         s390_l   (code, s390_r3, 0, s390_r1, S390_RET_ADDR_OFFSET);
347
348         /*------------------------------------------------------*/
349         /* save the floating point registers                    */
350         /*------------------------------------------------------*/
351         pos = S390_THROWSTACK_FLTREGS;
352         for (i = 0; i < 16; ++i) {
353                 s390_std (code, i, 0,STK_BASE, pos);
354                 pos += sizeof (gdouble);
355         }
356         /*------------------------------------------------------*/
357         /* save the access registers                            */
358         /*------------------------------------------------------*/
359         s390_stam (code, s390_r0, s390_r15, STK_BASE, S390_THROWSTACK_ACCREGS);
360
361         /*------------------------------------------------------*/
362         /* call throw_exception (exc, ip, sp, gr, fr, ar)       */
363         /* exc is already in place in r2                        */
364         /*------------------------------------------------------*/
365         s390_lr   (code, s390_r4, s390_r1);        /* caller sp */
366         /*------------------------------------------------------*/
367         /* pointer to the saved int regs                        */
368         /*------------------------------------------------------*/
369         s390_la   (code, s390_r5, 0, STK_BASE, S390_THROWSTACK_INTREGS);
370         s390_la   (code, s390_r6, 0, STK_BASE, S390_THROWSTACK_FLTREGS);
371         s390_la   (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCREGS);
372         s390_st   (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_ACCPRM);
373         s390_stfpc(code, STK_BASE, S390_THROWSTACK_FPCPRM);
374         s390_lhi  (code, s390_r7, rethrow);
375         s390_st   (code, s390_r7, 0, STK_BASE, S390_THROWSTACK_RETHROW);
376         offset = (guint32) S390_RELATIVE(throw_exception, code);
377         s390_brasl(code, s390_r14, offset);
378         /* we should never reach this breakpoint */
379         s390_break (code);
380         g_assert ((code - start) < size);
381         return start;
382 }
383
384 /*========================= End of Function ========================*/
385
386 /*------------------------------------------------------------------*/
387 /*                                                                  */
388 /* Name         - arch_get_throw_exception                          */
389 /*                                                                  */
390 /* Function     - Return a function pointer which can be used to    */
391 /*                raise exceptions. The returned function has the   */
392 /*                following signature:                              */
393 /*                void (*func) (MonoException *exc);                */
394 /*                                                                  */
395 /*------------------------------------------------------------------*/
396
397 gpointer 
398 mono_arch_get_throw_exception (void)
399 {
400         static guint8 start [384];
401         static int inited = 0;
402
403         if (inited)
404                 return start;
405         get_throw_exception_generic (start, sizeof (start), FALSE, FALSE);
406         inited = 1;
407         return start;
408 }
409
410 /*========================= End of Function ========================*/
411
412 /*------------------------------------------------------------------*/
413 /*                                                                  */
414 /* Name         - arch_get_rethrow_exception                        */
415 /*                                                                  */
416 /* Function     - Return a function pointer which can be used to    */
417 /*                raise exceptions. The returned function has the   */
418 /*                following signature:                              */
419 /*                void (*func) (MonoException *exc);                */
420 /*                                                                  */
421 /*------------------------------------------------------------------*/
422
423 gpointer 
424 mono_arch_get_rethrow_exception (void)
425 {
426         static guint8 start [384];
427         static int inited = 0;
428
429         if (inited)
430                 return start;
431         get_throw_exception_generic (start, sizeof (start), FALSE, TRUE);
432         inited = 1;
433         return start;
434 }
435
436 /*========================= End of Function ========================*/
437
438 /*------------------------------------------------------------------*/
439 /*                                                                  */
440 /* Name         - arch_get_throw_exception_by_name                  */
441 /*                                                                  */
442 /* Function     - Return a function pointer which can be used to    */
443 /*                raise corlib exceptions. The return function has  */
444 /*                the following signature:                          */
445 /*                void (*func) (char *exc_name);                    */
446 /*                                                                  */
447 /*------------------------------------------------------------------*/
448
449 gpointer 
450 mono_arch_get_throw_exception_by_name (void)
451 {
452         static guint8 start [384];
453         static int inited = 0;
454
455         if (inited)
456                 return start;
457         get_throw_exception_generic (start, sizeof (start), TRUE, FALSE);
458         inited = 1;
459         return start;
460 }       
461
462 /*========================= End of Function ========================*/
463
464 /*------------------------------------------------------------------*/
465 /*                                                                  */
466 /* Name         - glist_to_array                                    */
467 /*                                                                  */
468 /* Function     - Convert a list to a mono array.                   */
469 /*                                                                  */
470 /*------------------------------------------------------------------*/
471
472 static MonoArray *
473 glist_to_array (GList *list) 
474 {
475         MonoDomain *domain = mono_domain_get ();
476         MonoArray *res;
477         int len, i;
478
479         if (!list)
480                 return NULL;
481
482         len = g_list_length (list);
483         res = mono_array_new (domain, mono_defaults.int_class, len);
484
485         for (i = 0; list; list = list->next, i++)
486                 mono_array_set (res, gpointer, i, list->data);
487
488         return res;
489 }
490
491 /*========================= End of Function ========================*/
492
493 /*------------------------------------------------------------------*/
494 /*                                                                  */
495 /* Name         - mono_arch_find_jit_info                           */
496 /*                                                                  */
497 /* Function     - This function is used to gather informatoin from  */
498 /*                @ctx. It returns the MonoJitInfo of the corres-   */
499 /*                ponding function, unwinds one stack frame and     */
500 /*                stores the resulting context into @new_ctx. It    */
501 /*                also stores a string describing the stack location*/
502 /*                into @trace (if not NULL), and modifies the @lmf  */
503 /*                if necessary. @native_offset returns the IP off-  */
504 /*                set from the start of the function or -1 if that  */
505 /*                informatoin is not available.                     */
506 /*                                                                  */
507 /*------------------------------------------------------------------*/
508
509 MonoJitInfo *
510 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, 
511                          MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx, 
512                          MonoContext *new_ctx, char **trace, MonoLMF **lmf, 
513                          int *native_offset, gboolean *managed)
514 {
515         MonoJitInfo *ji;
516         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
517         unsigned long *ptr;
518         char *p;
519         MonoS390StackFrame *sframe;
520
521         if (prev_ji && 
522             (ip > prev_ji->code_start && 
523             ((guint8 *) ip < ((guint8 *) prev_ji->code_start) + prev_ji->code_size)))
524                 ji = prev_ji;
525         else
526                 ji = mono_jit_info_table_find (domain, ip);
527
528         if (trace)
529                 *trace = NULL;
530
531         if (native_offset)
532                 *native_offset = -1;
533
534         if (managed)
535                 *managed = FALSE;
536
537         if (ji != NULL) {
538                 char *source_location, *tmpaddr, *fname;
539                 gint32 address, iloffset;
540                 int offset;
541
542                 *new_ctx = *ctx;
543
544                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
545                         /* remove any unused lmf */
546                         *lmf = (*lmf)->previous_lmf;
547                 }
548
549                 address = (char *)ip - (char *)ji->code_start;
550
551                 if (native_offset)
552                         *native_offset = address;
553
554                 if (managed)
555                         if (!ji->method->wrapper_type)
556                                 *managed = TRUE;
557
558                 if (trace) {
559                         source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
560                         iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
561
562                         if (iloffset < 0)
563                                 tmpaddr = g_strdup_printf ("<0x%08x>", address);
564                         else
565                                 tmpaddr = g_strdup_printf ("[0x%08x]", iloffset);
566                 
567                         fname = mono_method_full_name (ji->method, TRUE);
568
569                         if (source_location)
570                                 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
571                         else
572                                 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
573
574                         g_free (fname);
575                         g_free (source_location);
576                         g_free (tmpaddr);
577                 }
578                 sframe = (MonoS390StackFrame *) MONO_CONTEXT_GET_BP (ctx);
579                 MONO_CONTEXT_SET_BP (new_ctx, sframe->prev);
580                 sframe = (MonoS390StackFrame *) sframe->prev;
581                 MONO_CONTEXT_SET_IP (new_ctx, sframe->return_address);
582                 memcpy (&new_ctx->uc_mcontext.gregs[6], sframe->regs, (8*sizeof(gint32)));
583                 *res = *ji;
584                 return res;
585         } else if (*lmf) {
586                 
587                 *new_ctx = *ctx;
588
589                 if (!(*lmf)->method)
590                         return (gpointer)-1;
591
592                 if (trace)
593                         *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
594                 
595                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
596                         *res = *ji;
597                 } else {
598                         memset (res, 0, sizeof (MonoJitInfo));
599                         res->method = (*lmf)->method;
600                 }
601
602                 memcpy(new_ctx->uc_mcontext.gregs, (*lmf)->gregs, sizeof((*lmf)->gregs));
603                 memcpy(new_ctx->uc_mcontext.fpregs.fprs, (*lmf)->fregs, sizeof((*lmf)->fregs));
604
605                 MONO_CONTEXT_SET_BP (new_ctx, (*lmf)->ebp);
606                 MONO_CONTEXT_SET_IP (new_ctx, (*lmf)->eip);
607                 *lmf = (*lmf)->previous_lmf;
608
609                 return res;
610                 
611         }
612
613         return NULL;
614 }
615
616 /*========================= End of Function ========================*/
617
618 /*------------------------------------------------------------------*/
619 /*                                                                  */
620 /* Name         - ves_icall_get_trace                               */
621 /*                                                                  */
622 /* Function     -                                                   */
623 /*                                                                  */
624 /*------------------------------------------------------------------*/
625
626 MonoArray *
627 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
628 {
629         MonoDomain *domain = mono_domain_get ();
630         MonoArray *res;
631         MonoArray *ta = exc->trace_ips;
632         int i, len;
633         
634         if (ta == NULL) {
635                 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
636         }
637
638         len = mono_array_length (ta);
639
640         res = mono_array_new (domain, mono_defaults.stack_frame_class, 
641                               len > skip ? len - skip : 0);
642
643         for (i = skip; i < len; i++) {
644                 MonoJitInfo *ji;
645                 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
646                 gpointer ip = mono_array_get (ta, gpointer, i);
647
648                 ji = mono_jit_info_table_find (domain, ip);
649                 if (ji == NULL) {
650                         mono_array_set (res, gpointer, i, sf);
651                         continue;
652                 }
653
654                 sf->method = mono_method_get_object (domain, ji->method, NULL);
655                 sf->native_offset = (char *)ip - (char *)ji->code_start;
656
657                 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
658
659                 if (need_file_info) {
660                         gchar *filename;
661                         
662                         filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
663
664                         sf->filename = filename? mono_string_new (domain, filename): NULL;
665                         sf->column = 0;
666
667                         g_free (filename);
668                 }
669
670                 mono_array_set (res, gpointer, i, sf);
671         }
672
673         return res;
674 }
675
676 /*========================= End of Function ========================*/
677
678 /*------------------------------------------------------------------*/
679 /*                                                                  */
680 /* Name         - mono_jit_walk_stack                               */
681 /*                                                                  */
682 /* Function     -                                                   */
683 /*                                                                  */
684 /*------------------------------------------------------------------*/
685
686 void
687 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
688         MonoDomain *domain = mono_domain_get ();
689         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
690         MonoLMF *lmf = jit_tls->lmf;
691         MonoJitInfo *ji, rji;
692         gint native_offset, il_offset;
693         gboolean managed;
694         MonoContext ctx, new_ctx;
695
696         MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
697         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
698
699         while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
700                 
701                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, 
702                                               &ctx, &new_ctx, NULL, &lmf, 
703                                               &native_offset, &managed);
704                 g_assert (ji);
705
706                 if (ji == (gpointer)-1)
707                         return;
708
709                 il_offset = do_il_offset ? mono_debug_il_offset_from_address (ji->method, native_offset, domain): -1;
710
711                 if (func (ji->method, native_offset, il_offset, managed, user_data))
712                         return;
713                 
714                 ctx = new_ctx;
715         }
716 }
717
718 /*========================= End of Function ========================*/
719
720 /*------------------------------------------------------------------*/
721 /*                                                                  */
722 /* Name         - ves_icall_get_frame_info                          */
723 /*                                                                  */
724 /* Function     -                                                   */
725 /*                                                                  */
726 /*------------------------------------------------------------------*/
727
728 MonoBoolean
729 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
730                           MonoReflectionMethod **method, 
731                           gint32 *iloffset, gint32 *native_offset,
732                           MonoString **file, gint32 *line, gint32 *column)
733 {
734         MonoDomain *domain = mono_domain_get ();
735         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
736         MonoLMF *lmf = jit_tls->lmf;
737         MonoJitInfo *ji, rji;
738         MonoContext ctx, new_ctx;
739
740         MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
741         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
742
743         skip++;
744
745         do {
746                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, 
747                                               &ctx, &new_ctx, NULL, &lmf, 
748                                               native_offset, NULL);
749
750                 ctx = new_ctx;
751                 
752                 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
753                         return FALSE;
754
755                 /* skip all wrappers ??*/
756                 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
757                     ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
758                     ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
759                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
760                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
761                         continue;
762
763                 skip--;
764
765         } while (skip >= 0);
766
767         *method = mono_method_get_object (domain, ji->method, NULL);
768         *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
769
770         if (need_file_info) {
771                 gchar *filename;
772
773                 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
774
775                 *file = filename? mono_string_new (domain, filename): NULL;
776                 *column = 0;
777
778                 g_free (filename);
779         }
780
781         return TRUE;
782 }
783
784 /*========================= End of Function ========================*/
785
786 /*------------------------------------------------------------------*/
787 /*                                                                  */
788 /* Name         - mono_arch_handle_exception                        */
789 /*                                                                  */
790 /* Function     - Handle an exception raised by the JIT code.       */
791 /*                                                                  */
792 /* Parameters   - ctx       - Saved processor state                 */
793 /*                obj       - The exception object                  */
794 /*                test_only - Only test if the exception is caught, */
795 /*                            but don't call handlers               */
796 /*                                                                  */
797 /*------------------------------------------------------------------*/
798
799 gboolean
800 mono_arch_handle_exception (void *uc, gpointer obj, gboolean test_only)
801 {
802         MonoContext     *ctx = uc;
803         MonoDomain      *domain = mono_domain_get ();
804         MonoJitInfo     *ji, rji;
805         static int      (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
806         MonoJitTlsData  *jit_tls = TlsGetValue (mono_jit_tls_id);
807         MonoLMF         *lmf = jit_tls->lmf;            
808         GList           *trace_ips = NULL;
809         GString         *traceStr  = NULL;
810         MonoException   *mono_ex;
811         MonoString      *initialStackTrace = NULL;
812         int             frameCount = 0;
813
814         g_assert (ctx != NULL);
815         memset(&rji, 0, sizeof(rji));
816         if (!obj) {
817                 MonoException *ex = mono_get_exception_null_reference ();
818                 ex->message = mono_string_new (domain, 
819                         "Object reference not set to an instance of an object");
820                 obj = (MonoObject *)ex;
821         } 
822
823         if (mono_object_isinst (obj, mono_defaults.exception_class)) {
824                 mono_ex = (MonoException*)obj;
825                 mono_ex->stack_trace = NULL;
826         } else {
827                 mono_ex = NULL;
828         }
829
830
831         if (!call_filter)
832                 call_filter = arch_get_call_filter ();
833
834         g_assert (jit_tls->end_of_stack);
835         g_assert (jit_tls->abort_func);
836
837         if (!test_only) {
838                 MonoContext ctx_cp = *ctx;
839                 if (mono_jit_trace_calls != NULL)
840                         g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
841                 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
842                         if (mono_break_on_exc)
843                                 G_BREAKPOINT ();
844                         mono_unhandled_exception (obj);
845                 }
846         }
847
848         memset (&rji, 0, sizeof(rji));
849
850         while (1) {
851                 MonoContext new_ctx;
852                 char *trace = NULL;
853                 gboolean needTrace = FALSE;
854
855                 if (test_only && (frameCount < 1000)) {
856                         needTrace = TRUE;
857                         if (!traceStr)
858                                 traceStr = g_string_new ("");
859                 }       
860                 
861                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx, 
862                                               test_only ? &trace : NULL, &lmf, NULL, NULL);
863                 if (!ji) {
864                         g_warning ("Exception inside function without unwind info");
865                         g_assert_not_reached ();
866                 }
867
868                 if (ji != (gpointer)-1) {
869                         frameCount++;
870
871                         if ((test_only) && 
872                             (ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE) && 
873                             (mono_ex)) {
874                                 if (!initialStackTrace && (frameCount < 1000)) {
875                                         trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
876                                         g_string_append   (traceStr, trace);
877                                         g_string_append_c (traceStr, '\n');
878                                 }
879                         }               
880                         
881                         if (ji->num_clauses) {
882                                 int i;
883                                 
884                                 g_assert (ji->clauses);
885                         
886                                 for (i = 0; i < ji->num_clauses; i++) {
887                                         MonoJitExceptionInfo *ei = &ji->clauses [i];
888                                         gboolean filtered = FALSE;
889
890                                         if (ei->try_start < MONO_CONTEXT_GET_IP (ctx) && 
891                                             MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) { 
892                                                 /* catch block */
893                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) ||
894                                                     (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
895                                                         g_assert (ji->exvar_offset);
896                                                         *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
897                                                         if (!initialStackTrace &&
898                                                             traceStr) {
899                                                                 mono_ex->stack_trace =  mono_string_new (domain, traceStr->str);
900                                                         }
901                                                 }
902
903                                                 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) 
904                                                       filtered = call_filter (ctx, ei->data.filter, obj);
905
906                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) &&
907                                                     (mono_object_isinst (obj, ei->data.catch_class)) || 
908                                                     (filtered)) {
909                                                         if (test_only) {
910                                                                 if (mono_ex) {
911                                                                         trace_ips = g_list_reverse (trace_ips);
912                                                                         mono_ex->trace_ips = glist_to_array (trace_ips);
913                                                                 }
914                                                                 g_list_free (trace_ips);
915                                                                 g_free (trace);
916                                                                 if (traceStr)
917                                                                         g_string_free (traceStr, TRUE);
918
919                                                                 return TRUE;
920                                                         }
921
922                                                         if (mono_jit_trace_calls != NULL)
923                                                                 g_print ("EXCEPTION: catch found at clause %d of %s - caught at %p with sp %p\n", 
924                                                                          i, mono_method_full_name (ji->method, TRUE),
925                                                                          ei->handler_start,
926                                                                          MONO_CONTEXT_GET_BP(ctx));
927                                                         MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
928                                                         jit_tls->lmf = lmf;
929                                                         g_free (trace);
930                                                         if (traceStr)
931                                                                 g_string_free (traceStr, TRUE);
932                                                         return FALSE;
933                                                 }
934
935                                                 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
936                                                     MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
937                                                     (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
938                                                         if (mono_jit_trace_calls != NULL)
939                                                                 g_print ("EXCEPTION: finally clause %d of %s handled at: %p using sp: %p\n", 
940                                                                 i, mono_method_full_name (ji->method, TRUE),
941                                                                 ei->handler_start,
942                                                                 MONO_CONTEXT_GET_BP(ctx));
943                                                         call_filter (ctx, ei->handler_start, NULL);
944                                                 }
945                                                 
946                                         }
947                                 }
948                         }
949                 }
950
951                 g_free (trace);
952                         
953                 *ctx = new_ctx;
954
955                 if ((ji == (gpointer)-1) || 
956                     (MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack)) {
957                         if (!test_only) {
958                                 jit_tls->lmf = lmf;
959                                 jit_tls->abort_func (obj);
960                                 g_assert_not_reached ();
961                         } else {
962                                 if (mono_ex) {
963                                         trace_ips = g_list_reverse (trace_ips);
964                                         mono_ex->trace_ips = glist_to_array (trace_ips);
965                                 }
966                                 g_list_free (trace_ips);
967                                 if (traceStr)
968                                         g_string_free (traceStr, TRUE);
969                                 return FALSE;
970                         }
971                 }
972         }
973
974         g_assert_not_reached ();
975 }
976
977 /*========================= End of Function ========================*/
978
979 /*------------------------------------------------------------------*/
980 /*                                                                  */
981 /* Name         - mono_arch_ip_from_context                         */
982 /*                                                                  */
983 /* Function     - Return the instruction pointer from the context.  */
984 /*                                                                  */
985 /* Parameters   - sigctx    - Saved processor state                 */
986 /*                                                                  */
987 /*------------------------------------------------------------------*/
988
989 gpointer
990 mono_arch_ip_from_context (void *sigctx)
991 {
992         return context_get_ip (sigctx);
993 }
994