2008-06-23 Mark Probst <mark.probst@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 #ifdef HAVE_VALGRIND_H
24 #include <valgrind/valgrind.h>
25 #endif
26
27 #ifdef MONO_DEBUGGER_SUPPORTED
28 #include <libgc/include/libgc-mono-debugger.h>
29 #endif
30
31 typedef struct {
32         guint32 index;
33         MonoMethodDesc *desc;
34 } MiniDebugBreakpointInfo;
35
36 typedef struct
37 {
38         MonoDebugMethodJitInfo *jit;
39         GArray *line_numbers;
40         guint32 has_line_numbers;
41         guint32 breakpoint_id;
42 } MiniDebugMethodInfo;
43
44 struct _MonoDebuggerThreadInfo {
45         guint64 tid;
46         guint64 lmf_addr;
47         guint64 end_stack;
48
49         guint64 extended_notifications;
50
51         /* Next pointer. */
52         MonoDebuggerThreadInfo *next;
53
54         /*
55          * The stack bounds are only used when reading a core file.
56          */
57         guint64 stack_start;
58         guint64 signal_stack_start;
59         guint32 stack_size;
60         guint32 signal_stack_size;
61
62         /*
63          * The debugger doesn't access anything beyond this point.
64          */
65         MonoJitTlsData *jit_tls;
66         MonoThread *thread;
67 };
68
69 MonoDebuggerThreadInfo *mono_debugger_thread_table = NULL;
70
71 static inline void
72 record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
73 {
74         MonoDebugLineNumberEntry lne;
75
76         lne.native_offset = address;
77         lne.il_offset = offset;
78
79         g_array_append_val (info->line_numbers, lne);
80 }
81
82
83 void
84 mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block, guint32 breakpoint_id)
85 {
86         MiniDebugMethodInfo *info;
87
88         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
89                 return;
90
91         info = g_new0 (MiniDebugMethodInfo, 1);
92         info->breakpoint_id = breakpoint_id;
93
94         cfg->debug_info = info;
95 }
96
97 void
98 mono_debug_open_method (MonoCompile *cfg)
99 {
100         MiniDebugMethodInfo *info;
101         MonoDebugMethodJitInfo *jit;
102         MonoMethodHeader *header;
103
104         info = (MiniDebugMethodInfo *) cfg->debug_info;
105         if (!info)
106                 return;
107
108         mono_class_init (cfg->method->klass);
109
110         header = mono_method_get_header (cfg->method);
111         g_assert (header);
112         
113         info->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
114         info->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
115         jit->num_locals = header->num_locals;
116         jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
117 }
118
119 static void
120 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
121 {
122         var->type = inst->inst_vtype;
123
124         if (inst->opcode == OP_REGVAR)
125                 var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
126         else {
127                 /* the debug interface needs fixing to allow 0(%base) address */
128                 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET;
129                 var->offset = inst->inst_offset;
130         }
131 }
132
133 /*
134  * mono_debug_add_vg_method:
135  *
136  *  Register symbol information for the method with valgrind
137  */
138 static void 
139 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
140 {
141 #ifdef VALGRIND_ADD_LINE_INFO
142         MonoMethodHeader *header;
143         MonoDebugMethodInfo *minfo;
144         int i;
145         char *filename = NULL;
146         guint32 address, line_number;
147         const char *full_name;
148         guint32 *addresses;
149         guint32 *lines;
150
151         if (!RUNNING_ON_VALGRIND)
152                 return;
153
154         header = mono_method_get_header (method);
155
156         full_name = mono_method_full_name (method, TRUE);
157
158         addresses = g_new0 (guint32, header->code_size + 1);
159         lines = g_new0 (guint32, header->code_size + 1);
160
161         /* 
162          * Very simple code to convert the addr->offset mappings that mono has
163          * into [addr-addr] ->line number mappings.
164          */
165
166         minfo = mono_debug_lookup_method (method);
167         if (minfo) {
168                 /* Create offset->line number mapping */
169                 for (i = 0; i < header->code_size; ++i) {
170                         MonoDebugSourceLocation *location;
171
172                         location = mono_debug_symfile_lookup_location (minfo, i);
173                         if (!location)
174                                 continue;
175
176                         lines [i] = location.row;
177                         if (!filename)
178                                 filename = location.source_file;
179
180                         mono_debug_free_source_location (location);
181                 }
182         }
183
184         /* Create address->offset mapping */
185         for (i = 0; i < jit->num_line_numbers; ++i) {
186                 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
187
188                 g_assert (lne->offset <= header->code_size);
189
190                 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
191                         addresses [lne->offset] = lne->address;
192         }
193         /* Fill out missing addresses */
194         address = 0;
195         for (i = 0; i < header->code_size; ++i) {
196                 if (addresses [i] == 0)
197                         addresses [i] = address;
198                 else
199                         address = addresses [i];
200         }
201         
202         address = 0;
203         line_number = 0;
204         i = 0;
205         while (i < header->code_size) {
206                 if (lines [i] == line_number)
207                         i ++;
208                 else {
209                         if (line_number > 0) {
210                                 //g_assert (addresses [i] - 1 >= address);
211                                 
212                                 if (addresses [i] - 1 >= address) {
213                                         VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
214                                         //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
215                                 }
216                         }
217                         address = addresses [i];
218                         line_number = lines [i];
219                 }
220         }
221
222         if (line_number > 0) {
223                 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
224                 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
225         }
226
227         VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
228
229         g_free (addresses);
230         g_free (lines);
231 #endif /* VALGRIND_ADD_LINE_INFO */
232 }
233
234 void
235 mono_debug_close_method (MonoCompile *cfg)
236 {
237         MiniDebugMethodInfo *info;
238         MonoDebugMethodJitInfo *jit;
239         MonoMethodHeader *header;
240         MonoMethodSignature *sig;
241         MonoDebugMethodAddress *debug_info;
242         MonoMethod *method;
243         int i;
244
245         info = (MiniDebugMethodInfo *) cfg->debug_info;
246         if (!info || !info->jit) {
247                 if (info)
248                         g_free (info);
249                 return;
250         }
251
252         method = cfg->method;
253         header = mono_method_get_header (method);
254         sig = mono_method_signature (method);
255
256         jit = info->jit;
257         jit->code_start = cfg->native_code;
258         jit->epilogue_begin = cfg->epilog_begin;
259         jit->code_size = cfg->code_len;
260
261         if (jit->epilogue_begin)
262                    record_line_number (info, jit->epilogue_begin, header->code_size);
263
264         jit->num_params = sig->param_count;
265         jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
266
267         for (i = 0; i < jit->num_locals; i++)
268                 write_variable (cfg->varinfo [cfg->locals_start + i], &jit->locals [i]);
269
270         if (sig->hasthis) {
271                 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
272                 write_variable (cfg->varinfo [0], jit->this_var);
273         }
274
275         for (i = 0; i < jit->num_params; i++)
276                 write_variable (cfg->varinfo [i + sig->hasthis], &jit->params [i]);
277
278         jit->num_line_numbers = info->line_numbers->len;
279         jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
280
281         for (i = 0; i < jit->num_line_numbers; i++)
282                 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
283
284         debug_info = mono_debug_add_method (method, jit, cfg->domain);
285
286         mono_debug_add_vg_method (method, jit);
287
288         if (info->breakpoint_id)
289                 mono_debugger_breakpoint_callback (method, info->breakpoint_id);
290
291         mono_debugger_check_breakpoints (method, debug_info);
292
293         mono_debug_free_method_jit_info (jit);
294         g_array_free (info->line_numbers, TRUE);
295         g_free (info);
296 }
297
298 void
299 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
300 {
301         MiniDebugMethodInfo *info;
302         MonoMethodHeader *header;
303         guint32 offset;
304
305         info = (MiniDebugMethodInfo *) cfg->debug_info;
306         if (!info || !info->jit || !ins->cil_code)
307                 return;
308
309         header = mono_method_get_header (cfg->method);
310         g_assert (header);
311
312         if ((ins->cil_code < header->code) ||
313             (ins->cil_code > header->code + header->code_size))
314                 return;
315
316         offset = ins->cil_code - header->code;
317         if (!info->has_line_numbers) {
318                 info->jit->prologue_end = address;
319                 info->has_line_numbers = TRUE;
320         }
321
322         record_line_number (info, address, offset);
323 }
324
325 void
326 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
327 {
328         MiniDebugMethodInfo *info;
329         MonoMethodHeader *header;
330         guint32 offset;
331
332         info = (MiniDebugMethodInfo *) cfg->debug_info;
333         if (!info || !info->jit || !bb->cil_code)
334                 return;
335
336         header = mono_method_get_header (cfg->method);
337         g_assert (header);
338
339         if ((bb->cil_code < header->code) ||
340             (bb->cil_code > header->code + header->code_size))
341                 return;
342
343         offset = bb->cil_code - header->code;
344         if (!info->has_line_numbers) {
345                 info->jit->prologue_end = address;
346                 info->has_line_numbers = TRUE;
347         }
348
349         record_line_number (info, address, offset);
350 }
351
352 static inline void
353 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
354 {
355         guint8 *p = buf;
356
357         //printf ("ENCODE: %d 0x%x.\n", value, value);
358
359         /* 
360          * Same encoding as the one used in the metadata, extended to handle values
361          * greater than 0x1fffffff.
362          */
363         if ((value >= 0) && (value <= 127))
364                 *p++ = value;
365         else if ((value >= 0) && (value <= 16383)) {
366                 p [0] = 0x80 | (value >> 8);
367                 p [1] = value & 0xff;
368                 p += 2;
369         } else if ((value >= 0) && (value <= 0x1fffffff)) {
370                 p [0] = (value >> 24) | 0xc0;
371                 p [1] = (value >> 16) & 0xff;
372                 p [2] = (value >> 8) & 0xff;
373                 p [3] = value & 0xff;
374                 p += 4;
375         }
376         else {
377                 p [0] = 0xff;
378                 p [1] = (value >> 24) & 0xff;
379                 p [2] = (value >> 16) & 0xff;
380                 p [3] = (value >> 8) & 0xff;
381                 p [4] = value & 0xff;
382                 p += 5;
383         }
384         if (endbuf)
385                 *endbuf = p;
386 }
387
388 static inline gint32
389 decode_value (guint8 *ptr, guint8 **rptr)
390 {
391         guint8 b = *ptr;
392         gint32 len;
393         
394         if ((b & 0x80) == 0){
395                 len = b;
396                 ++ptr;
397         } else if ((b & 0x40) == 0){
398                 len = ((b & 0x3f) << 8 | ptr [1]);
399                 ptr += 2;
400         } else if (b != 0xff) {
401                 len = ((b & 0x1f) << 24) |
402                         (ptr [1] << 16) |
403                         (ptr [2] << 8) |
404                         ptr [3];
405                 ptr += 4;
406         }
407         else {
408                 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
409                 ptr += 5;
410         }
411         if (rptr)
412                 *rptr = ptr;
413
414         //printf ("DECODE: %d.\n", len);
415         return len;
416 }
417
418 static void
419 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
420 {
421         guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
422
423         switch (flags) {
424         case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
425                 encode_value (var->index, p, &p);
426                 break;
427         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
428                 encode_value (var->index, p, &p);
429                 encode_value (var->offset, p, &p);
430                 break;
431         default:
432                 g_assert_not_reached ();
433         }
434         *endbuf = p;
435 }
436
437 void
438 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
439 {
440         MiniDebugMethodInfo *info;
441         MonoDebugMethodJitInfo *jit;
442         guint32 size, prev_offset, prev_native_offset;
443         guint8 *buf, *p;
444         int i;
445
446         info = (MiniDebugMethodInfo *) cfg->debug_info;
447         if (!info || !info->jit) {
448                 *buf_len = 0;
449                 return;
450         }
451         jit = info->jit;
452
453         size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
454         p = buf = g_malloc (size);
455         encode_value (jit->epilogue_begin, p, &p);
456         encode_value (jit->prologue_end, p, &p);
457         encode_value (jit->code_size, p, &p);
458
459         for (i = 0; i < jit->num_params; ++i)
460                 serialize_variable (&jit->params [i], p, &p);
461
462         if (mono_method_signature (cfg->method)->hasthis)
463                 serialize_variable (jit->this_var, p, &p);
464
465         for (i = 0; i < jit->num_locals; i++)
466                 serialize_variable (&jit->locals [i], p, &p);
467
468         encode_value (jit->num_line_numbers, p, &p);
469
470         prev_offset = 0;
471         prev_native_offset = 0;
472         for (i = 0; i < jit->num_line_numbers; ++i) {
473                 /* Sometimes, the offset values are not in increasing order */
474                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
475                 encode_value (lne->il_offset - prev_offset, p, &p);
476                 encode_value (lne->native_offset - prev_native_offset, p, &p);
477                 prev_offset = lne->il_offset;
478                 prev_native_offset = lne->native_offset;
479         }
480
481         g_assert (p - buf < size);
482
483         *out_buf = buf;
484         *buf_len = p - buf;
485 }
486
487 static void
488 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
489 {
490         guint32 flags;
491
492         var->index = decode_value (p, &p);
493
494         flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
495
496         switch (flags) {
497         case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
498                 break;
499         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
500                 var->offset = decode_value (p, &p);
501                 break;
502         default:
503                 g_assert_not_reached ();
504         }
505         *endbuf = p;
506 }
507
508 static MonoDebugMethodJitInfo *
509 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
510 {
511         MonoMethodHeader *header;
512         gint32 offset, native_offset, prev_offset, prev_native_offset;
513         MonoDebugMethodJitInfo *jit;
514         guint8 *p;
515         int i;
516
517         header = mono_method_get_header (method);
518         g_assert (header);
519
520         jit = g_new0 (MonoDebugMethodJitInfo, 1);
521         jit->code_start = code_start;
522         jit->num_locals = header->num_locals;
523         jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
524         jit->num_params = mono_method_signature (method)->param_count;
525         jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
526
527         p = buf;
528         jit->epilogue_begin = decode_value (p, &p);
529         jit->prologue_end = decode_value (p, &p);
530         jit->code_size = decode_value (p, &p);
531
532         for (i = 0; i < jit->num_params; ++i)
533                 deserialize_variable (&jit->params [i], p, &p);
534
535         if (mono_method_signature (method)->hasthis) {
536                 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
537                 deserialize_variable (jit->this_var, p, &p);
538         }
539
540         for (i = 0; i < jit->num_locals; i++)
541                 deserialize_variable (&jit->locals [i], p, &p);
542
543         jit->num_line_numbers = decode_value (p, &p);
544         jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
545
546         prev_offset = 0;
547         prev_native_offset = 0;
548         for (i = 0; i < jit->num_line_numbers; ++i) {
549                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
550
551                 offset = prev_offset + decode_value (p, &p);
552                 native_offset = prev_native_offset + decode_value (p, &p);
553
554                 lne->native_offset = native_offset;
555                 lne->il_offset = offset;
556
557                 prev_offset = offset;
558                 prev_native_offset = native_offset;
559         }
560
561         return jit;
562 }
563
564 void
565 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start, 
566                            guint8 *debug_info, guint32 debug_info_len)
567 {
568         MonoDebugMethodJitInfo *jit;
569
570         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
571                 return;
572
573         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
574             (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
575             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
576             (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
577             (method->wrapper_type != MONO_WRAPPER_NONE))
578                 return;
579
580         if (debug_info_len == 0)
581                 return;
582
583         jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
584
585         mono_debug_add_method (method, jit, domain);
586
587         mono_debug_add_vg_method (method, jit);
588
589         mono_debug_free_method_jit_info (jit);
590 }
591
592 void
593 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
594 {
595         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
596                 return;
597
598         // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
599 }
600
601 static void
602 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
603 {
604         switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
605         case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
606                 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
607                 break;
608         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
609                 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);
610                 break;
611         case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
612         default:
613                 g_assert_not_reached ();
614         }
615 }
616
617 /**
618  * mono_debug_print_locals:
619  *
620  * Prints to stdout the information about the local variables in
621  * a method (if @only_arguments is false) or about the arguments.
622  * The information includes the storage info (where the variable 
623  * lives, in a register or in memory).
624  * The method is found by looking up what method has been emitted at
625  * the instruction address @ip.
626  * This is for use inside a debugger.
627  */
628 void
629 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
630 {
631         MonoDomain *domain = mono_domain_get ();
632         MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
633         MonoDebugMethodJitInfo *jit;
634         int i;
635
636         if (!ji)
637                 return;
638
639         jit = mono_debug_find_method (mono_jit_info_get_method (ji), domain);
640         if (!jit)
641                 return;
642
643         if (only_arguments) {
644                 char **names;
645                 names = g_new (char *, jit->num_params);
646                 mono_method_get_param_names (mono_jit_info_get_method (ji), (const char **) names);
647                 if (jit->this_var)
648                         print_var_info (jit->this_var, 0, "this", "Arg");
649                 for (i = 0; i < jit->num_params; ++i) {
650                         print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
651                 }
652                 g_free (names);
653         } else {
654                 for (i = 0; i < jit->num_locals; ++i) {
655                         print_var_info (&jit->locals [i], i, "", "Local");
656                 }
657         }
658         mono_debug_free_method_jit_info (jit);
659 }
660
661 /*
662  * The old Debugger breakpoint interface.
663  *
664  * This interface is used to insert breakpoints on methods which are not yet JITed.
665  * The debugging code keeps a list of all such breakpoints and automatically inserts the
666  * breakpoint when the method is JITed.
667  */
668
669 static GPtrArray *breakpoints = NULL;
670
671 int
672 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
673 {
674         static int last_breakpoint_id = 0;
675         MiniDebugBreakpointInfo *info;
676
677         info = g_new0 (MiniDebugBreakpointInfo, 1);
678         info->desc = desc;
679         info->index = ++last_breakpoint_id;
680
681         if (!breakpoints)
682                 breakpoints = g_ptr_array_new ();
683
684         g_ptr_array_add (breakpoints, info);
685
686         return info->index;
687 }
688
689 int
690 mono_debugger_remove_breakpoint (int breakpoint_id)
691 {
692         int i;
693
694         if (!breakpoints)
695                 return 0;
696
697         for (i = 0; i < breakpoints->len; i++) {
698                 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
699
700                 if (info->index != breakpoint_id)
701                         continue;
702
703                 mono_method_desc_free (info->desc);
704                 g_ptr_array_remove (breakpoints, info);
705                 g_free (info);
706                 return 1;
707         }
708
709         return 0;
710 }
711
712 int
713 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
714 {
715         MonoMethodDesc *desc;
716
717         desc = mono_method_desc_new (method_name, include_namespace);
718         if (!desc)
719                 return 0;
720
721         return mono_debugger_insert_breakpoint_full (desc);
722 }
723
724 int
725 mono_debugger_method_has_breakpoint (MonoMethod *method)
726 {
727         int i;
728
729         if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
730                 return 0;
731
732         for (i = 0; i < breakpoints->len; i++) {
733                 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
734
735                 if (!mono_method_desc_full_match (info->desc, method))
736                         continue;
737
738                 return info->index;
739         }
740
741         return 0;
742 }
743
744 void
745 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
746 {
747         mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
748 }
749
750 void
751 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls)
752 {
753 #ifdef MONO_DEBUGGER_SUPPORTED
754         size_t stsize = 0;
755         guint8 *staddr = NULL;
756         MonoDebuggerThreadInfo *info;
757
758         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
759                 return;
760
761         mono_debugger_lock ();
762
763         mono_thread_get_stack_bounds (&staddr, &stsize);
764
765         info = g_new0 (MonoDebuggerThreadInfo, 1);
766         info->tid = tid;
767         info->thread = thread;
768         info->stack_start = (guint64) (gsize) staddr;
769         info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
770         info->stack_size = stsize;
771         info->signal_stack_size = jit_tls->signal_stack_size;
772         info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
773         info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
774         info->jit_tls = jit_tls;
775
776         info->next = mono_debugger_thread_table;
777         mono_debugger_thread_table = info;
778
779         mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
780                              tid, (guint64) (gsize) info);
781
782         mono_debugger_unlock ();
783 #endif /* MONO_DEBUGGER_SUPPORTED */
784 }
785
786 void
787 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
788 {
789 #ifdef MONO_DEBUGGER_SUPPORTED
790         MonoDebuggerThreadInfo **ptr;
791
792         if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
793                 return;
794
795         mono_debugger_lock ();
796
797         for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
798                 MonoDebuggerThreadInfo *info = *ptr;
799
800                 if (info->jit_tls != jit_tls)
801                         continue;
802
803                 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
804                                      info->tid, (guint64) (gsize) info);
805
806                 *ptr = info->next;
807                 g_free (info);
808                 break;
809         }
810
811         mono_debugger_unlock ();
812 #endif
813 }
814
815 void
816 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
817 {
818 #ifdef MONO_DEBUGGER_SUPPORTED
819         MonoDebuggerThreadInfo **ptr;
820         MonoThread *thread = mono_thread_current ();
821
822         if (!mono_debug_using_mono_debugger ())
823                 return;
824
825         mono_debugger_lock ();
826
827         for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
828                 MonoDebuggerThreadInfo *info = *ptr;
829
830                 if (info->thread != thread)
831                         continue;
832
833                 if ((info->extended_notifications & (int) event) == 0)
834                         continue;
835
836                 mono_debugger_event (event, data, arg);
837         }
838
839         mono_debugger_unlock ();
840 #endif
841 }
842
843 void
844 mono_debugger_trampoline_compiled (MonoMethod *method, const guint8 *code)
845 {
846         mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
847                                              (guint64) (gsize) method, (guint64) (gsize) code);
848 }