[profiler] log profiler: limit method instrumentation to selected methods (#5517)
[mono.git] / mono / mini / trace.c
index eeaa2c25b61ef884b4ac552073bc01a3e4b3aae7..0d6b19e857eb2c75b717b1d90e0ca8126c19a81f 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * trace.c: Tracing facilities for the Mono Runtime.
+/**
+ * \file
+ * Tracing facilities for the Mono Runtime.
  *
  * Author:
  *   Paolo Molaro (lupus@ximian.com)
@@ -7,10 +8,10 @@
  *
  * (C) 2002 Ximian, Inc.
  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include <config.h>
-#include <signal.h>
 #ifdef HAVE_ALLOCA_H
 #include <alloca.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 <mono/utils/mono-memory-model.h>
 #include "trace.h"
+#include <mono/metadata/callspec.h>
 
-#if defined (PLATFORM_ANDROID) || (defined (TARGET_IOS) && defined (TARGET_IOS))
+#if defined (HOST_ANDROID) || (defined (TARGET_IOS) && defined (TARGET_IOS))
 #  undef printf
 #  define printf(...) g_log("mono", G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
 #  undef fprintf
 #  define fprintf(__ignore, ...) g_log ("mono-gc", G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
 #endif
 
-#ifdef __GNUC__
-
-#define RETURN_ADDRESS_N(N) (__builtin_extract_return_addr (__builtin_return_address (N)))
-#define RETURN_ADDRESS() RETURN_ADDRESS_N(0)
-
-#elif defined(_MSC_VER)
-
-#include <intrin.h>
-#pragma intrinsic(_ReturnAddress)
-
-#define RETURN_ADDRESS() _ReturnAddress()
-#define RETURN_ADDRESS_N(N) NULL
-
-#else
-
-#error "Missing return address intrinsics implementation"
-
-#endif
-
-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;
-       }
+static MonoCallSpec trace_spec;
 
-       return include;
-}
+static volatile gint32 output_lock = 0;
 
-gboolean
-mono_trace_eval (MonoMethod *method)
+gboolean mono_trace_eval_exception (MonoClass *klass)
 {
-       int include = 0;
-       int i;
-
-       for (i = 0; i < trace_spec.len; i++){
-               MonoTraceOperation *op = &trace_spec.ops [i];
-               int inc = 0;
-               
-               switch (op->op){
-               case MONO_TRACEOP_ALL:
-                       inc = 1; break;
-               case MONO_TRACEOP_PROGRAM:
-                       if (trace_spec.assembly && (method->klass->image == mono_assembly_get_image (trace_spec.assembly)))
-                               inc = 1; break;
-               case MONO_TRACEOP_WRAPPER:
-                       if ((method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) ||
-                               (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE))
-                               inc = 1; break;
-               case MONO_TRACEOP_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)
-                               if (strcmp (method->klass->name, op->data2) == 0)
-                                       inc = 1;
-                       break;
-               case MONO_TRACEOP_ASSEMBLY:
-                       if (strcmp (mono_image_get_name (method->klass->image), op->data) == 0)
-                               inc = 1; break;
-               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)
-                               include = 0;
-               } else if (inc)
-                       include = 1;
-       }
-       return include;
+       return mono_callspec_eval_exception (klass, &trace_spec);
 }
 
-static int is_filenamechar (char p)
+gboolean mono_trace_eval (MonoMethod *method)
 {
-       if (p >= 'A' && p <= 'Z')
-               return TRUE;
-       if (p >= 'a' && p <= 'z')
-               return TRUE;
-       if (p >= '0' && p <= '9')
-               return TRUE;
-       if (p == '.' || p == ':' || p == '_' || p == '-' || p == '`')
-               return TRUE;
-       return FALSE;
+       return mono_callspec_eval (method, &trace_spec);
 }
 
-static char *input;
-static char *value;
-
-static void get_string (void)
+MonoCallSpec *mono_trace_set_options (const char *options)
 {
-       char *start = input;
-       while (is_filenamechar (*input)){
-               input++;
+       char *errstr;
+       if (!mono_callspec_parse (options, &trace_spec, &errstr)) {
+               fprintf (stderr, "%s\n", errstr);
+               g_free (errstr);
+               return NULL;
        }
-       if (value != NULL)
-               g_free (value);
-       value = g_malloc (input - start + 1);
-       strncpy (value, start, input-start);
-       value [input-start] = 0;
-}
-
-enum Token {
-       TOKEN_METHOD,
-       TOKEN_CLASS,
-       TOKEN_ALL,
-       TOKEN_PROGRAM,
-       TOKEN_EXCEPTION,
-       TOKEN_NAMESPACE,
-       TOKEN_WRAPPER,
-       TOKEN_STRING,
-       TOKEN_EXCLUDE,
-       TOKEN_DISABLED,
-       TOKEN_SEPARATOR,
-       TOKEN_END,
-       TOKEN_ERROR
-};
-
-static int
-get_token (void)
-{
-       while (input [0] == '+')
-               input++;
 
-       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 (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)
-                       return TOKEN_ALL;
-               if (strcmp (value, "program") == 0)
-                       return TOKEN_PROGRAM;
-               if (strcmp (value, "wrapper") == 0)
-                       return TOKEN_WRAPPER;
-               if (strcmp (value, "disabled") == 0)
-                       return TOKEN_DISABLED;
-               return TOKEN_STRING;
-       }
-       if (*input == ','){
-               input++;
-               return TOKEN_SEPARATOR;
-       }
-
-       fprintf (stderr, "Syntax error at or around '%s'\n", input);    
-       return TOKEN_ERROR;
-}
-
-static void
-cleanup (void)
-{
-       if (value != NULL)
-               g_free (value);
-}
-
-static int
-get_spec (int *last)
-{
-       int token = get_token ();
-       if (token == TOKEN_EXCLUDE){
-               token = get_spec (last);
-               if (token == TOKEN_EXCLUDE){
-                       fprintf (stderr, "Expecting an expression");
-                       return TOKEN_ERROR;
-               }
-               if (token == TOKEN_ERROR)
-                       return token;
-               trace_spec.ops [(*last)-1].exclude = 1;
-               return TOKEN_SEPARATOR;
-       }
-       if (token == TOKEN_END || token == TOKEN_SEPARATOR || token == TOKEN_ERROR)
-               return token;
-       
-       if (token == TOKEN_METHOD){
-               MonoMethodDesc *desc = mono_method_desc_new (value, TRUE);
-               if (desc == NULL){
-                       fprintf (stderr, "Invalid method name: %s\n", value);
-                       return TOKEN_ERROR;
-               }
-               trace_spec.ops [*last].op = MONO_TRACEOP_METHOD;
-               trace_spec.ops [*last].data = desc;
-       } else if (token == TOKEN_ALL)
-               trace_spec.ops [*last].op = MONO_TRACEOP_ALL;
-       else if (token == TOKEN_PROGRAM)
-               trace_spec.ops [*last].op = MONO_TRACEOP_PROGRAM;
-       else if (token == TOKEN_WRAPPER)
-               trace_spec.ops [*last].op = MONO_TRACEOP_WRAPPER;
-       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 || token == TOKEN_EXCEPTION){
-               char *p = strrchr (value, '.');
-               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 = 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);
-       } else if (token == TOKEN_DISABLED) {
-               trace_spec.enabled = FALSE;
-       } else {
-               fprintf (stderr, "Syntax error in trace option specification\n");
-               return TOKEN_ERROR;
-       }
-       (*last)++;
-       return TOKEN_SEPARATOR;
-}
-
-MonoTraceSpec *
-mono_trace_parse_options (const char *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);
-               trace_spec.ops [0].op = MONO_TRACEOP_ALL;
-               return &trace_spec;
-       }
-               
-       for (p = (char*)options; *p != 0; p++)
-               if (*p == ',')
-                       size++;
-       
-       trace_spec.ops = g_new0 (MonoTraceOperation, size);
-
-       input = (char*)options;
-       last_used = 0;
-       
-       while ((token = (get_spec (&last_used))) != TOKEN_END){
-               if (token == TOKEN_ERROR)
-                       return NULL;
-               if (token == TOKEN_SEPARATOR)
-                       continue;
-       }
-       trace_spec.len = last_used;
-       cleanup ();
        return &trace_spec;
 }
 
-void
-mono_trace_set_assembly (MonoAssembly *assembly)
-{
-       trace_spec.assembly = assembly;
-}
-
 static
 #ifdef HAVE_KW_THREAD
 __thread 
@@ -358,7 +77,7 @@ static void indent (int diff) {
                indent_level += diff;
        if (start_time == 0)
                start_time = mono_100ns_ticks ();
-       printf ("[%p: %.5f %d] ", (void*)GetCurrentThreadId (), seconds_since_start (), indent_level);
+       printf ("[%p: %.5f %d] ", (void*)mono_native_thread_id_get (), seconds_since_start (), indent_level);
        if (diff > 0)
                indent_level += diff;
 }
@@ -404,7 +123,7 @@ void
 mono_trace_enter_method (MonoMethod *method, char *ebp)
 {
        int i, j;
-       MonoClass *class;
+       MonoClass *klass;
        MonoObject *o;
        MonoJitArgumentInfo *arg_info;
        MonoMethodSignature *sig;
@@ -414,34 +133,37 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
        if (!trace_spec.enabled)
                return;
 
+       while (output_lock != 0 || InterlockedCompareExchange (&output_lock, 1, 0) != 0)
+               mono_thread_info_yield ();
+
        fname = mono_method_full_name (method, TRUE);
        indent (1);
        printf ("ENTER: %s(", fname);
        g_free (fname);
 
        if (!ebp) {
-               printf (") ip: %p\n", RETURN_ADDRESS_N (1));
-               return;
-       }       
+               printf (") ip: %p\n", MONO_RETURN_ADDRESS_N (1));
+               goto unlock;
+       }
 
        sig = mono_method_signature (method);
 
-       arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
+       arg_info = (MonoJitArgumentInfo *)alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
 
        if (method->is_inflated) {
                /* FIXME: Might be better to pass the ji itself */
-               MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), RETURN_ADDRESS (), NULL);
+               MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (char *)MONO_RETURN_ADDRESS (), NULL);
                if (ji) {
                        gsctx = mono_jit_info_get_generic_sharing_context (ji);
-                       if (gsctx && (gsctx->var_is_vt || gsctx->mvar_is_vt)) {
+                       if (gsctx && gsctx->is_gsharedvt) {
                                /* Needs a ctx to get precise method */
                                printf (") <gsharedvt>\n");
-                               return;
+                               goto unlock;
                        }
                }
        }
 
