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