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