2 * trace.c: Tracing facilities for the Mono Runtime.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
9 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
22 #include <mono/metadata/debug-helpers.h>
23 #include <mono/metadata/assembly.h>
24 #include <mono/utils/mono-time.h>
27 static MonoTraceSpec trace_spec;
30 mono_trace_eval_exception (MonoClass *klass)
38 for (i = 0; i < trace_spec.len; i++) {
39 MonoTraceOperation *op = &trace_spec.ops [i];
43 case MONO_TRACEOP_EXCEPTION:
44 if (strcmp ("", op->data) == 0 && strcmp ("all", op->data2) == 0)
46 else if (strcmp ("", op->data) == 0 || strcmp (klass->name_space, op->data) == 0)
47 if (strcmp (klass->name, op->data2) == 0)
64 mono_trace_eval (MonoMethod *method)
69 for (i = 0; i < trace_spec.len; i++){
70 MonoTraceOperation *op = &trace_spec.ops [i];
74 case MONO_TRACEOP_ALL:
76 case MONO_TRACEOP_PROGRAM:
77 if (trace_spec.assembly && (method->klass->image == mono_assembly_get_image (trace_spec.assembly)))
79 case MONO_TRACEOP_WRAPPER:
80 if ((method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) ||
81 (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE))
83 case MONO_TRACEOP_METHOD:
84 if (mono_method_desc_full_match ((MonoMethodDesc *) op->data, method))
86 case MONO_TRACEOP_CLASS:
87 if (strcmp (method->klass->name_space, op->data) == 0)
88 if (strcmp (method->klass->name, op->data2) == 0)
91 case MONO_TRACEOP_ASSEMBLY:
92 if (strcmp (mono_image_get_name (method->klass->image), op->data) == 0)
94 case MONO_TRACEOP_NAMESPACE:
95 if (strcmp (method->klass->name_space, op->data) == 0)
97 case MONO_TRACEOP_EXCEPTION:
109 static int is_filenamechar (char p)
111 if (p >= 'A' && p <= 'Z')
113 if (p >= 'a' && p <= 'z')
115 if (p >= '0' && p <= '9')
117 if (p == '.' || p == ':' || p == '_' || p == '-')
125 static void get_string (void)
128 while (is_filenamechar (*input)){
133 value = g_malloc (input - start + 1);
134 strncpy (value, start, input-start);
135 value [input-start] = 0;
157 while (input [0] == '+')
160 if (input [0] == '\0') {
163 if (input [0] == 'M' && input [1] == ':'){
168 if (input [0] == 'N' && input [1] == ':'){
171 return TOKEN_NAMESPACE;
173 if (input [0] == 'T' && input [1] == ':'){
178 if (input [0] == 'E' && input [1] == ':'){
181 return TOKEN_EXCEPTION;
185 return TOKEN_EXCLUDE;
187 if (is_filenamechar (*input)){
189 if (strcmp (value, "all") == 0)
191 if (strcmp (value, "program") == 0)
192 return TOKEN_PROGRAM;
193 if (strcmp (value, "wrapper") == 0)
194 return TOKEN_WRAPPER;
195 if (strcmp (value, "disabled") == 0)
196 return TOKEN_DISABLED;
201 return TOKEN_SEPARATOR;
204 fprintf (stderr, "Syntax error at or around '%s'\n", input);
218 int token = get_token ();
219 if (token == TOKEN_EXCLUDE){
220 token = get_spec (last);
221 if (token == TOKEN_EXCLUDE){
222 fprintf (stderr, "Expecting an expression");
225 if (token == TOKEN_ERROR)
227 trace_spec.ops [(*last)-1].exclude = 1;
228 return TOKEN_SEPARATOR;
230 if (token == TOKEN_END || token == TOKEN_SEPARATOR || token == TOKEN_ERROR)
233 if (token == TOKEN_METHOD){
234 MonoMethodDesc *desc = mono_method_desc_new (value, TRUE);
236 fprintf (stderr, "Invalid method name: %s\n", value);
239 trace_spec.ops [*last].op = MONO_TRACEOP_METHOD;
240 trace_spec.ops [*last].data = desc;
241 } else if (token == TOKEN_ALL)
242 trace_spec.ops [*last].op = MONO_TRACEOP_ALL;
243 else if (token == TOKEN_PROGRAM)
244 trace_spec.ops [*last].op = MONO_TRACEOP_PROGRAM;
245 else if (token == TOKEN_WRAPPER)
246 trace_spec.ops [*last].op = MONO_TRACEOP_WRAPPER;
247 else if (token == TOKEN_NAMESPACE){
248 trace_spec.ops [*last].op = MONO_TRACEOP_NAMESPACE;
249 trace_spec.ops [*last].data = g_strdup (value);
250 } else if (token == TOKEN_CLASS || token == TOKEN_EXCEPTION){
251 char *p = strrchr (value, '.');
254 trace_spec.ops [*last].data = g_strdup (value);
255 trace_spec.ops [*last].data2 = g_strdup (p);
258 trace_spec.ops [*last].data = g_strdup ("");
259 trace_spec.ops [*last].data2 = g_strdup (value);
261 trace_spec.ops [*last].op = token == TOKEN_CLASS ? MONO_TRACEOP_CLASS : MONO_TRACEOP_EXCEPTION;
262 } else if (token == TOKEN_STRING){
263 trace_spec.ops [*last].op = MONO_TRACEOP_ASSEMBLY;
264 trace_spec.ops [*last].data = g_strdup (value);
265 } else if (token == TOKEN_DISABLED) {
266 trace_spec.enabled = FALSE;
268 fprintf (stderr, "Syntax error in trace option specification\n");
272 return TOKEN_SEPARATOR;
276 mono_trace_parse_options (const char *options)
278 char *p = (char*)options;
283 trace_spec.enabled = TRUE;
286 trace_spec.ops = g_new0 (MonoTraceOperation, 1);
287 trace_spec.ops [0].op = MONO_TRACEOP_ALL;
291 for (p = (char*)options; *p != 0; p++)
295 trace_spec.ops = g_new0 (MonoTraceOperation, size);
297 input = (char*)options;
300 while ((token = (get_spec (&last_used))) != TOKEN_END){
301 if (token == TOKEN_ERROR)
303 if (token == TOKEN_SEPARATOR)
306 trace_spec.len = last_used;
312 mono_trace_set_assembly (MonoAssembly *assembly)
314 trace_spec.assembly = assembly;
318 #ifdef HAVE_KW_THREAD
321 int indent_level = 0;
322 static guint64 start_time = 0;
324 static double seconds_since_start (void)
326 guint64 diff = mono_100ns_ticks () - start_time;
327 return diff/10000000.0;
330 static void indent (int diff) {
332 indent_level += diff;
334 start_time = mono_100ns_ticks ();
335 printf ("[%p: %.5f %d] ", (void*)GetCurrentThreadId (), seconds_since_start (), indent_level);
337 indent_level += diff;
341 string_to_utf8 (MonoString *s)
344 GError *error = NULL;
349 return g_strdup ("");
351 as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, NULL, &error);
353 /* Happens with StringBuilders */
354 g_error_free (error);
355 return g_strdup ("<INVALID UTF8>");
362 * cpos (ebp + arg_info[n].offset) points to the beginning of the
363 * stack slot for this argument. On little-endian systems, we can
364 * simply dereference it. On big-endian systems, we need to adjust
365 * cpos upward first if the datatype we're referencing is smaller than
366 * a stack slot. Also - one can't assume that gpointer is also the
367 * size of a stack slot - use SIZEOF_REGISTER instead. The following
368 * helper macro tries to keep down the mess of all the pointer
371 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
372 #define arg_in_stack_slot(cpos, type) ((type *)(cpos))
374 #define arg_in_stack_slot(cpos, type) ((type *)((sizeof(type) < SIZEOF_REGISTER) ? (((gssize)(cpos)) + SIZEOF_REGISTER - sizeof(type)) : (gssize)(cpos)))
378 mono_trace_enter_method (MonoMethod *method, char *ebp)
383 MonoJitArgumentInfo *arg_info;
384 MonoMethodSignature *sig;
387 if (!trace_spec.enabled)
390 fname = mono_method_full_name (method, TRUE);
392 printf ("ENTER: %s(", fname);
396 printf (") ip: %p\n", __builtin_return_address (1));
400 sig = mono_method_signature (method);
402 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
404 mono_arch_get_argument_info (NULL, sig, sig->param_count, arg_info);
406 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret)) {
407 g_assert (!mono_method_signature (method)->ret->byref);
409 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
412 if (mono_method_signature (method)->hasthis) {
413 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
414 if (method->klass->valuetype) {
415 printf ("value:%p, ", *arg_in_stack_slot(this, gpointer *));
417 o = *arg_in_stack_slot(this, MonoObject *);
420 class = o->vtable->klass;
422 if (class == mono_defaults.string_class) {
423 MonoString *s = (MonoString*)o;
424 char *as = string_to_utf8 (s);
426 printf ("this:[STRING:%p:%s], ", o, as);
429 printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
432 printf ("this:NULL, ");
436 for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
437 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
438 int size = arg_info [i + 1].size;
440 MonoType *type = mono_method_signature (method)->params [i];
443 printf ("[BYREF:%p], ", *arg_in_stack_slot(cpos, gpointer *));
444 } else switch (mono_type_get_underlying_type (type)->type) {
448 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer *));
450 case MONO_TYPE_BOOLEAN:
454 printf ("%d, ", *arg_in_stack_slot(cpos, gint8));
458 printf ("%d, ", *arg_in_stack_slot(cpos, gint16));
462 printf ("%d, ", *arg_in_stack_slot(cpos, int));
464 case MONO_TYPE_STRING: {
465 MonoString *s = *arg_in_stack_slot(cpos, MonoString *);
469 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
470 as = string_to_utf8 (s);
472 printf ("[STRING:%p:%s], ", s, as);
475 printf ("[STRING:null], ");
478 case MONO_TYPE_CLASS:
479 case MONO_TYPE_OBJECT: {
480 o = *arg_in_stack_slot(cpos, MonoObject *);
482 class = o->vtable->klass;
484 if (class == mono_defaults.string_class) {
485 char *as = string_to_utf8 ((MonoString*)o);
487 printf ("[STRING:%p:%s], ", o, as);
489 } else if (class == mono_defaults.int32_class) {
490 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
491 } else if (class == mono_defaults.monotype_class) {
492 printf ("[TYPE:%s], ", mono_type_full_name (((MonoReflectionType*)o)->type));
494 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
496 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
501 case MONO_TYPE_FNPTR:
502 case MONO_TYPE_ARRAY:
503 case MONO_TYPE_SZARRAY:
504 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
508 printf ("0x%016llx, ", (long long)*arg_in_stack_slot(cpos, gint64));
511 printf ("%f, ", *arg_in_stack_slot(cpos, float));
514 printf ("%f, ", *arg_in_stack_slot(cpos, double));
516 case MONO_TYPE_VALUETYPE:
518 for (j = 0; j < size; j++)
519 printf ("%02x,", *((guint8*)cpos +j));
532 mono_trace_leave_method (MonoMethod *method, ...)
538 if (!trace_spec.enabled)
541 va_start(ap, method);
543 fname = mono_method_full_name (method, TRUE);
545 printf ("LEAVE: %s", fname);
548 type = mono_method_signature (method)->ret;
551 switch (type->type) {
554 case MONO_TYPE_BOOLEAN: {
555 int eax = va_arg (ap, int);
557 printf ("TRUE:%d", eax);
572 int eax = va_arg (ap, int);
573 printf ("result=%d", eax);
576 case MONO_TYPE_STRING: {
577 MonoString *s = va_arg (ap, MonoString *);
582 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
583 as = string_to_utf8 (s);
584 printf ("[STRING:%p:%s]", s, as);
587 printf ("[STRING:null], ");
590 case MONO_TYPE_CLASS:
591 case MONO_TYPE_OBJECT: {
592 MonoObject *o = va_arg (ap, MonoObject *);
595 if (o->vtable->klass == mono_defaults.boolean_class) {
596 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
597 } else if (o->vtable->klass == mono_defaults.int32_class) {
598 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
599 } else if (o->vtable->klass == mono_defaults.int64_class) {
600 printf ("[INT64:%p:%lld]", o, (long long)*((gint64 *)((char *)o + sizeof (MonoObject))));
602 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
604 printf ("[OBJECT:%p]", o);
609 case MONO_TYPE_FNPTR:
610 case MONO_TYPE_ARRAY:
611 case MONO_TYPE_SZARRAY: {
612 gpointer p = va_arg (ap, gpointer);
613 printf ("result=%p", p);
617 gint64 l = va_arg (ap, gint64);
618 printf ("lresult=0x%16llx", (long long)l);
622 gint64 l = va_arg (ap, gint64);
623 printf ("lresult=0x%16llx", (long long)l);
628 double f = va_arg (ap, double);
629 printf ("FP=%f\n", f);
632 case MONO_TYPE_VALUETYPE:
633 if (type->data.klass->enumtype) {
634 type = mono_class_enum_basetype (type->data.klass);
637 guint8 *p = va_arg (ap, gpointer);
639 size = mono_type_size (type, &align);
641 for (j = 0; p && j < size; j++)
642 printf ("%02x,", p [j]);
647 printf ("(unknown return type %x)", mono_method_signature (method)->ret->type);
650 //printf (" ip: %p\n", __builtin_return_address (1));
656 mono_trace_enable (gboolean enable)
658 trace_spec.enabled = enable;
662 mono_trace_is_enabled ()
664 return trace_spec.enabled;