Merge branch 'master' of github.com:mono/mono
[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         if ((method->wrapper_type != MONO_WRAPPER_NONE) &&
779                 (method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD))
780                 return 0;
781
782         for (i = 0; i < breakpoints->len; i++) {
783                 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
784
785                 if (!mono_method_desc_full_match (info->desc, method))
786                         continue;
787
788                 return info->index;
789         }
790
791         return 0;
792 }
793
794 void
795 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
796 {
797         mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
798 }
799
800 void
801 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls, gpointer func)
802 {
803 #ifdef MONO_DEBUGGER_SUPPORTED
804         size_t stsize = 0;
805         guint8 *staddr = NULL;
806         MonoDebuggerThreadInfo *info;
807
808         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
809                 return;
810
811         mono_debugger_lock ();
812
813         mono_thread_get_stack_bounds (&staddr, &stsize);
814
815         info = g_new0 (MonoDebuggerThreadInfo, 1);
816         info->tid = tid;
817         info->thread = thread->internal_thread;
818         info->stack_start = (guint64) (gsize) staddr;
819         info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
820         info->stack_size = stsize;
821         info->signal_stack_size = jit_tls->signal_stack_size;
822         info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
823         info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
824         info->jit_tls = jit_tls;
825
826         if (func)
827                 info->thread_flags = MONO_DEBUGGER_THREAD_FLAGS_INTERNAL;
828         if (thread->internal_thread->threadpool_thread)
829                 info->thread_flags |= MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL;
830
831         info->next = mono_debugger_thread_table;
832         mono_debugger_thread_table = info;
833
834         mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
835                              tid, (guint64) (gsize) info);
836
837         mono_debugger_unlock ();
838 #endif /* MONO_DEBUGGER_SUPPORTED */
839 }
840
841 void
842 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
843 {
844 #ifdef MONO_DEBUGGER_SUPPORTED
845         MonoDebuggerThreadInfo **ptr;
846
847         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
848                 return;
849
850         mono_debugger_lock ();
851
852         for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
853                 MonoDebuggerThreadInfo *info = *ptr;
854
855                 if (info->jit_tls != jit_tls)
856                         continue;
857
858                 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
859                                      info->tid, (guint64) (gsize) info);
860
861                 *ptr = info->next;
862                 g_free (info);
863                 break;
864         }
865
866         mono_debugger_unlock ();
867 #endif
868 }
869
870 void
871 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
872 {
873 #ifdef MONO_DEBUGGER_SUPPORTED
874         MonoDebuggerThreadInfo **ptr;
875         MonoInternalThread *thread = mono_thread_internal_current ();
876
877         if (!mono_debug_using_mono_debugger ())
878                 return;
879
880         mono_debugger_lock ();
881
882         for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
883                 MonoDebuggerThreadInfo *info = *ptr;
884
885                 if (info->thread != thread)
886                         continue;
887
888                 if ((info->extended_notifications & (int) event) == 0)
889                         continue;
890
891                 mono_debugger_event (event, data, arg);
892         }
893
894         mono_debugger_unlock ();
895 #endif
896 }
897
898 void
899 mono_debugger_trampoline_compiled (const guint8 *trampoline, MonoMethod *method, const guint8 *code)
900 {
901 #ifdef MONO_DEBUGGER_SUPPORTED
902         struct {
903                 const guint8 * trampoline;
904                 MonoMethod *method;
905                 const guint8 *code;
906         } info = { trampoline, method, code };
907
908         mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_OLD_TRAMPOLINE,
909                                              (guint64) (gsize) method, (guint64) (gsize) code);
910         mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
911                                              (guint64) (gsize) &info, 0);
912 #endif
913 }
914
915 #if MONO_DEBUGGER_SUPPORTED
916 static MonoDebuggerThreadInfo *
917 find_debugger_thread_info (MonoInternalThread *thread)
918 {
919         MonoDebuggerThreadInfo **ptr;
920
921         for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
922                 MonoDebuggerThreadInfo *info = *ptr;
923
924                 if (info->thread == thread)
925                         return info;
926         }
927
928         return NULL;
929 }
930 #endif
931
932 MonoDebuggerExceptionAction
933 _mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
934 {
935 #ifdef MONO_DEBUGGER_SUPPORTED
936         MonoDebuggerExceptionInfo exc_info;
937         MonoDebuggerThreadInfo *thread_info;
938
939         if (!mono_debug_using_mono_debugger ())
940                 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
941
942         mono_debugger_lock ();
943
944         thread_info = find_debugger_thread_info (mono_thread_internal_current ());
945         if (!thread_info) {
946                 mono_debugger_unlock ();
947                 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
948         }
949
950         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
951                 mono_debugger_unlock ();
952                 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
953         }
954
955         if (thread_info->exception_state.stopped_on_exception ||
956             thread_info->exception_state.stopped_on_unhandled) {
957                 thread_info->exception_state.stopped_on_exception = 0;
958                 mono_debugger_unlock ();
959                 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
960         }
961
962         /* Protect the exception object from being garbage collected. */
963
964         thread_info->exception_state.stopped_on_unhandled = 0;
965         thread_info->exception_state.stopped_on_exception = 1;
966         thread_info->exception_state.last_exception = exc;
967
968         /*
969          * Backwards compatibility:
970          *
971          * Older debugger versions only know `exc_info.stop' and older runtime versions check
972          * `exc_info.stop != 0'.
973          *
974          * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
975          * `stop_unhandled' field.
976          */
977
978         exc_info.stack_pointer = stack;
979         exc_info.exception_obj = exc;
980         exc_info.stop = 0;
981         exc_info.stop_unhandled = 0;
982
983         mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &exc_info,
984                              (guint64) (gsize) addr);
985
986         if (!exc_info.stop) {
987                 thread_info->exception_state.stopped_on_exception = 0;
988                 thread_info->exception_state.last_exception = NULL;
989         } 
990
991         mono_debugger_unlock ();
992
993         if (exc_info.stop)
994                 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP;
995         else if (exc_info.stop_unhandled)
996                 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED;
997 #endif
998
999         return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
1000 }
1001
1002 gboolean
1003 _mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
1004 {
1005 #ifdef MONO_DEBUGGER_SUPPORTED
1006         MonoDebuggerThreadInfo *thread_info;
1007
1008         if (!mono_debug_using_mono_debugger ())
1009                 return FALSE;
1010
1011         if (exc) {
1012                 const gchar *name = mono_class_get_name (mono_object_get_class (exc));
1013                 if (!strcmp (name, "ThreadAbortException"))
1014                         return FALSE;
1015         }
1016
1017         mono_debugger_lock ();
1018
1019         thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1020         if (!thread_info) {
1021                 mono_debugger_unlock ();
1022                 return FALSE;
1023         }
1024
1025         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1026                 mono_debugger_unlock ();
1027                 return FALSE;
1028         }
1029
1030         if (thread_info->exception_state.stopped_on_unhandled) {
1031                 thread_info->exception_state.stopped_on_unhandled = 0;
1032                 mono_debugger_unlock ();
1033                 return FALSE;
1034         }
1035
1036         thread_info->exception_state.stopped_on_unhandled = 1;
1037         thread_info->exception_state.last_exception = exc;
1038
1039         mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
1040                              (guint64) (gsize) exc, (guint64) (gsize) addr);
1041
1042         return TRUE;
1043 #else
1044         return FALSE;
1045 #endif
1046 }
1047
1048 /*
1049  * mono_debugger_call_exception_handler:
1050  *
1051  * Called from mono_handle_exception_internal() to tell the debugger that we're about
1052  * to invoke an exception handler.
1053  *
1054  * The debugger may choose to set a breakpoint at @addr.  This is used if the user is
1055  * single-stepping from a `try' into a `catch' block, for instance.
1056  */
1057
1058 void
1059 mono_debugger_call_exception_handler (gpointer addr, gpointer stack, MonoObject *exc)
1060 {
1061 #ifdef MONO_DEBUGGER_SUPPORTED
1062         MonoDebuggerThreadInfo *thread_info;
1063         MonoDebuggerExceptionInfo exc_info;
1064
1065         if (!mono_debug_using_mono_debugger ())
1066                 return;
1067
1068         mono_debugger_lock ();
1069
1070         thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1071         if (!thread_info) {
1072                 mono_debugger_unlock ();
1073                 return;
1074         }
1075
1076         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1077                 mono_debugger_unlock ();
1078                 return;
1079         }
1080
1081         // Prevent the object from being finalized.
1082         thread_info->exception_state.last_exception = exc;
1083
1084         exc_info.stack_pointer = stack;
1085         exc_info.exception_obj = exc;
1086         exc_info.stop = 0;
1087         exc_info.stop_unhandled = 0;
1088
1089         mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &exc_info,
1090                              (guint64) (gsize) addr);
1091
1092         mono_debugger_unlock ();
1093 #endif
1094 }
1095
1096 #ifdef MONO_DEBUGGER_SUPPORTED
1097
1098 static gchar *
1099 get_exception_message (MonoObject *exc)
1100 {
1101         char *message = NULL;
1102         MonoString *str; 
1103         MonoMethod *method;
1104         MonoClass *klass;
1105         gint i;
1106
1107         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1108                 klass = exc->vtable->klass;
1109                 method = NULL;
1110                 while (klass && method == NULL) {
1111                         for (i = 0; i < klass->method.count; ++i) {
1112                                 method = klass->methods [i];
1113                                 if (!strcmp ("ToString", method->name) &&
1114                                     mono_method_signature (method)->param_count == 0 &&
1115                                     method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1116                                     method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1117                                         break;
1118                                 }
1119                                 method = NULL;
1120                         }
1121                         
1122                         if (method == NULL)
1123                                 klass = klass->parent;
1124                 }
1125
1126                 g_assert (method);
1127
1128                 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1129                 if (str)
1130                         message = mono_string_to_utf8 (str);
1131         }
1132
1133         return message;
1134 }
1135
1136 MonoObject *
1137 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1138 {
1139         MonoDebuggerThreadInfo *thread_info;
1140         MonoDebuggerExceptionState saved_exception_state;
1141         MonoObject *retval;
1142         gchar *message;
1143
1144         mono_debugger_lock ();
1145
1146         thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1147         if (!thread_info) {
1148                 mono_debugger_unlock ();
1149                 return NULL;
1150         }
1151
1152         saved_exception_state = thread_info->exception_state;
1153
1154         thread_info->exception_state.last_exception = NULL;
1155         thread_info->exception_state.stopped_on_unhandled = 0;
1156         thread_info->exception_state.stopped_on_exception = 0;
1157
1158         thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1159
1160         mono_debugger_unlock ();
1161
1162         if (!strcmp (method->name, ".ctor")) {
1163                 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1164
1165                 mono_runtime_invoke (method, obj, params, exc);
1166         } else
1167                 retval = mono_runtime_invoke (method, obj, params, exc);
1168
1169         mono_debugger_lock ();
1170
1171         thread_info->exception_state = saved_exception_state;
1172         thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1173
1174         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1175                 thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1176                 mono_thread_internal_reset_abort (thread_info->thread);
1177
1178                 mono_debugger_unlock ();
1179
1180                 *exc = NULL;
1181                 return NULL;
1182         }
1183
1184         mono_debugger_unlock ();
1185
1186         if (!exc || (*exc == NULL))
1187                 return retval;
1188
1189         retval = *exc;
1190         message = get_exception_message (*exc);
1191         if (message) {
1192                 *exc = (MonoObject *) mono_string_new_wrapper (message);
1193                 g_free (message);
1194         }
1195
1196         return retval;
1197 }
1198
1199 gboolean
1200 mono_debugger_abort_runtime_invoke ()
1201 {
1202         MonoInternalThread *thread = mono_thread_internal_current ();
1203         MonoDebuggerThreadInfo *thread_info;
1204
1205         mono_debugger_lock ();
1206
1207         thread_info = find_debugger_thread_info (thread);
1208         if (!thread_info) {
1209                 mono_debugger_unlock ();
1210                 return FALSE;
1211         }
1212
1213         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE) == 0) {
1214                 mono_debugger_unlock ();
1215                 return FALSE;
1216         }
1217
1218         if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1219                 mono_debugger_unlock ();
1220                 return TRUE;
1221         }
1222
1223         thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1224         ves_icall_System_Threading_Thread_Abort (thread_info->thread, NULL);
1225
1226         mono_debugger_unlock ();
1227         return TRUE;
1228 }
1229
1230 #endif