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