[sgen] Fix locking of the worker distribute gray queue.
[mono.git] / mono / metadata / exception.c
index fa1d6126ac8e18c85ccf058dbd1a12cc05a50b28..f120041e2d255cd8eb735b76f8cd803add86a46f 100644 (file)
@@ -7,14 +7,21 @@
  *     Dick Porter     (dick@ximian.com)
  *      Miguel de Icaza (miguel@ximian.com)
  *
- * (C) 2001, 2002 Ximian, Inc.
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include <mono/metadata/exception.h>
 #include <mono/metadata/object-internals.h>
+#include <mono/metadata/metadata-internals.h>
 #include <mono/metadata/appdomain.h>
+#include <mono/metadata/mono-debug.h>
 #include <string.h>
 
+#ifdef HAVE_EXECINFO_H
+#include <execinfo.h>
+#endif
+
 /**
  * mono_exception_from_name:
  * @image: the Mono image where to look for the class
@@ -51,13 +58,18 @@ mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
 {
        MonoClass *klass;
        MonoObject *o;
+       MonoDomain *caller_domain = mono_domain_get ();
 
        klass = mono_class_from_name (image, name_space, name);
 
        o = mono_object_new (domain, klass);
        g_assert (o != NULL);
-       
+
+       if (domain != caller_domain)
+               mono_domain_set_internal (domain);
        mono_runtime_object_init (o);
+       if (domain != caller_domain)
+               mono_domain_set_internal (caller_domain);
 
        return (MonoException *)o;
 }
@@ -88,25 +100,10 @@ mono_exception_from_token (MonoImage *image, guint32 token)
        return (MonoException *)o;
 }
 
-/**
- * mono_exception_from_name_two_strings:
- * @image: the Mono image where to look for the class
- * @name_space: the namespace for the class
- * @name: class name
- * @a1: first string argument to pass
- * @a2: second string argument to pass
- *
- * Creates an exception from a constructor that takes two string
- * arguments.
- *
- * Returns: the initialized exception instance.
- */
-MonoException *
-mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
-                                     const char *name, MonoString *a1, MonoString *a2)
+static MonoException *
+create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2)
 {
        MonoDomain *domain = mono_domain_get ();
-       MonoClass *klass;
        MonoMethod *method = NULL;
        MonoObject *o;
        int count = 1;
@@ -117,7 +114,6 @@ mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
        if (a2 != NULL)
                count++;
        
-       klass = mono_class_from_name (image, name_space, name);
        o = mono_object_new (domain, klass);
 
        iter = NULL;
@@ -135,6 +131,7 @@ mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
                if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
                        continue;
                method = m;
+               break;
        }
 
        args [0] = a1;
@@ -143,6 +140,28 @@ mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
        return (MonoException *) o;
 }
 
+/**
+ * mono_exception_from_name_two_strings:
+ * @image: the Mono image where to look for the class
+ * @name_space: the namespace for the class
+ * @name: class name
+ * @a1: first string argument to pass
+ * @a2: second string argument to pass
+ *
+ * Creates an exception from a constructor that takes two string
+ * arguments.
+ *
+ * Returns: the initialized exception instance.
+ */
+MonoException *
+mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
+                                     const char *name, MonoString *a1, MonoString *a2)
+{
+       MonoClass *klass = mono_class_from_name (image, name_space, name);
+
+       return create_exception_two_strings (klass, a1, a2);
+}
+
 /**
  * mono_exception_from_name_msg:
  * @image: the Mono image where to look for the class
@@ -168,6 +187,21 @@ mono_exception_from_name_msg (MonoImage *image, const char *name_space,
        return ex;
 }
 
+/**
+ * mono_exception_from_token_two_strings:
+ *
+ *   Same as mono_exception_from_name_two_strings, but lookup the exception class using
+ * IMAGE and TOKEN.
+ */
+MonoException *
+mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
+                                                                          MonoString *a1, MonoString *a2)
+{
+       MonoClass *klass = mono_class_get (image, token);
+
+       return create_exception_two_strings (klass, a1, a2);
+}
+
 /**
  * mono_get_exception_divide_by_zero:
  *
@@ -204,6 +238,18 @@ mono_get_exception_thread_abort ()
                                         "ThreadAbortException");
 }
 
+/**
+ * mono_get_exception_thread_interrupted:
+ *
+ * Returns: a new instance of the System.Threading.ThreadInterruptedException.
+ */
+MonoException *
+mono_get_exception_thread_interrupted ()
+{
+       return mono_exception_from_name (mono_get_corlib (), "System.Threading",
+                                        "ThreadInterruptedException");
+}
+
 /**
  * mono_get_exception_arithmetic:
  *
@@ -501,7 +547,7 @@ mono_get_exception_file_not_found (MonoString *fname)
 MonoException *
 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
 {
-       MonoString *s = mono_string_new (mono_domain_get (), msg);
+       MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
 
        return mono_exception_from_name_two_strings (
                mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname);
@@ -551,7 +597,7 @@ mono_get_exception_type_initialization (const gchar *type_name, MonoException *i
  * mono_get_exception_synchronization_lock:
  * @inner: the inner exception.
  *
- * Returns: a new instance of the System.TypeInitializationException
+ * Returns: a new instance of the System.SynchronizationLockException
  */
 MonoException *
 mono_get_exception_synchronization_lock (const char *msg)