-       mono_arch_get_argument_info (gsctx, sig, sig->param_count, arg_info);
+       mono_arch_get_argument_info (sig, sig->param_count, arg_info);
 
        if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret)) {
                g_assert (!mono_method_signature (method)->ret->byref);
@@ -450,23 +172,23 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
        }
 
        if (mono_method_signature (method)->hasthis) {
-               gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
+               gpointer *this_obj = (gpointer *)(ebp + arg_info [0].offset);
                if (method->klass->valuetype) {
-                       printf ("value:%p, ", *arg_in_stack_slot(this, gpointer *));
+                       printf ("value:%p, ", *arg_in_stack_slot(this_obj, gpointer *));
                } else {
-                       o = *arg_in_stack_slot(this, MonoObject *);
+                       o = *arg_in_stack_slot(this_obj, MonoObject *);
 
                        if (o) {
-                               class = o->vtable->klass;
+                               klass = o->vtable->klass;
 
-                               if (class == mono_defaults.string_class) {
+                               if (klass == mono_defaults.string_class) {
                                        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);
+                                       printf ("this:%p[%s.%s %s], ", o, klass->name_space, klass->name, o->vtable->domain->friendly_name);
                                }
                        } else 
                                printf ("this:NULL, ");
@@ -481,7 +203,7 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                
                if (type->byref) {
                        printf ("[BYREF:%p], ", *arg_in_stack_slot(cpos, gpointer *));
-               } else switch (mono_type_get_underlying_type (type)->type) {
+               } else switch (mini_get_underlying_type (type)->type) {
                        
                case MONO_TYPE_I:
                case MONO_TYPE_U:
@@ -519,19 +241,19 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
                case MONO_TYPE_OBJECT: {
                        o = *arg_in_stack_slot(cpos, MonoObject *);
                        if (o) {
-                               class = o->vtable->klass;
+                               klass = o->vtable->klass;
                    
-                               if (class == mono_defaults.string_class) {
+                               if (klass == mono_defaults.string_class) {
                                        char *as = string_to_utf8 ((MonoString*)o);
 
                                        printf ("[STRING:%p:%s], ", o, as);
                                        g_free (as);
-                               } else if (class == mono_defaults.int32_class) {
+                               } else if (klass == mono_defaults.int32_class) {
                                        printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
-                               } else if (class == mono_defaults.monotype_class) {
+                               } else if (klass == mono_defaults.runtimetype_class) {
                                        printf ("[TYPE:%s], ", mono_type_full_name (((MonoReflectionType*)o)->type));
                                } else
-                                       printf ("[%s.%s:%p], ", class->name_space, class->name, o);
+                                       printf ("[%s.%s:%p], ", klass->name_space, klass->name, o);
                        } else {
                                printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
                        }
@@ -566,6 +288,9 @@ mono_trace_enter_method (MonoMethod *method, char *ebp)
 
        printf (")\n");
        fflush (stdout);
+
+unlock:
+       mono_atomic_store_release (&output_lock, 0);
 }
 
 void
@@ -579,6 +304,9 @@ mono_trace_leave_method (MonoMethod *method, ...)
        if (!trace_spec.enabled)
                return;
 
+       while (output_lock != 0 || InterlockedCompareExchange (&output_lock, 1, 0) != 0)
+               mono_thread_info_yield ();
+
        va_start(ap, method);
 
        fname = mono_method_full_name (method, TRUE);
@@ -588,20 +316,19 @@ mono_trace_leave_method (MonoMethod *method, ...)
 
        if (method->is_inflated) {
                /* FIXME: Might be better to pass the ji itself */
-               MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), RETURN_ADDRESS (), NULL);
+               MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (char *)MONO_RETURN_ADDRESS (), NULL);
                if (ji) {
                        gsctx = mono_jit_info_get_generic_sharing_context (ji);
-                       if (gsctx && (gsctx->var_is_vt || gsctx->mvar_is_vt)) {
+                       if (gsctx && gsctx->is_gsharedvt) {
                                /* Needs a ctx to get precise method */
                                printf (") <gsharedvt>\n");
-                               return;
+                               goto unlock;
                        }
                }
        }
 
-       type = mono_method_signature (method)->ret;
+       type = mini_get_underlying_type (mono_method_signature (method)->ret);
 
-handle_enum:
        switch (type->type) {
        case MONO_TYPE_VOID:
                break;
@@ -683,27 +410,26 @@ handle_enum:
                printf ("FP=%f", f);
                break;
        }
-       case MONO_TYPE_VALUETYPE: 
-               if (type->data.klass->enumtype) {
-                       type = mono_class_enum_basetype (type->data.klass);
-                       goto handle_enum;
-               } else {
-                       guint8 *p = va_arg (ap, gpointer);
-                       int j, size, align;
-                       size = mono_type_size (type, &align);
-                       printf ("[");
-                       for (j = 0; p && j < size; j++)
-                               printf ("%02x,", p [j]);
-                       printf ("]");
-               }
+       case MONO_TYPE_VALUETYPE:  {
+               guint8 *p = (guint8 *)va_arg (ap, gpointer);
+               int j, size, align;
+               size = mono_type_size (type, &align);
+               printf ("[");
+               for (j = 0; p && j < size; j++)
+                       printf ("%02x,", p [j]);
+               printf ("]");
                break;
+       }
        default:
                printf ("(unknown return type %x)", mono_method_signature (method)->ret->type);
        }
 
-       //printf (" ip: %p\n", RETURN_ADDRESS_N (1));
+       //printf (" ip: %p\n", MONO_RETURN_ADDRESS_N (1));
        printf ("\n");
        fflush (stdout);
+
+unlock:
+       mono_atomic_store_release (&output_lock, 0);
 }
 
 void