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