Standardized Mainsoft ConstraintCollection tests.
[mono.git] / mono / mini / mini-exceptions.c
index 227b0d9fffe02293a29809eaeb7174c51ee939a8..36946b60f45c45fedbbdd907abfff0e66c6cf9d1 100644 (file)
@@ -61,23 +61,27 @@ mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *re
 
        if (managed2 || ji->method->wrapper_type) {
                char *source_location, *tmpaddr, *fname;
-               gint32 address, iloffset;
+               gint32 offset, iloffset;
 
-               address = (char *)ip - (char *)ji->code_start;
+               if (!managed2)
+                       /* ctx->ip points into native code */
+                       offset = (char*)MONO_CONTEXT_GET_IP (new_ctx) - (char*)ji->code_start;
+               else
+                       offset = (char *)ip - (char *)ji->code_start;
 
                if (native_offset)
-                       *native_offset = address;
+                       *native_offset = offset;
 
                if (managed)
                        if (!ji->method->wrapper_type)
                                *managed = TRUE;
 
                if (trace) {
-                       source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
-                       iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
+                       source_location = mono_debug_source_location_from_address (ji->method, offset, NULL, domain);
+                       iloffset = mono_debug_il_offset_from_address (ji->method, offset, domain);
 
                        if (iloffset < 0)
-                               tmpaddr = g_strdup_printf ("<0x%05x>", address);
+                               tmpaddr = g_strdup_printf ("<0x%05x>", offset);
                        else
                                tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
                
@@ -106,6 +110,62 @@ mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *re
 
 #endif /* mono_find_jit_info */
 
+MonoString *
+ves_icall_System_Exception_get_trace (MonoException *ex)
+{
+       MonoDomain *domain = mono_domain_get ();
+       MonoString *res;
+       MonoArray *ta = ex->trace_ips;
+       int i, len;
+       GString *trace_str;
+       char tmpaddr [256];
+
+       if (ta == NULL)
+               /* Exception is not thrown yet */
+               return NULL;
+
+       len = mono_array_length (ta);
+       trace_str = g_string_new ("");
+       for (i = 0; i < len; i++) {
+               MonoJitInfo *ji;
+               gpointer ip = mono_array_get (ta, gpointer, i);
+
+               ji = mono_jit_info_table_find (domain, ip);
+               if (ji == NULL) {
+                       /* Unmanaged frame */
+                       g_string_append_printf (trace_str, "in (unmanaged) %p\n", ip);
+               } else {
+                       char *source_location, *fname;
+                       gint32 address, iloffset;
+
+                       address = (char *)ip - (char *)ji->code_start;
+
+                       source_location = mono_debug_source_location_from_address (ji->method, address, NULL, ex->object.vtable->domain);
+                       iloffset = mono_debug_il_offset_from_address (ji->method, address, ex->object.vtable->domain);
+
+                       if (iloffset < 0)
+                               sprintf (tmpaddr, "<0x%05x>", address);
+                       else
+                               sprintf (tmpaddr, "[0x%05x]", iloffset);
+               
+                       fname = mono_method_full_name (ji->method, TRUE);
+
+                       if (source_location)
+                               g_string_append_printf (trace_str, "in %s (at %s) %s\n", tmpaddr, source_location, fname);
+                       else
+                               g_string_append_printf (trace_str, "in %s %s\n", tmpaddr, fname);
+
+                       g_free (fname);
+                       g_free (source_location);
+               }
+       }
+
+       res = mono_string_new (ex->object.vtable->domain, trace_str->str);
+       g_string_free (trace_str, TRUE);
+
+       return res;
+}
+
 MonoArray *
 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
 {
@@ -137,7 +197,16 @@ ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info
 
                g_assert (ji != NULL);
 
-               sf->method = mono_method_get_object (domain, ji->method, NULL);
+               if (ji->method->wrapper_type) {
+                       char *s;
+
+                       sf->method = NULL;
+                       s = mono_method_full_name (ji->method, TRUE);
+                       sf->internal_method_name = mono_string_new (domain, s);
+                       g_free (s);
+               }
+               else
+                       sf->method = mono_method_get_object (domain, ji->method, NULL);
                sf->native_offset = (char *)ip - (char *)ji->code_start;
 
                sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
@@ -159,7 +228,6 @@ ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info
        return res;
 }
 
-
 /**
  * mono_walk_stack:
  * @domain: starting appdomain
@@ -215,11 +283,20 @@ mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_da
 
        MonoContext ctx, new_ctx;
 
+#ifdef _MSC_VER
+       unsigned int stackptr, retaddr;
+       __asm mov stackptr, ebp;
+       __asm mov eax, DWORD PTR [ebp + 4];
+       __asm mov retaddr, eax;
+       MONO_CONTEXT_SET_IP (&ctx, retaddr);
+       /* FIXME: NOT WORKING -- THIS IS __builtin_frame_address (0) NOT (1) */
+       MONO_CONTEXT_SET_BP (&ctx, stackptr);
+#else
        mono_arch_flush_register_windows ();
 
        MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
        MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
-
+#endif
        while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
                
                ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
@@ -248,11 +325,18 @@ ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
        MonoLMF *lmf = jit_tls->lmf;
        MonoJitInfo *ji, rji;
        MonoContext ctx, new_ctx;
+       gssize stackptr;
 
        mono_arch_flush_register_windows ();
 
+#ifdef _MSC_VER
+       __asm mov stackptr, ebp;
+#else
+       stackptr = (gssize)__builtin_frame_address (0);
+#endif
+
        MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
-       MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
+       MONO_CONTEXT_SET_BP (&ctx, stackptr);
 
        skip++;
 
@@ -342,7 +426,16 @@ ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip)
        MonoFrameSecurityInfo si;
        MonoContext ctx;
 
+#ifdef _MSC_VER
+       /* seems that MSC doesn't like having __asm in macros */
+       unsigned int stackptr;
+       mono_arch_flush_register_windows ();
+       __asm mov stackptr, ebp;
+       MONO_CONTEXT_SET_IP (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
+       MONO_CONTEXT_SET_BP (&ctx, stackptr);
+#else
        MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
+#endif
 
        si.skips = skip;
        si.frame = NULL;
@@ -354,9 +447,26 @@ ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip)
 
 typedef struct {
        guint32 skips;
-       GList *stack;
+       MonoArray *stack;
+       guint32 count;
+       guint32 maximum;
 } MonoSecurityStack;
 
+static void
+grow_array (MonoSecurityStack *stack)
+{
+       MonoDomain *domain = mono_domain_get ();
+       guint32 newsize = (stack->maximum << 1);
+       MonoArray *newstack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, newsize);
+       int i;
+       for (i=0; i < stack->maximum; i++) {
+               gpointer frame = mono_array_get (stack->stack, gpointer, i);
+               mono_array_set (newstack, gpointer, i, frame);
+       }
+       stack->maximum = newsize;
+       stack->stack = newstack;
+}
+
 static gboolean
 callback_get_stack_frames_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
 {
@@ -376,7 +486,10 @@ callback_get_stack_frames_security_info (MonoDomain *domain, MonoContext *ctx, M
                return FALSE;
        }
 
-       ss->stack = g_list_prepend (ss->stack, mono_declsec_create_frame (domain, ji));
+       if (ss->count == ss->maximum)
+               grow_array (ss);
+       
+       mono_array_set (ss->stack, gpointer, ss->count++, mono_declsec_create_frame (domain, ji));
 
        /* continue down the stack */
        return FALSE;
@@ -417,19 +530,25 @@ ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
        MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
        MonoSecurityStack ss;
        MonoContext ctx;
-       MonoArray *stack;
 
+#ifdef _MSC_VER
+       /* seems that MSC doesn't like having __asm in macros */
+       unsigned int stackptr;
+       mono_arch_flush_register_windows ();
+       __asm mov stackptr, ebp;
+       MONO_CONTEXT_SET_IP (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
+       MONO_CONTEXT_SET_BP (&ctx, stackptr);
+#else
        MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
+#endif
 
        ss.skips = skip;
-       ss.stack = NULL;
+       ss.count = 0;
+       ss.maximum = MONO_CAS_INITIAL_STACK_SIZE;
+       ss.stack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, ss.maximum);
        mono_walk_stack (domain, jit_tls, &ctx, callback_get_stack_frames_security_info, (gpointer)&ss);
-
-       stack = glist_to_array (ss.stack, mono_defaults.runtimesecurityframe_class);
-       if (ss.stack)
-               g_list_free (ss.stack);
-
-       return stack;
+       /* g_warning ("STACK RESULT: %d out of %d", ss.count, ss.maximum); */
+       return ss.stack;
 }
 
 #ifndef CUSTOM_EXCEPTION_HANDLING
