Switch exception reporting to use utils/mono-logger-internal.h.
authorRodrigo Kumpera <kumpera@gmail.com>
Mon, 19 Nov 2012 22:31:55 +0000 (17:31 -0500)
committerRodrigo Kumpera <kumpera@gmail.com>
Mon, 19 Nov 2012 22:34:56 +0000 (17:34 -0500)
mono/mini/mini-exceptions.c

index e16a66d34498f1f9253bf97ec0c5b9837fb0ec17..86dc4340937bcc24395c7e12137ba5f0cbe82edc 100644 (file)
@@ -47,6 +47,7 @@
 #include <mono/metadata/mono-endian.h>
 #include <mono/metadata/environment.h>
 #include <mono/utils/mono-mmap.h>
+#include <mono/utils/mono-logger-internal.h>
 
 #include "mini.h"
 #include "debug-mini.h"
@@ -1562,7 +1563,7 @@ mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gboolean resume,
                        mono_debugger_agent_handle_exception (obj, ctx, NULL);
 
                        if (mini_get_debug_options ()->suspend_on_unhandled) {
-                               fprintf (stderr, "Unhandled exception, suspending...");
+                               mono_runtime_printf_err ("Unhandled exception, suspending...");
                                while (1)
                                        ;
                        }
@@ -2092,7 +2093,7 @@ mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx,
                        /* We print a message: after this even managed stack overflows
                         * may crash the runtime
                         */
-                       fprintf (stderr, "Stack overflow in unmanaged: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx), fault_addr);
+                       mono_runtime_printf_err ("Stack overflow in unmanaged: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
                        if (!jit_tls->handling_stack_ovf) {
                                jit_tls->restore_stack_prot = restore_stack_protection_tramp;
                                jit_tls->handling_stack_ovf = 1;
@@ -2106,7 +2107,6 @@ mono_handle_soft_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx,
 }
 
 typedef struct {
-       FILE *stream;
        MonoMethod *omethod;
        int count;
 } PrintOverflowUserData;
@@ -2116,7 +2116,6 @@ print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer da
 {
        MonoMethod *method = NULL;
        PrintOverflowUserData *user_data = data;
-       FILE *stream = user_data->stream;
        gchar *location;
 
        if (frame->ji)
@@ -2134,11 +2133,11 @@ print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer da
                        return FALSE;
 
                location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
-               fprintf (stream, "  %s\n", location);
+               mono_runtime_printf_err ("  %s", location);
                g_free (location);
 
                if (user_data->count == 1) {
-                       fprintf (stream, "  <...>\n");
+                       mono_runtime_printf_err ("  <...>");
                        user_data->omethod = method;
                } else {
                        user_data->omethod = NULL;
@@ -2146,7 +2145,7 @@ print_overflow_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer da
 
                user_data->count ++;
        } else
-               fprintf (stream, "  at <unknown> <0x%05x>\n", frame->native_offset);
+               mono_runtime_printf_err ("  at <unknown> <0x%05x>", frame->native_offset);
 
        return FALSE;
 }
@@ -2158,41 +2157,39 @@ mono_handle_hard_stack_ovf (MonoJitTlsData *jit_tls, MonoJitInfo *ji, void *ctx,
        MonoContext mctx;
 
        /* we don't do much now, but we can warn the user with a useful message */
-       fprintf (stderr, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx), fault_addr);
+       mono_runtime_printf_err ("Stack overflow: IP: %p, fault addr: %p", mono_arch_ip_from_context (ctx), fault_addr);
 
 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
        mono_arch_sigctx_to_monoctx (ctx, &mctx);
                        
-       fprintf (stderr, "Stacktrace:\n");
+       mono_runtime_printf_err ("Stacktrace:");
 
        memset (&ud, 0, sizeof (ud));
-       ud.stream = stderr;
 
        mono_walk_stack_with_ctx (print_overflow_stack_frame, &mctx, MONO_UNWIND_LOOKUP_ACTUAL_METHOD, &ud);
 #else
        if (ji && ji->method)
-               fprintf (stderr, "At %s\n", mono_method_full_name (ji->method, TRUE));
+               mono_runtime_printf_err ("At %s", mono_method_full_name (ji->method, TRUE));
        else
-               fprintf (stderr, "At <unmanaged>.\n");
+               mono_runtime_printf_err ("At <unmanaged>.");
 #endif
 
        _exit (1);
 }
 
 static gboolean
-print_stack_frame (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
+print_stack_frame_to_stderr (StackFrameInfo *frame, MonoContext *ctx, gpointer data)
 {
-       FILE *stream = (FILE*)data;
        MonoMethod *method = NULL;
        if (frame->ji)
                method = frame->ji->method;
 
        if (method) {
                gchar *location = mono_debug_print_stack_frame (method, frame->native_offset, mono_domain_get ());
-               fprintf (stream, "  %s\n", location);
+               mono_runtime_printf_err ("  %s", location);
                g_free (location);
        } else
-               fprintf (stream, "  at <unknown> <0x%05x>\n", frame->native_offset);
+               mono_runtime_printf_err ("  at <unknown> <0x%05x>", frame->native_offset);
 
        return FALSE;
 }
@@ -2235,7 +2232,7 @@ mono_handle_native_sigsegv (int signal, void *ctx)
                return;
 
        if (mini_get_debug_options ()->suspend_on_sigsegv) {
-               fprintf (stderr, "Received SIGSEGV, suspending...");
+               mono_runtime_printf_err ("Received SIGSEGV, suspending...");
                while (1)
                        ;
        }
@@ -2245,11 +2242,9 @@ mono_handle_native_sigsegv (int signal, void *ctx)
 
        /* !jit_tls means the thread was not registered with the runtime */
        if (jit_tls && mono_thread_internal_current ()) {
-               fprintf (stderr, "Stacktrace:\n\n");
+               mono_runtime_printf_err ("Stacktrace:\n");
 
-               mono_walk_stack (print_stack_frame, TRUE, stderr);
-
-               fflush (stderr);
+               mono_walk_stack (print_stack_frame_to_stderr, TRUE, NULL);
        }
 
 #ifdef HAVE_BACKTRACE_SYMBOLS
@@ -2259,17 +2254,15 @@ mono_handle_native_sigsegv (int signal, void *ctx)
        int i, size;
        const char *signal_str = (signal == SIGSEGV) ? "SIGSEGV" : "SIGABRT";
 
-       fprintf (stderr, "\nNative stacktrace:\n\n");
+       mono_runtime_printf_err ("\nNative stacktrace:\n");
 
        size = backtrace (array, 256);
        names = backtrace_symbols (array, size);
        for (i =0; i < size; ++i) {
-               fprintf (stderr, "\t%s\n", names [i]);
+               mono_runtime_printf_err ("\t%s", names [i]);
        }
        free (names);
 
-       fflush (stderr);
-
        /* Try to get more meaningful information using gdb */
 
 #if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_fork)
@@ -2293,7 +2286,7 @@ mono_handle_native_sigsegv (int signal, void *ctx)
                        exit (1);
                }
 
-               fprintf (stderr, "\nDebug info from gdb:\n\n");
+               mono_runtime_printf_err ("\nDebug info from gdb:\n");
                waitpid (pid, &status, 0);
        }
 #endif
