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