2 * exception.c: exception support
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/arch/x86/x86-codegen.h>
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/threads.h>
17 #include <mono/metadata/debug-helpers.h>
24 # define SC_EAX sc_eax
25 # define SC_EBX sc_ebx
26 # define SC_ECX sc_ecx
27 # define SC_EDX sc_edx
28 # define SC_EBP sc_ebp
29 # define SC_EIP sc_eip
30 # define SC_ESP sc_esp
31 # define SC_EDI sc_edi
32 # define SC_ESI sc_esi
46 * arch_get_restore_context:
48 * Returns a pointer to a method which restores a previously saved sigcontext.
51 arch_get_restore_context (void)
53 static guint8 *start = NULL;
59 /* restore_contect (struct sigcontext *ctx) */
60 /* we do not restore X86_EAX, X86_EDX */
62 start = code = g_malloc (1024);
65 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
67 /* get return address, stored in EDX */
68 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EIP), 4);
70 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
72 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
74 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
76 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
78 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
79 /* restore ECX. the exception object is passed here to the catch handler */
80 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ECX), 4);
82 /* jump to the saved IP */
83 x86_jump_reg (code, X86_EDX);
89 * arch_get_call_finally:
91 * Returns a pointer to a method which calls a finally handler.
94 arch_get_call_finally (void)
96 static guint8 start [64];
97 static int inited = 0;
104 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
107 x86_push_reg (code, X86_EBP);
108 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
109 x86_push_reg (code, X86_EBX);
110 x86_push_reg (code, X86_EDI);
111 x86_push_reg (code, X86_ESI);
114 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
116 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
118 x86_push_reg (code, X86_EBP);
120 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
121 /* restore registers used by global register allocation (EBX & ESI) */
122 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
123 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
124 /* save the ESP - this is used by endfinally */
125 x86_mov_membase_reg (code, X86_EBP, mono_exc_esp_offset, X86_ESP, 4);
126 /* call the handler */
127 x86_call_reg (code, X86_ECX);
129 x86_pop_reg (code, X86_EBP);
130 /* restore saved regs */
131 x86_pop_reg (code, X86_ESI);
132 x86_pop_reg (code, X86_EDI);
133 x86_pop_reg (code, X86_EBX);
137 g_assert ((code - start) < 64);
142 glist_to_array (GList *list)
144 MonoDomain *domain = mono_domain_get ();
151 len = g_list_length (list);
152 res = mono_array_new (domain, mono_defaults.int_class, len);
154 for (i = 0; list; list = list->next, i++)
155 mono_array_set (res, gpointer, i, list->data);
161 * return TRUE if the exception is catched. It also sets the
162 * stack_trace String.
165 arch_exc_is_caught (MonoDomain *domain, MonoJitTlsData *jit_tls, gpointer ip,
166 gpointer *bp, gpointer obj)
169 gpointer *end_of_stack;
170 MonoLMF *lmf = jit_tls->lmf;
172 GList *trace_ips = NULL;
175 end_of_stack = jit_tls->end_of_stack;
176 g_assert (end_of_stack);
180 ji = mono_jit_info_table_find (domain, ip);
182 if (ji) { /* we are inside managed code */
185 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
187 char *tmp, *source_location, *tmpaddr, *fname;
188 gint32 address, iloffset;
190 trace_ips = g_list_append (trace_ips, ip);
192 if (!((MonoException*)obj)->stack_trace)
193 strace = g_strdup ("");
195 strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
197 address = (char *)ip - (char *)ji->code_start;
199 source_location = mono_debug_source_location_from_address (m, address, NULL);
200 iloffset = mono_debug_il_offset_from_address (m, address);
203 tmpaddr = g_strdup_printf ("<0x%05x>", address);
205 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
207 fname = mono_method_full_name (m, TRUE);
210 tmp = g_strdup_printf ("%sin %s (at %s) %s\n", strace, tmpaddr,
211 source_location, fname);
213 tmp = g_strdup_printf ("%sin %s %s\n", strace, tmpaddr,
216 g_free (source_location);
219 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
223 if (ji->num_clauses) {
225 g_assert (ji->clauses);
227 for (i = 0; i < ji->num_clauses; i++) {
228 MonoJitExceptionInfo *ei = &ji->clauses [i];
230 if (ei->try_start <= ip && ip <= (ei->try_end)) {
232 if (ei->flags == 0 &&
233 (m->wrapper_type != MONO_WRAPPER_NONE ||
234 mono_object_isinst (obj, mono_class_get (m->klass->image, ei->token_or_filter)))) {
235 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
236 g_list_free (trace_ips);
243 /* continue unwinding */
245 ip = (gpointer)(*((int *)bp + 1) - 5);
246 bp = (gpointer)(*((int *)bp));
248 if (bp >= end_of_stack) {
249 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
250 g_list_free (trace_ips);
256 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
257 g_list_free (trace_ips);
261 bp = (gpointer)lmf->ebp;
262 ip = (gpointer)lmf->eip;
266 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
270 trace_ips = g_list_append (trace_ips, lmf->method->info);
272 if (!((MonoException*)obj)->stack_trace)
273 strace = g_strdup ("");
275 strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
277 tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,
278 m->klass->name, m->name);
282 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
286 lmf = lmf->previous_lmf;
288 if (bp >= end_of_stack) {
289 ((MonoException*)obj)->trace_ips = glist_to_array (trace_ips);
290 g_list_free (trace_ips);
300 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
302 MonoDomain *domain = mono_domain_get ();
304 MonoArray *ta = exc->trace_ips;
307 len = mono_array_length (ta);
309 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
311 for (i = skip; i < len; i++) {
313 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
314 gpointer ip = mono_array_get (ta, gpointer, i);
316 ji = mono_jit_info_table_find (domain, ip);
317 g_assert (ji != NULL);
319 sf->method = mono_method_get_object (domain, ji->method);
320 sf->native_offset = (char *)ip - (char *)ji->code_start;
321 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset);
323 if (need_file_info) {
326 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line);
328 sf->filename = mono_string_new (domain, filename ? filename : "<unknown>");
334 mono_array_set (res, gpointer, i, sf);
341 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
342 MonoReflectionMethod **method,
343 gint32 *iloffset, gint32 *native_offset,
344 MonoString **file, gint32 *line, gint32 *column)
346 MonoDomain *domain = mono_domain_get ();
347 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
348 MonoLMF *lmf = jit_tls->lmf;
349 gpointer *sf = (gpointer *)&skip;
350 gpointer ip = sf [-1];
352 gpointer *bp = sf [-2];
353 MonoMethod *m = NULL;
357 addr = -1; /* unknown */
359 if ((ji = mono_jit_info_table_find (domain, ip))) {
361 addr = (char *)ip - (char *)ji->code_start;
362 ip = (gpointer)((char *)bp [1] - 5);
370 bp = (gpointer)lmf->ebp;
371 ip = (gpointer)lmf->eip;
373 lmf = lmf->previous_lmf;
376 if ((unsigned)bp >= (unsigned)jit_tls->end_of_stack)
379 } while (skip-- > 0);
383 *method = mono_method_get_object (domain, m);
384 *iloffset = mono_debug_il_offset_from_address (m, addr);
385 *native_offset = addr;
387 if (need_file_info) {
390 filename = mono_debug_source_location_from_address (m, addr, line);
392 *file = mono_string_new (domain, filename ? filename : "<unknown>");
402 * arch_handle_exception:
403 * @ctx: saved processor state
407 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
409 MonoDomain *domain = mono_domain_get ();
411 gpointer ip = (gpointer)ctx->SC_EIP;
412 static void (*restore_context) (struct sigcontext *);
413 static void (*call_finally) (struct sigcontext *, unsigned long);
414 void (*cleanup) (MonoObject *exc);
415 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
416 gpointer end_of_stack;
418 g_assert (ctx != NULL);
421 MonoException *ex = mono_get_exception_null_reference ();
422 ex->message = mono_string_new (domain,
423 "Object reference not set to an instance of an object");
424 obj = (MonoObject *)ex;
427 ((MonoException *)obj)->stack_trace = NULL;
429 if (!restore_context)
430 restore_context = arch_get_restore_context ();
433 call_finally = arch_get_call_finally ();
435 end_of_stack = jit_tls->end_of_stack;
436 g_assert (end_of_stack);
438 cleanup = jit_tls->abort_func;
440 if (!arch_exc_is_caught (domain, jit_tls, ip, (gpointer *)ctx->SC_EBP, obj)) {
441 if (mono_break_on_exc) {
442 if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
443 mono_debug_make_symbols ();
446 mono_unhandled_exception (obj);
451 ji = mono_jit_info_table_find (domain, ip);
453 if (ji) { /* we are inside managed code */
454 MonoMethod *m = ji->method;
457 if (ji->num_clauses) {
460 g_assert (ji->clauses);
462 for (i = 0; i < ji->num_clauses; i++) {
463 MonoJitExceptionInfo *ei = &ji->clauses [i];
465 if (ei->try_start <= ip && ip <= (ei->try_end)) {
467 if (ei->flags == 0 &&
468 (m->wrapper_type != MONO_WRAPPER_NONE ||
469 mono_object_isinst (obj, mono_class_get (m->klass->image, ei->token_or_filter)))) {
471 ctx->SC_EIP = (unsigned long)ei->handler_start;
472 ctx->SC_ECX = (unsigned long)obj;
473 restore_context (ctx);
474 g_assert_not_reached ();
479 /* no handler found - we need to call all finally handlers */
480 for (i = 0; i < ji->num_clauses; i++) {
481 MonoJitExceptionInfo *ei = &ji->clauses [i];
483 if (ei->try_start <= ip && ip < (ei->try_end) &&
484 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
485 call_finally (ctx, (unsigned long)ei->handler_start);
490 /* continue unwinding */
493 /* restore caller saved registers */
494 if (ji->used_regs & X86_EBX_MASK) {
495 ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
498 if (ji->used_regs & X86_EDI_MASK) {
499 ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
502 if (ji->used_regs & X86_ESI_MASK) {
503 ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
506 ctx->SC_ESP = ctx->SC_EBP;
507 ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 5;
508 ctx->SC_EBP = *((int *)ctx->SC_EBP);
510 ip = (gpointer)ctx->SC_EIP;
512 if (ctx->SC_EBP > (unsigned)end_of_stack) {
515 g_assert_not_reached ();
519 MonoLMF *lmf = jit_tls->lmf;
524 g_assert_not_reached ();
527 jit_tls->lmf = lmf->previous_lmf;
529 ctx->SC_ESI = lmf->esi;
530 ctx->SC_EDI = lmf->edi;
531 ctx->SC_EBX = lmf->ebx;
532 ctx->SC_EBP = lmf->ebp;
533 ctx->SC_EIP = lmf->eip;
534 ctx->SC_ESP = (unsigned long)&lmf->eip;
536 ip = (gpointer)ctx->SC_EIP;
538 if (ctx->SC_EBP >= (unsigned)end_of_stack) {
541 g_assert_not_reached ();
546 g_assert_not_reached ();
550 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
551 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
552 unsigned long eip, unsigned long esp)
554 struct sigcontext ctx;
556 /* adjust eip so that it point to the call instruction */
569 arch_handle_exception (&ctx, exc);
571 g_assert_not_reached ();
575 * arch_get_throw_exception:
577 * Returns a function pointer which can be used to raise
578 * exceptions. The returned function has the following
579 * signature: void (*func) (MonoException *exc);
580 * For example to raise an arithmetic exception you can use:
582 * x86_push_imm (code, mono_get_exception_arithmetic ());
583 * x86_call_code (code, arch_get_throw_exception ());
587 arch_get_throw_exception (void)
589 static guint8 start [24];
590 static int inited = 0;
599 x86_push_reg (code, X86_ESP);
600 x86_push_membase (code, X86_ESP, 4); /* IP */
601 x86_push_membase (code, X86_ESP, 12); /* exception */
602 x86_push_reg (code, X86_EBP);
603 x86_push_reg (code, X86_EDI);
604 x86_push_reg (code, X86_ESI);
605 x86_push_reg (code, X86_EBX);
606 x86_push_reg (code, X86_EDX);
607 x86_push_reg (code, X86_ECX);
608 x86_push_reg (code, X86_EAX);
609 x86_call_code (code, throw_exception);
610 /* we should never reach this breakpoint */
611 x86_breakpoint (code);
613 g_assert ((code - start) < 24);
618 * arch_get_throw_exception_by_name:
620 * Returns a function pointer which can be used to raise
621 * corlib exceptions. The returned function has the following
622 * signature: void (*func) (char *exc_name);
623 * For example to raise an arithmetic exception you can use:
625 * x86_push_imm (code, "ArithmeticException");
626 * x86_call_code (code, arch_get_throw_exception ());
630 arch_get_throw_exception_by_name ()
632 static guint8 start [32];
633 static int inited = 0;
642 /* fixme: we do not save EAX, EDX, ECD - unsure if we need that */
644 x86_push_membase (code, X86_ESP, 4); /* exception name */
645 x86_push_imm (code, "System");
646 x86_push_imm (code, mono_defaults.exception_class->image);
647 x86_call_code (code, mono_exception_from_name);
648 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
649 /* save the newly create object (overwrite exception name)*/
650 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
651 x86_jump_code (code, arch_get_throw_exception ());
653 g_assert ((code - start) < 32);