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