[pedump] Print failure cause message when a class cannot be initialized
[mono.git] / tools / pedump / pedump.c
index 4bbcc699335a9bc8bd75a9e8e5376de774b74580..322185faaf7dfd8049a94c213c41a76be9beb4b7 100644 (file)
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/tokentype.h>
 #include <mono/metadata/appdomain.h>
-#include <mono/metadata/assembly.h>
+#include <mono/metadata/assembly-internals.h>
 #include <mono/metadata/metadata-internals.h>
 #include <mono/metadata/class-internals.h>
 #include <mono/metadata/verify-internals.h>
 #include <mono/metadata/marshal.h>
+#include <mono/metadata/w32handle.h>
 #include "mono/utils/mono-digest.h"
 #include <mono/utils/mono-mmap.h>
 #include <mono/utils/mono-counters.h>
@@ -487,13 +488,21 @@ verify_image_file (const char *fname)
                }
                mono_class_init (klass);
                if (mono_class_has_failure (klass)) {
-                       printf ("Error verifying class(0x%08x) %s.%s a type load error happened\n", token, klass->name_space, klass->name);
+                       MonoError type_load_error;
+                       error_init (&type_load_error);
+                       mono_error_set_for_class_failure (&type_load_error, klass);
+                       printf ("Could not initialize class(0x%08x) %s.%s due to %s\n", token, klass->name_space, klass->name, mono_error_get_message (&type_load_error));
+                       mono_error_cleanup (&type_load_error);
                        ++count;
                }
 
                mono_class_setup_vtable (klass);
                if (mono_class_has_failure (klass)) {
-                       printf ("Error verifying class(0x%08x) %s.%s a type load error happened\n", token, klass->name_space, klass->name);
+                       MonoError type_load_error;
+                       error_init (&type_load_error);
+                       mono_error_set_for_class_failure (&type_load_error, klass);
+                       printf ("Could not initialize vtable of class(0x%08x) %s.%s due to %s\n", token, klass->name_space, klass->name, mono_error_get_message (&type_load_error));
+                       mono_error_cleanup (&type_load_error);
                        ++count;
                }
        }
@@ -523,7 +532,7 @@ try_load_from (MonoAssembly **assembly, const gchar *path1, const gchar *path2,
        *assembly = NULL;
        fullpath = g_build_filename (path1, path2, path3, path4, NULL);
        if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR))
-               *assembly = mono_assembly_open_full (fullpath, NULL, refonly);
+               *assembly = mono_assembly_open_predicate (fullpath, refonly, FALSE, NULL, NULL, NULL);
 
        g_free (fullpath);
        return (*assembly != NULL);
@@ -616,6 +625,11 @@ pedump_assembly_search_hook (MonoAssemblyName *aname, gpointer user_data)
        return NULL;
 }
 
+static void
+thread_state_init (MonoThreadUnwindState *ctx)
+{
+}
+
 #define VALID_ONLY_FLAG 0x08000000
 #define VERIFY_CODE_ONLY MONO_VERIFY_ALL + 1 
 #define VERIFY_METADATA_ONLY VERIFY_CODE_ONLY + 1
@@ -632,6 +646,7 @@ main (int argc, char *argv [])
        const char *flag_desc [] = {"error", "warn", "cls", "all", "code", "fail-on-verifiable", "non-strict", "valid-only", "metadata", "partial-md", NULL};
        guint flag_vals [] = {MONO_VERIFY_ERROR, MONO_VERIFY_WARNING, MONO_VERIFY_CLS, MONO_VERIFY_ALL, VERIFY_CODE_ONLY, MONO_VERIFY_FAIL_FAST, MONO_VERIFY_NON_STRICT, VALID_ONLY_FLAG, VERIFY_METADATA_ONLY, VERIFY_PARTIAL_METADATA, 0};
        int i, verify_flags = MONO_VERIFY_REPORT_ALL_ERRORS, run_new_metadata_verifier = 0;
+       MonoThreadInfoRuntimeCallbacks ticallbacks;
        
        for (i = 1; i < argc; i++){
                if (argv [i][0] != '-'){
@@ -658,11 +673,20 @@ main (int argc, char *argv [])
        mono_perfcounters_init ();
 #endif
        mono_counters_init ();
+       mono_tls_init_runtime_keys ();
+       memset (&ticallbacks, 0, sizeof (ticallbacks));
+       ticallbacks.thread_state_init = thread_state_init;
+#ifndef HOST_WIN32
+       mono_w32handle_init ();
+#endif
+       mono_thread_info_runtime_init (&ticallbacks);
+
        mono_metadata_init ();
        mono_images_init ();
        mono_assemblies_init ();
        mono_loader_init ();
  
+
        if (verify_pe) {
                char *tok = strtok (flags, ",");
 
@@ -701,7 +725,7 @@ main (int argc, char *argv [])
        if (verify_pe || run_new_metadata_verifier) {
                run_new_metadata_verifier = 1;
        }
-       
+
        if (run_new_metadata_verifier) {
                mono_verifier_set_mode (verifier_mode);
 
@@ -726,7 +750,7 @@ main (int argc, char *argv [])
 
                mono_verifier_set_mode (verifier_mode);
 
-               assembly = mono_assembly_open (file, NULL);
+               assembly = mono_assembly_open_predicate (file, FALSE, FALSE, NULL, NULL, NULL);
                /*fake an assembly for netmodules so the verifier works*/
                if (!assembly && (image = mono_image_open (file, &status)) && image->tables [MONO_TABLE_ASSEMBLY].rows == 0) {
                        assembly = g_new0 (MonoAssembly, 1);