2010-04-01 Zoltan Varga <vargaz@gmail.com>
[mono/afaerber.git] / mono / mini / trace.c
blob6348a434fb22abb8e9ec44cdf7038c126271dbb9
1 /*
2 * trace.c: Tracing facilities for the Mono Runtime.
4 * Author:
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
9 */
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"
26 static MonoTraceSpec trace_spec;
28 gboolean
29 mono_trace_eval_exception (MonoClass *klass)
31 int include = 0;
32 int i;
34 if (!klass)
35 return FALSE;
37 for (i = 0; i < trace_spec.len; i++) {
38 MonoTraceOperation *op = &trace_spec.ops [i];
39 int inc = 0;
41 switch (op->op){
42 case MONO_TRACEOP_EXCEPTION:
43 if (strcmp ("", op->data) == 0 && strcmp ("all", op->data2) == 0)
44 inc = 1;
45 else if (strcmp ("", op->data) == 0 || strcmp (klass->name_space, op->data) == 0)
46 if (strcmp (klass->name, op->data2) == 0)
47 inc = 1;
48 break;
49 default:
50 break;
52 if (op->exclude){
53 if (inc)
54 include = 0;
55 } else if (inc)
56 include = 1;
59 return include;
62 gboolean
63 mono_trace_eval (MonoMethod *method)
65 int include = 0;
66 int i;
68 for (i = 0; i < trace_spec.len; i++){
69 MonoTraceOperation *op = &trace_spec.ops [i];
70 int inc = 0;
72 switch (op->op){
73 case MONO_TRACEOP_ALL:
74 inc = 1; break;
75 case MONO_TRACEOP_PROGRAM:
76 if (trace_spec.assembly && (method->klass->image == mono_assembly_get_image (trace_spec.assembly)))
77 inc = 1; break;
78 case MONO_TRACEOP_METHOD:
79 if (mono_method_desc_full_match ((MonoMethodDesc *) op->data, method))
80 inc = 1; break;
81 case MONO_TRACEOP_CLASS:
82 if (strcmp (method->klass->name_space, op->data) == 0)
83 if (strcmp (method->klass->name, op->data2) == 0)
84 inc = 1;
85 break;
86 case MONO_TRACEOP_ASSEMBLY:
87 if (strcmp (mono_image_get_name (method->klass->image), op->data) == 0)
88 inc = 1; break;
89 case MONO_TRACEOP_NAMESPACE:
90 if (strcmp (method->klass->name_space, op->data) == 0)
91 inc = 1;
92 case MONO_TRACEOP_EXCEPTION:
93 break;
95 if (op->exclude){
96 if (inc)
97 include = 0;
98 } else if (inc)
99 include = 1;
101 return include;
104 static int is_filenamechar (char p)
106 if (p >= 'A' && p <= 'Z')
107 return TRUE;
108 if (p >= 'a' && p <= 'z')
109 return TRUE;
110 if (p >= '0' && p <= '9')
111 return TRUE;
112 if (p == '.' || p == ':' || p == '_' || p == '-')
113 return TRUE;
114 return FALSE;
117 static char *input;
118 static char *value;
120 static void get_string (void)
122 char *start = input;
123 while (is_filenamechar (*input)){
124 input++;
126 if (value != NULL)
127 g_free (value);
128 value = g_malloc (input - start + 1);
129 strncpy (value, start, input-start);
130 value [input-start] = 0;
133 enum Token {
134 TOKEN_METHOD,
135 TOKEN_CLASS,
136 TOKEN_ALL,
137 TOKEN_PROGRAM,
138 TOKEN_EXCEPTION,
139 TOKEN_NAMESPACE,
140 TOKEN_STRING,
141 TOKEN_EXCLUDE,
142 TOKEN_DISABLED,
143 TOKEN_SEPARATOR,
144 TOKEN_END,
145 TOKEN_ERROR
148 static int
149 get_token (void)
151 while (input [0] == '+')
152 input++;
154 if (input [0] == '\0') {
155 return TOKEN_END;
157 if (input [0] == 'M' && input [1] == ':'){
158 input += 2;
159 get_string ();
160 return TOKEN_METHOD;
162 if (input [0] == 'N' && input [1] == ':'){
163 input += 2;
164 get_string ();
165 return TOKEN_NAMESPACE;
167 if (input [0] == 'T' && input [1] == ':'){
168 input += 2;
169 get_string ();
170 return TOKEN_CLASS;
172 if (input [0] == 'E' && input [1] == ':'){
173 input += 2;
174 get_string ();
175 return TOKEN_EXCEPTION;
177 if (*input == '-'){
178 input++;
179 return TOKEN_EXCLUDE;
181 if (is_filenamechar (*input)){
182 get_string ();
183 if (strcmp (value, "all") == 0)
184 return TOKEN_ALL;
185 if (strcmp (value, "program") == 0)
186 return TOKEN_PROGRAM;
187 if (strcmp (value, "disabled") == 0)
188 return TOKEN_DISABLED;
189 return TOKEN_STRING;
191 if (*input == ','){
192 input++;
193 return TOKEN_SEPARATOR;
196 fprintf (stderr, "Syntax error at or around '%s'\n", input);
197 return TOKEN_ERROR;
200 static void
201 cleanup (void)
203 if (value != NULL)
204 g_free (value);
207 static int
208 get_spec (int *last)
210 int token = get_token ();
211 if (token == TOKEN_EXCLUDE){
212 token = get_spec (last);
213 if (token == TOKEN_EXCLUDE){
214 fprintf (stderr, "Expecting an expression");
215 return TOKEN_ERROR;
217 if (token == TOKEN_ERROR)
218 return token;
219 trace_spec.ops [(*last)-1].exclude = 1;
220 return TOKEN_SEPARATOR;
222 if (token == TOKEN_END || token == TOKEN_SEPARATOR || token == TOKEN_ERROR)
223 return token;
225 if (token == TOKEN_METHOD){
226 MonoMethodDesc *desc = mono_method_desc_new (value, TRUE);
227 if (desc == NULL){
228 fprintf (stderr, "Invalid method name: %s\n", value);
229 return TOKEN_ERROR;
231 trace_spec.ops [*last].op = MONO_TRACEOP_METHOD;
232 trace_spec.ops [*last].data = desc;
233 } else if (token == TOKEN_ALL)
234 trace_spec.ops [*last].op = MONO_TRACEOP_ALL;
235 else if (token == TOKEN_PROGRAM)
236 trace_spec.ops [*last].op = MONO_TRACEOP_PROGRAM;
237 else if (token == TOKEN_NAMESPACE){
238 trace_spec.ops [*last].op = MONO_TRACEOP_NAMESPACE;
239 trace_spec.ops [*last].data = g_strdup (value);
240 } else if (token == TOKEN_CLASS || token == TOKEN_EXCEPTION){
241 char *p = strrchr (value, '.');
242 if (p) {
243 *p++ = 0;
244 trace_spec.ops [*last].data = g_strdup (value);
245 trace_spec.ops [*last].data2 = g_strdup (p);
247 else {
248 trace_spec.ops [*last].data = g_strdup ("");
249 trace_spec.ops [*last].data2 = g_strdup (value);
251 trace_spec.ops [*last].op = token == TOKEN_CLASS ? MONO_TRACEOP_CLASS : MONO_TRACEOP_EXCEPTION;
252 } else if (token == TOKEN_STRING){
253 trace_spec.ops [*last].op = MONO_TRACEOP_ASSEMBLY;
254 trace_spec.ops [*last].data = g_strdup (value);
255 } else if (token == TOKEN_DISABLED) {
256 trace_spec.enabled = FALSE;
257 } else {
258 fprintf (stderr, "Syntax error in trace option specification\n");
259 return TOKEN_ERROR;
261 (*last)++;
262 return TOKEN_SEPARATOR;
265 MonoTraceSpec *
266 mono_trace_parse_options (const char *options)
268 char *p = (char*)options;
269 int size = 1;
270 int last_used;
271 int token;
273 trace_spec.enabled = TRUE;
274 if (*p == 0){
275 trace_spec.len = 1;
276 trace_spec.ops = g_new0 (MonoTraceOperation, 1);
277 trace_spec.ops [0].op = MONO_TRACEOP_ALL;
278 return &trace_spec;
281 for (p = (char*)options; *p != 0; p++)
282 if (*p == ',')
283 size++;
285 trace_spec.ops = g_new0 (MonoTraceOperation, size);
287 input = (char*)options;
288 last_used = 0;
290 while ((token = (get_spec (&last_used))) != TOKEN_END){
291 if (token == TOKEN_ERROR)
292 return NULL;
293 if (token == TOKEN_SEPARATOR)
294 continue;
296 trace_spec.len = last_used;
297 cleanup ();
298 return &trace_spec;
301 void
302 mono_trace_set_assembly (MonoAssembly *assembly)
304 trace_spec.assembly = assembly;
307 static
308 #ifdef HAVE_KW_THREAD
309 __thread
310 #endif
311 int indent_level = 0;
312 static guint64 start_time = 0;
314 static double seconds_since_start (void)
316 guint64 diff = mono_100ns_ticks () - start_time;
317 return diff/10000000.0;
320 static void indent (int diff) {
321 int v;
322 if (diff < 0)
323 indent_level += diff;
324 v = indent_level;
325 if (start_time == 0)
326 start_time = mono_100ns_ticks ();
327 printf ("[%p: %.5f %d] ", (void*)GetCurrentThreadId (), seconds_since_start (), indent_level);
328 if (diff > 0)
329 indent_level += diff;
332 static char *
333 string_to_utf8 (MonoString *s)
335 char *as;
336 GError *error = NULL;
338 g_assert (s);
340 if (!s->length)
341 return g_strdup ("");
343 as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, NULL, &error);
344 if (error) {
345 /* Happens with StringBuilders */
346 g_error_free (error);
347 return g_strdup ("<INVALID UTF8>");
349 else
350 return as;
354 * cpos (ebp + arg_info[n].offset) points to the beginning of the
355 * stack slot for this argument. On little-endian systems, we can
356 * simply dereference it. On big-endian systems, we need to adjust
357 * cpos upward first if the datatype we're referencing is smaller than
358 * a stack slot. Also - one can't assume that gpointer is also the
359 * size of a stack slot - use SIZEOF_REGISTER instead. The following
360 * helper macro tries to keep down the mess of all the pointer
361 * calculations.
363 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
364 #define arg_in_stack_slot(cpos, type) ((type *)(cpos))
365 #else
366 #define arg_in_stack_slot(cpos, type) ((type *)((sizeof(type) < SIZEOF_REGISTER) ? (((gssize)(cpos)) + SIZEOF_REGISTER - sizeof(type)) : (gssize)(cpos)))
367 #endif
369 void
370 mono_trace_enter_method (MonoMethod *method, char *ebp)
372 int i, j;
373 MonoClass *class;
374 MonoObject *o;
375 MonoJitArgumentInfo *arg_info;
376 MonoMethodSignature *sig;
377 char *fname;
379 if (!trace_spec.enabled)
380 return;
382 fname = mono_method_full_name (method, TRUE);
383 indent (1);
384 printf ("ENTER: %s(", fname);
385 g_free (fname);
387 if (!ebp) {
388 printf (") ip: %p\n", __builtin_return_address (1));
389 return;
392 sig = mono_method_signature (method);
394 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
396 mono_arch_get_argument_info (sig, sig->param_count, arg_info);
398 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret)) {
399 g_assert (!mono_method_signature (method)->ret->byref);
401 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
404 if (mono_method_signature (method)->hasthis) {
405 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
406 if (method->klass->valuetype) {
407 printf ("value:%p, ", *arg_in_stack_slot(this, gpointer *));
408 } else {
409 o = *arg_in_stack_slot(this, MonoObject *);
411 if (o) {
412 class = o->vtable->klass;
414 if (class == mono_defaults.string_class) {
415 MonoString *s = (MonoString*)o;
416 char *as = string_to_utf8 (s);
418 printf ("this:[STRING:%p:%s], ", o, as);
419 g_free (as);
420 } else {
421 printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
423 } else
424 printf ("this:NULL, ");
428 for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
429 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
430 int size = arg_info [i + 1].size;
432 MonoType *type = mono_method_signature (method)->params [i];
434 if (type->byref) {
435 printf ("[BYREF:%p], ", *arg_in_stack_slot(cpos, gpointer *));
436 } else switch (mono_type_get_underlying_type (type)->type) {
438 case MONO_TYPE_I:
439 case MONO_TYPE_U:
440 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer *));
441 break;
442 case MONO_TYPE_BOOLEAN:
443 case MONO_TYPE_CHAR:
444 case MONO_TYPE_I1:
445 case MONO_TYPE_U1:
446 printf ("%d, ", *arg_in_stack_slot(cpos, gint8));
447 break;
448 case MONO_TYPE_I2:
449 case MONO_TYPE_U2:
450 printf ("%d, ", *arg_in_stack_slot(cpos, gint16));
451 break;
452 case MONO_TYPE_I4:
453 case MONO_TYPE_U4:
454 printf ("%d, ", *arg_in_stack_slot(cpos, int));
455 break;
456 case MONO_TYPE_STRING: {
457 MonoString *s = *arg_in_stack_slot(cpos, MonoString *);
458 if (s) {
459 char *as;
461 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
462 as = string_to_utf8 (s);
464 printf ("[STRING:%p:%s], ", s, as);
465 g_free (as);
466 } else
467 printf ("[STRING:null], ");
468 break;
470 case MONO_TYPE_CLASS:
471 case MONO_TYPE_OBJECT: {
472 o = *arg_in_stack_slot(cpos, MonoObject *);
473 if (o) {
474 class = o->vtable->klass;
476 if (class == mono_defaults.string_class) {
477 char *as = string_to_utf8 ((MonoString*)o);
479 printf ("[STRING:%p:%s], ", o, as);
480 g_free (as);
481 } else if (class == mono_defaults.int32_class) {
482 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
483 } else if (class == mono_defaults.monotype_class) {
484 printf ("[TYPE:%s], ", mono_type_full_name (((MonoReflectionType*)o)->type));
485 } else
486 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
487 } else {
488 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
490 break;
492 case MONO_TYPE_PTR:
493 case MONO_TYPE_FNPTR:
494 case MONO_TYPE_ARRAY:
495 case MONO_TYPE_SZARRAY:
496 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
497 break;
498 case MONO_TYPE_I8:
499 case MONO_TYPE_U8:
500 printf ("0x%016llx, ", (long long)*arg_in_stack_slot(cpos, gint64));
501 break;
502 case MONO_TYPE_R4:
503 printf ("%f, ", *arg_in_stack_slot(cpos, float));
504 break;
505 case MONO_TYPE_R8:
506 printf ("%f, ", *arg_in_stack_slot(cpos, double));
507 break;
508 case MONO_TYPE_VALUETYPE:
509 printf ("[");
510 for (j = 0; j < size; j++)
511 printf ("%02x,", *((guint8*)cpos +j));
512 printf ("], ");
513 break;
514 default:
515 printf ("XX, ");
519 printf (")\n");
520 fflush (stdout);
523 void
524 mono_trace_leave_method (MonoMethod *method, ...)
526 MonoType *type;
527 char *fname;
528 va_list ap;
530 if (!trace_spec.enabled)
531 return;
533 va_start(ap, method);
535 fname = mono_method_full_name (method, TRUE);
536 indent (-1);
537 printf ("LEAVE: %s", fname);
538 g_free (fname);
540 type = mono_method_signature (method)->ret;
542 handle_enum:
543 switch (type->type) {
544 case MONO_TYPE_VOID:
545 break;
546 case MONO_TYPE_BOOLEAN: {
547 int eax = va_arg (ap, int);
548 if (eax)
549 printf ("TRUE:%d", eax);
550 else
551 printf ("FALSE");
553 break;
555 case MONO_TYPE_CHAR:
556 case MONO_TYPE_I1:
557 case MONO_TYPE_U1:
558 case MONO_TYPE_I2:
559 case MONO_TYPE_U2:
560 case MONO_TYPE_I4:
561 case MONO_TYPE_U4:
562 case MONO_TYPE_I:
563 case MONO_TYPE_U: {
564 int eax = va_arg (ap, int);
565 printf ("result=%d", eax);
566 break;
568 case MONO_TYPE_STRING: {
569 MonoString *s = va_arg (ap, MonoString *);
571 if (s) {
572 char *as;
574 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
575 as = string_to_utf8 (s);
576 printf ("[STRING:%p:%s]", s, as);
577 g_free (as);
578 } else
579 printf ("[STRING:null], ");
580 break;
582 case MONO_TYPE_CLASS:
583 case MONO_TYPE_OBJECT: {
584 MonoObject *o = va_arg (ap, MonoObject *);
586 if (o) {
587 if (o->vtable->klass == mono_defaults.boolean_class) {
588 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
589 } else if (o->vtable->klass == mono_defaults.int32_class) {
590 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
591 } else if (o->vtable->klass == mono_defaults.int64_class) {
592 printf ("[INT64:%p:%lld]", o, (long long)*((gint64 *)((char *)o + sizeof (MonoObject))));
593 } else
594 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
595 } else
596 printf ("[OBJECT:%p]", o);
598 break;
600 case MONO_TYPE_PTR:
601 case MONO_TYPE_FNPTR:
602 case MONO_TYPE_ARRAY:
603 case MONO_TYPE_SZARRAY: {
604 gpointer p = va_arg (ap, gpointer);
605 printf ("result=%p", p);
606 break;
608 case MONO_TYPE_I8: {
609 gint64 l = va_arg (ap, gint64);
610 printf ("lresult=0x%16llx", (long long)l);
611 break;
613 case MONO_TYPE_U8: {
614 gint64 l = va_arg (ap, gint64);
615 printf ("lresult=0x%16llx", (long long)l);
616 break;
618 case MONO_TYPE_R4:
619 case MONO_TYPE_R8: {
620 double f = va_arg (ap, double);
621 printf ("FP=%f\n", f);
622 break;
624 case MONO_TYPE_VALUETYPE:
625 if (type->data.klass->enumtype) {
626 type = mono_class_enum_basetype (type->data.klass);
627 goto handle_enum;
628 } else {
629 guint8 *p = va_arg (ap, gpointer);
630 int j, size, align;
631 size = mono_type_size (type, &align);
632 printf ("[");
633 for (j = 0; p && j < size; j++)
634 printf ("%02x,", p [j]);
635 printf ("]");
637 break;
638 default:
639 printf ("(unknown return type %x)", mono_method_signature (method)->ret->type);
642 //printf (" ip: %p\n", __builtin_return_address (1));
643 printf ("\n");
644 fflush (stdout);
647 void
648 mono_trace_enable (gboolean enable)
650 trace_spec.enabled = enable;
653 gboolean
654 mono_trace_is_enabled ()
656 return trace_spec.enabled;