3 * Tracing facilities for the Mono Runtime.
6 * Paolo Molaro (lupus@ximian.com)
7 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2002 Ximian, Inc.
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
23 #include <mono/metadata/debug-helpers.h>
24 #include <mono/utils/mono-time.h>
25 #include <mono/utils/mono-memory-model.h>
27 #include <mono/metadata/callspec.h>
29 #if defined (HOST_ANDROID) || (defined (TARGET_IOS) && defined (TARGET_IOS))
31 # define printf(...) g_log("mono", G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
33 # define fprintf(__ignore, ...) g_log ("mono-gc", G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
36 static MonoCallSpec trace_spec;
38 static volatile gint32 output_lock = 0;
40 gboolean mono_trace_eval_exception (MonoClass *klass)
42 return mono_callspec_eval_exception (klass, &trace_spec);
45 gboolean mono_trace_eval (MonoMethod *method)
47 return mono_callspec_eval (method, &trace_spec);
50 MonoCallSpec *mono_trace_set_options (const char *options)
53 if (!mono_callspec_parse (options, &trace_spec, &errstr)) {
54 fprintf (stderr, "%s\n", errstr);
67 static guint64 start_time = 0;
69 static double seconds_since_start (void)
71 guint64 diff = mono_100ns_ticks () - start_time;
72 return diff/10000000.0;
75 static void indent (int diff) {
79 start_time = mono_100ns_ticks ();
80 printf ("[%p: %.5f %d] ", (void*)mono_native_thread_id_get (), seconds_since_start (), indent_level);
86 string_to_utf8 (MonoString *s)
96 as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, NULL, &error);
98 /* Happens with StringBuilders */
100 return g_strdup ("<INVALID UTF8>");
107 * cpos (ebp + arg_info[n].offset) points to the beginning of the
108 * stack slot for this argument. On little-endian systems, we can
109 * simply dereference it. On big-endian systems, we need to adjust
110 * cpos upward first if the datatype we're referencing is smaller than
111 * a stack slot. Also - one can't assume that gpointer is also the
112 * size of a stack slot - use SIZEOF_REGISTER instead. The following
113 * helper macro tries to keep down the mess of all the pointer
116 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
117 #define arg_in_stack_slot(cpos, type) ((type *)(cpos))
119 #define arg_in_stack_slot(cpos, type) ((type *)((sizeof(type) < SIZEOF_REGISTER) ? (((gssize)(cpos)) + SIZEOF_REGISTER - sizeof(type)) : (gssize)(cpos)))
123 mono_trace_enter_method (MonoMethod *method, char *ebp)
128 MonoJitArgumentInfo *arg_info;
129 MonoMethodSignature *sig;
131 MonoGenericSharingContext *gsctx = NULL;
133 if (!trace_spec.enabled)
136 while (output_lock != 0 || InterlockedCompareExchange (&output_lock, 1, 0) != 0)
137 mono_thread_info_yield ();
139 fname = mono_method_full_name (method, TRUE);
141 printf ("ENTER: %s(", fname);
145 printf (") ip: %p\n", MONO_RETURN_ADDRESS_N (1));
149 sig = mono_method_signature (method);
151 arg_info = (MonoJitArgumentInfo *)alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
153 if (method->is_inflated) {
154 /* FIXME: Might be better to pass the ji itself */
155 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (char *)MONO_RETURN_ADDRESS (), NULL);
157 gsctx = mono_jit_info_get_generic_sharing_context (ji);
158 if (gsctx && gsctx->is_gsharedvt) {
159 /* Needs a ctx to get precise method */
160 printf (") <gsharedvt>\n");
166 mono_arch_get_argument_info (sig, sig->param_count, arg_info);
168 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret)) {
169 g_assert (!mono_method_signature (method)->ret->byref);
171 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
174 if (mono_method_signature (method)->hasthis) {
175 gpointer *this_obj = (gpointer *)(ebp + arg_info [0].offset);
176 if (method->klass->valuetype) {
177 printf ("value:%p, ", *arg_in_stack_slot(this_obj, gpointer *));
179 o = *arg_in_stack_slot(this_obj, MonoObject *);
182 klass = o->vtable->klass;
184 if (klass == mono_defaults.string_class) {
185 MonoString *s = (MonoString*)o;
186 char *as = string_to_utf8 (s);
188 printf ("this:[STRING:%p:%s], ", o, as);
191 printf ("this:%p[%s.%s %s], ", o, klass->name_space, klass->name, o->vtable->domain->friendly_name);
194 printf ("this:NULL, ");
198 for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
199 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
200 int size = arg_info [i + 1].size;
202 MonoType *type = mono_method_signature (method)->params [i];
205 printf ("[BYREF:%p], ", *arg_in_stack_slot(cpos, gpointer *));
206 } else switch (mini_get_underlying_type (type)->type) {
210 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer *));
212 case MONO_TYPE_BOOLEAN:
216 printf ("%d, ", *arg_in_stack_slot(cpos, gint8));
220 printf ("%d, ", *arg_in_stack_slot(cpos, gint16));
224 printf ("%d, ", *arg_in_stack_slot(cpos, int));
226 case MONO_TYPE_STRING: {
227 MonoString *s = *arg_in_stack_slot(cpos, MonoString *);
231 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
232 as = string_to_utf8 (s);
234 printf ("[STRING:%p:%s], ", s, as);
237 printf ("[STRING:null], ");
240 case MONO_TYPE_CLASS:
241 case MONO_TYPE_OBJECT: {
242 o = *arg_in_stack_slot(cpos, MonoObject *);
244 klass = o->vtable->klass;
246 if (klass == mono_defaults.string_class) {
247 char *as = string_to_utf8 ((MonoString*)o);
249 printf ("[STRING:%p:%s], ", o, as);
251 } else if (klass == mono_defaults.int32_class) {
252 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
253 } else if (klass == mono_defaults.runtimetype_class) {
254 printf ("[TYPE:%s], ", mono_type_full_name (((MonoReflectionType*)o)->type));
256 printf ("[%s.%s:%p], ", klass->name_space, klass->name, o);
258 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
263 case MONO_TYPE_FNPTR:
264 case MONO_TYPE_ARRAY:
265 case MONO_TYPE_SZARRAY:
266 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
270 printf ("0x%016llx, ", (long long)*arg_in_stack_slot(cpos, gint64));
273 printf ("%f, ", *arg_in_stack_slot(cpos, float));
276 printf ("%f, ", *arg_in_stack_slot(cpos, double));
278 case MONO_TYPE_VALUETYPE:
280 for (j = 0; j < size; j++)
281 printf ("%02x,", *((guint8*)cpos +j));
293 mono_atomic_store_release (&output_lock, 0);
297 mono_trace_leave_method (MonoMethod *method, ...)
302 MonoGenericSharingContext *gsctx;
304 if (!trace_spec.enabled)
307 while (output_lock != 0 || InterlockedCompareExchange (&output_lock, 1, 0) != 0)
308 mono_thread_info_yield ();
310 va_start(ap, method);
312 fname = mono_method_full_name (method, TRUE);
314 printf ("LEAVE: %s", fname);
317 if (method->is_inflated) {
318 /* FIXME: Might be better to pass the ji itself */
319 MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (char *)MONO_RETURN_ADDRESS (), NULL);
321 gsctx = mono_jit_info_get_generic_sharing_context (ji);
322 if (gsctx && gsctx->is_gsharedvt) {
323 /* Needs a ctx to get precise method */
324 printf (") <gsharedvt>\n");
330 type = mini_get_underlying_type (mono_method_signature (method)->ret);
332 switch (type->type) {
335 case MONO_TYPE_BOOLEAN: {
336 int eax = va_arg (ap, int);
338 printf ("TRUE:%d", eax);
353 int eax = va_arg (ap, int);
354 printf ("result=%d", eax);
357 case MONO_TYPE_STRING: {
358 MonoString *s = va_arg (ap, MonoString *);
363 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
364 as = string_to_utf8 (s);
365 printf ("[STRING:%p:%s]", s, as);
368 printf ("[STRING:null], ");
371 case MONO_TYPE_CLASS:
372 case MONO_TYPE_OBJECT: {
373 MonoObject *o = va_arg (ap, MonoObject *);
376 if (o->vtable->klass == mono_defaults.boolean_class) {
377 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
378 } else if (o->vtable->klass == mono_defaults.int32_class) {
379 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
380 } else if (o->vtable->klass == mono_defaults.int64_class) {
381 printf ("[INT64:%p:%lld]", o, (long long)*((gint64 *)((char *)o + sizeof (MonoObject))));
383 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
385 printf ("[OBJECT:%p]", o);
390 case MONO_TYPE_FNPTR:
391 case MONO_TYPE_ARRAY:
392 case MONO_TYPE_SZARRAY: {
393 gpointer p = va_arg (ap, gpointer);
394 printf ("result=%p", p);
398 gint64 l = va_arg (ap, gint64);
399 printf ("lresult=0x%16llx", (long long)l);
403 gint64 l = va_arg (ap, gint64);
404 printf ("lresult=0x%16llx", (long long)l);
409 double f = va_arg (ap, double);
413 case MONO_TYPE_VALUETYPE: {
414 guint8 *p = (guint8 *)va_arg (ap, gpointer);
416 size = mono_type_size (type, &align);
418 for (j = 0; p && j < size; j++)
419 printf ("%02x,", p [j]);
424 printf ("(unknown return type %x)", mono_method_signature (method)->ret->type);
427 //printf (" ip: %p\n", MONO_RETURN_ADDRESS_N (1));
432 mono_atomic_store_release (&output_lock, 0);
436 mono_trace_enable (gboolean enable)
438 trace_spec.enabled = enable;
442 mono_trace_is_enabled ()
444 return trace_spec.enabled;