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