2008-08-28 Zoltan Varga <vargaz@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 "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 = (char*)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 = (char*)options; *p != 0; p++)
233                 if (*p == ',')
234                         size++;
235         
236         trace_spec.ops = g_new0 (MonoTraceOperation, size);
237
238         input = (char*)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         printf ("[%d] ", indent_level);
266         if (diff > 0)
267                 indent_level += diff;
268 }
269
270 static char *
271 string_to_utf8 (MonoString *s)
272 {
273         char *as;
274         GError *error = NULL;
275
276         g_assert (s);
277
278         if (!s->length)
279                 return g_strdup ("");
280
281         as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, NULL, &error);
282         if (error) {
283                 /* Happens with StringBuilders */
284                 g_error_free (error);
285                 return g_strdup ("<INVALID UTF8>");
286         }
287         else
288                 return as;
289 }
290
291 void
292 mono_trace_enter_method (MonoMethod *method, char *ebp)
293 {
294         int i, j;
295         MonoClass *class;
296         MonoObject *o;
297         MonoJitArgumentInfo *arg_info;
298         MonoMethodSignature *sig;
299         char *fname;
300
301         if (!trace_spec.enabled)
302                 return;
303
304         fname = mono_method_full_name (method, TRUE);
305         indent (1);
306         printf ("ENTER: %s(", fname);
307         g_free (fname);
308
309         if (!ebp) {
310                 printf (") ip: %p\n", __builtin_return_address (1));
311                 return;
312         }       
313
314         sig = mono_method_signature (method);
315
316         arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
317
318         mono_arch_get_argument_info (sig, sig->param_count, arg_info);
319
320         if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret)) {
321                 g_assert (!mono_method_signature (method)->ret->byref);
322
323                 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
324         }
325
326         if (mono_method_signature (method)->hasthis) {
327                 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
328                 if (method->klass->valuetype) {
329                         printf ("value:%p, ", *this);
330                 } else {
331                         o = *((MonoObject **)this);
332
333                         if (o) {
334                                 class = o->vtable->klass;
335
336                                 if (class == mono_defaults.string_class) {
337                                         MonoString *s = (MonoString*)o;
338                                         char *as = string_to_utf8 (s);
339
340                                         printf ("this:[STRING:%p:%s], ", o, as);
341                                         g_free (as);
342                                 } else {
343                                         printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
344                                 }
345                         } else 
346                                 printf ("this:NULL, ");
347                 }
348         }
349
350         for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
351                 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
352                 int size = arg_info [i + 1].size;
353
354                 MonoType *type = mono_method_signature (method)->params [i];
355                 
356                 if (type->byref) {
357                         printf ("[BYREF:%p], ", *cpos); 
358                 } else switch (mono_type_get_underlying_type (type)->type) {
359                         
360                 case MONO_TYPE_I:
361                 case MONO_TYPE_U:
362                         printf ("%p, ", *((gpointer **)(cpos)));
363                         break;
364                 case MONO_TYPE_BOOLEAN:
365                 case MONO_TYPE_CHAR:
366                 case MONO_TYPE_I1:
367                 case MONO_TYPE_U1:
368                         printf ("%d, ", *((gint8 *)(cpos)));
369                         break;
370                 case MONO_TYPE_I2:
371                 case MONO_TYPE_U2:
372                         printf ("%d, ", *((gint16 *)(cpos)));
373                         break;
374                 case MONO_TYPE_I4:
375                 case MONO_TYPE_U4:
376                         printf ("%d, ", *((int *)(cpos)));
377                         break;
378                 case MONO_TYPE_STRING: {
379                         MonoString *s = *((MonoString **)cpos);
380                         if (s) {
381                                 char *as;
382
383                                 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
384                                 as = string_to_utf8 (s);
385
386                                 printf ("[STRING:%p:%s], ", s, as);
387                                 g_free (as);
388                         } else 
389                                 printf ("[STRING:null], ");
390                         break;
391                 }
392                 case MONO_TYPE_CLASS:
393                 case MONO_TYPE_OBJECT: {
394                         o = *((MonoObject **)cpos);
395                         if (o) {
396                                 class = o->vtable->klass;
397                     
398                                 if (class == mono_defaults.string_class) {
399                                         char *as = string_to_utf8 ((MonoString*)o);
400
401                                         printf ("[STRING:%p:%s], ", o, as);
402                                         g_free (as);
403                                 } else if (class == mono_defaults.int32_class) {
404                                         printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
405                                 } else if (class == mono_defaults.monotype_class) {
406                                         printf ("[TYPE:%s], ", mono_type_full_name (((MonoReflectionType*)o)->type));
407                                 } else
408                                         printf ("[%s.%s:%p], ", class->name_space, class->name, o);
409                         } else {
410                                 printf ("%p, ", *((gpointer *)(cpos)));                         
411                         }
412                         break;
413                 }
414                 case MONO_TYPE_PTR:
415                 case MONO_TYPE_FNPTR:
416                 case MONO_TYPE_ARRAY:
417                 case MONO_TYPE_SZARRAY:
418                         printf ("%p, ", *((gpointer *)(cpos)));
419                         break;
420                 case MONO_TYPE_I8:
421                 case MONO_TYPE_U8:
422                         printf ("0x%016llx, ", (long long)*((gint64 *)(cpos)));
423                         break;
424                 case MONO_TYPE_R4:
425                         printf ("%f, ", *((float *)(cpos)));
426                         break;
427                 case MONO_TYPE_R8:
428                         printf ("%f, ", *((double *)(cpos)));
429                         break;
430                 case MONO_TYPE_VALUETYPE: 
431                         printf ("[");
432                         for (j = 0; j < size; j++)
433                                 printf ("%02x,", *((guint8*)cpos +j));
434                         printf ("], ");
435                         break;
436                 default:
437                         printf ("XX, ");
438                 }
439         }
440
441         printf (")\n");
442         fflush (stdout);
443 }
444
445 void
446 mono_trace_leave_method (MonoMethod *method, ...)
447 {
448         MonoType *type;
449         char *fname;
450         va_list ap;
451
452         if (!trace_spec.enabled)
453                 return;
454
455         va_start(ap, method);
456
457         fname = mono_method_full_name (method, TRUE);
458         indent (-1);
459         printf ("LEAVE: %s", fname);
460         g_free (fname);
461
462         type = mono_method_signature (method)->ret;
463
464 handle_enum:
465         switch (type->type) {
466         case MONO_TYPE_VOID:
467                 break;
468         case MONO_TYPE_BOOLEAN: {
469                 int eax = va_arg (ap, int);
470                 if (eax)
471                         printf ("TRUE:%d", eax);
472                 else 
473                         printf ("FALSE");
474                         
475                 break;
476         }
477         case MONO_TYPE_CHAR:
478         case MONO_TYPE_I1:
479         case MONO_TYPE_U1:
480         case MONO_TYPE_I2:
481         case MONO_TYPE_U2:
482         case MONO_TYPE_I4:
483         case MONO_TYPE_U4:
484         case MONO_TYPE_I:
485         case MONO_TYPE_U: {
486                 int eax = va_arg (ap, int);
487                 printf ("result=%d", eax);
488                 break;
489         }
490         case MONO_TYPE_STRING: {
491                 MonoString *s = va_arg (ap, MonoString *);
492 ;
493                 if (s) {
494                         char *as;
495
496                         g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
497                         as = string_to_utf8 (s);
498                         printf ("[STRING:%p:%s]", s, as);
499                         g_free (as);
500                 } else 
501                         printf ("[STRING:null], ");
502                 break;
503         }
504         case MONO_TYPE_CLASS: 
505         case MONO_TYPE_OBJECT: {
506                 MonoObject *o = va_arg (ap, MonoObject *);
507
508                 if (o) {
509                         if (o->vtable->klass == mono_defaults.boolean_class) {
510                                 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));            
511                         } else if  (o->vtable->klass == mono_defaults.int32_class) {
512                                 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));    
513                         } else if  (o->vtable->klass == mono_defaults.int64_class) {
514                                 printf ("[INT64:%p:%lld]", o, (long long)*((gint64 *)((char *)o + sizeof (MonoObject))));       
515                         } else
516                                 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
517                 } else
518                         printf ("[OBJECT:%p]", o);
519                
520                 break;
521         }
522         case MONO_TYPE_PTR:
523         case MONO_TYPE_FNPTR:
524         case MONO_TYPE_ARRAY:
525         case MONO_TYPE_SZARRAY: {
526                 gpointer p = va_arg (ap, gpointer);
527                 printf ("result=%p", p);
528                 break;
529         }
530         case MONO_TYPE_I8: {
531                 gint64 l =  va_arg (ap, gint64);
532                 printf ("lresult=0x%16llx", (long long)l);
533                 break;
534         }
535         case MONO_TYPE_U8: {
536                 gint64 l =  va_arg (ap, gint64);
537                 printf ("lresult=0x%16llx", (long long)l);
538                 break;
539         }
540         case MONO_TYPE_R4:
541         case MONO_TYPE_R8: {
542                 double f = va_arg (ap, double);
543                 printf ("FP=%f\n", f);
544                 break;
545         }
546         case MONO_TYPE_VALUETYPE: 
547                 if (type->data.klass->enumtype) {
548                         type = type->data.klass->enum_basetype;
549                         goto handle_enum;
550                 } else {
551                         guint8 *p = va_arg (ap, gpointer);
552                         int j, size, align;
553                         size = mono_type_size (type, &align);
554                         printf ("[");
555                         for (j = 0; p && j < size; j++)
556                                 printf ("%02x,", p [j]);
557                         printf ("]");
558                 }
559                 break;
560         default:
561                 printf ("(unknown return type %x)", mono_method_signature (method)->ret->type);
562         }
563
564         //printf (" ip: %p\n", __builtin_return_address (1));
565         printf ("\n");
566         fflush (stdout);
567 }
568
569 void
570 mono_trace_enable (gboolean enable)
571 {
572         trace_spec.enabled = enable;
573 }
574
575 gboolean
576 mono_trace_is_enabled ()
577 {
578         return trace_spec.enabled;
579 }
580