@@ -594,6 +640,22 @@ mono_get_exception_bad_image_format (const char *msg)
        return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
 }      
 
+/**
+ * mono_get_exception_bad_image_format2:
+ * @msg: an informative message for the user.
+ * @fname: The full name of the file with the invalid image.
+ *
+ * Returns: a new instance of the System.BadImageFormatException
+ */
+MonoException *
+mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
+{
+       MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;
+
+       return mono_exception_from_name_two_strings (
+               mono_get_corlib (), "System", "BadImageFormatException", s, fname);
+}
+
 /**
  * mono_get_exception_stack_overflow:
  *
@@ -605,6 +667,63 @@ mono_get_exception_stack_overflow (void)
        return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");       
 }
 
+/**
+ * mono_get_exception_out_of_memory:
+ *
+ * Returns: a new instance of the System.OutOfMemoryException
+ */
+MonoException *
+mono_get_exception_out_of_memory (void)
+{
+       return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
+}
+
+/**
+ * mono_get_exception_field_access:
+ *
+ * Returns: a new instance of the System.FieldAccessException
+ */
+MonoException *
+mono_get_exception_field_access (void)
+{
+       return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
+}
+
+/**
+ * mono_get_exception_field_access2:
+ * @msg: an informative message for the user.
+ *
+ * Returns: a new instance of the System.FieldAccessException
+ */
+MonoException *
+mono_get_exception_field_access_msg (const char *msg)
+{
+       return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
+}
+
+/**
+ * mono_get_exception_method_access:
+ *
+ * Returns: a new instance of the System.MethodAccessException
+ */
+MonoException *
+mono_get_exception_method_access (void)
+{
+       return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
+}
+
+/**
+ * mono_get_exception_method_access2:
+ * @msg: an informative message for the user.
+ *
+ * Returns: a new instance of the System.MethodAccessException
+ */
+MonoException *
+mono_get_exception_method_access_msg (const char *msg)
+{
+       return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
+}
+
 /**
  * mono_get_exception_reflection_type_load:
  * @types: an array of types that were defined in the moduled loaded.
@@ -635,3 +754,65 @@ mono_get_exception_reflection_type_load (MonoArray *types, MonoArray *exceptions
 
        return (MonoException *) exc;
 }
+
+MonoException *
+mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
+{
+       MonoRuntimeWrappedException *ex = (MonoRuntimeWrappedException*)
+               mono_exception_from_name (mono_get_corlib (), "System.Runtime.CompilerServices",
+                                                                 "RuntimeWrappedException");
+
+   MONO_OBJECT_SETREF (ex, wrapped_exception, wrapped_exception);
+   return (MonoException*)ex;
+}      
+
+char *
+mono_exception_get_native_backtrace (MonoException *exc)
+{
+#ifdef HAVE_BACKTRACE_SYMBOLS
+       MonoDomain *domain;
+       MonoArray *arr = exc->native_trace_ips;
+       int i, len;
+       GString *text;
+       char **messages;
+
+       if (!arr)
+               return g_strdup ("");
+       domain = mono_domain_get ();
+       len = mono_array_length (arr);
+       text = g_string_new_len (NULL, len * 20);
+       messages = backtrace_symbols (mono_array_addr (arr, gpointer, 0), len);
+
+
+       for (i = 0; i < len; ++i) {
+               gpointer ip = mono_array_get (arr, gpointer, i);
+               MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), ip);
+               if (ji) {
+                       char *msg = mono_debug_print_stack_frame (ji->method, (char*)ip - (char*)ji->code_start, domain);
+                       g_string_append_printf (text, "%s\n", msg);
+                       g_free (msg);
+               } else {
+                       g_string_append_printf (text, "%s\n", messages [i]);
+               }
+       }
+
+       free (messages);
+       return g_string_free (text, FALSE);
+#else
+       return g_strdup ("");
+#endif
+}
+
+MonoString *
+ves_icall_Mono_Runtime_GetNativeStackTrace (MonoException *exc)
+{
+       char *trace;
+       MonoString *res;
+       if (!exc)
+               mono_raise_exception (mono_get_exception_argument_null ("exception"));
+
+       trace = mono_exception_get_native_backtrace (exc);
+       res = mono_string_new (mono_domain_get (), trace);
+       g_free (trace);
+       return res;
+}