@@ -450,14 +569,14 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
        static void (*restore_context) (void *);
        MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
        MonoLMF *lmf = jit_tls->lmf;            
+       MonoArray *initial_trace_ips = NULL;
        GList *trace_ips = NULL;
        MonoException *mono_ex;
        gboolean stack_overflow = FALSE;
        MonoContext initial_ctx;
        int frame_count = 0;
        gboolean gc_disabled = FALSE;
-       MonoString *initial_stack_trace = NULL;
-       GString *trace_str = NULL;
+       gboolean has_dynamic_methods = FALSE;
        
        /*
         * This function might execute on an alternate signal stack, and Boehm GC
@@ -489,6 +608,7 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
         */
        if (obj == domain->stack_overflow_ex) {
                obj = mono_get_exception_stack_overflow ();
+               stack_overflow = TRUE;
        }
        else if (obj == domain->null_reference_ex) {
                obj = mono_get_exception_null_reference ();
@@ -496,14 +616,11 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
 
        if (mono_object_isinst (obj, mono_defaults.exception_class)) {
                mono_ex = (MonoException*)obj;
-               initial_stack_trace = mono_ex->stack_trace;
+               initial_trace_ips = mono_ex->trace_ips;
        } else {
                mono_ex = NULL;
        }
 
-       if (obj == domain->stack_overflow_ex)
-               stack_overflow = TRUE;
-
        if (!call_filter)
                call_filter = mono_arch_get_call_filter ();
 
@@ -542,18 +659,10 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
 
        while (1) {
                MonoContext new_ctx;
-               char *trace = NULL;
-               gboolean need_trace = FALSE;
                guint32 free_stack;
 
-               if (test_only && (frame_count < 1000) && !initial_stack_trace && mono_ex) {
-                       need_trace = TRUE;
-                       if (!trace_str)
-                               trace_str = g_string_new ("");
-               }
-
                ji = mono_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx, 
-                                                                need_trace ? &trace : NULL, &lmf, NULL, NULL);
+                                                                NULL, &lmf, NULL, NULL);
                if (!ji) {
                        g_warning ("Exception inside function without unwind info");
                        g_assert_not_reached ();
@@ -569,14 +678,14 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
                                 * rethrown. Also avoid giant stack traces during a stack
                                 * overflow.
                                 */
-                               if (!initial_stack_trace && (frame_count < 1000)) {
+                               if (!initial_trace_ips && (frame_count < 1000)) {
                                        trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
-
-                                       g_string_append (trace_str, trace);
-                                       g_string_append_c (trace_str, '\n');
                                }
                        }
 
+                       if (ji->method->dynamic)
+                               has_dynamic_methods = TRUE;
+
                        if (stack_overflow)
                                free_stack = (guint8*)(MONO_CONTEXT_GET_BP (ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
                        else
@@ -589,8 +698,6 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
                        if ((free_stack > (64 * 1024)) && ji->num_clauses) {
                                int i;
                                
-                               g_assert (ji->clauses);
-                       
                                for (i = 0; i < ji->num_clauses; i++) {
                                        MonoJitExceptionInfo *ei = &ji->clauses [i];
                                        gboolean filtered = FALSE;
@@ -604,12 +711,9 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
                                                /* catch block */
 
                                                if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
-                                                       /* store the exception object int cfg->excvar */
+                                                       /* store the exception object in cfg->excvar */
                                                        g_assert (ei->exvar_offset);
                                                        *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = obj;
-                                                       if (!initial_stack_trace && trace_str) {
-                                                               mono_ex->stack_trace = mono_string_new (domain, trace_str->str);
-                                                       }
                                                }
 
                                                if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
@@ -620,17 +724,17 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
                                                if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE && 
                                                     mono_object_isinst (obj, ei->data.catch_class)) || filtered) {
                                                        if (test_only) {
-                                                               if (mono_ex) {
+                                                               if (mono_ex && !initial_trace_ips) {
                                                                        trace_ips = g_list_reverse (trace_ips);
                                                                        mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
+                                                                       if (has_dynamic_methods)
+                                                                               /* These methods could go away anytime, so compute the stack trace now */
+                                                                               mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
                                                                }
                                                                g_list_free (trace_ips);
-                                                               g_free (trace);
 
                                                                if (gc_disabled)
                                                                        mono_gc_enable ();
-                                                               if (trace_str)
-                                                                       g_string_free (trace_str, TRUE);
                                                                return TRUE;
                                                        }
                                                        if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
@@ -638,12 +742,9 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
                                                        mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
                                                        MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
                                                        jit_tls->lmf = lmf;
-                                                       g_free (trace);
 
                                                        if (gc_disabled)
                                                                mono_gc_enable ();
-                                                       if (trace_str)
-                                                               g_string_free (trace_str, TRUE);
                                                        return 0;
                                                }
                                                if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) && 
@@ -660,8 +761,6 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
                        }
                }
 
