2008-08-18 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / trace.c
index 49c9d86d636b3c80e2dfd6d6b79d912375908b08..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>
@@ -33,10 +35,10 @@ mono_trace_eval (MonoMethod *method)
                case MONO_TRACEOP_ALL:
                        inc = 1; break;
                case MONO_TRACEOP_PROGRAM:
-                       if (method->klass->image == mono_assembly_get_image (trace_spec.assembly))
+                       if (trace_spec.assembly && (method->klass->image == mono_assembly_get_image (trace_spec.assembly)))
                                inc = 1; break;
                case MONO_TRACEOP_METHOD:
-                       if (mono_method_desc_match ((MonoMethodDesc *) op->data, method))
+                       if (mono_method_desc_full_match ((MonoMethodDesc *) op->data, method))
                                inc = 1; break;
                case MONO_TRACEOP_CLASS:
                        if (strcmp (method->klass->name_space, op->data) == 0)
@@ -65,7 +67,9 @@ static int is_filenamechar (char p)
                return TRUE;
        if (p >= 'a' && p <= 'z')
                return TRUE;
-       if (p == '.' || p == ':')
+       if (p >= '0' && p <= '9')
+               return TRUE;
+       if (p == '.' || p == ':' || p == '_' || p == '-')
                return TRUE;
        return FALSE;
 }
@@ -94,6 +98,7 @@ enum Token {
        TOKEN_NAMESPACE,
        TOKEN_STRING,
        TOKEN_EXCLUDE,
+       TOKEN_DISABLED,
        TOKEN_SEPARATOR,
        TOKEN_END,
        TOKEN_ERROR
@@ -102,42 +107,45 @@ enum Token {
 static int
 get_token (void)
 {
-       while (*input != 0){
-               if (input [0] == 'M' && input [1] == ':'){
-                       input += 2;
-                       get_string ();
-                       return TOKEN_METHOD;
-               }
-               if (input [0] == 'N' && input [1] == ':'){
-                       input += 2;
-                       get_string ();
-                       return TOKEN_NAMESPACE;
-               }
-               if (input [0] == 'T' && input [1] == ':'){
-                       input += 2;
-                       get_string ();
-                       return TOKEN_CLASS;
-               }
-               if (is_filenamechar (*input)){
-                       get_string ();
-                       if (strcmp (value, "all") == 0)
-                               return TOKEN_ALL;
-                       if (strcmp (value, "program") == 0)
-                               return TOKEN_PROGRAM;
-                       return TOKEN_STRING;
-               }
-               if (*input == '-'){
-                       input++;
-                       return TOKEN_EXCLUDE;
-               }
-               if (*input == ','){
-                       input++;
-                       return TOKEN_SEPARATOR;
-               }
+       if (input [0] == '\0') {
+               return TOKEN_END;
+       }
+       if (input [0] == 'M' && input [1] == ':'){
+               input += 2;
+               get_string ();
+               return TOKEN_METHOD;
+       }
+       if (input [0] == 'N' && input [1] == ':'){
+               input += 2;
+               get_string ();
+               return TOKEN_NAMESPACE;
+       }
+       if (input [0] == 'T' && input [1] == ':'){
+               input += 2;
+               get_string ();
+               return TOKEN_CLASS;
+       }
+       if (is_filenamechar (*input)){
+               get_string ();
+               if (strcmp (value, "all") == 0)
+                       return TOKEN_ALL;
+               if (strcmp (value, "program") == 0)
+                       return TOKEN_PROGRAM;
+               if (strcmp (value, "disabled") == 0)
+                       return TOKEN_DISABLED;
+               return TOKEN_STRING;
+       }
+       if (*input == '-'){
                input++;
-                       
+               return TOKEN_EXCLUDE;
        }
-       return TOKEN_END;
+       if (*input == ','){
+               input++;
+               return TOKEN_SEPARATOR;
+       }
+
+       fprintf (stderr, "Syntax error at or around '%s'\n", input);    
+       return TOKEN_ERROR;
 }
 
 static void
@@ -182,15 +190,22 @@ get_spec (int *last)
                trace_spec.ops [*last].data = g_strdup (value);
        } else if (token == TOKEN_CLASS){
                char *p = strrchr (value, '.');
-               *p++ = 0;
+               if (p) {
+                       *p++ = 0;
+                       trace_spec.ops [*last].data = g_strdup (value);
+                       trace_spec.ops [*last].data2 = g_strdup (p);
+               }
+               else {
+                       trace_spec.ops [*last].data = g_strdup ("");
+                       trace_spec.ops [*last].data2 = g_strdup (value);
+               }
                trace_spec.ops [*last].op = MONO_TRACEOP_CLASS;
-               trace_spec.ops [*last].data = g_strdup (value);
-               trace_spec.ops [*last].data2 = g_strdup (p);
        } 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;
        }
@@ -199,15 +214,14 @@ get_spec (int *last)
 }
 
 MonoTraceSpec *
