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