-               g_free (trace);
-                       
                *ctx = new_ctx;
 
                if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
@@ -675,21 +774,22 @@ mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gbo
                                        /* Switch back to normal stack */
                                        if (stack_overflow)
                                                /* Free up some stack space */
-                                               MONO_CONTEXT_SET_SP (&initial_ctx, (guint32)(MONO_CONTEXT_GET_SP (&initial_ctx)) + (64 * 1024));
-                                       MONO_CONTEXT_SET_IP (&initial_ctx, (unsigned int)jit_tls->abort_func);
+                                               MONO_CONTEXT_SET_SP (&initial_ctx, (gssize)(MONO_CONTEXT_GET_SP (&initial_ctx)) + (64 * 1024));
+                                       MONO_CONTEXT_SET_IP (&initial_ctx, (gssize)jit_tls->abort_func);
                                        restore_context (&initial_ctx);
                                }
                                else
                                        jit_tls->abort_func (obj);
                                g_assert_not_reached ();
                        } else {
-                               if (mono_ex) {
+                               if (mono_ex && !initial_trace_ips) {
                                        trace_ips = g_list_reverse (trace_ips);
                                        mono_ex->trace_ips = glist_to_array (trace_ips, mono_defaults.int_class);
+                                       if (has_dynamic_methods)
+                                               /* These methods could go away anytime, so compute the stack trace now */
+                                               mono_ex->stack_trace = ves_icall_System_Exception_get_trace (mono_ex);
                                }
                                g_list_free (trace_ips);
-                               if (trace_str)
-                                       g_string_free (trace_str, TRUE);
                                return FALSE;
                        }
                }