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