Merge pull request #743 from gvillanueva/master
[mono.git] / mono / mini / debug-mini.c
1 /*
2  * debug-mini.c: Mini-specific debugging stuff.
3  *
4  * Author:
5  *   Martin Baulig (martin@ximian.com)
6  *
7  * (C) 2003 Ximian, Inc.
8  */
9
10 #include "mini.h"
11 #include "jit.h"
12 #include "config.h"
13 #include <mono/metadata/verify.h>
14 #include <mono/metadata/mono-config.h>
15 #include <mono/metadata/mono-debug.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/threads-types.h>
18
19 #define _IN_THE_MONO_DEBUGGER
20 #include <mono/metadata/mono-debug-debugger.h>
21 #include "debug-mini.h"
22
23 #include <mono/utils/valgrind.h>
24
25 #ifdef MONO_DEBUGGER_SUPPORTED
26 #include <libgc/include/libgc-mono-debugger.h>
27 #endif
28
29 typedef struct {
30         guint32 index;
31         MonoMethodDesc *desc;
32 } MiniDebugBreakpointInfo;
33
34 typedef struct
35 {
36         MonoDebugMethodJitInfo *jit;
37         GArray *line_numbers;
38         guint32 has_line_numbers;
39         guint32 breakpoint_id;
40 } MiniDebugMethodInfo;
41
42 typedef struct {
43         MonoObject *last_exception;
44         guint32 stopped_on_exception : 1;
45         guint32 stopped_on_unhandled : 1;
46 } MonoDebuggerExceptionState;
47
48 typedef enum {
49         MONO_DEBUGGER_THREAD_FLAGS_NONE         = 0,
50         MONO_DEBUGGER_THREAD_FLAGS_INTERNAL     = 1,
51         MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL   = 2
52 } MonoDebuggerThreadFlags;
53
54 typedef enum {
55         MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_NONE                = 0,
56         MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE   = 1,
57         MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED     = 2
58 } MonoDebuggerInternalThreadFlags;
59
60 struct _MonoDebuggerThreadInfo {
61         guint64 tid;
62         guint64 lmf_addr;
63         guint64 end_stack;
64
65         guint64 extended_notifications;
66
67         /* Next pointer. */
68         MonoDebuggerThreadInfo *next;
69
70         /*
71          * The stack bounds are only used when reading a core file.
72          */
73         guint64 stack_start;
74         guint64 signal_stack_start;
75         guint32 stack_size;
76         guint32 signal_stack_size;
77
78         guint32 thread_flags;
79
80         /*
81          * The debugger doesn't access anything beyond this point.
82          */
83         MonoDebuggerExceptionState exception_state;
84
85         guint32 internal_flags;
86
87         MonoJitTlsData *jit_tls;
88         MonoInternalThread *thread;
89 };
90
91 typedef struct {
92         gpointer stack_pointer;
93         MonoObject *exception_obj;
94         guint32 stop;
95         guint32 stop_unhandled;
96 } MonoDebuggerExceptionInfo;
97
98 MonoDebuggerThreadInfo *mono_debugger_thread_table = NULL;
99
100 static inline void
101 record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
102 {
103         MonoDebugLineNumberEntry lne;
104
105         lne.native_offset = address;
106         lne.il_offset = offset;
107
108         g_array_append_val (info->line_numbers, lne);
109 }
110
111
112 void
113 mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block, guint32 breakpoint_id)
114 {
115         MiniDebugMethodInfo *info;
116
117         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
118                 return;
119
120         info = g_new0 (MiniDebugMethodInfo, 1);
121         info->breakpoint_id = breakpoint_id;
122
123         cfg->debug_info = info;
124 }
125
126 void
127 mono_debug_open_method (MonoCompile *cfg)
128 {
129         MiniDebugMethodInfo *info;
130         MonoDebugMethodJitInfo *jit;
131         MonoMethodHeader *header;
132
133         info = (MiniDebugMethodInfo *) cfg->debug_info;
134         if (!info)
135                 return;
136
137         mono_class_init (cfg->method->klass);
138
139         header = cfg->header;
140         g_assert (header);
141         
142         info->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
143         info->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
144         jit->num_locals = header->num_locals;
145         jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
146 }
147
148 static void
149 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
150 {
151         var->type = inst->inst_vtype;
152
153         if (inst->opcode == OP_REGVAR)
154                 var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
155         else if (inst->flags & MONO_INST_IS_DEAD)
156                 var->index = MONO_DEBUG_VAR_ADDRESS_MODE_DEAD;
157         else if (inst->opcode == OP_REGOFFSET) {
158                 /* the debug interface needs fixing to allow 0(%base) address */
159                 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET;
160                 var->offset = inst->inst_offset;
161         } else if (inst->opcode == OP_GSHAREDVT_ARG_REGOFFSET) {
162                 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR;
163                 var->offset = inst->inst_offset;
164         } else if (inst->opcode == OP_GSHAREDVT_LOCAL) {
165                 var->index = inst->inst_imm | MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL;
166         } else if (inst->opcode == OP_VTARG_ADDR) {
167                 var->offset = inst->inst_offset;
168                 var->index  = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR;
169         } else {
170                 g_assert_not_reached ();
171         }
172 }
173
174 /*
175  * mono_debug_add_vg_method:
176  *
177  *  Register symbol information for the method with valgrind
178  */
179 static void 
180 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
181 {
182 #ifdef VALGRIND_ADD_LINE_INFO
183         MonoMethodHeader *header;
184         MonoDebugMethodInfo *minfo;
185         int i;
186         char *filename = NULL;
187         guint32 address, line_number;
188         const char *full_name;
189         guint32 *addresses;
190         guint32 *lines;
191
192         if (!RUNNING_ON_VALGRIND)
193                 return;
194
195         header = mono_method_get_header (method);
196
197         full_name = mono_method_full_name (method, TRUE);
198
199         addresses = g_new0 (guint32, header->code_size + 1);
200         lines = g_new0 (guint32, header->code_size + 1);
201
202         /* 
203          * Very simple code to convert the addr->offset mappings that mono has
204          * into [addr-addr] ->line number mappings.
205          */
206
207         minfo = mono_debug_lookup_method (method);
208         if (minfo) {
209                 /* Create offset->line number mapping */
210                 for (i = 0; i < header->code_size; ++i) {
211                         MonoDebugSourceLocation *location;
212
213                         location = mono_debug_symfile_lookup_location (minfo, i);
214                         if (!location)
215                                 continue;
216
217                         lines [i] = location.row;
218                         if (!filename)
219                                 filename = location.source_file;
220
221                         mono_debug_free_source_location (location);
222                 }
223         }
224
225         /* Create address->offset mapping */
226         for (i = 0; i < jit->num_line_numbers; ++i) {
227                 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
228
229                 g_assert (lne->offset <= header->code_size);
230
231                 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
232                         addresses [lne->offset] = lne->address;
233         }
234         /* Fill out missing addresses */
235         address = 0;
236         for (i = 0; i < header->code_size; ++i) {
237                 if (addresses [i] == 0)
238                         addresses [i] = address;
239                 else
240                         address = addresses [i];
241         }
242         
243         address = 0;
244         line_number = 0;
245         i = 0;
246         while (i < header->code_size) {
247                 if (lines [i] == line_number)
248                         i ++;
249                 else {
250                         if (line_number > 0) {
251                                 //g_assert (addresses [i] - 1 >= address);
252                                 
253                                 if (addresses [i] - 1 >= address) {
254                                         VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
255                                         //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
256                                 }
257                         }
258                         address = addresses [i];
259                         line_number = lines [i];
260                 }
261         }
262
263         if (line_number > 0) {
264                 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
265                 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
266         }
267
268         VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
269
270         g_free (addresses);
271         g_free (lines);
272         mono_metadata_free_mh (header);
273 #endif /* VALGRIND_ADD_LINE_INFO */
274 }
275
276 void
277 mono_debug_close_method (MonoCompile *cfg)
278 {
279         MiniDebugMethodInfo *info;
280         MonoDebugMethodJitInfo *jit;
281         MonoMethodHeader *header;
282         MonoMethodSignature *sig;
283         MonoDebugMethodAddress *debug_info;
284         MonoMethod *method;
285         int i;
286
287         info = (MiniDebugMethodInfo *) cfg->debug_info;
288         if (!info || !info->jit) {
289                 if (info)
290                         g_free (info);
291                 return;
292         }
293
294         method = cfg->method;
295         header = cfg->header;
296         sig = mono_method_signature (method);
297
298         jit = info->jit;
299         jit->code_start = cfg->native_code;
300         jit->epilogue_begin = cfg->epilog_begin;
301         jit->code_size = cfg->code_len;
302
303         if (jit->epilogue_begin)
304                    record_line_number (info, jit->epilogue_begin, header->code_size);
305
306         jit->num_params = sig->param_count;
307         jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
308
309         for (i = 0; i < jit->num_locals; i++)
310                 write_variable (cfg->locals [i], &jit->locals [i]);
311
312         if (sig->hasthis) {
313                 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
314                 write_variable (cfg->args [0], jit->this_var);
315         }
316
317         for (i = 0; i < jit->num_params; i++)
318                 write_variable (cfg->args [i + sig->hasthis], &jit->params [i]);
319
320         if (cfg->gsharedvt_info_var) {
321                 jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
322                 jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
323                 write_variable (cfg->gsharedvt_info_var, jit->gsharedvt_info_var);
324                 write_variable (cfg->gsharedvt_locals_var, jit->gsharedvt_locals_var);
325         }
326
327         jit->num_line_numbers = info->line_numbers->len;
328         jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
329
330         for (i = 0; i < jit->num_line_numbers; i++)
331                 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
332
333         debug_info = mono_debug_add_method (cfg->method_to_register, jit, cfg->domain);
334
335         mono_debug_add_vg_method (method, jit);
336
337         mono_debugger_check_breakpoints (method, debug_info);
338
339         mono_debug_free_method_jit_info (jit);
340         mono_debug_free_method (cfg);
341 }
342
343 void
344 mono_debug_free_method (MonoCompile *cfg)
345 {
346         MiniDebugMethodInfo *info;
347
348         info = (MiniDebugMethodInfo *) cfg->debug_info;
349         if (info) {
350                 if (info->line_numbers)
351                         g_array_free (info->line_numbers, TRUE);
352                 g_free (info);
353                 cfg->debug_info = NULL; 
354         }
355 }
356
357 void
358 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
359 {
360         MiniDebugMethodInfo *info;
361         MonoMethodHeader *header;
362         guint32 offset;
363
364         info = (MiniDebugMethodInfo *) cfg->debug_info;
365         if (!info || !info->jit || !ins->cil_code)
366                 return;
367
368         header = cfg->header;
369         g_assert (header);
370
371         if ((ins->cil_code < header->code) ||
372             (ins->cil_code > header->code + header->code_size))
373                 return;
374
375         offset = ins->cil_code - header->code;
376         if (!info->has_line_numbers) {
377                 info->jit->prologue_end = address;
378                 info->has_line_numbers = TRUE;
379         }
380
381         record_line_number (info, address, offset);
382 }
383
384 void
385 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
386 {
387         MiniDebugMethodInfo *info;
388         MonoMethodHeader *header;
389         guint32 offset;
390
391         info = (MiniDebugMethodInfo *) cfg->debug_info;
392         if (!info || !info->jit || !bb->cil_code)
393                 return;
394
395         header = cfg->header;
396         g_assert (header);
397
398         if ((bb->cil_code < header->code) ||
399             (bb->cil_code > header->code + header->code_size))
400                 return;
401
402         offset = bb->cil_code - header->code;
403         if (!info->has_line_numbers) {
404                 info->jit->prologue_end = address;
405                 info->has_line_numbers = TRUE;
406         }
407
408         record_line_number (info, address, offset);
409 }
410
411 static inline void
412 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
413 {
414         guint8 *p = buf;
415
416         //printf ("ENCODE: %d 0x%x.\n", value, value);
417
418         /* 
419          * Same encoding as the one used in the metadata, extended to handle values
420          * greater than 0x1fffffff.
421          */
422         if ((value >= 0) && (value <= 127))
423                 *p++ = value;
424         else if ((value >= 0) && (value <= 16383)) {
425                 p [0] = 0x80 | (value >> 8);
426                 p [1] = value & 0xff;
427                 p += 2;
428         } else if ((value >= 0) && (value <= 0x1fffffff)) {
429                 p [0] = (value >> 24) | 0xc0;
430                 p [1] = (value >> 16) & 0xff;
431                 p [2] = (value >> 8) & 0xff;
432                 p [3] = value & 0xff;
433                 p += 4;
434         }
435         else {
436                 p [0] = 0xff;
437                 p [1] = (value >> 24) & 0xff;
438                 p [2] = (value >> 16) & 0xff;
439                 p [3] = (value >> 8) & 0xff;
440                 p [4] = value & 0xff;
441                 p += 5;
442         }
443         if (endbuf)
444                 *endbuf = p;
445 }
446
447 static inline gint32
448 decode_value (guint8 *ptr, guint8 **rptr)
449 {
450         guint8 b = *ptr;
451         gint32 len;
452         
453         if ((b & 0x80) == 0){
454                 len = b;
455                 ++ptr;
456         } else if ((b & 0x40) == 0){
457                 len = ((b & 0x3f) << 8 | ptr [1]);
458                 ptr += 2;
459         } else if (b != 0xff) {
460                 len = ((b & 0x1f) << 24) |
461                         (ptr [1] << 16) |
462                         (ptr [2] << 8) |
463                         ptr [3];
464                 ptr += 4;
465         }
466         else {
467                 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
468                 ptr += 5;
469         }
470         if (rptr)
471                 *rptr = ptr;
472
473         //printf ("DECODE: %d.\n", len);
474         return len;
475 }
476
477 static void
478 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
479 {
480         guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
481
482         encode_value (var->index, p, &p);
483
484         switch (flags) {
485         case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
486                 break;
487         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
488         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
489                 encode_value (var->offset, p, &p);
490                 break;
491         case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
492         case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
493         case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
494                 break;
495         default:
496                 g_assert_not_reached ();
497         }
498         *endbuf = p;
499 }
500
501 void
502 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
503 {
504         MonoDebugMethodJitInfo *jit;
505         guint32 size, prev_offset, prev_native_offset;
506         guint8 *buf, *p;
507         int i;
508
509         /* Can't use cfg->debug_info as it is freed by close_method () */
510         jit = mono_debug_find_method (cfg->method, mono_domain_get ());
511         if (!jit) {
512                 *buf_len = 0;
513                 return;
514         }
515
516         size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
517         p = buf = g_malloc (size);
518         encode_value (jit->epilogue_begin, p, &p);
519         encode_value (jit->prologue_end, p, &p);
520         encode_value (jit->code_size, p, &p);
521
522         for (i = 0; i < jit->num_params; ++i)
523                 serialize_variable (&jit->params [i], p, &p);
524
525         if (mono_method_signature (cfg->method)->hasthis)
526                 serialize_variable (jit->this_var, p, &p);
527
528         for (i = 0; i < jit->num_locals; i++)
529                 serialize_variable (&jit->locals [i], p, &p);
530
531         if (jit->gsharedvt_info_var) {
532                 encode_value (1, p, &p);
533                 serialize_variable (jit->gsharedvt_info_var, p, &p);
534                 serialize_variable (jit->gsharedvt_locals_var, p, &p);
535         } else {
536                 encode_value (0, p, &p);
537         }
538
539         encode_value (jit->num_line_numbers, p, &p);
540
541         prev_offset = 0;
542         prev_native_offset = 0;
543         for (i = 0; i < jit->num_line_numbers; ++i) {
544                 /* Sometimes, the offset values are not in increasing order */
545                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
546                 encode_value (lne->il_offset - prev_offset, p, &p);
547                 encode_value (lne->native_offset - prev_native_offset, p, &p);
548                 prev_offset = lne->il_offset;
549                 prev_native_offset = lne->native_offset;
550         }
551
552         g_assert (p - buf < size);
553
554         *out_buf = buf;
555         *buf_len = p - buf;
556 }
557
558 static void
559 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
560 {
561         guint32 flags;
562
563         var->index = decode_value (p, &p);
564
565         flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
566
567         switch (flags) {
568         case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
569                 break;
570         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
571         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
572                 var->offset = decode_value (p, &p);
573                 break;
574         case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
575         case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
576         case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
577                 break;
578         default:
579                 g_assert_not_reached ();
580         }
581         *endbuf = p;
582 }
583
584 static MonoDebugMethodJitInfo *
585 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
586 {
587         MonoMethodHeader *header;
588         gint32 offset, native_offset, prev_offset, prev_native_offset;
589         MonoDebugMethodJitInfo *jit;
590         guint8 *p;
591         int i;
592
593         header = mono_method_get_header (method);
594         g_assert (header);
595
596         jit = g_new0 (MonoDebugMethodJitInfo, 1);
597         jit->code_start = code_start;
598         jit->num_locals = header->num_locals;
599         jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
600         jit->num_params = mono_method_signature (method)->param_count;
601         jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
602
603         p = buf;
604         jit->epilogue_begin = decode_value (p, &p);
605         jit->prologue_end = decode_value (p, &p);
606         jit->code_size = decode_value (p, &p);
607
608         for (i = 0; i < jit->num_params; ++i)
609                 deserialize_variable (&jit->params [i], p, &p);
610
611         if (mono_method_signature (method)->hasthis) {
612                 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
613                 deserialize_variable (jit->this_var, p, &p);
614         }
615
616         for (i = 0; i < jit->num_locals; i++)
617                 deserialize_variable (&jit->locals [i], p, &p);
618
619         if (decode_value (p, &p)) {
620                 jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
621                 jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
622                 deserialize_variable (jit->gsharedvt_info_var, p, &p);
623                 deserialize_variable (jit->gsharedvt_locals_var, p, &p);
624         }
625
626         jit->num_line_numbers = decode_value (p, &p);
627         jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
628
629         prev_offset = 0;
630         prev_native_offset = 0;
631         for (i = 0; i < jit->num_line_numbers; ++i) {
632                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
633
634                 offset = prev_offset + decode_value (p, &p);
635                 native_offset = prev_native_offset + decode_value (p, &p);
636
637                 lne->native_offset = native_offset;
638                 lne->il_offset = offset;
639
640                 prev_offset = offset;
641                 prev_native_offset = native_offset;
642         }
643
644         mono_metadata_free_mh (header);
645         return jit;
646 }
647
648 void
649 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start, 
650                            guint8 *debug_info, guint32 debug_info_len)
651 {
652         MonoDebugMethodJitInfo *jit;
653
654         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
655                 return;
656
657         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
658             (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
659             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
660             (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
661             (method->wrapper_type != MONO_WRAPPER_NONE))
662                 return;
663
664         if (debug_info_len == 0)
665                 return;
666
667         jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
668
669         mono_debug_add_method (method, jit, domain);
670
671         mono_debug_add_vg_method (method, jit);
672
673         mono_debug_free_method_jit_info (jit);
674 }
675
676 void
677 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
678 {
679         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
680                 return;
681
682         // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
683 }
684
685 static void
686 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
687 {
688         switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
689         case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
690                 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
691                 break;
692         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
693                 g_print ("%s %s (%d) in memory: base register %s + %d\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)), info->offset);
694                 break;
695         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
696                 g_print ("%s %s (%d) in indir memory: base register %s + %d\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)), info->offset);
697                 break;
698         case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
699                 g_print ("%s %s (%d) gsharedvt local.\n", type, name, idx);
700                 break;
701         case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
702                 g_print ("%s %s (%d) vt address: base register %s + %d\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)), info->offset);
703                 break;
704         case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
705         default:
706                 g_assert_not_reached ();
707         }
708 }
709
710 /**
711  * mono_debug_print_locals:
712  *
713  * Prints to stdout the information about the local variables in
714  * a method (if @only_arguments is false) or about the arguments.
715  * The information includes the storage info (where the variable 
716  * lives, in a register or in memory).
717  * The method is found by looking up what method has been emitted at
718  * the instruction address @ip.
719  * This is for use inside a debugger.
720  */
721 void
722 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
723 {
724         MonoDomain *domain = mono_domain_get ();
725         MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
726         MonoDebugMethodJitInfo *jit;
727         int i;
728
729         if (!ji)
730                 return;
731
732         jit = mono_debug_find_method (mono_jit_info_get_method (ji), domain);
733         if (!jit)
734                 return;
735
736         if (only_arguments) {
737                 char **names;
738                 names = g_new (char *, jit->num_params);
739                 mono_method_get_param_names (mono_jit_info_get_method (ji), (const char **) names);
740                 if (jit->this_var)
741                         print_var_info (jit->this_var, 0, "this", "Arg");
742                 for (i = 0; i < jit->num_params; ++i) {
743                         print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
744                 }
745                 g_free (names);
746         } else {
747                 for (i = 0; i < jit->num_locals; ++i) {
748                         print_var_info (&jit->locals [i], i, "", "Local");
749                 }
750         }
751         mono_debug_free_method_jit_info (jit);
752 }
753
754 /*
755  * The old Debugger breakpoint interface.
756  *
757  * This interface is used to insert breakpoints on methods which are not yet JITed.
758  * The debugging code keeps a list of all such breakpoints and automatically inserts the
759  * breakpoint when the method is JITed.
760  */
761
762 static GPtrArray *breakpoints = NULL;
763
764 int
765 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
766 {
767         static int last_breakpoint_id = 0;
768         MiniDebugBreakpointInfo *info;
769
770         info = g_new0 (MiniDebugBreakpointInfo, 1);
771         info->desc = desc;
772         info->index = ++last_breakpoint_id;
773
774         if (!breakpoints)
775                 breakpoints = g_ptr_array_new ();
776
777         g_ptr_array_add (breakpoints, info);
778
779         return info->index;
780 }
781
782 int
783 mono_debugger_remove_breakpoint (int breakpoint_id)
784 {
785         int i;
786
787         if (!breakpoints)
788                 return 0;
789
790         for (i = 0; i < breakpoints->len; i++) {
791                 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
792
793                 if (info->index != breakpoint_id)
794                         continue;
795
796                 mono_method_desc_free (info->desc);
797                 g_ptr_array_remove (breakpoints, info);
798                 g_free (info);
799                 return 1;
800         }
801
802         return 0;
803 }
804
805 int
806 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
807 {
808         MonoMethodDesc *desc;
809
810         desc = mono_method_desc_new (method_name, include_namespace);
811         if (!desc)
812                 return 0;
813
814         return mono_debugger_insert_breakpoint_full (desc);
815 }
816
817 int
818 mono_debugger_method_has_breakpoint (MonoMethod *method)
819 {
820         int i;
821
822         if (!breakpoints)
823                 return 0;
824
825         for (i = 0; i < breakpoints->len; i++) {
826                 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
827
828                 if (!mono_method_desc_full_match (info->desc, method))
829                         continue;
830
831                 return info->index;
832         }
833
834         return 0;
835 }
836
837 void
838 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
839 {
840         mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
841 }
842
843 void
844 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls, gpointer func)
845 {
846 #ifdef MONO_DEBUGGER_SUPPORTED
847         size_t stsize = 0;
848         guint8 *staddr = NULL;
849         MonoDebuggerThreadInfo *info;
850
851         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
852                 return;
853
854         mono_debugger_lock ();
855
856         mono_thread_get_stack_bounds (&staddr, &stsize);
857
858         info = g_new0 (MonoDebuggerThreadInfo, 1);
859         info->tid = tid;
860         info->thread = thread->internal_thread;
861         info->stack_start = (guint64) (gsize) staddr;
862         info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
863         info->stack_size = stsize;
864         info->signal_stack_size = jit_tls->signal_stack_size;
865         info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
866         info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
867         info->jit_tls = jit_tls;
868
869         if (func)
870                 info->thread_flags = MONO_DEBUGGER_THREAD_FLAGS_INTERNAL;
871         if (thread->internal_thread->threadpool_thread)
872                 info->thread_flags |= MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL;
873
874         info->next = mono_debugger_thread_table;
875         mono_debugger_thread_table = info;
876
877         mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
878                              tid, (guint64) (gsize) info);
879
880         mono_debugger_unlock ();
881 #endif /* MONO_DEBUGGER_SUPPORTED */
882 }
883
884 void
885 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
886 {
887 #ifdef MONO_DEBUGGER_SUPPORTED
888         MonoDebuggerThreadInfo **ptr;
889
890         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
891                 return;
892
893         mono_debugger_lock ();
894
895         for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
896                 MonoDebuggerThreadInfo *info = *ptr;
897
898                 if (info->jit_tls != jit_tls)
899                         continue;
900
901                 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
902                                      info->tid, (guint64) (gsize) info);
903
904                 *ptr = info->next;
905                 g_free (info);
906                 break;
907         }
908
909         mono_debugger_unlock ();
910 #endif
911 }
912
913 void
914 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
915 {
916 #ifdef MONO_DEBUGGER_SUPPORTED
917         MonoDebuggerThreadInfo **ptr;
918         MonoInternalThread *thread = mono_thread_internal_current ();
919
920         if (!mono_debug_using_mono_debugger ())
921                 return;
922
923         mono_debugger_lock ();
924
925         for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
926                 MonoDebuggerThreadInfo *info = *ptr;
927
928                 if (info->thread != thread)
929                         continue;
930
931                 if ((info->extended_notifications & (int) event) == 0)
932                         continue;
933
934                 mono_debugger_event (event, data, arg);
935         }
936
937         mono_debugger_unlock ();
938 #endif
939 }
940
941 void
942 mono_debugger_trampoline_compiled (const guint8 *trampoline, MonoMethod *method, const guint8 *code)
943 {
944 #ifdef MONO_DEBUGGER_SUPPORTED
945         struct {
946                 const guint8 * trampoline;
947                 MonoMethod *method;
948                 const guint8 *code;
949         } info = { trampoline, method, code };
950
951         mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_OLD_TRAMPOLINE,
952                                              (guint64) (gsize) method, (guint64) (gsize) code);
953         mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
954                                              (guint64) (gsize) &info, 0);
955 #endif
956 }
957
958 #if MONO_DEBUGGER_SUPPORTED
959 static MonoDebuggerThreadInfo *
960 find_debugger_thread_info (MonoInternalThread *thread)
961 {
962         MonoDebuggerThreadInfo **ptr;
963
964         for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
965                 MonoDebuggerThreadInfo *info = *ptr;
966
967                 if (info->thread == thread)
968                         return info;
969         }
970
971         return NULL;
972 }
973 #endif
974
975 MonoDebuggerExceptionAction
976 _mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
977 {
978 #ifdef MONO_DEBUGGER_SUPPORTED
979         MonoDebuggerExceptionInfo exc_info;
980         MonoDebuggerThreadInfo *thread_info;
981
982         if (!mono_debug_using_mono_debugger ())
983                 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
984
985         mono_debugger_lock ();
986
987         thread_info = find_debugger_thread_info (mono_thread_internal_current ());
988         if (!thread_info) {
989                 mono_debugger_unlock ();
990                 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
991         }
992
993         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
994                 mono_debugger_unlock ();
995                 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
996         }
997
998         if (thread_info->exception_state.stopped_on_exception ||
999             thread_info->exception_state.stopped_on_unhandled) {
1000                 thread_info->exception_state.stopped_on_exception = 0;
1001                 mono_debugger_unlock ();
1002                 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
1003         }
1004
1005         /* Protect the exception object from being garbage collected. */
1006
1007         thread_info->exception_state.stopped_on_unhandled = 0;
1008         thread_info->exception_state.stopped_on_exception = 1;
1009         thread_info->exception_state.last_exception = exc;
1010
1011         /*
1012          * Backwards compatibility:
1013          *
1014          * Older debugger versions only know `exc_info.stop' and older runtime versions check
1015          * `exc_info.stop != 0'.
1016          *
1017          * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
1018          * `stop_unhandled' field.
1019          */
1020
1021         exc_info.stack_pointer = stack;
1022         exc_info.exception_obj = exc;
1023         exc_info.stop = 0;
1024         exc_info.stop_unhandled = 0;
1025
1026         mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &exc_info,
1027                              (guint64) (gsize) addr);
1028
1029         if (!exc_info.stop) {
1030                 thread_info->exception_state.stopped_on_exception = 0;
1031                 thread_info->exception_state.last_exception = NULL;
1032         } 
1033
1034         mono_debugger_unlock ();
1035
1036         if (exc_info.stop)
1037                 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP;
1038         else if (exc_info.stop_unhandled)
1039                 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED;
1040 #endif
1041
1042         return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
1043 }
1044
1045 gboolean
1046 _mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
1047 {
1048 #ifdef MONO_DEBUGGER_SUPPORTED
1049         MonoDebuggerThreadInfo *thread_info;
1050
1051         if (!mono_debug_using_mono_debugger ())
1052                 return FALSE;
1053
1054         if (exc) {
1055                 const gchar *name = mono_class_get_name (mono_object_get_class (exc));
1056                 if (!strcmp (name, "ThreadAbortException"))
1057                         return FALSE;
1058         }
1059
1060         mono_debugger_lock ();
1061
1062         thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1063         if (!thread_info) {
1064                 mono_debugger_unlock ();
1065                 return FALSE;
1066         }
1067
1068         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1069                 mono_debugger_unlock ();
1070                 return FALSE;
1071         }
1072
1073         if (thread_info->exception_state.stopped_on_unhandled) {
1074                 thread_info->exception_state.stopped_on_unhandled = 0;
1075                 mono_debugger_unlock ();
1076                 return FALSE;
1077         }
1078
1079         thread_info->exception_state.stopped_on_unhandled = 1;
1080         thread_info->exception_state.last_exception = exc;
1081
1082         mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
1083                              (guint64) (gsize) exc, (guint64) (gsize) addr);
1084
1085         return TRUE;
1086 #else
1087         return FALSE;
1088 #endif
1089 }
1090
1091 /*
1092  * mono_debugger_call_exception_handler:
1093  *
1094  * Called from mono_handle_exception_internal() to tell the debugger that we're about
1095  * to invoke an exception handler.
1096  *
1097  * The debugger may choose to set a breakpoint at @addr.  This is used if the user is
1098  * single-stepping from a `try' into a `catch' block, for instance.
1099  */
1100
1101 void
1102 mono_debugger_call_exception_handler (gpointer addr, gpointer stack, MonoObject *exc)
1103 {
1104 #ifdef MONO_DEBUGGER_SUPPORTED
1105         MonoDebuggerThreadInfo *thread_info;
1106         MonoDebuggerExceptionInfo exc_info;
1107
1108         if (!mono_debug_using_mono_debugger ())
1109                 return;
1110
1111         mono_debugger_lock ();
1112
1113         thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1114         if (!thread_info) {
1115                 mono_debugger_unlock ();
1116                 return;
1117         }
1118
1119         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1120                 mono_debugger_unlock ();
1121                 return;
1122         }
1123
1124         // Prevent the object from being finalized.
1125         thread_info->exception_state.last_exception = exc;
1126
1127         exc_info.stack_pointer = stack;
1128         exc_info.exception_obj = exc;
1129         exc_info.stop = 0;
1130         exc_info.stop_unhandled = 0;
1131
1132         mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &exc_info,
1133                              (guint64) (gsize) addr);
1134
1135         mono_debugger_unlock ();
1136 #endif
1137 }
1138
1139 #ifdef MONO_DEBUGGER_SUPPORTED
1140
1141 static gchar *
1142 get_exception_message (MonoObject *exc)
1143 {
1144         char *message = NULL;
1145         MonoString *str; 
1146         MonoMethod *method;
1147         MonoClass *klass;
1148         gint i;
1149
1150         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1151                 klass = exc->vtable->klass;
1152                 method = NULL;
1153                 while (klass && method == NULL) {
1154                         for (i = 0; i < klass->method.count; ++i) {
1155                                 method = klass->methods [i];
1156                                 if (!strcmp ("ToString", method->name) &&
1157                                     mono_method_signature (method)->param_count == 0 &&
1158                                     method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1159                                     method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1160                                         break;
1161                                 }
1162                                 method = NULL;
1163                         }
1164                         
1165                         if (method == NULL)
1166                                 klass = klass->parent;
1167                 }
1168
1169                 g_assert (method);
1170
1171                 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1172                 if (str)
1173                         message = mono_string_to_utf8 (str);
1174         }
1175
1176         return message;
1177 }
1178
1179 MonoObject *
1180 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1181 {
1182         MonoDebuggerThreadInfo *thread_info;
1183         MonoDebuggerExceptionState saved_exception_state;
1184         MonoObject *retval;
1185         gchar *message;
1186
1187         mono_debugger_lock ();
1188
1189         thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1190         if (!thread_info) {
1191                 mono_debugger_unlock ();
1192                 return NULL;
1193         }
1194
1195         saved_exception_state = thread_info->exception_state;
1196
1197         thread_info->exception_state.last_exception = NULL;
1198         thread_info->exception_state.stopped_on_unhandled = 0;
1199         thread_info->exception_state.stopped_on_exception = 0;
1200
1201         thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1202
1203         mono_debugger_unlock ();
1204
1205         if (!strcmp (method->name, ".ctor")) {
1206                 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1207
1208                 mono_runtime_invoke (method, obj, params, exc);
1209         } else
1210                 retval = mono_runtime_invoke (method, obj, params, exc);
1211
1212         mono_debugger_lock ();
1213
1214         thread_info->exception_state = saved_exception_state;
1215         thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1216
1217         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1218                 thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1219                 mono_thread_internal_reset_abort (thread_info->thread);
1220
1221                 mono_debugger_unlock ();
1222
1223                 *exc = NULL;
1224                 return NULL;
1225         }
1226
1227         mono_debugger_unlock ();
1228
1229         if (!exc || (*exc == NULL))
1230                 return retval;
1231
1232         retval = *exc;
1233         message = get_exception_message (*exc);
1234         if (message) {
1235                 *exc = (MonoObject *) mono_string_new_wrapper (message);
1236                 g_free (message);
1237         }
1238
1239         return retval;
1240 }
1241
1242 gboolean
1243 mono_debugger_abort_runtime_invoke ()
1244 {
1245         MonoInternalThread *thread = mono_thread_internal_current ();
1246         MonoDebuggerThreadInfo *thread_info;
1247
1248         mono_debugger_lock ();
1249
1250         thread_info = find_debugger_thread_info (thread);
1251         if (!thread_info) {
1252                 mono_debugger_unlock ();
1253                 return FALSE;
1254         }
1255
1256         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE) == 0) {
1257                 mono_debugger_unlock ();
1258                 return FALSE;
1259         }
1260
1261         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1262                 mono_debugger_unlock ();
1263                 return TRUE;
1264         }
1265
1266         thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1267         ves_icall_System_Threading_Thread_Abort (thread_info->thread, NULL);
1268
1269         mono_debugger_unlock ();
1270         return TRUE;
1271 }
1272
1273 #endif