2 * trace.c: Tracing facilities for the Mono Runtime.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
9 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
22 #include <mono/metadata/debug-helpers.h>
23 #include <mono/metadata/assembly.h>
24 #include <mono/utils/mono-time.h>
27 #if defined (PLATFORM_ANDROID) || (defined (TARGET_IOS) && defined (TARGET_IOS))
29 # define printf(...) g_log("mono", G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
31 # define fprintf(__ignore, ...) g_log ("mono-gc", G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
36 #define RETURN_ADDRESS_N(N) (__builtin_extract_return_addr (__builtin_return_address (N)))
37 #define RETURN_ADDRESS() RETURN_ADDRESS_N(0)
39 #elif defined(_MSC_VER)
42 #pragma intrinsic(_ReturnAddress)
44 #define RETURN_ADDRESS() _ReturnAddress()
45 #define RETURN_ADDRESS_N(N) NULL
49 #error "Missing return address intrinsics implementation"
53 static MonoTraceSpec trace_spec;
56 mono_trace_eval_exception (MonoClass *klass)
64 for (i = 0; i < trace_spec.len; i++) {
65 MonoTraceOperation *op = &trace_spec.ops [i];
69 case MONO_TRACEOP_EXCEPTION:
70 if (strcmp ("", op->data) == 0 && strcmp ("all", op->data2) == 0)
72 else if (strcmp ("", op->data) == 0 || strcmp (klass->name_space, op->data) == 0)
73 if (strcmp (klass->name, op->data2) == 0)
90 mono_trace_eval (MonoMethod *method)
95 for (i = 0; i < trace_spec.len; i++){
96 MonoTraceOperation *op = &trace_spec.ops [i];
100 case MONO_TRACEOP_ALL:
102 case MONO_TRACEOP_PROGRAM:
103 if (trace_spec.assembly && (method->klass->image == mono_assembly_get_image (trace_spec.assembly)))
105 case MONO_TRACEOP_WRAPPER:
106 if ((method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) ||
107 (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE))
109 case MONO_TRACEOP_METHOD:
110 if (mono_method_desc_full_match ((MonoMethodDesc *) op->data, method))
112 case MONO_TRACEOP_CLASS:
113 if (strcmp (method->klass->name_space, op->data) == 0)
114 if (strcmp (method->klass->name, op->data2) == 0)
117 case MONO_TRACEOP_ASSEMBLY:
118 if (strcmp (mono_image_get_name (method->klass->image), op->data) == 0)
120 case MONO_TRACEOP_NAMESPACE:
121 if (strcmp (method->klass->name_space, op->data) == 0)
123 case MONO_TRACEOP_EXCEPTION:
135 static int is_filenamechar (char p)
137 if (p >= 'A' && p <= 'Z')
139 if (p >= 'a' && p <= 'z')
141 if (p >= '0' && p <= '9')
143 if (p == '.' || p == ':' || p == '_' || p == '-')
151 static void get_string (void)
154 while (is_filenamechar (*input)){
159 value = g_malloc (input - start + 1);
160 strncpy (value, start, input-start);
161 value [input-start] = 0;
183 while (input [0] == '+')
186 if (input [0] == '\0') {
189 if (input [0] == 'M' && input [1] == ':'){
194 if (input [0] == 'N' && input [1] == ':'){
197 return TOKEN_NAMESPACE;
199 if (input [0] == 'T' && input [1] == ':'){
204 if (input [0] == 'E' && input [1] == ':'){
207 return TOKEN_EXCEPTION;
211 return TOKEN_EXCLUDE;
213 if (is_filenamechar (*input)){
215 if (strcmp (value, "all") == 0)
217 if (strcmp (value, "program") == 0)
218 return TOKEN_PROGRAM;
219 if (strcmp (value, "wrapper") == 0)
220 return TOKEN_WRAPPER;
221 if (strcmp (value, "disabled") == 0)
222 return TOKEN_DISABLED;
227 return TOKEN_SEPARATOR;
230 fprintf (stderr, "Syntax error at or around '%s'\n", input);
244 int token = get_token ();
245 if (token == TOKEN_EXCLUDE){
246 token = get_spec (last);
247 if (token == TOKEN_EXCLUDE){
248 fprintf (stderr, "Expecting an expression");
251 if (token == TOKEN_ERROR)
253 trace_spec.ops [(*last)-1].exclude = 1;
254 return TOKEN_SEPARATOR;
256 if (token == TOKEN_END || token == TOKEN_SEPARATOR || token == TOKEN_ERROR)
259 if (token == TOKEN_METHOD){
260 MonoMethodDesc *desc = mono_method_desc_new (value, TRUE);
262 fprintf (stderr, "Invalid method name: %s\n", value);
265 trace_spec.ops [*last].op = MONO_TRACEOP_METHOD;
266 trace_spec.ops [*last].data = desc;
267 } else if (token == TOKEN_ALL)
268 trace_spec.ops [*last].op = MONO_TRACEOP_ALL;
269 else if (token == TOKEN_PROGRAM)
270 trace_spec.ops [*last].op = MONO_TRACEOP_PROGRAM;
271 else if (token == TOKEN_WRAPPER)
272 trace_spec.ops [*last].op = MONO_TRACEOP_WRAPPER;
273 else if (token == TOKEN_NAMESPACE){
274 trace_spec.ops [*last].op = MONO_TRACEOP_NAMESPACE;
275 trace_spec.ops [*last].data = g_strdup (value);
276 } else if (token == TOKEN_CLASS || token == TOKEN_EXCEPTION){
277 char *p = strrchr (value, '.');
280 trace_spec.ops [*last].data = g_strdup (value);
281 trace_spec.ops [*last].data2 = g_strdup (p);
284 trace_spec.ops [*last].data = g_strdup ("");
285 trace_spec.ops [*last].data2 = g_strdup (value);
287 trace_spec.ops [*last].op = token == TOKEN_CLASS ? MONO_TRACEOP_CLASS : MONO_TRACEOP_EXCEPTION;
288 } else if (token == TOKEN_STRING){
289 trace_spec.ops [*last].op = MONO_TRACEOP_ASSEMBLY;
290 trace_spec.ops [*last].data = g_strdup (value);
291 } else if (token == TOKEN_DISABLED) {
292 trace_spec.enabled = FALSE;
294 fprintf (stderr, "Syntax error in trace option specification\n");
298 return TOKEN_SEPARATOR;
302 mono_trace_parse_options (const char *options)
304 char *p = (char*)options;
309 trace_spec.enabled = TRUE;
312 trace_spec.ops = g_new0 (MonoTraceOperation, 1);
313 trace_spec.ops [0].op = MONO_TRACEOP_ALL;
317 for (p = (char*)options; *p != 0; p++)
321 trace_spec.ops = g_new0 (MonoTraceOperation, size);
323 input = (char*)options;
326 while ((token = (get_spec (&last_used))) != TOKEN_END){
327 if (token == TOKEN_ERROR)
329 if (token == TOKEN_SEPARATOR)
332 trace_spec.len = last_used;
338 mono_trace_set_assembly (MonoAssembly *assembly)
340 trace_spec.assembly = assembly;
344 #ifdef HAVE_KW_THREAD
347 int indent_level = 0;
348 static guint64 start_time = 0;
350 static double seconds_since_start (void)
352 guint64 diff = mono_100ns_ticks () - start_time;
353 return diff/10000000.0;
356 static void indent (int diff) {
358 indent_level += diff;
360 start_time = mono_100ns_ticks ();
361 printf ("[%p: %.5f %d] ", (void*)GetCurrentThreadId (), seconds_since_start (), indent_level);
363 indent_level += diff;
367 string_to_utf8 (MonoString *s)
370 GError *error = NULL;
375 return g_strdup ("");
377 as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, NULL, &error);
379 /* Happens with StringBuilders */
380 g_error_free (error);
381 return g_strdup ("<INVALID UTF8>");
388 * cpos (ebp + arg_info[n].offset) points to the beginning of the
389 * stack slot for this argument. On little-endian systems, we can
390 * simply dereference it. On big-endian systems, we need to adjust
391 * cpos upward first if the datatype we're referencing is smaller than
392 * a stack slot. Also - one can't assume that gpointer is also the
393 * size of a stack slot - use SIZEOF_REGISTER instead. The following
394 * helper macro tries to keep down the mess of all the pointer
397 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
398 #define arg_in_stack_slot(cpos, type) ((type *)(cpos))
400 #define arg_in_stack_slot(cpos, type) ((type *)((sizeof(type) < SIZEOF_REGISTER) ? (((gssize)(cpos)) + SIZEOF_REGISTER - sizeof(type)) : (gssize)(cpos)))
404 mono_trace_enter_method (MonoMethod *method, char *ebp)
409 MonoJitArgumentInfo *arg_info;
410 MonoMethodSignature *sig;
412 MonoGenericSharingContext *gsctx = NULL;
414 if (!trace_spec.enabled)
417 fname = mono_method_full_name (method, TRUE);
419 printf ("ENTER: %s(", fname);
423 printf (") ip: %p\n", RETURN_ADDRESS_N (1));
427 sig = mono_method_signature (method);
429 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
431 if (method->is_inflated) {
432 /* FIXME: Might be better to pass the ji itself */
433 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), RETURN_ADDRESS (), NULL);
435 gsctx = mono_jit_info_get_generic_sharing_context (ji);
436 if (gsctx && (gsctx->var_is_vt || gsctx->mvar_is_vt)) {
437 /* Needs a ctx to get precise method */
438 printf (") <gsharedvt>\n");
444 mono_arch_get_argument_info (gsctx, sig, sig->param_count, arg_info);
446 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret)) {
447 g_assert (!mono_method_signature (method)->ret->byref);
449 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
452 if (mono_method_signature (method)->hasthis) {
453 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
454 if (method->klass->valuetype) {
455 printf ("value:%p, ", *arg_in_stack_slot(this, gpointer *));
457 o = *arg_in_stack_slot(this, MonoObject *);
460 class = o->vtable->klass;
462 if (class == mono_defaults.string_class) {
463 MonoString *s = (MonoString*)o;
464 char *as = string_to_utf8 (s);
466 printf ("this:[STRING:%p:%s], ", o, as);
469 printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
472 printf ("this:NULL, ");
476 for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
477 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
478 int size = arg_info [i + 1].size;
480 MonoType *type = mono_method_signature (method)->params [i];
483 printf ("[BYREF:%p], ", *arg_in_stack_slot(cpos, gpointer *));
484 } else switch (mono_type_get_underlying_type (type)->type) {
488 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer *));
490 case MONO_TYPE_BOOLEAN:
494 printf ("%d, ", *arg_in_stack_slot(cpos, gint8));
498 printf ("%d, ", *arg_in_stack_slot(cpos, gint16));
502 printf ("%d, ", *arg_in_stack_slot(cpos, int));
504 case MONO_TYPE_STRING: {
505 MonoString *s = *arg_in_stack_slot(cpos, MonoString *);
509 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
510 as = string_to_utf8 (s);
512 printf ("[STRING:%p:%s], ", s, as);
515 printf ("[STRING:null], ");
518 case MONO_TYPE_CLASS:
519 case MONO_TYPE_OBJECT: {
520 o = *arg_in_stack_slot(cpos, MonoObject *);
522 class = o->vtable->klass;
524 if (class == mono_defaults.string_class) {
525 char *as = string_to_utf8 ((MonoString*)o);
527 printf ("[STRING:%p:%s], ", o, as);
529 } else if (class == mono_defaults.int32_class) {
530 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
531 } else if (class == mono_defaults.monotype_class) {
532 printf ("[TYPE:%s], ", mono_type_full_name (((MonoReflectionType*)o)->type));
534 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
536 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
541 case MONO_TYPE_FNPTR:
542 case MONO_TYPE_ARRAY:
543 case MONO_TYPE_SZARRAY:
544 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
548 printf ("0x%016llx, ", (long long)*arg_in_stack_slot(cpos, gint64));
551 printf ("%f, ", *arg_in_stack_slot(cpos, float));
554 printf ("%f, ", *arg_in_stack_slot(cpos, double));
556 case MONO_TYPE_VALUETYPE:
558 for (j = 0; j < size; j++)
559 printf ("%02x,", *((guint8*)cpos +j));
572 mono_trace_leave_method (MonoMethod *method, ...)
577 MonoGenericSharingContext *gsctx;
579 if (!trace_spec.enabled)
582 va_start(ap, method);
584 fname = mono_method_full_name (method, TRUE);
586 printf ("LEAVE: %s", fname);
589 if (method->is_inflated) {
590 /* FIXME: Might be better to pass the ji itself */
591 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), RETURN_ADDRESS (), NULL);
593 gsctx = mono_jit_info_get_generic_sharing_context (ji);
594 if (gsctx && (gsctx->var_is_vt || gsctx->mvar_is_vt)) {
595 /* Needs a ctx to get precise method */
596 printf (") <gsharedvt>\n");
602 type = mono_method_signature (method)->ret;
605 switch (type->type) {
608 case MONO_TYPE_BOOLEAN: {
609 int eax = va_arg (ap, int);
611 printf ("TRUE:%d", eax);
626 int eax = va_arg (ap, int);
627 printf ("result=%d", eax);
630 case MONO_TYPE_STRING: {
631 MonoString *s = va_arg (ap, MonoString *);
636 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
637 as = string_to_utf8 (s);
638 printf ("[STRING:%p:%s]", s, as);
641 printf ("[STRING:null], ");
644 case MONO_TYPE_CLASS:
645 case MONO_TYPE_OBJECT: {
646 MonoObject *o = va_arg (ap, MonoObject *);
649 if (o->vtable->klass == mono_defaults.boolean_class) {
650 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
651 } else if (o->vtable->klass == mono_defaults.int32_class) {
652 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
653 } else if (o->vtable->klass == mono_defaults.int64_class) {
654 printf ("[INT64:%p:%lld]", o, (long long)*((gint64 *)((char *)o + sizeof (MonoObject))));
656 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
658 printf ("[OBJECT:%p]", o);
663 case MONO_TYPE_FNPTR:
664 case MONO_TYPE_ARRAY:
665 case MONO_TYPE_SZARRAY: {
666 gpointer p = va_arg (ap, gpointer);
667 printf ("result=%p", p);
671 gint64 l = va_arg (ap, gint64);
672 printf ("lresult=0x%16llx", (long long)l);
676 gint64 l = va_arg (ap, gint64);
677 printf ("lresult=0x%16llx", (long long)l);
682 double f = va_arg (ap, double);
686 case MONO_TYPE_VALUETYPE:
687 if (type->data.klass->enumtype) {
688 type = mono_class_enum_basetype (type->data.klass);
691 guint8 *p = va_arg (ap, gpointer);
693 size = mono_type_size (type, &align);
695 for (j = 0; p && j < size; j++)
696 printf ("%02x,", p [j]);
701 printf ("(unknown return type %x)", mono_method_signature (method)->ret->type);
704 //printf (" ip: %p\n", RETURN_ADDRESS_N (1));
710 mono_trace_enable (gboolean enable)
712 trace_spec.enabled = enable;
716 mono_trace_is_enabled ()
718 return trace_spec.enabled;