Make sure x86 ATOMIC_CAS doesn't overwrite its own operands.
[mono-debugger.git] / mono / mini / trace.c
blobf4ebc8a726ca39d55e57de2d61b2210fbbdaea13
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 (MonoMethod *method)
31 int include = 0;
32 int i;
34 for (i = 0; i < trace_spec.len; i++){
35 MonoTraceOperation *op = &trace_spec.ops [i];
36 int inc = 0;
38 switch (op->op){
39 case MONO_TRACEOP_ALL:
40 inc = 1; break;
41 case MONO_TRACEOP_PROGRAM:
42 if (trace_spec.assembly && (method->klass->image == mono_assembly_get_image (trace_spec.assembly)))
43 inc = 1; break;
44 case MONO_TRACEOP_METHOD:
45 if (mono_method_desc_full_match ((MonoMethodDesc *) op->data, method))
46 inc = 1; break;
47 case MONO_TRACEOP_CLASS:
48 if (strcmp (method->klass->name_space, op->data) == 0)
49 if (strcmp (method->klass->name, op->data2) == 0)
50 inc = 1;
51 break;
52 case MONO_TRACEOP_ASSEMBLY:
53 if (strcmp (mono_image_get_name (method->klass->image), op->data) == 0)
54 inc = 1; break;
55 case MONO_TRACEOP_NAMESPACE:
56 if (strcmp (method->klass->name_space, op->data) == 0)
57 inc = 1;
59 if (op->exclude){
60 if (inc)
61 include = 0;
62 } else if (inc)
63 include = 1;
65 return include;
68 static int is_filenamechar (char p)
70 if (p >= 'A' && p <= 'Z')
71 return TRUE;
72 if (p >= 'a' && p <= 'z')
73 return TRUE;
74 if (p >= '0' && p <= '9')
75 return TRUE;
76 if (p == '.' || p == ':' || p == '_' || p == '-')
77 return TRUE;
78 return FALSE;
81 static char *input;
82 static char *value;
84 static void get_string (void)
86 char *start = input;
87 while (is_filenamechar (*input)){
88 input++;
90 if (value != NULL)
91 g_free (value);
92 value = g_malloc (input - start + 1);
93 strncpy (value, start, input-start);
94 value [input-start] = 0;
97 enum Token {
98 TOKEN_METHOD,
99 TOKEN_CLASS,
100 TOKEN_ALL,
101 TOKEN_PROGRAM,
102 TOKEN_NAMESPACE,
103 TOKEN_STRING,
104 TOKEN_EXCLUDE,
105 TOKEN_DISABLED,
106 TOKEN_SEPARATOR,
107 TOKEN_END,
108 TOKEN_ERROR
111 static int
112 get_token (void)
114 if (input [0] == '\0') {
115 return TOKEN_END;
117 if (input [0] == 'M' && input [1] == ':'){
118 input += 2;
119 get_string ();
120 return TOKEN_METHOD;
122 if (input [0] == 'N' && input [1] == ':'){
123 input += 2;
124 get_string ();
125 return TOKEN_NAMESPACE;
127 if (input [0] == 'T' && input [1] == ':'){
128 input += 2;
129 get_string ();
130 return TOKEN_CLASS;
132 if (is_filenamechar (*input)){
133 get_string ();
134 if (strcmp (value, "all") == 0)
135 return TOKEN_ALL;
136 if (strcmp (value, "program") == 0)
137 return TOKEN_PROGRAM;
138 if (strcmp (value, "disabled") == 0)
139 return TOKEN_DISABLED;
140 return TOKEN_STRING;
142 if (*input == '-'){
143 input++;
144 return TOKEN_EXCLUDE;
146 if (*input == ','){
147 input++;
148 return TOKEN_SEPARATOR;
151 fprintf (stderr, "Syntax error at or around '%s'\n", input);
152 return TOKEN_ERROR;
155 static void
156 cleanup (void)
158 if (value != NULL)
159 g_free (value);
162 static int
163 get_spec (int *last)
165 int token = get_token ();
166 if (token == TOKEN_EXCLUDE){
167 token = get_spec (last);
168 if (token == TOKEN_EXCLUDE){
169 fprintf (stderr, "Expecting an expression");
170 return TOKEN_ERROR;
172 if (token == TOKEN_ERROR)
173 return token;
174 trace_spec.ops [(*last)-1].exclude = 1;
175 return TOKEN_SEPARATOR;
177 if (token == TOKEN_END || token == TOKEN_SEPARATOR || token == TOKEN_ERROR)
178 return token;
180 if (token == TOKEN_METHOD){
181 MonoMethodDesc *desc = mono_method_desc_new (value, TRUE);
182 if (desc == NULL){
183 fprintf (stderr, "Invalid method name: %s\n", value);
184 return TOKEN_ERROR;
186 trace_spec.ops [*last].op = MONO_TRACEOP_METHOD;
187 trace_spec.ops [*last].data = desc;
188 } else if (token == TOKEN_ALL)
189 trace_spec.ops [*last].op = MONO_TRACEOP_ALL;
190 else if (token == TOKEN_PROGRAM)
191 trace_spec.ops [*last].op = MONO_TRACEOP_PROGRAM;
192 else if (token == TOKEN_NAMESPACE){
193 trace_spec.ops [*last].op = MONO_TRACEOP_NAMESPACE;
194 trace_spec.ops [*last].data = g_strdup (value);
195 } else if (token == TOKEN_CLASS){
196 char *p = strrchr (value, '.');
197 if (p) {
198 *p++ = 0;
199 trace_spec.ops [*last].data = g_strdup (value);
200 trace_spec.ops [*last].data2 = g_strdup (p);
202 else {
203 trace_spec.ops [*last].data = g_strdup ("");
204 trace_spec.ops [*last].data2 = g_strdup (value);
206 trace_spec.ops [*last].op = MONO_TRACEOP_CLASS;
207 } else if (token == TOKEN_STRING){
208 trace_spec.ops [*last].op = MONO_TRACEOP_ASSEMBLY;
209 trace_spec.ops [*last].data = g_strdup (value);
210 } else if (token == TOKEN_DISABLED) {
211 trace_spec.enabled = FALSE;
212 } else {
213 fprintf (stderr, "Syntax error in trace option specification\n");
214 return TOKEN_ERROR;
216 (*last)++;
217 return TOKEN_SEPARATOR;
220 MonoTraceSpec *
221 mono_trace_parse_options (const char *options)
223 char *p = (char*)options;
224 int size = 1;
225 int last_used;
226 int token;
228 trace_spec.enabled = TRUE;
229 if (*p == 0){
230 trace_spec.len = 1;
231 trace_spec.ops = g_new0 (MonoTraceOperation, 1);
232 trace_spec.ops [0].op = MONO_TRACEOP_ALL;
233 return &trace_spec;
236 for (p = (char*)options; *p != 0; p++)
237 if (*p == ',')
238 size++;
240 trace_spec.ops = g_new0 (MonoTraceOperation, size);
242 input = (char*)options;
243 last_used = 0;
245 while ((token = (get_spec (&last_used))) != TOKEN_END){
246 if (token == TOKEN_ERROR)
247 return NULL;
248 if (token == TOKEN_SEPARATOR)
249 continue;
251 trace_spec.len = last_used;
252 cleanup ();
253 return &trace_spec;
256 void
257 mono_trace_set_assembly (MonoAssembly *assembly)
259 trace_spec.assembly = assembly;
262 static
263 #ifdef HAVE_KW_THREAD
264 __thread
265 #endif
266 int indent_level = 0;
267 static guint64 start_time = 0;
269 static double seconds_since_start (void)
271 guint64 diff = mono_100ns_ticks () - start_time;
272 return diff/10000000.0;
275 static void indent (int diff) {
276 int v;
277 if (diff < 0)
278 indent_level += diff;
279 v = indent_level;
280 if (start_time == 0)
281 start_time = mono_100ns_ticks ();
282 printf ("[%p: %.5f %d] ", (void*)GetCurrentThreadId (), seconds_since_start (), indent_level);
283 if (diff > 0)
284 indent_level += diff;
287 static char *
288 string_to_utf8 (MonoString *s)
290 char *as;
291 GError *error = NULL;
293 g_assert (s);
295 if (!s->length)
296 return g_strdup ("");
298 as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, NULL, &error);
299 if (error) {
300 /* Happens with StringBuilders */
301 g_error_free (error);
302 return g_strdup ("<INVALID UTF8>");
304 else
305 return as;
309 * cpos (ebp + arg_info[n].offset) points to the beginning of the
310 * stack slot for this argument. On little-endian systems, we can
311 * simply dereference it. On big-endian systems, we need to adjust
312 * cpos upward first if the datatype we're referencing is smaller than
313 * a stack slot. Also - one can't assume that gpointer is also the
314 * size of a stack slot - use SIZEOF_REGISTER instead. The following
315 * helper macro tries to keep down the mess of all the pointer
316 * calculations.
318 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
319 #define arg_in_stack_slot(cpos, type) ((type *)(cpos))
320 #else
321 #define arg_in_stack_slot(cpos, type) ((type *)((sizeof(type) < SIZEOF_REGISTER) ? (((gssize)(cpos)) + SIZEOF_REGISTER - sizeof(type)) : (gssize)(cpos)))
322 #endif
324 void
325 mono_trace_enter_method (MonoMethod *method, char *ebp)
327 int i, j;
328 MonoClass *class;
329 MonoObject *o;
330 MonoJitArgumentInfo *arg_info;
331 MonoMethodSignature *sig;
332 char *fname;
334 if (!trace_spec.enabled)
335 return;
337 fname = mono_method_full_name (method, TRUE);
338 indent (1);
339 printf ("ENTER: %s(", fname);
340 g_free (fname);
342 if (!ebp) {
343 printf (") ip: %p\n", __builtin_return_address (1));
344 return;
347 sig = mono_method_signature (method);
349 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
351 mono_arch_get_argument_info (sig, sig->param_count, arg_info);
353 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret)) {
354 g_assert (!mono_method_signature (method)->ret->byref);
356 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
359 if (mono_method_signature (method)->hasthis) {
360 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
361 if (method->klass->valuetype) {
362 printf ("value:%p, ", *arg_in_stack_slot(this, gpointer *));
363 } else {
364 o = *arg_in_stack_slot(this, MonoObject *);
366 if (o) {
367 class = o->vtable->klass;
369 if (class == mono_defaults.string_class) {
370 MonoString *s = (MonoString*)o;
371 char *as = string_to_utf8 (s);
373 printf ("this:[STRING:%p:%s], ", o, as);
374 g_free (as);
375 } else {
376 printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
378 } else
379 printf ("this:NULL, ");
383 for (i = 0; i < mono_method_signature (method)->param_count; ++i) {
384 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
385 int size = arg_info [i + 1].size;
387 MonoType *type = mono_method_signature (method)->params [i];
389 if (type->byref) {
390 printf ("[BYREF:%p], ", *arg_in_stack_slot(cpos, gpointer *));
391 } else switch (mono_type_get_underlying_type (type)->type) {
393 case MONO_TYPE_I:
394 case MONO_TYPE_U:
395 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer *));
396 break;
397 case MONO_TYPE_BOOLEAN:
398 case MONO_TYPE_CHAR:
399 case MONO_TYPE_I1:
400 case MONO_TYPE_U1:
401 printf ("%d, ", *arg_in_stack_slot(cpos, gint8));
402 break;
403 case MONO_TYPE_I2:
404 case MONO_TYPE_U2:
405 printf ("%d, ", *arg_in_stack_slot(cpos, gint16));
406 break;
407 case MONO_TYPE_I4:
408 case MONO_TYPE_U4:
409 printf ("%d, ", *arg_in_stack_slot(cpos, int));
410 break;
411 case MONO_TYPE_STRING: {
412 MonoString *s = *arg_in_stack_slot(cpos, MonoString *);
413 if (s) {
414 char *as;
416 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
417 as = string_to_utf8 (s);
419 printf ("[STRING:%p:%s], ", s, as);
420 g_free (as);
421 } else
422 printf ("[STRING:null], ");
423 break;
425 case MONO_TYPE_CLASS:
426 case MONO_TYPE_OBJECT: {
427 o = *arg_in_stack_slot(cpos, MonoObject *);
428 if (o) {
429 class = o->vtable->klass;
431 if (class == mono_defaults.string_class) {
432 char *as = string_to_utf8 ((MonoString*)o);
434 printf ("[STRING:%p:%s], ", o, as);
435 g_free (as);
436 } else if (class == mono_defaults.int32_class) {
437 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
438 } else if (class == mono_defaults.monotype_class) {
439 printf ("[TYPE:%s], ", mono_type_full_name (((MonoReflectionType*)o)->type));
440 } else
441 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
442 } else {
443 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
445 break;
447 case MONO_TYPE_PTR:
448 case MONO_TYPE_FNPTR:
449 case MONO_TYPE_ARRAY:
450 case MONO_TYPE_SZARRAY:
451 printf ("%p, ", *arg_in_stack_slot(cpos, gpointer));
452 break;
453 case MONO_TYPE_I8:
454 case MONO_TYPE_U8:
455 printf ("0x%016llx, ", (long long)*arg_in_stack_slot(cpos, gint64));
456 break;
457 case MONO_TYPE_R4:
458 printf ("%f, ", *arg_in_stack_slot(cpos, float));
459 break;
460 case MONO_TYPE_R8:
461 printf ("%f, ", *arg_in_stack_slot(cpos, double));
462 break;
463 case MONO_TYPE_VALUETYPE:
464 printf ("[");
465 for (j = 0; j < size; j++)
466 printf ("%02x,", *((guint8*)cpos +j));
467 printf ("], ");
468 break;
469 default:
470 printf ("XX, ");
474 printf (")\n");
475 fflush (stdout);
478 void
479 mono_trace_leave_method (MonoMethod *method, ...)
481 MonoType *type;
482 char *fname;
483 va_list ap;
485 if (!trace_spec.enabled)
486 return;
488 va_start(ap, method);
490 fname = mono_method_full_name (method, TRUE);
491 indent (-1);
492 printf ("LEAVE: %s", fname);
493 g_free (fname);
495 type = mono_method_signature (method)->ret;
497 handle_enum:
498 switch (type->type) {
499 case MONO_TYPE_VOID:
500 break;
501 case MONO_TYPE_BOOLEAN: {
502 int eax = va_arg (ap, int);
503 if (eax)
504 printf ("TRUE:%d", eax);
505 else
506 printf ("FALSE");
508 break;
510 case MONO_TYPE_CHAR:
511 case MONO_TYPE_I1:
512 case MONO_TYPE_U1:
513 case MONO_TYPE_I2:
514 case MONO_TYPE_U2:
515 case MONO_TYPE_I4:
516 case MONO_TYPE_U4:
517 case MONO_TYPE_I:
518 case MONO_TYPE_U: {
519 int eax = va_arg (ap, int);
520 printf ("result=%d", eax);
521 break;
523 case MONO_TYPE_STRING: {
524 MonoString *s = va_arg (ap, MonoString *);
526 if (s) {
527 char *as;
529 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
530 as = string_to_utf8 (s);
531 printf ("[STRING:%p:%s]", s, as);
532 g_free (as);
533 } else
534 printf ("[STRING:null], ");
535 break;
537 case MONO_TYPE_CLASS:
538 case MONO_TYPE_OBJECT: {
539 MonoObject *o = va_arg (ap, MonoObject *);
541 if (o) {
542 if (o->vtable->klass == mono_defaults.boolean_class) {
543 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
544 } else if (o->vtable->klass == mono_defaults.int32_class) {
545 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
546 } else if (o->vtable->klass == mono_defaults.int64_class) {
547 printf ("[INT64:%p:%lld]", o, (long long)*((gint64 *)((char *)o + sizeof (MonoObject))));
548 } else
549 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
550 } else
551 printf ("[OBJECT:%p]", o);
553 break;
555 case MONO_TYPE_PTR:
556 case MONO_TYPE_FNPTR:
557 case MONO_TYPE_ARRAY:
558 case MONO_TYPE_SZARRAY: {
559 gpointer p = va_arg (ap, gpointer);
560 printf ("result=%p", p);
561 break;
563 case MONO_TYPE_I8: {
564 gint64 l = va_arg (ap, gint64);
565 printf ("lresult=0x%16llx", (long long)l);
566 break;
568 case MONO_TYPE_U8: {
569 gint64 l = va_arg (ap, gint64);
570 printf ("lresult=0x%16llx", (long long)l);
571 break;
573 case MONO_TYPE_R4:
574 case MONO_TYPE_R8: {
575 double f = va_arg (ap, double);
576 printf ("FP=%f\n", f);
577 break;
579 case MONO_TYPE_VALUETYPE:
580 if (type->data.klass->enumtype) {
581 type = mono_class_enum_basetype (type->data.klass);
582 goto handle_enum;
583 } else {
584 guint8 *p = va_arg (ap, gpointer);
585 int j, size, align;
586 size = mono_type_size (type, &align);
587 printf ("[");
588 for (j = 0; p && j < size; j++)
589 printf ("%02x,", p [j]);
590 printf ("]");
592 break;
593 default:
594 printf ("(unknown return type %x)", mono_method_signature (method)->ret->type);
597 //printf (" ip: %p\n", __builtin_return_address (1));
598 printf ("\n");
599 fflush (stdout);
602 void
603 mono_trace_enable (gboolean enable)
605 trace_spec.enabled = enable;
608 gboolean
609 mono_trace_is_enabled ()
611 return trace_spec.enabled;