Enforce inline limit.
[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 (method->klass->image == mono_assembly_get_image (trace_spec.assembly))
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 (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 == ':')
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                 *p++ = 0;
188                 trace_spec.ops [*last].op = MONO_TRACEOP_CLASS;
189                 trace_spec.ops [*last].data = g_strdup (value);
190                 trace_spec.ops [*last].data2 = g_strdup (p);
191         } else if (token == TOKEN_STRING){
192                 trace_spec.ops [*last].op = MONO_TRACEOP_ASSEMBLY;
193                 trace_spec.ops [*last].data = g_strdup (value);
194         }
195         else {
196                 fprintf (stderr, "Syntax error in trace option specification\n");
197                 return TOKEN_ERROR;
198         }
199         (*last)++;
200         return TOKEN_SEPARATOR;
201 }
202
203 MonoTraceSpec *
204 mono_trace_parse_options (MonoAssembly *assembly, char *options)
205 {
206         char *p = options;
207         int size = 1;
208         int last_used;
209         int token;
210         
211         trace_spec.assembly = assembly;
212         
213         if (*p == 0){
214                 trace_spec.len = 1;
215                 trace_spec.ops = g_new0 (MonoTraceOperation, 1);
216                 trace_spec.ops [0].op = MONO_TRACEOP_ALL;
217                 return &trace_spec;
218         }
219                 
220         for (p = options; *p != 0; p++)
221                 if (*p == ',')
222                         size++;
223         
224         trace_spec.ops = g_new0 (MonoTraceOperation, size);
225
226         input = options;
227         last_used = 0;
228         
229         while ((token = (get_spec (&last_used))) != TOKEN_END){
230                 if (token == TOKEN_ERROR)
231                         return NULL;
232                 if (token == TOKEN_SEPARATOR)
233                         continue;
234         }
235         trace_spec.len = last_used;
236         cleanup ();
237         return &trace_spec;
238 }
239
240 static int indent_level = 0;
241
242 static void indent (int diff) {
243         int v;
244         if (diff < 0)
245                 indent_level += diff;
246         v = indent_level;
247         while (v-- > 0) {
248                 printf (". ");
249         }
250         if (diff > 0)
251                 indent_level += diff;
252 }
253
254 static gboolean enable_trace = TRUE;
255
256 void
257 mono_trace_enter_method (MonoMethod *method, char *ebp)
258 {
259         int i, j;
260         MonoClass *class;
261         MonoObject *o;
262         MonoJitArgumentInfo *arg_info;
263         MonoMethodSignature *sig;
264         char *fname;
265
266         if (!enable_trace)
267                 return;
268
269         fname = mono_method_full_name (method, TRUE);
270         indent (1);
271         printf ("ENTER: %s(", fname);
272         g_free (fname);
273
274         if (!ebp) {
275                 printf (") ip: %p\n", __builtin_return_address (1));
276                 return;
277         }       
278         if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
279                 g_error ("unaligned stack detected (%p)", ebp);
280         }
281
282         sig = method->signature;
283
284         arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
285
286         mono_arch_get_argument_info (sig, sig->param_count, arg_info);
287
288         if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
289                 g_assert (!method->signature->ret->byref);
290
291                 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
292         }
293
294         if (method->signature->hasthis) {
295                 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
296                 if (method->klass->valuetype) {
297                         printf ("value:%p, ", *this);
298                 } else {
299                         o = *((MonoObject **)this);
300
301                         if (o) {
302                                 class = o->vtable->klass;
303
304                                 if (class == mono_defaults.string_class) {
305                                         printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
306                                 } else {
307                                         printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
308                                 }
309                         } else 
310                                 printf ("this:NULL, ");
311                 }
312         }
313
314         for (i = 0; i < method->signature->param_count; ++i) {
315                 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
316                 int size = arg_info [i + 1].size;
317
318                 MonoType *type = method->signature->params [i];
319                 
320                 if (type->byref) {
321                         printf ("[BYREF:%p], ", *cpos); 
322                 } else switch (type->type) {
323                         
324                 case MONO_TYPE_I:
325                 case MONO_TYPE_U:
326                         printf ("%p, ", (gpointer)*((int *)(cpos)));
327                         break;
328                 case MONO_TYPE_BOOLEAN:
329                 case MONO_TYPE_CHAR:
330                 case MONO_TYPE_I1:
331                 case MONO_TYPE_U1:
332                         printf ("%d, ", *((gint8 *)(cpos)));
333                         break;
334                 case MONO_TYPE_I2:
335                 case MONO_TYPE_U2:
336                         printf ("%d, ", *((gint16 *)(cpos)));
337                         break;
338                 case MONO_TYPE_I4:
339                 case MONO_TYPE_U4:
340                         printf ("%d, ", *((int *)(cpos)));
341                         break;
342                 case MONO_TYPE_STRING: {
343                         MonoString *s = *((MonoString **)cpos);
344                         if (s) {
345                                 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
346                                 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
347                         } else 
348                                 printf ("[STRING:null], ");
349                         break;
350                 }
351                 case MONO_TYPE_CLASS:
352                 case MONO_TYPE_OBJECT: {
353                         o = *((MonoObject **)cpos);
354                         if (o) {
355                                 class = o->vtable->klass;
356                     
357                                 if (class == mono_defaults.string_class) {
358                                         printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
359                                 } else if (class == mono_defaults.int32_class) {
360                                         printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
361                                 } else
362                                         printf ("[%s.%s:%p], ", class->name_space, class->name, o);
363                         } else {
364                                 printf ("%p, ", *((gpointer *)(cpos)));                         
365                         }
366                         break;
367                 }
368                 case MONO_TYPE_PTR:
369                 case MONO_TYPE_FNPTR:
370                 case MONO_TYPE_ARRAY:
371                 case MONO_TYPE_SZARRAY:
372                         printf ("%p, ", *((gpointer *)(cpos)));
373                         break;
374                 case MONO_TYPE_I8:
375                 case MONO_TYPE_U8:
376                         printf ("0x%016llx, ", *((gint64 *)(cpos)));
377                         break;
378                 case MONO_TYPE_R4:
379                         printf ("%f, ", *((float *)(cpos)));
380                         break;
381                 case MONO_TYPE_R8:
382                         printf ("%f, ", *((double *)(cpos)));
383                         break;
384                 case MONO_TYPE_VALUETYPE: 
385                         printf ("[");
386                         for (j = 0; j < size; j++)
387                                 printf ("%02x,", *((guint8*)cpos +j));
388                         printf ("], ");
389                         break;
390                 default:
391                         printf ("XX, ");
392                 }
393         }
394
395         printf (")\n");
396 }
397
398 void
399 mono_trace_leave_method (MonoMethod *method, ...)
400 {
401         MonoType *type;
402         char *fname;
403         va_list ap;
404
405         if (!enable_trace)
406                 return;
407
408         va_start(ap, method);
409
410         fname = mono_method_full_name (method, TRUE);
411         indent (-1);
412         printf ("LEAVE: %s", fname);
413         g_free (fname);
414
415         type = method->signature->ret;
416
417 handle_enum:
418         switch (type->type) {
419         case MONO_TYPE_VOID:
420                 break;
421         case MONO_TYPE_BOOLEAN: {
422                 int eax = va_arg (ap, int);
423                 if (eax)
424                         printf ("TRUE:%d", eax);
425                 else 
426                         printf ("FALSE");
427                         
428                 break;
429         }
430         case MONO_TYPE_CHAR:
431         case MONO_TYPE_I1:
432         case MONO_TYPE_U1:
433         case MONO_TYPE_I2:
434         case MONO_TYPE_U2:
435         case MONO_TYPE_I4:
436         case MONO_TYPE_U4:
437         case MONO_TYPE_I:
438         case MONO_TYPE_U: {
439                 int eax = va_arg (ap, int);
440                 printf ("result=%d", eax);
441                 break;
442         }
443         case MONO_TYPE_STRING: {
444                 MonoString *s = va_arg (ap, MonoString *);
445 ;
446                 if (s) {
447                         g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
448                         printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
449                 } else 
450                         printf ("[STRING:null], ");
451                 break;
452         }
453         case MONO_TYPE_CLASS: 
454         case MONO_TYPE_OBJECT: {
455                 MonoObject *o = va_arg (ap, MonoObject *);
456
457                 if (o) {
458                         if (o->vtable->klass == mono_defaults.boolean_class) {
459                                 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));            
460                         } else if  (o->vtable->klass == mono_defaults.int32_class) {
461                                 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));    
462                         } else if  (o->vtable->klass == mono_defaults.int64_class) {
463                                 printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));  
464                         } else
465                                 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
466                 } else
467                         printf ("[OBJECT:%p]", o);
468                
469                 break;
470         }
471         case MONO_TYPE_PTR:
472         case MONO_TYPE_FNPTR:
473         case MONO_TYPE_ARRAY:
474         case MONO_TYPE_SZARRAY: {
475                 gpointer p = va_arg (ap, gpointer);
476                 printf ("result=%p", p);
477                 break;
478         }
479         case MONO_TYPE_I8: {
480                 gint64 l =  va_arg (ap, gint64);
481                 printf ("lresult=0x%16llx", l);
482                 break;
483         }
484         case MONO_TYPE_U8: {
485                 gint64 l =  va_arg (ap, gint64);
486                 printf ("lresult=0x%16llx", l);
487                 break;
488         }
489         case MONO_TYPE_R4:
490         case MONO_TYPE_R8: {
491                 double f = va_arg (ap, double);
492                 printf ("FP=%f\n", f);
493                 break;
494         }
495         case MONO_TYPE_VALUETYPE: 
496                 if (type->data.klass->enumtype) {
497                         type = type->data.klass->enum_basetype;
498                         goto handle_enum;
499                 } else {
500                         guint8 *p = va_arg (ap, gpointer);
501                         int j, size, align;
502                         size = mono_type_size (type, &align);
503                         printf ("[");
504                         for (j = 0; p && j < size; j++)
505                                 printf ("%02x,", p [j]);
506                         printf ("]");
507                 }
508                 break;
509         default:
510                 printf ("(unknown return type %x)", method->signature->ret->type);
511         }
512
513         //printf (" ip: %p\n", __builtin_return_address (1));
514         printf ("\n");
515 }