@@ -2302,14 +2295,14 @@ mono_handle_native_sigsegv (int signal, void *ctx)
         * on anything working. So try to print out lots of diagnostics, starting 
         * with ones which have a greater chance of working.
         */
-       fprintf (stderr,
+       mono_runtime_printf_err (
                         "\n"
                         "=================================================================\n"
                         "Got a %s while executing native code. This usually indicates\n"
                         "a fatal error in the mono runtime or one of the native libraries \n"
                         "used by your application.\n"
-                        "=================================================================\n"
-                        "\n", signal_str);
+                        "=================================================================\n",
+                       signal_str);
 
  }
 #endif
@@ -2366,17 +2359,17 @@ mono_print_thread_dump_internal (void *sigctx, MonoContext *start_ctx)
 
        mono_walk_stack_with_ctx (print_stack_frame_to_string, &ctx, MONO_UNWIND_LOOKUP_ALL, text);
 #else
-       printf ("\t<Stack traces in thread dumps not supported on this platform>\n");
+       mono_runtime_printf ("\t<Stack traces in thread dumps not supported on this platform>");
 #endif
 
-       fprintf (stdout, "%s", text->str);
+       mono_runtime_printf ("%s", text->str);
 
 #if PLATFORM_WIN32 && TARGET_WIN32 && _DEBUG
        OutputDebugStringA(text->str);
 #endif
 
        g_string_free (text, TRUE);
-       fflush (stdout);
+       mono_runtime_stdout_fflush ();
 }
 
 /*
@@ -2666,8 +2659,7 @@ mono_invoke_unhandled_exception_hook (MonoObject *exc)
                MonoString *str = mono_object_to_string (exc, &other);
                if (str) {
                        char *msg = mono_string_to_utf8 (str);
-                       fprintf (stderr, "[ERROR] FATAL UNHANDLED EXCEPTION: %s\n", msg);
-                       fflush (stderr);
+                       mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
                        g_free (msg);
                }