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