In man:
[mono.git] / mono / mini / trace.c
index a3366b226beb350b6beeb0e05257448d8cb1eee6..6348a434fb22abb8e9ec44cdf7038c126271dbb9 100644 (file)
 
 #include <config.h>
 #include <signal.h>
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <string.h>
 #include "mini.h"
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/assembly.h>
+#include <mono/utils/mono-time.h>
 #include "trace.h"
 
 static MonoTraceSpec trace_spec;
 
+gboolean
+mono_trace_eval_exception (MonoClass *klass)
+{
+       int include = 0;
+       int i;
+
+       if (!klass)
+               return FALSE;
+
+       for (i = 0; i < trace_spec.len; i++) {
+               MonoTraceOperation *op = &trace_spec.ops [i];
+               int inc = 0;
+               
+               switch (op->op){
+               case MONO_TRACEOP_EXCEPTION:
+                       if (strcmp ("", op->data) == 0 && strcmp ("all", op->data2) == 0)
+                               inc = 1;
+                       else if (strcmp ("", op->data) == 0 || strcmp (klass->name_space, op->data) == 0)
+                               if (strcmp (klass->name, op->data2) == 0)
+                                       inc = 1;
+                       break;
+               default:
+                       break;
+               }
+               if (op->exclude){
+                       if (inc)
+                               include = 0;
+               } else if (inc)
+                       include = 1;
+       }
+
+       return include;
+}
+
 gboolean
 mono_trace_eval (MonoMethod *method)
 {
@@ -49,6 +89,8 @@ mono_trace_eval (MonoMethod *method)
                case MONO_TRACEOP_NAMESPACE:
                        if (strcmp (method->klass->name_space, op->data) == 0)
                                inc = 1;
+               case MONO_TRACEOP_EXCEPTION:
+                       break;
                }
                if (op->exclude){
                        if (inc)
@@ -67,7 +109,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;
 }
