Merge pull request #3344 from alexanderkyte/propagate_mono_error_async
[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         jit->has_var_info = debug_options.mdb_optimizations != 0;
244
245         if (jit->epilogue_begin)
246                    record_line_number (info, jit->epilogue_begin, header->code_size);
247
248         if (jit->has_var_info) {
249                 jit->num_params = sig->param_count;
250                 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
251
252                 for (i = 0; i < jit->num_locals; i++)
253                         write_variable (cfg->locals [i], &jit->locals [i]);
254
255                 if (sig->hasthis) {
256                         jit->this_var = g_new0 (MonoDebugVarInfo, 1);
257                         write_variable (cfg->args [0], jit->this_var);
258                 }
259
260                 for (i = 0; i < jit->num_params; i++)
261                         write_variable (cfg->args [i + sig->hasthis], &jit->params [i]);
262
263                 if (cfg->gsharedvt_info_var) {
264                         jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
265                         jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
266                         write_variable (cfg->gsharedvt_info_var, jit->gsharedvt_info_var);
267                         write_variable (cfg->gsharedvt_locals_var, jit->gsharedvt_locals_var);
268                 }
269         }
270
271         jit->num_line_numbers = info->line_numbers->len;
272         jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
273
274         for (i = 0; i < jit->num_line_numbers; i++)
275                 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
276
277         mono_debug_add_method (cfg->method_to_register, jit, cfg->domain);
278
279         mono_debug_add_vg_method (method, jit);
280
281         mono_debug_free_method_jit_info (jit);
282         mono_debug_free_method (cfg);
283 }
284
285 void
286 mono_debug_free_method (MonoCompile *cfg)
287 {
288         MiniDebugMethodInfo *info;
289
290         info = (MiniDebugMethodInfo *) cfg->debug_info;
291         if (info) {
292                 if (info->line_numbers)
293                         g_array_free (info->line_numbers, TRUE);
294                 g_free (info);
295                 cfg->debug_info = NULL; 
296         }
297 }
298
299 void
300 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
301 {
302         MiniDebugMethodInfo *info;
303         MonoMethodHeader *header;
304         guint32 offset;
305
306         info = (MiniDebugMethodInfo *) cfg->debug_info;
307         if (!info || !info->jit || !ins->cil_code)
308                 return;
309
310         header = cfg->header;
311         g_assert (header);
312
313         if ((ins->cil_code < header->code) ||
314             (ins->cil_code > header->code + header->code_size))
315                 return;
316
317         offset = ins->cil_code - header->code;
318         if (!info->has_line_numbers) {
319                 info->jit->prologue_end = address;
320                 info->has_line_numbers = TRUE;
321         }
322
323         record_line_number (info, address, offset);
324 }
325
326 void
327 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
328 {
329         MiniDebugMethodInfo *info;
330         MonoMethodHeader *header;
331         guint32 offset;
332
333         info = (MiniDebugMethodInfo *) cfg->debug_info;
334         if (!info || !info->jit || !bb->cil_code)
335                 return;
336
337         header = cfg->header;
338         g_assert (header);
339
340         if ((bb->cil_code < header->code) ||
341             (bb->cil_code > header->code + header->code_size))
342                 return;
343
344         offset = bb->cil_code - header->code;
345         if (!info->has_line_numbers) {
346                 info->jit->prologue_end = address;
347                 info->has_line_numbers = TRUE;
348         }
349
350         record_line_number (info, address, offset);
351 }
352
353 static inline void
354 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
355 {
356         guint8 *p = buf;
357
358         //printf ("ENCODE: %d 0x%x.\n", value, value);
359
360         /* 
361          * Same encoding as the one used in the metadata, extended to handle values
362          * greater than 0x1fffffff.
363          */
364         if ((value >= 0) && (value <= 127))
365                 *p++ = value;
366         else if ((value >= 0) && (value <= 16383)) {
367                 p [0] = 0x80 | (value >> 8);
368                 p [1] = value & 0xff;
369                 p += 2;
370         } else if ((value >= 0) && (value <= 0x1fffffff)) {
371                 p [0] = (value >> 24) | 0xc0;
372                 p [1] = (value >> 16) & 0xff;
373                 p [2] = (value >> 8) & 0xff;
374                 p [3] = value & 0xff;
375                 p += 4;
376         }
377         else {
378                 p [0] = 0xff;
379                 p [1] = (value >> 24) & 0xff;
380                 p [2] = (value >> 16) & 0xff;
381                 p [3] = (value >> 8) & 0xff;
382                 p [4] = value & 0xff;
383                 p += 5;
384         }
385         if (endbuf)
386                 *endbuf = p;
387 }
388
389 static inline gint32
390 decode_value (guint8 *ptr, guint8 **rptr)
391 {
392         guint8 b = *ptr;
393         gint32 len;
394         
395         if ((b & 0x80) == 0){
396                 len = b;
397                 ++ptr;
398         } else if ((b & 0x40) == 0){
399                 len = ((b & 0x3f) << 8 | ptr [1]);
400                 ptr += 2;
401         } else if (b != 0xff) {
402                 len = ((b & 0x1f) << 24) |
403                         (ptr [1] << 16) |
404                         (ptr [2] << 8) |
405                         ptr [3];
406                 ptr += 4;
407         }
408         else {
409                 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
410                 ptr += 5;
411         }
412         if (rptr)
413                 *rptr = ptr;
414
415         //printf ("DECODE: %d.\n", len);
416         return len;
417 }
418
419 static void
420 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
421 {
422         guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
423
424         encode_value (var->index, p, &p);
425
426         switch (flags) {
427         case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
428                 break;
429         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
430         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
431         case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
432                 encode_value (var->offset, p, &p);
433                 break;
434         case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
435         case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
436                 break;
437         default:
438                 g_assert_not_reached ();
439         }
440         *endbuf = p;
441 }
442
443 void
444 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
445 {
446         MonoDebugMethodJitInfo *jit;
447         guint32 size, prev_offset, prev_native_offset;
448         guint8 *buf, *p;
449         int i;
450
451         /* Can't use cfg->debug_info as it is freed by close_method () */
452         jit = mono_debug_find_method (cfg->method, mono_domain_get ());
453         if (!jit) {
454                 *buf_len = 0;
455                 return;
456         }
457
458         size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
459         p = buf = (guint8 *)g_malloc (size);
460         encode_value (jit->epilogue_begin, p, &p);
461         encode_value (jit->prologue_end, p, &p);
462         encode_value (jit->code_size, p, &p);
463         encode_value (jit->has_var_info, p, &p);
464
465         if (jit->has_var_info) {
466                 for (i = 0; i < jit->num_params; ++i)
467                         serialize_variable (&jit->params [i], p, &p);
468
469                 if (jit->this_var)
470                         serialize_variable (jit->this_var, p, &p);
471
472                 for (i = 0; i < jit->num_locals; i++)
473                         serialize_variable (&jit->locals [i], p, &p);
474
475                 if (jit->gsharedvt_info_var) {
476                         encode_value (1, p, &p);
477                         serialize_variable (jit->gsharedvt_info_var, p, &p);
478                         serialize_variable (jit->gsharedvt_locals_var, p, &p);
479                 } else {
480                         encode_value (0, p, &p);
481                 }
482         }
483
484         encode_value (jit->num_line_numbers, p, &p);
485
486         prev_offset = 0;
487         prev_native_offset = 0;
488         for (i = 0; i < jit->num_line_numbers; ++i) {
489                 /* Sometimes, the offset values are not in increasing order */
490                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
491                 encode_value (lne->il_offset - prev_offset, p, &p);
492                 encode_value (lne->native_offset - prev_native_offset, p, &p);
493                 prev_offset = lne->il_offset;
494                 prev_native_offset = lne->native_offset;
495         }
496
497         g_assert (p - buf < size);
498
499         *out_buf = buf;
500         *buf_len = p - buf;
501 }
502
503 static void
504 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
505 {
506         guint32 flags;
507
508         var->index = decode_value (p, &p);
509
510         flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
511
512         switch (flags) {
513         case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
514                 break;
515         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
516         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
517         case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
518                 var->offset = decode_value (p, &p);
519                 break;
520         case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
521         case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
522                 break;
523         default:
524                 g_assert_not_reached ();
525         }
526         *endbuf = p;
527 }
528
529 static MonoDebugMethodJitInfo *
530 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
531 {
532         MonoError error;
533         MonoMethodHeader *header;
534         gint32 offset, native_offset, prev_offset, prev_native_offset;
535         MonoDebugMethodJitInfo *jit;
536         guint8 *p;
537         int i;
538
539         header = mono_method_get_header_checked (method, &error);
540         mono_error_assert_ok (&error); /* FIXME don't swallow the error */
541
542         jit = g_new0 (MonoDebugMethodJitInfo, 1);
543         jit->code_start = code_start;
544
545         p = buf;
546         jit->epilogue_begin = decode_value (p, &p);
547         jit->prologue_end = decode_value (p, &p);
548         jit->code_size = decode_value (p, &p);
549         jit->has_var_info = decode_value (p, &p);
550
551         if (jit->has_var_info) {
552                 jit->num_locals = header->num_locals;
553                 jit->num_params = mono_method_signature (method)->param_count;
554                 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
555                 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
556
557                 for (i = 0; i < jit->num_params; ++i)
558                         deserialize_variable (&jit->params [i], p, &p);
559
560                 if (mono_method_signature (method)->hasthis) {
561                         jit->this_var = g_new0 (MonoDebugVarInfo, 1);
562                         deserialize_variable (jit->this_var, p, &p);
563                 }
564
565                 for (i = 0; i < jit->num_locals; i++)
566                         deserialize_variable (&jit->locals [i], p, &p);
567
568                 if (decode_value (p, &p)) {
569                         jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
570                         jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
571                         deserialize_variable (jit->gsharedvt_info_var, p, &p);
572                         deserialize_variable (jit->gsharedvt_locals_var, p, &p);
573                 }
574         }
575
576         jit->num_line_numbers = decode_value (p, &p);
577         jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
578
579         prev_offset = 0;
580         prev_native_offset = 0;
581         for (i = 0; i < jit->num_line_numbers; ++i) {
582                 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
583
584                 offset = prev_offset + decode_value (p, &p);
585                 native_offset = prev_native_offset + decode_value (p, &p);
586
587                 lne->native_offset = native_offset;
588                 lne->il_offset = offset;
589
590                 prev_offset = offset;
591                 prev_native_offset = native_offset;
592         }
593
594         mono_metadata_free_mh (header);
595         return jit;
596 }
597
598 void
599 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start, 
600                            guint8 *debug_info, guint32 debug_info_len)
601 {
602         MonoDebugMethodJitInfo *jit;
603
604         if (!mono_debug_enabled ())
605                 return;
606
607         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
608             (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
609             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
610             (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
611             (method->wrapper_type != MONO_WRAPPER_NONE))
612                 return;
613
614         if (debug_info_len == 0)
615                 return;
616
617         jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
618
619         mono_debug_add_method (method, jit, domain);
620
621         mono_debug_add_vg_method (method, jit);
622
623         mono_debug_free_method_jit_info (jit);
624 }
625
626 static void
627 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
628 {
629         switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
630         case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
631                 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
632                 break;
633         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
634                 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);
635                 break;
636         case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
637                 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);
638                 break;
639         case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
640                 g_print ("%s %s (%d) gsharedvt local.\n", type, name, idx);
641                 break;
642         case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
643                 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);
644                 break;
645         case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
646         default:
647                 g_assert_not_reached ();
648         }
649 }
650
651 /**
652  * mono_debug_print_locals:
653  *
654  * Prints to stdout the information about the local variables in
655  * a method (if @only_arguments is false) or about the arguments.
656  * The information includes the storage info (where the variable 
657  * lives, in a register or in memory).
658  * The method is found by looking up what method has been emitted at
659  * the instruction address @ip.
660  * This is for use inside a debugger.
661  */
662 void
663 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
664 {
665         MonoDomain *domain = mono_domain_get ();
666         MonoJitInfo *ji = mono_jit_info_table_find (domain, (char *)ip);
667         MonoDebugMethodJitInfo *jit;
668         int i;
669
670         if (!ji)
671                 return;
672
673         jit = mono_debug_find_method (jinfo_get_method (ji), domain);
674         if (!jit)
675                 return;
676
677         if (only_arguments) {
678                 char **names;
679                 names = g_new (char *, jit->num_params);
680                 mono_method_get_param_names (jinfo_get_method (ji), (const char **) names);
681                 if (jit->this_var)
682                         print_var_info (jit->this_var, 0, "this", "Arg");
683                 for (i = 0; i < jit->num_params; ++i) {
684                         print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
685                 }
686                 g_free (names);
687         } else {
688                 for (i = 0; i < jit->num_locals; ++i) {
689                         print_var_info (&jit->locals [i], i, "", "Local");
690                 }
691         }
692         mono_debug_free_method_jit_info (jit);
693 }
694
695 /*
696  * The old Debugger breakpoint interface.
697  *
698  * This interface is used to insert breakpoints on methods which are not yet JITed.
699  * The debugging code keeps a list of all such breakpoints and automatically inserts the
700  * breakpoint when the method is JITed.
701  */
702
703 static GPtrArray *breakpoints;
704
705 static int
706 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
707 {
708         static int last_breakpoint_id = 0;
709         MiniDebugBreakpointInfo *info;
710
711         info = g_new0 (MiniDebugBreakpointInfo, 1);
712         info->desc = desc;
713         info->index = ++last_breakpoint_id;
714
715         if (!breakpoints)
716                 breakpoints = g_ptr_array_new ();
717
718         g_ptr_array_add (breakpoints, info);
719
720         return info->index;
721 }
722
723 /*FIXME This is part of the public API by accident, remove it from there when possible. */
724 int
725 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
726 {
727         MonoMethodDesc *desc;
728
729         desc = mono_method_desc_new (method_name, include_namespace);
730         if (!desc)
731                 return 0;
732
733         return mono_debugger_insert_breakpoint_full (desc);
734 }
735
736 /*FIXME This is part of the public API by accident, remove it from there when possible. */
737 int
738 mono_debugger_method_has_breakpoint (MonoMethod *method)
739 {
740         int i;
741
742         if (!breakpoints)
743                 return 0;
744
745         for (i = 0; i < breakpoints->len; i++) {
746                 MiniDebugBreakpointInfo *info = (MiniDebugBreakpointInfo *)g_ptr_array_index (breakpoints, i);
747
748                 if (!mono_method_desc_full_match (info->desc, method))
749                         continue;
750
751                 return info->index;
752         }
753
754         return 0;
755 }