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