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