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