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