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