2008-08-18 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / trace.c
index 2af8363c6c7bc9f81ebb70905f1ec8b97f483edf..c6e4e82bc090379e7b9a2cb4421a7ecad19fc3fa 100644 (file)
@@ -10,7 +10,9 @@
 
 #include <config.h>
 #include <signal.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <string.h>
 #include "mini.h"
 #include <mono/metadata/debug-helpers.h>
@@ -67,7 +69,7 @@ static int is_filenamechar (char p)
                return TRUE;
        if (p >= '0' && p <= '9')
                return TRUE;
-       if (p == '.' || p == ':' || p == '_')
+       if (p == '.' || p == ':' || p == '_' || p == '-')
                return TRUE;
        return FALSE;
 }
@@ -96,6 +98,7 @@ enum Token {
        TOKEN_NAMESPACE,
        TOKEN_STRING,
        TOKEN_EXCLUDE,
+       TOKEN_DISABLED,
        TOKEN_SEPARATOR,
        TOKEN_END,
        TOKEN_ERROR
@@ -128,6 +131,8 @@ get_token (void)
                        return TOKEN_ALL;
                if (strcmp (value, "program") == 0)
                        return TOKEN_PROGRAM;
+               if (strcmp (value, "disabled") == 0)
+                       return TOKEN_DISABLED;
                return TOKEN_STRING;
        }
        if (*input == '-'){
@@ -198,8 +203,9 @@ get_spec (int *last)
        } else if (token == TOKEN_STRING){
                trace_spec.ops [*last].op = MONO_TRACEOP_ASSEMBLY;
                trace_spec.ops [*last].data = g_strdup (value);
-       }
-       else {
+       } else if (token == TOKEN_DISABLED) {
+               trace_spec.enabled = FALSE;
+       } else {
                fprintf (stderr, "Syntax error in trace option specification\n");
                return TOKEN_ERROR;
        }
@@ -208,13 +214,14 @@ get_spec (int *last)
 }
 
 MonoTraceSpec *
-mono_trace_parse_options (char *options)
+mono_trace_parse_options (const char *options)
 {
-       char *p = options;
+       char *p = (char*)options;
        int size = 1;
        int last_used;
        int token;
-       
+
+       trace_spec.enabled = TRUE;
        if (*p == 0){
                trace_spec.len = 1;
                trace_spec.ops = g_new0 (MonoTraceOperation, 1);
@@ -222,13 +229,13 @@ mono_trace_parse_options (char *options)
                return &trace_spec;
        }
                
-       for (p = options; *p != 0; p++)
+       for (p = (char*)options; *p != 0; p++)
                if (*p == ',')
                        size++;
        
        trace_spec.ops = g_new0 (MonoTraceOperation, size);
 
-       input = options;
+       input = (char*)options;
        last_used = 0;
        
        while ((token = (get_spec (&last_used))) != TOKEN_END){
@@ -255,14 +262,31 @@ static void indent (int diff) {
        if (diff < 0)
                indent_level += diff;
        v = indent_level;
-       while (v-- > 0) {
-               printf (". ");
-       }
+       printf ("[%d] ", indent_level);
        if (diff > 0)
                indent_level += diff;
 }
 
-static gboolean enable_trace = TRUE;
+static char *
+string_to_utf8 (MonoString *s)
+{
+       char *as;
+       GError *error = NULL;
+
+       g_assert (s);
+
+       if (!s->length)
+               return g_strdup ("");
+
+       as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, NULL, &error);
+       if (error) {
+               /* Happens with StringBuilders */
+               g_error_free (error);
+               return g_strdup ("<INVALID UTF8>");
+       }
+       else
+               return as;
+}
 
 void
 mono_trace_enter_method (MonoMethod *method, char *ebp)
@@ -274,7 +298,7 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
        MonoMethodSignature *sig;
        char *fname;
 
-       if (!enable_trace)
+       if (!trace_spec.enabled)
                return;
 
        fname = mono_method_full_name (method, TRUE);
@@ -286,9 +310,6 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                printf (") ip: %p\n", __builtin_return_address (1));
                return;
        }       
-       if ((GPOINTER_TO_INT (ebp) & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
-               g_error ("unaligned stack detected (%p)", ebp);
-       }
 
        sig = mono_method_signature (method);
 
@@ -313,7 +334,11 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                                class = o->vtable->klass;
 
                                if (class == mono_defaults.string_class) {
-                                       printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
+                                       MonoString *s = (MonoString*)o;
+                                       char *as = string_to_utf8 (s);
+
+                                       printf ("this:[STRING:%p:%s], ", o, as);
+                                       g_free (as);
                                } else {
                                        printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
                                }
@@ -353,8 +378,13 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                case MONO_TYPE_STRING: {
                        MonoString *s = *((MonoString **)cpos);
                        if (s) {
+                               char *as;
+
                                g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
-                               printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
+                               as = string_to_utf8 (s);
+
+                               printf ("[STRING:%p:%s], ", s, as);
+                               g_free (as);
                        } else 
                                printf ("[STRING:null], ");
                        break;
@@ -366,9 +396,14 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                                class = o->vtable->klass;
                    
                                if (class == mono_defaults.string_class) {
-                                       printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
+                                       char *as = string_to_utf8 ((MonoString*)o);
+
+                                       printf ("[STRING:%p:%s], ", o, as);
+                                       g_free (as);
                                } else if (class == mono_defaults.int32_class) {
                                        printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
+                               } else if (class == mono_defaults.monotype_class) {
+                                       printf ("[TYPE:%s], ", mono_type_full_name (((MonoReflectionType*)o)->type));
                                } else
                                        printf ("[%s.%s:%p], ", class->name_space, class->name, o);
                        } else {
@@ -404,6 +439,7 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
        }
 
        printf (")\n");
+       fflush (stdout);
 }
 
 void
@@ -413,7 +449,7 @@ mono_trace_leave_method (MonoMethod *method, ...)
        char *fname;
        va_list ap;
 
-       if (!enable_trace)
+       if (!trace_spec.enabled)
                return;
 
        va_start(ap, method);
@@ -455,8 +491,12 @@ handle_enum:
                MonoString *s = va_arg (ap, MonoString *);
 ;
                if (s) {
+                       char *as;
+
                        g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
-                       printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
+                       as = string_to_utf8 (s);
+                       printf ("[STRING:%p:%s]", s, as);
+                       g_free (as);
                } else 
                        printf ("[STRING:null], ");
                break;
@@ -523,4 +563,18 @@ handle_enum:
 
        //printf (" ip: %p\n", __builtin_return_address (1));
        printf ("\n");
+       fflush (stdout);
+}
+
+void
+mono_trace_enable (gboolean enable)
+{
+       trace_spec.enabled = enable;
+}
+
+gboolean
+mono_trace_is_enabled ()
+{
+       return trace_spec.enabled;
 }
+