-mono_trace_parse_options (MonoAssembly *assembly, 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.assembly = assembly;
-       
+
+       trace_spec.enabled = TRUE;
        if (*p == 0){
                trace_spec.len = 1;
                trace_spec.ops = g_new0 (MonoTraceOperation, 1);
@@ -215,13 +229,13 @@ mono_trace_parse_options (MonoAssembly *assembly, 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){
@@ -235,6 +249,12 @@ mono_trace_parse_options (MonoAssembly *assembly, char *options)
        return &trace_spec;
 }
 
+void
+mono_trace_set_assembly (MonoAssembly *assembly)
+{
+       trace_spec.assembly = assembly;
+}
+
 static int indent_level = 0;
 
 static void indent (int diff) {
@@ -242,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)
@@ -261,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);
@@ -273,23 +310,20 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                printf (") ip: %p\n", __builtin_return_address (1));
                return;
        }       
-       if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
-               g_error ("unaligned stack detected (%p)", ebp);
-       }
 
-       sig = method->signature;
+       sig = mono_method_signature (method);
 
        arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
 
        mono_arch_get_argument_info (sig, sig->param_count, arg_info);
 
-       if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
-               g_assert (!method->signature->ret->byref);
+       if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret)) {
+               g_assert (!mono_method_signature (method)->ret->byref);
 
                printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
        }
 
-       if (method->signature->hasthis) {
+       if (mono_method_signature (method)->hasthis) {
                gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
                if (method->klass->valuetype) {
                        printf ("value:%p, ", *this);
@@ -300,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);
                                }
@@ -309,19 +347,19 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                }
        }
 
-       for (i = 0; i < method->signature->param_count; ++i) {
+       for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
                gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
                int size = arg_info [i + 1].size;
 
-               MonoType *type = method->signature->params [i];
+               MonoType *type = mono_method_signature (method)->params [i];
                
                if (type->byref) {
                        printf ("[BYREF:%p], ", *cpos); 
-               } else switch (type->type) {
+               } else switch (mono_type_get_underlying_type (type)->type) {
                        
                case MONO_TYPE_I:
                case MONO_TYPE_U:
-                       printf ("%p, ", (gpointer)*((int *)(cpos)));
+                       printf ("%p, ", *((gpointer **)(cpos)));
                        break;
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_CHAR:
@@ -340,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;
@@ -353,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 {
@@ -371,7 +419,7 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                        break;
                case MONO_TYPE_I8:
                case MONO_TYPE_U8:
-                       printf ("0x%016llx, ", *((gint64 *)(cpos)));
+                       printf ("0x%016llx, ", (long long)*((gint64 *)(cpos)));
                        break;
                case MONO_TYPE_R4:
                        printf ("%f, ", *((float *)(cpos)));
@@ -391,6 +439,7 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
        }
 
        printf (")\n");
+       fflush (stdout);
 }
 
 void
@@ -400,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);
@@ -410,7 +459,7 @@ mono_trace_leave_method (MonoMethod *method, ...)
        printf ("LEAVE: %s", fname);
        g_free (fname);
 
-       type = method->signature->ret;
+       type = mono_method_signature (method)->ret;
 
 handle_enum:
        switch (type->type) {
@@ -442,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;
@@ -458,7 +511,7 @@ handle_enum:
                        } else if  (o->vtable->klass == mono_defaults.int32_class) {
                                printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));    
                        } else if  (o->vtable->klass == mono_defaults.int64_class) {
-                               printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));  
+                               printf ("[INT64:%p:%lld]", o, (long long)*((gint64 *)((char *)o + sizeof (MonoObject))));       
                        } else
                                printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
                } else
@@ -476,12 +529,12 @@ handle_enum:
        }
        case MONO_TYPE_I8: {
                gint64 l =  va_arg (ap, gint64);
-               printf ("lresult=0x%16llx", l);
+               printf ("lresult=0x%16llx", (long long)l);
                break;
        }
        case MONO_TYPE_U8: {
                gint64 l =  va_arg (ap, gint64);
-               printf ("lresult=0x%16llx", l);
+               printf ("lresult=0x%16llx", (long long)l);
                break;
        }
        case MONO_TYPE_R4:
@@ -505,9 +558,23 @@ handle_enum:
                }
                break;
        default:
-               printf ("(unknown return type %x)", method->signature->ret->type);
+               printf ("(unknown return type %x)", mono_method_signature (method)->ret->type);
        }
 
        //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;
+}
+