@@ -93,6 +135,7 @@ enum Token {
        TOKEN_CLASS,
        TOKEN_ALL,
        TOKEN_PROGRAM,
+       TOKEN_EXCEPTION,
        TOKEN_NAMESPACE,
        TOKEN_STRING,
        TOKEN_EXCLUDE,
@@ -105,6 +148,9 @@ enum Token {
 static int
 get_token (void)
 {
+       while (input [0] == '+')
+               input++;
+
        if (input [0] == '\0') {
                return TOKEN_END;
        }
@@ -123,6 +169,15 @@ get_token (void)
                get_string ();
                return TOKEN_CLASS;
        }
+       if (input [0] == 'E' && input [1] == ':'){
+               input += 2;
+               get_string ();
+               return TOKEN_EXCEPTION;
+       }
+       if (*input == '-'){
+               input++;
+               return TOKEN_EXCLUDE;
+       }
        if (is_filenamechar (*input)){
                get_string ();
                if (strcmp (value, "all") == 0)
@@ -133,10 +188,6 @@ get_token (void)
                        return TOKEN_DISABLED;
                return TOKEN_STRING;
        }
-       if (*input == '-'){
-               input++;
-               return TOKEN_EXCLUDE;
-       }
        if (*input == ','){
                input++;
                return TOKEN_SEPARATOR;
@@ -186,7 +237,7 @@ get_spec (int *last)
        else if (token == TOKEN_NAMESPACE){
                trace_spec.ops [*last].op = MONO_TRACEOP_NAMESPACE;
                trace_spec.ops [*last].data = g_strdup (value);
-       } else if (token == TOKEN_CLASS){
+       } else if (token == TOKEN_CLASS || token == TOKEN_EXCEPTION){
                char *p = strrchr (value, '.');
                if (p) {
                        *p++ = 0;
@@ -197,7 +248,7 @@ get_spec (int *last)
                        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].op = token == TOKEN_CLASS ? MONO_TRACEOP_CLASS : MONO_TRACEOP_EXCEPTION;
        } else if (token == TOKEN_STRING){
                trace_spec.ops [*last].op = MONO_TRACEOP_ASSEMBLY;
                trace_spec.ops [*last].data = g_strdup (value);
@@ -212,9 +263,9 @@ 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;
@@ -227,13 +278,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){
@@ -253,16 +304,27 @@ mono_trace_set_assembly (MonoAssembly *assembly)
        trace_spec.assembly = assembly;
 }
 
-static int indent_level = 0;
+static
+#ifdef HAVE_KW_THREAD
+__thread 
+#endif
+int indent_level = 0;
+static guint64 start_time = 0;
+
+static double seconds_since_start (void)
+{
+       guint64 diff = mono_100ns_ticks () - start_time;
+       return diff/10000000.0;
+}
 
 static void indent (int diff) {
        int v;
        if (diff < 0)
                indent_level += diff;
        v = indent_level;
-       while (v-- > 0) {
-               printf (". ");
-       }
+       if (start_time == 0)
+               start_time = mono_100ns_ticks ();
+       printf ("[%p: %.5f %d] ", (void*)GetCurrentThreadId (), seconds_since_start (), indent_level);
        if (diff > 0)
                indent_level += diff;
 }
@@ -288,6 +350,22 @@ string_to_utf8 (MonoString *s)
                return as;
 }
 
+/*
+ * cpos (ebp + arg_info[n].offset) points to the beginning of the
+ * stack slot for this argument.  On little-endian systems, we can
+ * simply dereference it. On big-endian systems, we need to adjust
+ * cpos upward first if the datatype we're referencing is smaller than
+ * a stack slot. Also - one can't assume that gpointer is also the
+ * size of a stack slot - use SIZEOF_REGISTER instead. The following
+ * helper macro tries to keep down the mess of all the pointer
+ * calculations.
+ */
+#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
+#define arg_in_stack_slot(cpos, type) ((type *)(cpos))
+#else
+#define arg_in_stack_slot(cpos, type) ((type *)((sizeof(type) < SIZEOF_REGISTER) ? (((gssize)(cpos)) + SIZEOF_REGISTER - sizeof(type)) : (gssize)(cpos)))
+#endif
+
 void
 mono_trace_enter_method (MonoMethod *method, char *ebp)
 {
@@ -310,9 +388,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);
 
@@ -329,9 +404,9 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
        if (mono_method_signature (method)->hasthis) {
                gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
                if (method->klass->valuetype) {
-                       printf ("value:%p, ", *this);
+                       printf ("value:%p, ", *arg_in_stack_slot(this, gpointer *));
                } else {
-                       o = *((MonoObject **)this);
+                       o = *arg_in_stack_slot(this, MonoObject *);
 
                        if (o) {
                                class = o->vtable->klass;
@@ -357,29 +432,29 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                MonoType *type = mono_method_signature (method)->params [i];
                
                if (type->byref) {
-                       printf ("[BYREF:%p], ", *cpos); 
+                       printf ("[BYREF:%p], ", *arg_in_stack_slot(cpos, gpointer *));
                } else switch (mono_type_get_underlying_type (type)->type) {
                        
                case MONO_TYPE_I:
                case MONO_TYPE_U:
-                       printf ("%p, ", *((gpointer **)(cpos)));
+                       printf ("%p, ", *arg_in_stack_slot(cpos, gpointer *));
                        break;
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_CHAR:
                case MONO_TYPE_I1:
                case MONO_TYPE_U1:
-                       printf ("%d, ", *((gint8 *)(cpos)));
+                       printf ("%d, ", *arg_in_stack_slot(cpos, gint8));
                        break;
                case MONO_TYPE_I2:
                case MONO_TYPE_U2:
-                       printf ("%d, ", *((gint16 *)(cpos)));
+                       printf ("%d, ", *arg_in_stack_slot(cpos, gint16));
                        break;
                case MONO_TYPE_I4:
                case MONO_TYPE_U4:
-                       printf ("%d, ", *((int *)(cpos)));
+                       printf ("%d, ", *arg_in_stack_slot(cpos, int));
                        break;
                case MONO_TYPE_STRING: {
-                       MonoString *s = *((MonoString **)cpos);
+                       MonoString *s = *arg_in_stack_slot(cpos, MonoString *);
                        if (s) {
                                char *as;
 
@@ -394,7 +469,7 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                }
                case MONO_TYPE_CLASS:
                case MONO_TYPE_OBJECT: {
-                       o = *((MonoObject **)cpos);
+                       o = *arg_in_stack_slot(cpos, MonoObject *);
                        if (o) {
                                class = o->vtable->klass;
                    
@@ -405,10 +480,12 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                                        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 {
-                               printf ("%p, ", *((gpointer *)(cpos)));                         
+                               printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
                        }
                        break;
                }
@@ -416,17 +493,17 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                case MONO_TYPE_FNPTR:
                case MONO_TYPE_ARRAY:
                case MONO_TYPE_SZARRAY:
-                       printf ("%p, ", *((gpointer *)(cpos)));
+                       printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
                        break;
                case MONO_TYPE_I8:
                case MONO_TYPE_U8:
-                       printf ("0x%016llx, ", (long long)*((gint64 *)(cpos)));
+                       printf ("0x%016llx, ", (long long)*arg_in_stack_slot(cpos, gint64));
                        break;
                case MONO_TYPE_R4:
-                       printf ("%f, ", *((float *)(cpos)));
+                       printf ("%f, ", *arg_in_stack_slot(cpos, float));
                        break;
                case MONO_TYPE_R8:
-                       printf ("%f, ", *((double *)(cpos)));
+                       printf ("%f, ", *arg_in_stack_slot(cpos, double));
                        break;
                case MONO_TYPE_VALUETYPE: 
                        printf ("[");
@@ -546,7 +623,7 @@ handle_enum:
        }
        case MONO_TYPE_VALUETYPE: 
                if (type->data.klass->enumtype) {
-                       type = type->data.klass->enum_basetype;
+                       type = mono_class_enum_basetype (type->data.klass);
                        goto handle_enum;
                } else {
                        guint8 *p = va_arg (ap, gpointer);
@@ -578,4 +655,3 @@ mono_trace_is_enabled ()
 {
        return trace_spec.enabled;
 }
-