Merge pull request #5439 from alexrp/master
[mono.git] / mono / mini / trace.c
1 /**
2  * \file
3  * Tracing facilities for the Mono Runtime.
4  *
5  * Author:
6  *   Paolo Molaro (lupus@ximian.com)
7  *   Dietmar Maurer (dietmar@ximian.com)
8  *
9  * (C) 2002 Ximian, Inc.
10  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12  */
13
14 #include <config.h>
15 #ifdef HAVE_ALLOCA_H
16 #include <alloca.h>
17 #endif
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #include <string.h>
22 #include "mini.h"
23 #include <mono/metadata/debug-helpers.h>
24 #include <mono/metadata/assembly.h>
25 #include <mono/utils/mono-time.h>
26 #include <mono/utils/mono-memory-model.h>
27 #include "trace.h"
28
29 #if defined (HOST_ANDROID) || (defined (TARGET_IOS) && defined (TARGET_IOS))
30 #  undef printf
31 #  define printf(...) g_log("mono", G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
32 #  undef fprintf
33 #  define fprintf(__ignore, ...) g_log ("mono-gc", G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
34 #endif
35
36 #ifdef __GNUC__
37
38 #define RETURN_ADDRESS_N(N) (__builtin_extract_return_addr (__builtin_return_address (N)))
39 #define RETURN_ADDRESS() RETURN_ADDRESS_N(0)
40
41 #elif defined(_MSC_VER)
42
43 #include <intrin.h>
44 #pragma intrinsic(_ReturnAddress)
45
46 #define RETURN_ADDRESS() _ReturnAddress()
47 #define RETURN_ADDRESS_N(N) NULL
48
49 #else
50
51 #error "Missing return address intrinsics implementation"
52
53 #endif
54
55 static MonoTraceSpec trace_spec;
56
57 static volatile gint32 output_lock = 0;
58
59 gboolean
60 mono_trace_eval_exception (MonoClass *klass)
61 {
62         int include = 0;
63         int i;
64
65         if (!klass)
66                 return FALSE;
67
68         for (i = 0; i < trace_spec.len; i++) {
69                 MonoTraceOperation *op = &trace_spec.ops [i];
70                 int inc = 0;
71                 
72                 switch (op->op){
73                 case MONO_TRACEOP_EXCEPTION:
74                         if (strcmp ("", op->data) == 0 && strcmp ("all", op->data2) == 0)
75                                 inc = 1;
76                         else if (strcmp ("", op->data) == 0 || strcmp (klass->name_space, op->data) == 0)
77                                 if (strcmp (klass->name, op->data2) == 0)
78                                         inc = 1;
79                         break;
80                 default:
81                         break;
82                 }
83                 if (op->exclude){
84                         if (inc)
85                                 include = 0;
86                 } else if (inc)
87                         include = 1;
88         }
89
90         return include;
91 }
92
93 gboolean
94 mono_trace_eval (MonoMethod *method)
95 {
96         int include = 0;
97         int i;
98
99         for (i = 0; i < trace_spec.len; i++){
100                 MonoTraceOperation *op = &trace_spec.ops [i];
101                 int inc = 0;
102                 
103                 switch (op->op){
104                 case MONO_TRACEOP_ALL:
105                         inc = 1;
106                         break;
107                 case MONO_TRACEOP_PROGRAM:
108                         if (trace_spec.assembly && (method->klass->image == mono_assembly_get_image (trace_spec.assembly)))
109                                 inc = 1;
110                         break;
111                 case MONO_TRACEOP_WRAPPER:
112                         if ((method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) ||
113                                 (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE))
114                                 inc = 1;
115                         break;
116                 case MONO_TRACEOP_METHOD:
117                         if (mono_method_desc_full_match ((MonoMethodDesc *) op->data, method))
118                                 inc = 1;
119                         break;
120                 case MONO_TRACEOP_CLASS:
121                         if (strcmp (method->klass->name_space, op->data) == 0)
122                                 if (strcmp (method->klass->name, op->data2) == 0)
123                                         inc = 1;
124                         break;
125                 case MONO_TRACEOP_ASSEMBLY:
126                         if (strcmp (mono_image_get_name (method->klass->image), op->data) == 0)
127                                 inc = 1;
128                         break;
129                 case MONO_TRACEOP_NAMESPACE:
130                         if (strcmp (method->klass->name_space, op->data) == 0)
131                                 inc = 1;
132                         break;
133                 case MONO_TRACEOP_EXCEPTION:
134                         break;
135                 }
136                 if (op->exclude) {
137                         if (inc)
138                                 include = 0;
139                 } else if (inc) {
140                         include = 1;
141                 }
142         }
143         return include;
144 }
145
146 static int is_filenamechar (char p)
147 {
148         if (p >= 'A' && p <= 'Z')
149                 return TRUE;
150         if (p >= 'a' && p <= 'z')
151                 return TRUE;
152         if (p >= '0' && p <= '9')
153                 return TRUE;
154         if (p == '.' || p == ':' || p == '_' || p == '-' || p == '`')
155                 return TRUE;
156         return FALSE;
157 }
158
159 static char *input;
160 static char *value;
161
162 static void get_string (void)
163 {
164         char *start = input;
165         while (is_filenamechar (*input)){
166                 input++;
167         }
168         if (value != NULL)
169                 g_free (value);
170         size_t len = input - start;
171         value = (char *)g_malloc (len + 1);
172         memcpy (value, start, len);
173         value [len] = 0;
174 }
175
176 enum Token {
177         TOKEN_METHOD,
178         TOKEN_CLASS,
179         TOKEN_ALL,
180         TOKEN_PROGRAM,
181         TOKEN_EXCEPTION,
182         TOKEN_NAMESPACE,
183         TOKEN_WRAPPER,
184         TOKEN_STRING,
185         TOKEN_EXCLUDE,
186         TOKEN_DISABLED,
187         TOKEN_SEPARATOR,
188         TOKEN_END,
189         TOKEN_ERROR
190 };
191
192 static int
193 get_token (void)
194 {
195         while (input [0] == '+')
196                 input++;
197
198         if (input [0] == '\0') {
199                 return TOKEN_END;
200         }
201         if (input [0] == 'M' && input [1] == ':'){
202                 input += 2;
203                 get_string ();
204                 return TOKEN_METHOD;
205         }
206         if (input [0] == 'N' && input [1] == ':'){
207                 input += 2;
208                 get_string ();
209                 return TOKEN_NAMESPACE;
210         }
211         if (input [0] == 'T' && input [1] == ':'){
212                 input += 2;
213                 get_string ();
214                 return TOKEN_CLASS;
215         }
216         if (input [0] == 'E' && input [1] == ':'){
217                 input += 2;
218                 get_string ();
219                 return TOKEN_EXCEPTION;
220         }
221         if (*input == '-'){
222                 input++;
223                 return TOKEN_EXCLUDE;
224         }
225         if (is_filenamechar (*input)){
226                 get_string ();
227                 if (strcmp (value, "all") == 0)
228                         return TOKEN_ALL;
229                 if (strcmp (value, "program") == 0)
230                         return TOKEN_PROGRAM;
231                 if (strcmp (value, "wrapper") == 0)
232                         return TOKEN_WRAPPER;
233                 if (strcmp (value, "disabled") == 0)
234                         return TOKEN_DISABLED;
235                 return TOKEN_STRING;
236         }
237         if (*input == ','){
238                 input++;
239                 return TOKEN_SEPARATOR;
240         }
241
242         fprintf (stderr, "Syntax error at or around '%s'\n", input);    
243         return TOKEN_ERROR;
244 }
245
246 static void
247 cleanup (void)
248 {
249         if (value != NULL)
250                 g_free (value);
251 }
252
253 static int
254 get_spec (int *last)
255 {
256         int token = get_token ();
257         if (token == TOKEN_EXCLUDE){
258                 token = get_spec (last);
259                 if (token == TOKEN_EXCLUDE){
260                         fprintf (stderr, "Expecting an expression");
261                         return TOKEN_ERROR;
262                 }
263                 if (token == TOKEN_ERROR)
264                         return token;
265                 trace_spec.ops [(*last)-1].exclude = 1;
266                 return TOKEN_SEPARATOR;
267         }
268         if (token == TOKEN_END || token == TOKEN_SEPARATOR || token == TOKEN_ERROR)
269                 return token;
270         
271         if (token == TOKEN_METHOD){
272                 MonoMethodDesc *desc = mono_method_desc_new (value, TRUE);
273                 if (desc == NULL){
274                         fprintf (stderr, "Invalid method name: %s\n", value);
275                         return TOKEN_ERROR;
276                 }
277                 trace_spec.ops [*last].op = MONO_TRACEOP_METHOD;
278                 trace_spec.ops [*last].data = desc;
279         } else if (token == TOKEN_ALL)
280                 trace_spec.ops [*last].op = MONO_TRACEOP_ALL;
281         else if (token == TOKEN_PROGRAM)
282                 trace_spec.ops [*last].op = MONO_TRACEOP_PROGRAM;
283         else if (token == TOKEN_WRAPPER)
284                 trace_spec.ops [*last].op = MONO_TRACEOP_WRAPPER;
285         else if (token == TOKEN_NAMESPACE){
286                 trace_spec.ops [*last].op = MONO_TRACEOP_NAMESPACE;
287                 trace_spec.ops [*last].data = g_strdup (value);
288         } else if (token == TOKEN_CLASS || token == TOKEN_EXCEPTION){
289                 char *p = strrchr (value, '.');
290                 if (p) {
291                         *p++ = 0;
292                         trace_spec.ops [*last].data = g_strdup (value);
293                         trace_spec.ops [*last].data2 = g_strdup (p);
294                 }
295                 else {
296                         trace_spec.ops [*last].data = g_strdup ("");
297                         trace_spec.ops [*last].data2 = g_strdup (value);
298                 }
299                 trace_spec.ops [*last].op = token == TOKEN_CLASS ? MONO_TRACEOP_CLASS : MONO_TRACEOP_EXCEPTION;
300         } else if (token == TOKEN_STRING){
301                 trace_spec.ops [*last].op = MONO_TRACEOP_ASSEMBLY;
302                 trace_spec.ops [*last].data = g_strdup (value);
303         } else if (token == TOKEN_DISABLED) {
304                 trace_spec.enabled = FALSE;
305         } else {
306                 fprintf (stderr, "Syntax error in trace option specification\n");
307                 return TOKEN_ERROR;
308         }
309         (*last)++;
310         return TOKEN_SEPARATOR;
311 }
312
313 MonoTraceSpec *
314 mono_trace_parse_options (const char *options)
315 {
316         char *p = (char*)options;
317         int size = 1;
318         int last_used;
319         int token;
320
321         trace_spec.enabled = TRUE;
322         if (*p == 0){
323                 trace_spec.len = 1;
324                 trace_spec.ops = g_new0 (MonoTraceOperation, 1);
325                 trace_spec.ops [0].op = MONO_TRACEOP_ALL;
326                 return &trace_spec;
327         }
328                 
329         for (p = (char*)options; *p != 0; p++)
330                 if (*p == ',')
331                         size++;
332         
333         trace_spec.ops = g_new0 (MonoTraceOperation, size);
334
335         input = (char*)options;
336         last_used = 0;
337         
338         while ((token = (get_spec (&last_used))) != TOKEN_END){
339                 if (token == TOKEN_ERROR)
340                         return NULL;
341                 if (token == TOKEN_SEPARATOR)
342                         continue;
343         }
344         trace_spec.len = last_used;
345         cleanup ();
346         return &trace_spec;
347 }
348
349 void
350 mono_trace_set_assembly (MonoAssembly *assembly)
351 {
352         trace_spec.assembly = assembly;
353 }
354
355 static
356 #ifdef HAVE_KW_THREAD
357 __thread 
358 #endif
359 int indent_level = 0;
360 static guint64 start_time = 0;
361
362 static double seconds_since_start (void)
363 {
364         guint64 diff = mono_100ns_ticks () - start_time;
365         return diff/10000000.0;
366 }
367
368 static void indent (int diff) {
369         if (diff < 0)
370                 indent_level += diff;
371         if (start_time == 0)
372                 start_time = mono_100ns_ticks ();
373         printf ("[%p: %.5f %d] ", (void*)mono_native_thread_id_get (), seconds_since_start (), indent_level);
374         if (diff > 0)
375                 indent_level += diff;
376 }
377
378 static char *
379 string_to_utf8 (MonoString *s)
380 {
381         char *as;
382         GError *error = NULL;
383
384         g_assert (s);
385
386         if (!s->length)
387                 return g_strdup ("");
388
389         as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, NULL, &error);
390         if (error) {
391                 /* Happens with StringBuilders */
392                 g_error_free (error);
393                 return g_strdup ("<INVALID UTF8>");
394         }
395         else
396                 return as;
397 }
398
399 /*
400  * cpos (ebp + arg_info[n].offset) points to the beginning of the
401  * stack slot for this argument.  On little-endian systems, we can
402  * simply dereference it. On big-endian systems, we need to adjust
403  * cpos upward first if the datatype we're referencing is smaller than
404  * a stack slot. Also - one can't assume that gpointer is also the
405  * size of a stack slot - use SIZEOF_REGISTER instead. The following
406  * helper macro tries to keep down the mess of all the pointer
407  * calculations.
408  */
409 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
410 #define arg_in_stack_slot(cpos, type) ((type *)(cpos))
411 #else
412 #define arg_in_stack_slot(cpos, type) ((type *)((sizeof(type) < SIZEOF_REGISTER) ? (((gssize)(cpos)) + SIZEOF_REGISTER - sizeof(type)) : (gssize)(cpos)))
413 #endif
414
415 void
416 mono_trace_enter_method (MonoMethod *method, char *ebp)
417 {
418         int i, j;
419         MonoClass *klass;
420         MonoObject *o;
421         MonoJitArgumentInfo *arg_info;
422         MonoMethodSignature *sig;
423         char *fname;
424         MonoGenericSharingContext *gsctx = NULL;
425
426         if (!trace_spec.enabled)
427                 return;
428
429         while (output_lock != 0 || InterlockedCompareExchange (&output_lock, 1, 0) != 0)
430                 mono_thread_info_yield ();
431
432         fname = mono_method_full_name (method, TRUE);
433         indent (1);
434         printf ("ENTER: %s(", fname);
435         g_free (fname);
436
437         if (!ebp) {
438                 printf (") ip: %p\n", RETURN_ADDRESS_N (1));
439                 goto unlock;
440         }
441
442         sig = mono_method_signature (method);
443
444         arg_info = (MonoJitArgumentInfo *)alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
445
446         if (method->is_inflated) {
447                 /* FIXME: Might be better to pass the ji itself */
448                 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (char *)RETURN_ADDRESS (), NULL);
449                 if (ji) {
450                         gsctx = mono_jit_info_get_generic_sharing_context (ji);
451                         if (gsctx && gsctx->is_gsharedvt) {
452                                 /* Needs a ctx to get precise method */
453                                 printf (") <gsharedvt>\n");
454                                 goto unlock;
455                         }
456                 }
457         }
458
459         mono_arch_get_argument_info (sig, sig->param_count, arg_info);
460
461         if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret)) {
462                 g_assert (!mono_method_signature (method)->ret->byref);
463
464                 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
465         }
466
467         if (mono_method_signature (method)->hasthis) {
468                 gpointer *this_obj = (gpointer *)(ebp + arg_info [0].offset);
469                 if (method->klass->valuetype) {
470                         printf ("value:%p, ", *arg_in_stack_slot(this_obj, gpointer *));
471                 } else {
472                         o = *arg_in_stack_slot(this_obj, MonoObject *);
473
474                         if (o) {
475                                 klass = o->vtable->klass;
476
477                                 if (klass == mono_defaults.string_class) {
478                                         MonoString *s = (MonoString*)o;
479                                         char *as = string_to_utf8 (s);
480
481                                         printf ("this:[STRING:%p:%s], ", o, as);
482                                         g_free (as);
483                                 } else {
484                                         printf ("this:%p[%s.%s %s], ", o, klass->name_space, klass->name, o->vtable->domain->friendly_name);
485                                 }
486                         } else 
487                                 printf ("this:NULL, ");
488                 }
489         }
490
491         for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
492                 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
493                 int size = arg_info [i + 1].size;
494
495                 MonoType *type = mono_method_signature (method)->params [i];
496                 
497                 if (type->byref) {
498                         printf ("[BYREF:%p], ", *arg_in_stack_slot(cpos, gpointer *));
499                 } else switch (mini_get_underlying_type (type)->type) {
500                         
501                 case MONO_TYPE_I:
502                 case MONO_TYPE_U:
503                         printf ("%p, ", *arg_in_stack_slot(cpos, gpointer *));
504                         break;
505                 case MONO_TYPE_BOOLEAN:
506                 case MONO_TYPE_CHAR:
507                 case MONO_TYPE_I1:
508                 case MONO_TYPE_U1:
509                         printf ("%d, ", *arg_in_stack_slot(cpos, gint8));
510                         break;
511                 case MONO_TYPE_I2:
512                 case MONO_TYPE_U2:
513                         printf ("%d, ", *arg_in_stack_slot(cpos, gint16));
514                         break;
515                 case MONO_TYPE_I4:
516                 case MONO_TYPE_U4:
517                         printf ("%d, ", *arg_in_stack_slot(cpos, int));
518                         break;
519                 case MONO_TYPE_STRING: {
520                         MonoString *s = *arg_in_stack_slot(cpos, MonoString *);
521                         if (s) {
522                                 char *as;
523
524                                 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
525                                 as = string_to_utf8 (s);
526
527                                 printf ("[STRING:%p:%s], ", s, as);
528                                 g_free (as);
529                         } else 
530                                 printf ("[STRING:null], ");
531                         break;
532                 }
533                 case MONO_TYPE_CLASS:
534                 case MONO_TYPE_OBJECT: {
535                         o = *arg_in_stack_slot(cpos, MonoObject *);
536                         if (o) {
537                                 klass = o->vtable->klass;
538                     
539                                 if (klass == mono_defaults.string_class) {
540                                         char *as = string_to_utf8 ((MonoString*)o);
541
542                                         printf ("[STRING:%p:%s], ", o, as);
543                                         g_free (as);
544                                 } else if (klass == mono_defaults.int32_class) {
545                                         printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
546                                 } else if (klass == mono_defaults.runtimetype_class) {
547                                         printf ("[TYPE:%s], ", mono_type_full_name (((MonoReflectionType*)o)->type));
548                                 } else
549                                         printf ("[%s.%s:%p], ", klass->name_space, klass->name, o);
550                         } else {
551                                 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
552                         }
553                         break;
554                 }
555                 case MONO_TYPE_PTR:
556                 case MONO_TYPE_FNPTR:
557                 case MONO_TYPE_ARRAY:
558                 case MONO_TYPE_SZARRAY:
559                         printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
560                         break;
561                 case MONO_TYPE_I8:
562                 case MONO_TYPE_U8:
563                         printf ("0x%016llx, ", (long long)*arg_in_stack_slot(cpos, gint64));
564                         break;
565                 case MONO_TYPE_R4:
566                         printf ("%f, ", *arg_in_stack_slot(cpos, float));
567                         break;
568                 case MONO_TYPE_R8:
569                         printf ("%f, ", *arg_in_stack_slot(cpos, double));
570                         break;
571                 case MONO_TYPE_VALUETYPE: 
572                         printf ("[");
573                         for (j = 0; j < size; j++)
574                                 printf ("%02x,", *((guint8*)cpos +j));
575                         printf ("], ");
576                         break;
577                 default:
578                         printf ("XX, ");
579                 }
580         }
581
582         printf (")\n");
583         fflush (stdout);
584
585 unlock:
586         mono_atomic_store_release (&output_lock, 0);
587 }
588
589 void
590 mono_trace_leave_method (MonoMethod *method, ...)
591 {
592         MonoType *type;
593         char *fname;
594         va_list ap;
595         MonoGenericSharingContext *gsctx;
596
597         if (!trace_spec.enabled)
598                 return;
599
600         while (output_lock != 0 || InterlockedCompareExchange (&output_lock, 1, 0) != 0)
601                 mono_thread_info_yield ();
602
603         va_start(ap, method);
604
605         fname = mono_method_full_name (method, TRUE);
606         indent (-1);
607         printf ("LEAVE: %s", fname);
608         g_free (fname);
609
610         if (method->is_inflated) {
611                 /* FIXME: Might be better to pass the ji itself */
612                 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (char *)RETURN_ADDRESS (), NULL);
613                 if (ji) {
614                         gsctx = mono_jit_info_get_generic_sharing_context (ji);
615                         if (gsctx && gsctx->is_gsharedvt) {
616                                 /* Needs a ctx to get precise method */
617                                 printf (") <gsharedvt>\n");
618                                 goto unlock;
619                         }
620                 }
621         }
622
623         type = mini_get_underlying_type (mono_method_signature (method)->ret);
624
625         switch (type->type) {
626         case MONO_TYPE_VOID:
627                 break;
628         case MONO_TYPE_BOOLEAN: {
629                 int eax = va_arg (ap, int);
630                 if (eax)
631                         printf ("TRUE:%d", eax);
632                 else 
633                         printf ("FALSE");
634                         
635                 break;
636         }
637         case MONO_TYPE_CHAR:
638         case MONO_TYPE_I1:
639         case MONO_TYPE_U1:
640         case MONO_TYPE_I2:
641         case MONO_TYPE_U2:
642         case MONO_TYPE_I4:
643         case MONO_TYPE_U4:
644         case MONO_TYPE_I:
645         case MONO_TYPE_U: {
646                 int eax = va_arg (ap, int);
647                 printf ("result=%d", eax);
648                 break;
649         }
650         case MONO_TYPE_STRING: {
651                 MonoString *s = va_arg (ap, MonoString *);
652 ;
653                 if (s) {
654                         char *as;
655
656                         g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
657                         as = string_to_utf8 (s);
658                         printf ("[STRING:%p:%s]", s, as);
659                         g_free (as);
660                 } else 
661                         printf ("[STRING:null], ");
662                 break;
663         }
664         case MONO_TYPE_CLASS: 
665         case MONO_TYPE_OBJECT: {
666                 MonoObject *o = va_arg (ap, MonoObject *);
667
668                 if (o) {
669                         if (o->vtable->klass == mono_defaults.boolean_class) {
670                                 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));            
671                         } else if  (o->vtable->klass == mono_defaults.int32_class) {
672                                 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));    
673                         } else if  (o->vtable->klass == mono_defaults.int64_class) {
674                                 printf ("[INT64:%p:%lld]", o, (long long)*((gint64 *)((char *)o + sizeof (MonoObject))));       
675                         } else
676                                 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
677                 } else
678                         printf ("[OBJECT:%p]", o);
679                
680                 break;
681         }
682         case MONO_TYPE_PTR:
683         case MONO_TYPE_FNPTR:
684         case MONO_TYPE_ARRAY:
685         case MONO_TYPE_SZARRAY: {
686                 gpointer p = va_arg (ap, gpointer);
687                 printf ("result=%p", p);
688                 break;
689         }
690         case MONO_TYPE_I8: {
691                 gint64 l =  va_arg (ap, gint64);
692                 printf ("lresult=0x%16llx", (long long)l);
693                 break;
694         }
695         case MONO_TYPE_U8: {
696                 gint64 l =  va_arg (ap, gint64);
697                 printf ("lresult=0x%16llx", (long long)l);
698                 break;
699         }
700         case MONO_TYPE_R4:
701         case MONO_TYPE_R8: {
702                 double f = va_arg (ap, double);
703                 printf ("FP=%f", f);
704                 break;
705         }
706         case MONO_TYPE_VALUETYPE:  {
707                 guint8 *p = (guint8 *)va_arg (ap, gpointer);
708                 int j, size, align;
709                 size = mono_type_size (type, &align);
710                 printf ("[");
711                 for (j = 0; p && j < size; j++)
712                         printf ("%02x,", p [j]);
713                 printf ("]");
714                 break;
715         }
716         default:
717                 printf ("(unknown return type %x)", mono_method_signature (method)->ret->type);
718         }
719
720         //printf (" ip: %p\n", RETURN_ADDRESS_N (1));
721         printf ("\n");
722         fflush (stdout);
723
724 unlock:
725         mono_atomic_store_release (&output_lock, 0);
726 }
727
728 void
729 mono_trace_enable (gboolean enable)
730 {
731         trace_spec.enabled = enable;
732 }
733
734 gboolean
735 mono_trace_is_enabled ()
736 {
737         return trace_spec.enabled;
738 }