Sat Dec 13 17:49:10 CET 2003 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / exceptions-ppc.c
1 /*
2  * exception.c: exception support
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc.
9  */
10
11 #include <config.h>
12 #include <glib.h>
13 #include <signal.h>
14 #include <string.h>
15
16 #include <mono/arch/ppc/ppc-codegen.h>
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/threads.h>
20 #include <mono/metadata/debug-helpers.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/mono-debug.h>
23
24 #include "mini.h"
25 #include "mini-ppc.h"
26
27 typedef struct sigcontext MonoContext;
28
29 /*
30
31 struct sigcontext {
32     int      sc_onstack;     // sigstack state to restore 
33     int      sc_mask;        // signal mask to restore 
34     int      sc_ir;          // pc 
35     int      sc_psw;         // processor status word 
36     int      sc_sp;          // stack pointer if sc_regs == NULL 
37     void    *sc_regs;        // (kernel private) saved state 
38 };
39
40 struct ucontext {
41         int             uc_onstack;
42         sigset_t        uc_sigmask;     // signal mask used by this context 
43         stack_t         uc_stack;       // stack used by this context 
44         struct ucontext *uc_link;       // pointer to resuming context 
45         size_t          uc_mcsize;      // size of the machine context passed in 
46         mcontext_t      uc_mcontext;    // machine specific context 
47 };
48
49 typedef struct ppc_exception_state {
50         unsigned long dar;      // Fault registers for coredump 
51         unsigned long dsisr;
52         unsigned long exception;// number of powerpc exception taken 
53         unsigned long pad0;     // align to 16 bytes 
54
55         unsigned long pad1[4];  // space in PCB "just in case" 
56 } ppc_exception_state_t;
57
58 typedef struct ppc_vector_state {
59         unsigned long   save_vr[32][4];
60         unsigned long   save_vscr[4];
61         unsigned int    save_pad5[4];
62         unsigned int    save_vrvalid;                   // VRs that have been saved 
63         unsigned int    save_pad6[7];
64 } ppc_vector_state_t;
65
66 typedef struct ppc_float_state {
67         double  fpregs[32];
68
69         unsigned int fpscr_pad; // fpscr is 64 bits, 32 bits of rubbish 
70         unsigned int fpscr;     // floating point status register 
71 } ppc_float_state_t;
72
73 typedef struct ppc_thread_state {
74         unsigned int srr0;      // Instruction address register (PC) 
75         unsigned int srr1;      // Machine state register (supervisor) 
76         unsigned int r0;
77         unsigned int r1;
78         unsigned int r2;
79         ... 
80         unsigned int r31;
81         unsigned int cr;        // Condition register 
82         unsigned int xer;       // User's integer exception register 
83         unsigned int lr;        // Link register 
84         unsigned int ctr;       // Count register 
85         unsigned int mq;        // MQ register (601 only) 
86
87         unsigned int vrsave;    // Vector Save Register 
88 } ppc_thread_state_t;
89
90 struct mcontext {
91         ppc_exception_state_t   es;
92         ppc_thread_state_t      ss;
93         ppc_float_state_t       fs;
94         ppc_vector_state_t      vs;
95 };
96
97 typedef struct mcontext  * mcontext_t;
98
99 */
100
101 #ifdef __APPLE__
102
103 /* we have the stack pointer, not the base pointer in sigcontext */
104 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (int)ip; } while (0); 
105 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (int)bp; } while (0); 
106
107 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
108 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_sp))
109
110 #else /* sigcontext is different on linux/ppc. See /usr/include/asm/ptrace.h. */
111
112 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->regs->nip = (unsigned long)ip; } while (0); 
113 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs->gpr[1] = (unsigned long)bp; } while (0); 
114
115 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->regs->nip))
116 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs->gpr[1]))
117
118 #endif
119
120 /* disbale this for now */
121 #undef MONO_USE_EXC_TABLES
122
123 #ifdef MONO_USE_EXC_TABLES
124
125 /*************************************/
126 /*    STACK UNWINDING STUFF          */
127 /*************************************/
128
129 /* These definitions are from unwind-dw2.c in glibc 2.2.5 */
130
131 /* For x86 */
132 #define DWARF_FRAME_REGISTERS 17
133
134 typedef struct frame_state
135 {
136   void *cfa;
137   void *eh_ptr;
138   long cfa_offset;
139   long args_size;
140   long reg_or_offset[DWARF_FRAME_REGISTERS+1];
141   unsigned short cfa_reg;
142   unsigned short retaddr_column;
143   char saved[DWARF_FRAME_REGISTERS+1];
144 } frame_state;
145
146 #if 0
147
148 static long
149 get_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum)
150 {
151         switch (dwarf_regnum) {
152         case X86_EAX:
153                 return ctx->SC_EAX;
154         case X86_EBX:
155                 return ctx->SC_EBX;
156         case X86_ECX:
157                 return ctx->SC_ECX;
158         case X86_EDX:
159                 return ctx->SC_EDX;
160         case X86_ESI:
161                 return ctx->SC_ESI;
162         case X86_EDI:
163                 return ctx->SC_EDI;
164         case X86_EBP:
165                 return ctx->SC_EBP;
166         case X86_ESP:
167                 return ctx->SC_ESP;
168         default:
169                 g_assert_not_reached ();
170         }
171
172         return 0;
173 }
174
175 static void
176 set_sigcontext_reg (struct sigcontext *ctx, int dwarf_regnum, long value)
177 {
178         switch (dwarf_regnum) {
179         case X86_EAX:
180                 ctx->SC_EAX = value;
181                 break;
182         case X86_EBX:
183                 ctx->SC_EBX = value;
184                 break;
185         case X86_ECX:
186                 ctx->SC_ECX = value;
187                 break;
188         case X86_EDX:
189                 ctx->SC_EDX = value;
190                 break;
191         case X86_ESI:
192                 ctx->SC_ESI = value;
193                 break;
194         case X86_EDI:
195                 ctx->SC_EDI = value;
196                 break;
197         case X86_EBP:
198                 ctx->SC_EBP = value;
199                 break;
200         case X86_ESP:
201                 ctx->SC_ESP = value;
202                 break;
203         case 8:
204                 ctx->SC_EIP = value;
205                 break;
206         default:
207                 g_assert_not_reached ();
208         }
209 }
210
211 typedef struct frame_state * (*framesf) (void *, struct frame_state *);
212
213 static framesf frame_state_for = NULL;
214
215 static gboolean inited = FALSE;
216
217 typedef char ** (*get_backtrace_symbols_type) (void *__const *__array, int __size);
218
219 static get_backtrace_symbols_type get_backtrace_symbols = NULL;
220
221 static void
222 init_frame_state_for (void)
223 {
224         GModule *module;
225
226         /*
227          * There are two versions of __frame_state_for: one in libgcc.a and the
228          * other in glibc.so. We need the version from glibc.
229          * For more info, see this:
230          * http://gcc.gnu.org/ml/gcc/2002-08/msg00192.html
231          */
232         if ((module = g_module_open ("libc.so.6", G_MODULE_BIND_LAZY))) {
233         
234                 if (!g_module_symbol (module, "__frame_state_for", (gpointer*)&frame_state_for))
235                         frame_state_for = NULL;
236
237                 if (!g_module_symbol (module, "backtrace_symbols", (gpointer*)&get_backtrace_symbols)) {
238                         get_backtrace_symbols = NULL;
239                         frame_state_for = NULL;
240                 }
241
242                 g_module_close (module);
243         }
244
245         inited = TRUE;
246 }
247
248 #endif
249
250 gboolean  mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
251
252 /* mono_arch_has_unwind_info:
253  *
254  * Tests if a function has an DWARF exception table able to restore
255  * all caller saved registers. 
256  */
257 gboolean
258 mono_arch_has_unwind_info (MonoMethod *method)
259 {
260 #if 0
261         struct frame_state state_in;
262         struct frame_state *res;
263
264         if (!inited) 
265                 init_frame_state_for ();
266         
267         if (!frame_state_for)
268                 return FALSE;
269
270         g_assert (method->addr);
271
272         memset (&state_in, 0, sizeof (state_in));
273
274         /* offset 10 is just a guess, but it works for all methods tested */
275         if ((res = frame_state_for ((char *)method->addr + 10, &state_in))) {
276
277                 if (res->saved [X86_EBX] != 1 ||
278                     res->saved [X86_EDI] != 1 ||
279                     res->saved [X86_EBP] != 1 ||
280                     res->saved [X86_ESI] != 1) {
281                         return FALSE;
282                 }
283                 return TRUE;
284         } else {
285                 return FALSE;
286         }
287 #else
288         return FALSE;
289 #endif
290 }
291
292 struct stack_frame
293 {
294   void *next;
295   void *return_address;
296 };
297
298 static MonoJitInfo *
299 ppc_unwind_native_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, struct sigcontext *ctx, 
300                          struct sigcontext *new_ctx, MonoLMF *lmf, char **trace)
301 {
302 #if 0
303         struct stack_frame *frame;
304         gpointer max_stack;
305         MonoJitInfo *ji;
306         struct frame_state state_in;
307         struct frame_state *res;
308
309         if (trace)
310                 *trace = NULL;
311
312         if (!inited) 
313                 init_frame_state_for ();
314
315         if (!frame_state_for)
316                 return FALSE;
317
318         frame = MONO_CONTEXT_GET_BP (ctx);
319
320         max_stack = lmf && lmf->method ? lmf : jit_tls->end_of_stack;
321
322         *new_ctx = *ctx;
323
324         memset (&state_in, 0, sizeof (state_in));
325
326         while ((gpointer)frame->next < (gpointer)max_stack) {
327                 gpointer ip, addr = frame->return_address;
328                 void *cfa;
329                 char *tmp, **symbols;
330
331                 if (trace) {
332                         ip = MONO_CONTEXT_GET_IP (new_ctx);
333                         symbols = get_backtrace_symbols (&ip, 1);
334                         if (*trace)
335                                 tmp = g_strdup_printf ("%s\nin (unmanaged) %s", *trace, symbols [0]);
336                         else
337                                 tmp = g_strdup_printf ("in (unmanaged) %s", symbols [0]);
338
339                         free (symbols);
340                         g_free (*trace);
341                         *trace = tmp;
342                 }
343
344                 if ((res = frame_state_for (addr, &state_in))) {        
345                         int i;
346
347                         cfa = (gint8*) (get_sigcontext_reg (new_ctx, res->cfa_reg) + res->cfa_offset);
348                         frame = (struct stack_frame *)((gint8*)cfa - 8);
349                         for (i = 0; i < DWARF_FRAME_REGISTERS + 1; i++) {
350                                 int how = res->saved[i];
351                                 long val;
352                                 g_assert ((how == 0) || (how == 1));
353                         
354                                 if (how == 1) {
355                                         val = * (long*) ((gint8*)cfa + res->reg_or_offset[i]);
356                                         set_sigcontext_reg (new_ctx, i, val);
357                                 }
358                         }
359                         new_ctx->SC_ESP = (long)cfa;
360
361                         if (res->saved [X86_EBX] == 1 &&
362                             res->saved [X86_EDI] == 1 &&
363                             res->saved [X86_EBP] == 1 &&
364                             res->saved [X86_ESI] == 1 &&
365                             (ji = mono_jit_info_table_find (domain, frame->return_address))) {
366                                 //printf ("FRAME CFA %s\n", mono_method_full_name (ji->method, TRUE));
367                                 return ji;
368                         }
369
370                 } else {
371                         //printf ("FRAME %p %p %p\n", frame, MONO_CONTEXT_GET_IP (new_ctx), mono_jit_info_table_find (domain, MONO_CONTEXT_GET_IP (new_ctx)));
372
373                         MONO_CONTEXT_SET_IP (new_ctx, frame->return_address);
374                         frame = frame->next;
375                         MONO_CONTEXT_SET_BP (new_ctx, frame);
376
377                         /* stop if !frame or when we detect an unexpected managed frame */
378                         if (!frame || mono_jit_info_table_find (domain, frame->return_address)) {
379                                 if (trace) {
380                                         g_free (*trace);
381                                         *trace = NULL;
382                                 }
383                                 return NULL;
384                         }
385                 }
386         }
387
388         //if (!lmf)
389         //g_assert_not_reached ();
390
391         if (trace) {
392                 g_free (*trace);
393                 *trace = NULL;
394         }
395 #endif
396         return NULL;
397 }
398
399 #endif
400
401 /*
402  * arch_get_restore_context:
403  *
404  * Returns a pointer to a method which restores a previously saved sigcontext.
405  */
406 static gpointer
407 arch_get_restore_context (void)
408 {
409         static guint8 *start = NULL;
410         guint8 *code;
411
412         if (start)
413                 return start;
414
415 #if 0
416         /* restore_contect (struct sigcontext *ctx) */
417         /* we do not restore X86_EAX, X86_EDX */
418
419         start = code = g_malloc (1024);
420         
421         /* load ctx */
422         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
423
424         /* get return address, stored in EDX */
425         x86_mov_reg_membase (code, X86_EDX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EIP), 4);
426         /* restore EBX */
427         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
428         /* restore EDI */
429         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
430         /* restore ESI */
431         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
432         /* restore ESP */
433         x86_mov_reg_membase (code, X86_ESP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
434         /* restore EBP */
435         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
436
437         /* jump to the saved IP */
438         x86_jump_reg (code, X86_EDX);
439
440 #endif
441         return start;
442 }
443
444 /*
445  * arch_get_call_filter:
446  *
447  * Returns a pointer to a method which calls an exception filter. We
448  * also use this function to call finally handlers (we pass NULL as 
449  * @exc object in this case).
450  */
451 static gpointer
452 arch_get_call_filter (void)
453 {
454         static guint8 start [64];
455         static int inited = 0;
456         guint8 *code;
457
458         if (inited)
459                 return start;
460
461         inited = 1;
462         /* call_filter (struct sigcontext *ctx, unsigned long eip, gpointer exc) */
463         code = start;
464
465 #if 0
466         x86_push_reg (code, X86_EBP);
467         x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
468         x86_push_reg (code, X86_EBX);
469         x86_push_reg (code, X86_EDI);
470         x86_push_reg (code, X86_ESI);
471
472         /* load ctx */
473         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
474         /* load eip */
475         x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
476         /* save EBP */
477         x86_push_reg (code, X86_EBP);
478         /* push exc */
479         x86_push_membase (code, X86_EBP, 16);
480         /* set new EBP */
481         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
482         /* restore registers used by global register allocation (EBX & ESI) */
483         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
484         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
485         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
486         /* save the ESP - this is used by endfinally */
487         x86_mov_membase_reg (code, X86_EBP, mono_exc_esp_offset, X86_ESP, 4);
488         /* call the handler */
489         x86_call_reg (code, X86_ECX);
490         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
491         /* restore EBP */
492         x86_pop_reg (code, X86_EBP);
493         /* restore saved regs */
494         x86_pop_reg (code, X86_ESI);
495         x86_pop_reg (code, X86_EDI);
496         x86_pop_reg (code, X86_EBX);
497         x86_leave (code);
498         x86_ret (code);
499
500         g_assert ((code - start) < 64);
501 #endif
502         return start;
503 }
504
505 static void
506 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
507                  unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
508                  unsigned long eip,  unsigned long esp)
509 {
510         static void (*restore_context) (struct sigcontext *);
511         struct sigcontext ctx;
512
513         if (!restore_context)
514                 restore_context = arch_get_restore_context ();
515
516         /* adjust eip so that it point into the call instruction */
517         eip -= 1;
518
519 #if 0
520         ctx.SC_ESP = esp;
521         ctx.SC_EIP = eip;
522         ctx.SC_EBP = ebp;
523         ctx.SC_EDI = edi;
524         ctx.SC_ESI = esi;
525         ctx.SC_EBX = ebx;
526         ctx.SC_EDX = edx;
527         ctx.SC_ECX = ecx;
528         ctx.SC_EAX = eax;
529 #endif  
530         mono_arch_handle_exception (&ctx, exc, FALSE);
531         restore_context (&ctx);
532
533         g_assert_not_reached ();
534 }
535
536 /**
537  * arch_get_throw_exception:
538  *
539  * Returns a function pointer which can be used to raise 
540  * exceptions. The returned function has the following 
541  * signature: void (*func) (MonoException *exc); 
542  * For example to raise an arithmetic exception you can use:
543  *
544  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
545  * x86_call_code (code, arch_get_throw_exception ()); 
546  *
547  */
548 gpointer 
549 mono_arch_get_throw_exception (void)
550 {
551         static guint8 start [24];
552         static int inited = 0;
553         guint8 *code;
554
555         if (inited)
556                 return start;
557
558         inited = 1;
559         code = start;
560
561 #if 0
562         x86_push_reg (code, X86_ESP);
563         x86_push_membase (code, X86_ESP, 4); /* IP */
564         x86_push_membase (code, X86_ESP, 12); /* exception */
565         x86_push_reg (code, X86_EBP);
566         x86_push_reg (code, X86_EDI);
567         x86_push_reg (code, X86_ESI);
568         x86_push_reg (code, X86_EBX);
569         x86_push_reg (code, X86_EDX);
570         x86_push_reg (code, X86_ECX);
571         x86_push_reg (code, X86_EAX);
572         x86_call_code (code, throw_exception);
573         /* we should never reach this breakpoint */
574         x86_breakpoint (code);
575
576         g_assert ((code - start) < 24);
577 #endif
578         return start;
579 }
580
581 /**
582  * arch_get_throw_exception_by_name:
583  *
584  * Returns a function pointer which can be used to raise 
585  * corlib exceptions. The returned function has the following 
586  * signature: void (*func) (char *exc_name); 
587  * For example to raise an arithmetic exception you can use:
588  *
589  * x86_push_imm (code, "ArithmeticException"); 
590  * x86_call_code (code, arch_get_throw_exception_by_name ()); 
591  *
592  */
593 gpointer 
594 mono_arch_get_throw_exception_by_name (void)
595 {
596         static guint8 start [32];
597         static int inited = 0;
598         guint8 *code;
599
600         if (inited)
601                 return start;
602
603         inited = 1;
604         code = start;
605 #if 0
606         x86_push_membase (code, X86_ESP, 4); /* exception name */
607         x86_push_imm (code, "System");
608         x86_push_imm (code, mono_defaults.exception_class->image);
609         x86_call_code (code, mono_exception_from_name);
610         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
611         /* save the newly create object (overwrite exception name)*/
612         x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
613         x86_jump_code (code, mono_arch_get_throw_exception ());
614
615         g_assert ((code - start) < 32);
616 #endif
617         return start;
618 }       
619
620 static MonoArray *
621 glist_to_array (GList *list) 
622 {
623         MonoDomain *domain = mono_domain_get ();
624         MonoArray *res;
625         int len, i;
626
627         if (!list)
628                 return NULL;
629
630         len = g_list_length (list);
631         res = mono_array_new (domain, mono_defaults.int_class, len);
632
633         for (i = 0; list; list = list->next, i++)
634                 mono_array_set (res, gpointer, i, list->data);
635
636         return res;
637 }
638
639 /* mono_arch_find_jit_info:
640  *
641  * This function is used to gather information from @ctx. It return the 
642  * MonoJitInfo of the corresponding function, unwinds one stack frame and
643  * stores the resulting context into @new_ctx. It also stores a string 
644  * describing the stack location into @trace (if not NULL), and modifies
645  * the @lmf if necessary. @native_offset return the IP offset from the 
646  * start of the function or -1 if that info is not available.
647  */
648 static MonoJitInfo *
649 mono_arch_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoContext *ctx, 
650                          MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
651                          gboolean *managed)
652 {
653         MonoJitInfo *ji;
654         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
655
656         ji = mono_jit_info_table_find (domain, ip);
657
658         if (trace)
659                 *trace = NULL;
660
661         if (native_offset)
662                 *native_offset = -1;
663
664         if (managed)
665                 *managed = FALSE;
666
667         if (ji != NULL) {
668                 char *source_location, *tmpaddr, *fname;
669                 gint32 address, iloffset;
670                 int offset;
671
672                 *new_ctx = *ctx;
673
674                 if (*lmf && (MONO_CONTEXT_GET_BP (ctx) >= (gpointer)(*lmf)->ebp)) {
675                         /* remove any unused lmf */
676                         *lmf = (*lmf)->previous_lmf;
677                 }
678
679                 address = (char *)ip - (char *)ji->code_start;
680
681                 if (native_offset)
682                         *native_offset = address;
683
684                 if (managed)
685                         if (!ji->method->wrapper_type)
686                                 *managed = TRUE;
687
688                 if (trace) {
689                         source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
690                         iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
691
692                         if (iloffset < 0)
693                                 tmpaddr = g_strdup_printf ("<0x%05x>", address);
694                         else
695                                 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
696                 
697                         fname = mono_method_full_name (ji->method, TRUE);
698
699                         if (source_location)
700                                 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
701                         else
702                                 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
703
704                         g_free (fname);
705                         g_free (source_location);
706                         g_free (tmpaddr);
707                 }
708 #if 0                           
709                 offset = -1;
710                 /* restore caller saved registers */
711                 if (ji->used_regs & X86_EBX_MASK) {
712                         new_ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
713                         offset--;
714                 }
715                 if (ji->used_regs & X86_EDI_MASK) {
716                         new_ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
717                         offset--;
718                 }
719                 if (ji->used_regs & X86_ESI_MASK) {
720                         new_ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
721                 }
722
723                 new_ctx->SC_ESP = ctx->SC_EBP;
724                 /* we substract 1, so that the IP points into the call instruction */
725                 new_ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 1;
726                 new_ctx->SC_EBP = *((int *)ctx->SC_EBP);
727 #endif
728                 *res = *ji;
729                 return res;
730 #ifdef MONO_USE_EXC_TABLES
731         } else if ((ji = ppc_unwind_native_frame (domain, jit_tls, ctx, new_ctx, *lmf, trace))) {
732                 *res = *ji;
733                 return res;
734 #endif
735         } else if (*lmf) {
736                 
737                 *new_ctx = *ctx;
738
739                 if (!(*lmf)->method)
740                         return (gpointer)-1;
741
742                 if (trace)
743                         *trace = g_strdup_printf ("in (unmanaged) %s", mono_method_full_name ((*lmf)->method, TRUE));
744                 
745                 if ((ji = mono_jit_info_table_find (domain, (gpointer)(*lmf)->eip))) {
746                         *res = *ji;
747                 } else {
748                         memset (res, 0, sizeof (MonoJitInfo));
749                         res->method = (*lmf)->method;
750                 }
751
752 #if 0
753                 new_ctx->SC_ESI = (*lmf)->esi;
754                 new_ctx->SC_EDI = (*lmf)->edi;
755                 new_ctx->SC_EBX = (*lmf)->ebx;
756                 new_ctx->SC_EBP = (*lmf)->ebp;
757                 new_ctx->SC_EIP = (*lmf)->eip;
758                 /* the lmf is always stored on the stack, so the following
759                  * expression points to a stack location which can be used as ESP */
760                 new_ctx->SC_ESP = (unsigned long)&((*lmf)->eip);
761 #endif
762                 *lmf = (*lmf)->previous_lmf;
763
764                 return res;
765                 
766         }
767
768         return NULL;
769 }
770
771 MonoArray *
772 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
773 {
774         MonoDomain *domain = mono_domain_get ();
775         MonoArray *res;
776         MonoArray *ta = exc->trace_ips;
777         int i, len;
778         
779         len = mono_array_length (ta);
780
781         res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
782
783         for (i = skip; i < len; i++) {
784                 MonoJitInfo *ji;
785                 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
786                 gpointer ip = mono_array_get (ta, gpointer, i);
787
788                 ji = mono_jit_info_table_find (domain, ip);
789                 g_assert (ji != NULL);
790
791                 sf->method = mono_method_get_object (domain, ji->method, NULL);
792                 sf->native_offset = (char *)ip - (char *)ji->code_start;
793
794                 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
795
796                 if (need_file_info) {
797                         gchar *filename;
798                         
799                         filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
800
801                         sf->filename = filename? mono_string_new (domain, filename): NULL;
802                         sf->column = 0;
803
804                         g_free (filename);
805                 }
806
807                 mono_array_set (res, gpointer, i, sf);
808         }
809
810         return res;
811 }
812
813 void
814 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
815         MonoDomain *domain = mono_domain_get ();
816         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
817         MonoLMF *lmf = jit_tls->lmf;
818         MonoJitInfo *ji, rji;
819         gint native_offset, il_offset;
820         gboolean managed;
821
822         MonoContext ctx, new_ctx;
823
824         MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
825         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
826
827         while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
828                 
829                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
830                 g_assert (ji);
831
832                 if (ji == (gpointer)-1)
833                         return;
834
835                 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
836
837                 if (func (ji->method, native_offset, il_offset, managed, user_data))
838                         return;
839                 
840                 ctx = new_ctx;
841         }
842 }
843
844 MonoBoolean
845 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
846                           MonoReflectionMethod **method, 
847                           gint32 *iloffset, gint32 *native_offset,
848                           MonoString **file, gint32 *line, gint32 *column)
849 {
850         MonoDomain *domain = mono_domain_get ();
851         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
852         MonoLMF *lmf = jit_tls->lmf;
853         MonoJitInfo *ji, rji;
854         MonoContext ctx, new_ctx;
855
856         MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
857         MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
858
859         skip++;
860
861         do {
862                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
863
864                 ctx = new_ctx;
865                 
866                 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
867                         return FALSE;
868
869                 /* skip all wrappers ??*/
870                 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
871                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
872                     ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
873                         continue;
874
875                 skip--;
876
877         } while (skip >= 0);
878
879         *method = mono_method_get_object (domain, ji->method, NULL);
880         *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
881
882         if (need_file_info) {
883                 gchar *filename;
884
885                 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
886
887                 *file = filename? mono_string_new (domain, filename): NULL;
888                 *column = 0;
889
890                 g_free (filename);
891         }
892
893         return TRUE;
894 }
895
896 /**
897  * arch_handle_exception:
898  * @ctx: saved processor state
899  * @obj: the exception object
900  * @test_only: only test if the exception is caught, but dont call handlers
901  *
902  *
903  */
904 gboolean
905 mono_arch_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
906 {
907         MonoDomain *domain = mono_domain_get ();
908         MonoJitInfo *ji, rji;
909         static int (*call_filter) (MonoContext *, gpointer, gpointer) = NULL;
910         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
911         MonoLMF *lmf = jit_tls->lmf;            
912         GList *trace_ips = NULL;
913         MonoException *mono_ex;
914
915         g_assert (ctx != NULL);
916         if (!obj) {
917                 MonoException *ex = mono_get_exception_null_reference ();
918                 ex->message = mono_string_new (domain, 
919                         "Object reference not set to an instance of an object");
920                 obj = (MonoObject *)ex;
921         } 
922
923         if (mono_object_isinst (obj, mono_defaults.exception_class)) {
924                 mono_ex = (MonoException*)obj;
925                 mono_ex->stack_trace = NULL;
926         } else {
927                 mono_ex = NULL;
928         }
929
930
931         if (!call_filter)
932                 call_filter = arch_get_call_filter ();
933
934         g_assert (jit_tls->end_of_stack);
935         g_assert (jit_tls->abort_func);
936
937         if (!test_only) {
938                 MonoContext ctx_cp = *ctx;
939                 if (mono_jit_trace_calls != NULL)
940                         g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
941                 if (!mono_arch_handle_exception (&ctx_cp, obj, TRUE)) {
942                         if (mono_break_on_exc)
943                                 G_BREAKPOINT ();
944                         mono_unhandled_exception (obj);
945                 }
946         }
947
948         while (1) {
949                 MonoContext new_ctx;
950                 char *trace = NULL;
951                 
952                 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, ctx, &new_ctx, 
953                                               test_only ? &trace : NULL, &lmf, NULL, NULL);
954                 if (!ji) {
955                         g_warning ("Exception inside function without unwind info");
956                         g_assert_not_reached ();
957                 }
958
959                 if (ji != (gpointer)-1) {
960                         
961                         if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
962                                 char *tmp, *strace;
963
964                                 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
965
966                                 if (!mono_ex->stack_trace)
967                                         strace = g_strdup ("");
968                                 else
969                                         strace = mono_string_to_utf8 (mono_ex->stack_trace);
970                         
971                                 tmp = g_strdup_printf ("%s%s\n", strace, trace);
972                                 g_free (strace);
973
974                                 mono_ex->stack_trace = mono_string_new (domain, tmp);
975
976                                 g_free (tmp);
977                         }
978
979                         if (ji->num_clauses) {
980                                 int i;
981                                 
982                                 g_assert (ji->clauses);
983                         
984                                 for (i = 0; i < ji->num_clauses; i++) {
985                                         MonoJitExceptionInfo *ei = &ji->clauses [i];
986
987                                         if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
988                                             MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) { 
989                                                 /* catch block */
990                                                 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
991                                                      mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
992                                                     ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
993                                                       call_filter (ctx, ei->data.filter, obj)))) {
994                                                         if (test_only) {
995                                                                 if (mono_ex)
996                                                                         mono_ex->trace_ips = glist_to_array (trace_ips);
997                                                                 g_list_free (trace_ips);
998                                                                 g_free (trace);
999                                                                 return TRUE;
1000                                                         }
1001                                                         if (mono_jit_trace_calls != NULL)
1002                                                                 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1003                                                         MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
1004                                                         *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
1005                                                         jit_tls->lmf = lmf;
1006                                                         g_free (trace);
1007                                                         return 0;
1008                                                 }
1009                                                 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
1010                                                     MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
1011                                                     (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1012                                                         if (mono_jit_trace_calls != NULL)
1013                                                                 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
1014                                                         call_filter (ctx, ei->handler_start, NULL);
1015                                                 }
1016                                                 
1017                                         }
1018                                 }
1019                         }
1020                 }
1021
1022                 g_free (trace);
1023                         
1024                 *ctx = new_ctx;
1025
1026                 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
1027                         if (!test_only) {
1028                                 jit_tls->lmf = lmf;
1029                                 jit_tls->abort_func (obj);
1030                                 g_assert_not_reached ();
1031                         } else {
1032                                 if (mono_ex)
1033                                         mono_ex->trace_ips = glist_to_array (trace_ips);
1034                                 g_list_free (trace_ips);
1035                                 return FALSE;
1036                         }
1037                 }
1038         }
1039
1040         g_assert_not_reached ();
1041 }
1042
1043 gboolean
1044 mono_arch_has_unwind_info (gconstpointer addr)
1045 {
1046         return FALSE;
1047 }
1048