2 * debug-mini.c: Mini-specific debugging stuff.
5 * Martin Baulig (martin@ximian.com)
7 * (C) 2003 Ximian, Inc.
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>
19 #define _IN_THE_MONO_DEBUGGER
20 #include <mono/metadata/mono-debug-debugger.h>
21 #include "debug-mini.h"
23 #ifdef HAVE_VALGRIND_H
24 #include <valgrind/valgrind.h>
27 #ifdef MONO_DEBUGGER_SUPPORTED
28 #include <libgc/include/libgc-mono-debugger.h>
34 } MiniDebugBreakpointInfo;
38 MonoDebugMethodJitInfo *jit;
40 guint32 has_line_numbers;
41 guint32 breakpoint_id;
42 } MiniDebugMethodInfo;
44 struct _MonoDebuggerThreadInfo {
49 guint64 extended_notifications;
52 MonoDebuggerThreadInfo *next;
55 * The stack bounds are only used when reading a core file.
58 guint64 signal_stack_start;
60 guint32 signal_stack_size;
63 * The debugger doesn't access anything beyond this point.
65 MonoJitTlsData *jit_tls;
69 MonoDebuggerThreadInfo *mono_debugger_thread_table = NULL;
72 record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
74 MonoDebugLineNumberEntry lne;
76 lne.native_offset = address;
77 lne.il_offset = offset;
79 g_array_append_val (info->line_numbers, lne);
84 mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block, guint32 breakpoint_id)
86 MiniDebugMethodInfo *info;
88 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
91 info = g_new0 (MiniDebugMethodInfo, 1);
92 info->breakpoint_id = breakpoint_id;
94 cfg->debug_info = info;
98 mono_debug_open_method (MonoCompile *cfg)
100 MiniDebugMethodInfo *info;
101 MonoDebugMethodJitInfo *jit;
102 MonoMethodHeader *header;
104 info = (MiniDebugMethodInfo *) cfg->debug_info;
108 mono_class_init (cfg->method->klass);
110 header = mono_method_get_header (cfg->method);
113 info->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
114 info->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
115 jit->num_locals = header->num_locals;
116 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
120 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
122 var->type = inst->inst_vtype;
124 if (inst->opcode == OP_REGVAR)
125 var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
126 else if (inst->flags & MONO_INST_IS_DEAD) {
128 var->index = 0 | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
130 /* the debug interface needs fixing to allow 0(%base) address */
131 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET;
132 var->offset = inst->inst_offset;
137 * mono_debug_add_vg_method:
139 * Register symbol information for the method with valgrind
142 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
144 #ifdef VALGRIND_ADD_LINE_INFO
145 MonoMethodHeader *header;
146 MonoDebugMethodInfo *minfo;
148 char *filename = NULL;
149 guint32 address, line_number;
150 const char *full_name;
154 if (!RUNNING_ON_VALGRIND)
157 header = mono_method_get_header (method);
159 full_name = mono_method_full_name (method, TRUE);
161 addresses = g_new0 (guint32, header->code_size + 1);
162 lines = g_new0 (guint32, header->code_size + 1);
165 * Very simple code to convert the addr->offset mappings that mono has
166 * into [addr-addr] ->line number mappings.
169 minfo = mono_debug_lookup_method (method);
171 /* Create offset->line number mapping */
172 for (i = 0; i < header->code_size; ++i) {
173 MonoDebugSourceLocation *location;
175 location = mono_debug_symfile_lookup_location (minfo, i);
179 lines [i] = location.row;
181 filename = location.source_file;
183 mono_debug_free_source_location (location);
187 /* Create address->offset mapping */
188 for (i = 0; i < jit->num_line_numbers; ++i) {
189 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
191 g_assert (lne->offset <= header->code_size);
193 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
194 addresses [lne->offset] = lne->address;
196 /* Fill out missing addresses */
198 for (i = 0; i < header->code_size; ++i) {
199 if (addresses [i] == 0)
200 addresses [i] = address;
202 address = addresses [i];
208 while (i < header->code_size) {
209 if (lines [i] == line_number)
212 if (line_number > 0) {
213 //g_assert (addresses [i] - 1 >= address);
215 if (addresses [i] - 1 >= address) {
216 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
217 //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
220 address = addresses [i];
221 line_number = lines [i];
225 if (line_number > 0) {
226 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
227 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
230 VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
234 #endif /* VALGRIND_ADD_LINE_INFO */
238 mono_debug_close_method (MonoCompile *cfg)
240 MiniDebugMethodInfo *info;
241 MonoDebugMethodJitInfo *jit;
242 MonoMethodHeader *header;
243 MonoMethodSignature *sig;
244 MonoDebugMethodAddress *debug_info;
248 info = (MiniDebugMethodInfo *) cfg->debug_info;
249 if (!info || !info->jit) {
255 method = cfg->method;
256 header = mono_method_get_header (method);
257 sig = mono_method_signature (method);
260 jit->code_start = cfg->native_code;
261 jit->epilogue_begin = cfg->epilog_begin;
262 jit->code_size = cfg->code_len;
264 if (jit->epilogue_begin)
265 record_line_number (info, jit->epilogue_begin, header->code_size);
267 jit->num_params = sig->param_count;
268 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
270 for (i = 0; i < jit->num_locals; i++)
271 write_variable (cfg->locals [i], &jit->locals [i]);
274 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
275 write_variable (cfg->args [0], jit->this_var);
278 for (i = 0; i < jit->num_params; i++)
279 write_variable (cfg->args [i + sig->hasthis], &jit->params [i]);
281 jit->num_line_numbers = info->line_numbers->len;
282 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
284 for (i = 0; i < jit->num_line_numbers; i++)
285 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
287 debug_info = mono_debug_add_method (method, jit, cfg->domain);
289 mono_debug_add_vg_method (method, jit);
291 if (info->breakpoint_id)
292 mono_debugger_breakpoint_callback (method, info->breakpoint_id);
294 mono_debugger_check_breakpoints (method, debug_info);
296 mono_debug_free_method_jit_info (jit);
297 g_array_free (info->line_numbers, TRUE);
302 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
304 MiniDebugMethodInfo *info;
305 MonoMethodHeader *header;
308 info = (MiniDebugMethodInfo *) cfg->debug_info;
309 if (!info || !info->jit || !ins->cil_code)
312 header = mono_method_get_header (cfg->method);
315 if ((ins->cil_code < header->code) ||
316 (ins->cil_code > header->code + header->code_size))
319 offset = ins->cil_code - header->code;
320 if (!info->has_line_numbers) {
321 info->jit->prologue_end = address;
322 info->has_line_numbers = TRUE;
325 record_line_number (info, address, offset);
329 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
331 MiniDebugMethodInfo *info;
332 MonoMethodHeader *header;
335 info = (MiniDebugMethodInfo *) cfg->debug_info;
336 if (!info || !info->jit || !bb->cil_code)
339 header = mono_method_get_header (cfg->method);
342 if ((bb->cil_code < header->code) ||
343 (bb->cil_code > header->code + header->code_size))
346 offset = bb->cil_code - header->code;
347 if (!info->has_line_numbers) {
348 info->jit->prologue_end = address;
349 info->has_line_numbers = TRUE;
352 record_line_number (info, address, offset);
356 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
360 //printf ("ENCODE: %d 0x%x.\n", value, value);
363 * Same encoding as the one used in the metadata, extended to handle values
364 * greater than 0x1fffffff.
366 if ((value >= 0) && (value <= 127))
368 else if ((value >= 0) && (value <= 16383)) {
369 p [0] = 0x80 | (value >> 8);
370 p [1] = value & 0xff;
372 } else if ((value >= 0) && (value <= 0x1fffffff)) {
373 p [0] = (value >> 24) | 0xc0;
374 p [1] = (value >> 16) & 0xff;
375 p [2] = (value >> 8) & 0xff;
376 p [3] = value & 0xff;
381 p [1] = (value >> 24) & 0xff;
382 p [2] = (value >> 16) & 0xff;
383 p [3] = (value >> 8) & 0xff;
384 p [4] = value & 0xff;
392 decode_value (guint8 *ptr, guint8 **rptr)
397 if ((b & 0x80) == 0){
400 } else if ((b & 0x40) == 0){
401 len = ((b & 0x3f) << 8 | ptr [1]);
403 } else if (b != 0xff) {
404 len = ((b & 0x1f) << 24) |
411 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
417 //printf ("DECODE: %d.\n", len);
422 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
424 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
427 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
428 encode_value (var->index, p, &p);
430 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
431 encode_value (var->index, p, &p);
432 encode_value (var->offset, p, &p);
435 g_assert_not_reached ();
441 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
443 MonoDebugMethodJitInfo *jit;
444 guint32 size, prev_offset, prev_native_offset;
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 ());
455 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
456 p = buf = 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);
461 for (i = 0; i < jit->num_params; ++i)
462 serialize_variable (&jit->params [i], p, &p);
464 if (mono_method_signature (cfg->method)->hasthis)
465 serialize_variable (jit->this_var, p, &p);
467 for (i = 0; i < jit->num_locals; i++)
468 serialize_variable (&jit->locals [i], p, &p);
470 encode_value (jit->num_line_numbers, p, &p);
473 prev_native_offset = 0;
474 for (i = 0; i < jit->num_line_numbers; ++i) {
475 /* Sometimes, the offset values are not in increasing order */
476 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
477 encode_value (lne->il_offset - prev_offset, p, &p);
478 encode_value (lne->native_offset - prev_native_offset, p, &p);
479 prev_offset = lne->il_offset;
480 prev_native_offset = lne->native_offset;
483 g_assert (p - buf < size);
490 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
494 var->index = decode_value (p, &p);
496 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
499 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
501 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
502 var->offset = decode_value (p, &p);
505 g_assert_not_reached ();
510 static MonoDebugMethodJitInfo *
511 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
513 MonoMethodHeader *header;
514 gint32 offset, native_offset, prev_offset, prev_native_offset;
515 MonoDebugMethodJitInfo *jit;
519 header = mono_method_get_header (method);
522 jit = g_new0 (MonoDebugMethodJitInfo, 1);
523 jit->code_start = code_start;
524 jit->num_locals = header->num_locals;
525 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
526 jit->num_params = mono_method_signature (method)->param_count;
527 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
530 jit->epilogue_begin = decode_value (p, &p);
531 jit->prologue_end = decode_value (p, &p);
532 jit->code_size = decode_value (p, &p);
534 for (i = 0; i < jit->num_params; ++i)
535 deserialize_variable (&jit->params [i], p, &p);
537 if (mono_method_signature (method)->hasthis) {
538 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
539 deserialize_variable (jit->this_var, p, &p);
542 for (i = 0; i < jit->num_locals; i++)
543 deserialize_variable (&jit->locals [i], p, &p);
545 jit->num_line_numbers = decode_value (p, &p);
546 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
549 prev_native_offset = 0;
550 for (i = 0; i < jit->num_line_numbers; ++i) {
551 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
553 offset = prev_offset + decode_value (p, &p);
554 native_offset = prev_native_offset + decode_value (p, &p);
556 lne->native_offset = native_offset;
557 lne->il_offset = offset;
559 prev_offset = offset;
560 prev_native_offset = native_offset;
567 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
568 guint8 *debug_info, guint32 debug_info_len)
570 MonoDebugMethodJitInfo *jit;
572 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
575 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
576 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
577 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
578 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
579 (method->wrapper_type != MONO_WRAPPER_NONE))
582 if (debug_info_len == 0)
585 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
587 mono_debug_add_method (method, jit, domain);
589 mono_debug_add_vg_method (method, jit);
591 mono_debug_free_method_jit_info (jit);
595 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
597 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
600 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
604 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
606 switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
607 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
608 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
610 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
611 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);
613 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
615 g_assert_not_reached ();
620 * mono_debug_print_locals:
622 * Prints to stdout the information about the local variables in
623 * a method (if @only_arguments is false) or about the arguments.
624 * The information includes the storage info (where the variable
625 * lives, in a register or in memory).
626 * The method is found by looking up what method has been emitted at
627 * the instruction address @ip.
628 * This is for use inside a debugger.
631 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
633 MonoDomain *domain = mono_domain_get ();
634 MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
635 MonoDebugMethodJitInfo *jit;
641 jit = mono_debug_find_method (mono_jit_info_get_method (ji), domain);
645 if (only_arguments) {
647 names = g_new (char *, jit->num_params);
648 mono_method_get_param_names (mono_jit_info_get_method (ji), (const char **) names);
650 print_var_info (jit->this_var, 0, "this", "Arg");
651 for (i = 0; i < jit->num_params; ++i) {
652 print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
656 for (i = 0; i < jit->num_locals; ++i) {
657 print_var_info (&jit->locals [i], i, "", "Local");
660 mono_debug_free_method_jit_info (jit);
664 * The old Debugger breakpoint interface.
666 * This interface is used to insert breakpoints on methods which are not yet JITed.
667 * The debugging code keeps a list of all such breakpoints and automatically inserts the
668 * breakpoint when the method is JITed.
671 static GPtrArray *breakpoints = NULL;
674 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
676 static int last_breakpoint_id = 0;
677 MiniDebugBreakpointInfo *info;
679 info = g_new0 (MiniDebugBreakpointInfo, 1);
681 info->index = ++last_breakpoint_id;
684 breakpoints = g_ptr_array_new ();
686 g_ptr_array_add (breakpoints, info);
692 mono_debugger_remove_breakpoint (int breakpoint_id)
699 for (i = 0; i < breakpoints->len; i++) {
700 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
702 if (info->index != breakpoint_id)
705 mono_method_desc_free (info->desc);
706 g_ptr_array_remove (breakpoints, info);
715 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
717 MonoMethodDesc *desc;
719 desc = mono_method_desc_new (method_name, include_namespace);
723 return mono_debugger_insert_breakpoint_full (desc);
727 mono_debugger_method_has_breakpoint (MonoMethod *method)
731 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
734 for (i = 0; i < breakpoints->len; i++) {
735 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
737 if (!mono_method_desc_full_match (info->desc, method))
747 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
749 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
753 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls)
755 #ifdef MONO_DEBUGGER_SUPPORTED
757 guint8 *staddr = NULL;
758 MonoDebuggerThreadInfo *info;
760 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
763 mono_debugger_lock ();
765 mono_thread_get_stack_bounds (&staddr, &stsize);
767 info = g_new0 (MonoDebuggerThreadInfo, 1);
769 info->thread = thread;
770 info->stack_start = (guint64) (gsize) staddr;
771 info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
772 info->stack_size = stsize;
773 info->signal_stack_size = jit_tls->signal_stack_size;
774 info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
775 info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
776 info->jit_tls = jit_tls;
778 info->next = mono_debugger_thread_table;
779 mono_debugger_thread_table = info;
781 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
782 tid, (guint64) (gsize) info);
784 mono_debugger_unlock ();
785 #endif /* MONO_DEBUGGER_SUPPORTED */
789 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
791 #ifdef MONO_DEBUGGER_SUPPORTED
792 MonoDebuggerThreadInfo **ptr;
794 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
797 mono_debugger_lock ();
799 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
800 MonoDebuggerThreadInfo *info = *ptr;
802 if (info->jit_tls != jit_tls)
805 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
806 info->tid, (guint64) (gsize) info);
813 mono_debugger_unlock ();
818 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
820 #ifdef MONO_DEBUGGER_SUPPORTED
821 MonoDebuggerThreadInfo **ptr;
822 MonoThread *thread = mono_thread_current ();
824 if (!mono_debug_using_mono_debugger ())
827 mono_debugger_lock ();
829 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
830 MonoDebuggerThreadInfo *info = *ptr;
832 if (info->thread != thread)
835 if ((info->extended_notifications & (int) event) == 0)
838 mono_debugger_event (event, data, arg);
841 mono_debugger_unlock ();
846 mono_debugger_trampoline_compiled (MonoMethod *method, const guint8 *code)
848 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
849 (guint64) (gsize) method, (guint64) (gsize) code);