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