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 #include <mono/utils/valgrind.h>
25 #ifdef MONO_DEBUGGER_SUPPORTED
26 #include <libgc/include/libgc-mono-debugger.h>
32 } MiniDebugBreakpointInfo;
36 MonoDebugMethodJitInfo *jit;
38 guint32 has_line_numbers;
39 guint32 breakpoint_id;
40 } MiniDebugMethodInfo;
43 MonoObject *last_exception;
44 guint32 stopped_on_exception : 1;
45 guint32 stopped_on_unhandled : 1;
46 } MonoDebuggerExceptionState;
49 MONO_DEBUGGER_THREAD_FLAGS_NONE = 0,
50 MONO_DEBUGGER_THREAD_FLAGS_INTERNAL = 1,
51 MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL = 2
52 } MonoDebuggerThreadFlags;
55 MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_NONE = 0,
56 MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE = 1,
57 MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED = 2
58 } MonoDebuggerInternalThreadFlags;
60 struct _MonoDebuggerThreadInfo {
65 guint64 extended_notifications;
68 MonoDebuggerThreadInfo *next;
71 * The stack bounds are only used when reading a core file.
74 guint64 signal_stack_start;
76 guint32 signal_stack_size;
81 * The debugger doesn't access anything beyond this point.
83 MonoDebuggerExceptionState exception_state;
85 guint32 internal_flags;
87 MonoJitTlsData *jit_tls;
88 MonoInternalThread *thread;
92 gpointer stack_pointer;
93 MonoObject *exception_obj;
95 guint32 stop_unhandled;
96 } MonoDebuggerExceptionInfo;
98 MonoDebuggerThreadInfo *mono_debugger_thread_table = NULL;
101 record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
103 MonoDebugLineNumberEntry lne;
105 lne.native_offset = address;
106 lne.il_offset = offset;
108 g_array_append_val (info->line_numbers, lne);
113 mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block, guint32 breakpoint_id)
115 MiniDebugMethodInfo *info;
117 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
120 info = g_new0 (MiniDebugMethodInfo, 1);
121 info->breakpoint_id = breakpoint_id;
123 cfg->debug_info = info;
127 mono_debug_open_method (MonoCompile *cfg)
129 MiniDebugMethodInfo *info;
130 MonoDebugMethodJitInfo *jit;
131 MonoMethodHeader *header;
133 info = (MiniDebugMethodInfo *) cfg->debug_info;
137 mono_class_init (cfg->method->klass);
139 header = cfg->header;
142 info->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
143 info->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
144 jit->num_locals = header->num_locals;
145 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
149 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
151 var->type = inst->inst_vtype;
153 if (inst->opcode == OP_REGVAR)
154 var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
155 else if (inst->flags & MONO_INST_IS_DEAD)
156 var->index = MONO_DEBUG_VAR_ADDRESS_MODE_DEAD;
157 else if (inst->opcode == OP_REGOFFSET) {
158 /* the debug interface needs fixing to allow 0(%base) address */
159 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET;
160 var->offset = inst->inst_offset;
161 } else if (inst->opcode == OP_GSHAREDVT_ARG_REGOFFSET) {
162 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR;
163 var->offset = inst->inst_offset;
164 } else if (inst->opcode == OP_GSHAREDVT_LOCAL) {
165 var->index = inst->inst_imm | MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL;
166 } else if (inst->opcode == OP_VTARG_ADDR) {
167 var->offset = inst->inst_offset;
168 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR;
170 g_assert_not_reached ();
175 * mono_debug_add_vg_method:
177 * Register symbol information for the method with valgrind
180 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
182 #ifdef VALGRIND_ADD_LINE_INFO
183 MonoMethodHeader *header;
184 MonoDebugMethodInfo *minfo;
186 char *filename = NULL;
187 guint32 address, line_number;
188 const char *full_name;
192 if (!RUNNING_ON_VALGRIND)
195 header = mono_method_get_header (method);
197 full_name = mono_method_full_name (method, TRUE);
199 addresses = g_new0 (guint32, header->code_size + 1);
200 lines = g_new0 (guint32, header->code_size + 1);
203 * Very simple code to convert the addr->offset mappings that mono has
204 * into [addr-addr] ->line number mappings.
207 minfo = mono_debug_lookup_method (method);
209 /* Create offset->line number mapping */
210 for (i = 0; i < header->code_size; ++i) {
211 MonoDebugSourceLocation *location;
213 location = mono_debug_symfile_lookup_location (minfo, i);
217 lines [i] = location.row;
219 filename = location.source_file;
221 mono_debug_free_source_location (location);
225 /* Create address->offset mapping */
226 for (i = 0; i < jit->num_line_numbers; ++i) {
227 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
229 g_assert (lne->offset <= header->code_size);
231 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
232 addresses [lne->offset] = lne->address;
234 /* Fill out missing addresses */
236 for (i = 0; i < header->code_size; ++i) {
237 if (addresses [i] == 0)
238 addresses [i] = address;
240 address = addresses [i];
246 while (i < header->code_size) {
247 if (lines [i] == line_number)
250 if (line_number > 0) {
251 //g_assert (addresses [i] - 1 >= address);
253 if (addresses [i] - 1 >= address) {
254 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
255 //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
258 address = addresses [i];
259 line_number = lines [i];
263 if (line_number > 0) {
264 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
265 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
268 VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
272 mono_metadata_free_mh (header);
273 #endif /* VALGRIND_ADD_LINE_INFO */
277 mono_debug_close_method (MonoCompile *cfg)
279 MiniDebugMethodInfo *info;
280 MonoDebugMethodJitInfo *jit;
281 MonoMethodHeader *header;
282 MonoMethodSignature *sig;
283 MonoDebugMethodAddress *debug_info;
287 info = (MiniDebugMethodInfo *) cfg->debug_info;
288 if (!info || !info->jit) {
294 method = cfg->method;
295 header = cfg->header;
296 sig = mono_method_signature (method);
299 jit->code_start = cfg->native_code;
300 jit->epilogue_begin = cfg->epilog_begin;
301 jit->code_size = cfg->code_len;
303 if (jit->epilogue_begin)
304 record_line_number (info, jit->epilogue_begin, header->code_size);
306 jit->num_params = sig->param_count;
307 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
309 for (i = 0; i < jit->num_locals; i++)
310 write_variable (cfg->locals [i], &jit->locals [i]);
313 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
314 write_variable (cfg->args [0], jit->this_var);
317 for (i = 0; i < jit->num_params; i++)
318 write_variable (cfg->args [i + sig->hasthis], &jit->params [i]);
320 if (cfg->gsharedvt_info_var) {
321 jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
322 jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
323 write_variable (cfg->gsharedvt_info_var, jit->gsharedvt_info_var);
324 write_variable (cfg->gsharedvt_locals_var, jit->gsharedvt_locals_var);
327 jit->num_line_numbers = info->line_numbers->len;
328 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
330 for (i = 0; i < jit->num_line_numbers; i++)
331 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
333 debug_info = mono_debug_add_method (cfg->method_to_register, jit, cfg->domain);
335 mono_debug_add_vg_method (method, jit);
337 mono_debugger_check_breakpoints (method, debug_info);
339 mono_debug_free_method_jit_info (jit);
340 mono_debug_free_method (cfg);
344 mono_debug_free_method (MonoCompile *cfg)
346 MiniDebugMethodInfo *info;
348 info = (MiniDebugMethodInfo *) cfg->debug_info;
350 if (info->line_numbers)
351 g_array_free (info->line_numbers, TRUE);
353 cfg->debug_info = NULL;
358 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
360 MiniDebugMethodInfo *info;
361 MonoMethodHeader *header;
364 info = (MiniDebugMethodInfo *) cfg->debug_info;
365 if (!info || !info->jit || !ins->cil_code)
368 header = cfg->header;
371 if ((ins->cil_code < header->code) ||
372 (ins->cil_code > header->code + header->code_size))
375 offset = ins->cil_code - header->code;
376 if (!info->has_line_numbers) {
377 info->jit->prologue_end = address;
378 info->has_line_numbers = TRUE;
381 record_line_number (info, address, offset);
385 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
387 MiniDebugMethodInfo *info;
388 MonoMethodHeader *header;
391 info = (MiniDebugMethodInfo *) cfg->debug_info;
392 if (!info || !info->jit || !bb->cil_code)
395 header = cfg->header;
398 if ((bb->cil_code < header->code) ||
399 (bb->cil_code > header->code + header->code_size))
402 offset = bb->cil_code - header->code;
403 if (!info->has_line_numbers) {
404 info->jit->prologue_end = address;
405 info->has_line_numbers = TRUE;
408 record_line_number (info, address, offset);
412 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
416 //printf ("ENCODE: %d 0x%x.\n", value, value);
419 * Same encoding as the one used in the metadata, extended to handle values
420 * greater than 0x1fffffff.
422 if ((value >= 0) && (value <= 127))
424 else if ((value >= 0) && (value <= 16383)) {
425 p [0] = 0x80 | (value >> 8);
426 p [1] = value & 0xff;
428 } else if ((value >= 0) && (value <= 0x1fffffff)) {
429 p [0] = (value >> 24) | 0xc0;
430 p [1] = (value >> 16) & 0xff;
431 p [2] = (value >> 8) & 0xff;
432 p [3] = value & 0xff;
437 p [1] = (value >> 24) & 0xff;
438 p [2] = (value >> 16) & 0xff;
439 p [3] = (value >> 8) & 0xff;
440 p [4] = value & 0xff;
448 decode_value (guint8 *ptr, guint8 **rptr)
453 if ((b & 0x80) == 0){
456 } else if ((b & 0x40) == 0){
457 len = ((b & 0x3f) << 8 | ptr [1]);
459 } else if (b != 0xff) {
460 len = ((b & 0x1f) << 24) |
467 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
473 //printf ("DECODE: %d.\n", len);
478 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
480 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
482 encode_value (var->index, p, &p);
485 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
487 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
488 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
489 encode_value (var->offset, p, &p);
491 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
492 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
493 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
496 g_assert_not_reached ();
502 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
504 MonoDebugMethodJitInfo *jit;
505 guint32 size, prev_offset, prev_native_offset;
509 /* Can't use cfg->debug_info as it is freed by close_method () */
510 jit = mono_debug_find_method (cfg->method, mono_domain_get ());
516 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
517 p = buf = g_malloc (size);
518 encode_value (jit->epilogue_begin, p, &p);
519 encode_value (jit->prologue_end, p, &p);
520 encode_value (jit->code_size, p, &p);
522 for (i = 0; i < jit->num_params; ++i)
523 serialize_variable (&jit->params [i], p, &p);
525 if (mono_method_signature (cfg->method)->hasthis)
526 serialize_variable (jit->this_var, p, &p);
528 for (i = 0; i < jit->num_locals; i++)
529 serialize_variable (&jit->locals [i], p, &p);
531 if (jit->gsharedvt_info_var) {
532 encode_value (1, p, &p);
533 serialize_variable (jit->gsharedvt_info_var, p, &p);
534 serialize_variable (jit->gsharedvt_locals_var, p, &p);
536 encode_value (0, p, &p);
539 encode_value (jit->num_line_numbers, p, &p);
542 prev_native_offset = 0;
543 for (i = 0; i < jit->num_line_numbers; ++i) {
544 /* Sometimes, the offset values are not in increasing order */
545 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
546 encode_value (lne->il_offset - prev_offset, p, &p);
547 encode_value (lne->native_offset - prev_native_offset, p, &p);
548 prev_offset = lne->il_offset;
549 prev_native_offset = lne->native_offset;
552 g_assert (p - buf < size);
559 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
563 var->index = decode_value (p, &p);
565 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
568 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
570 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
571 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
572 var->offset = decode_value (p, &p);
574 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
575 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
576 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
579 g_assert_not_reached ();
584 static MonoDebugMethodJitInfo *
585 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
587 MonoMethodHeader *header;
588 gint32 offset, native_offset, prev_offset, prev_native_offset;
589 MonoDebugMethodJitInfo *jit;
593 header = mono_method_get_header (method);
596 jit = g_new0 (MonoDebugMethodJitInfo, 1);
597 jit->code_start = code_start;
598 jit->num_locals = header->num_locals;
599 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
600 jit->num_params = mono_method_signature (method)->param_count;
601 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
604 jit->epilogue_begin = decode_value (p, &p);
605 jit->prologue_end = decode_value (p, &p);
606 jit->code_size = decode_value (p, &p);
608 for (i = 0; i < jit->num_params; ++i)
609 deserialize_variable (&jit->params [i], p, &p);
611 if (mono_method_signature (method)->hasthis) {
612 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
613 deserialize_variable (jit->this_var, p, &p);
616 for (i = 0; i < jit->num_locals; i++)
617 deserialize_variable (&jit->locals [i], p, &p);
619 if (decode_value (p, &p)) {
620 jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
621 jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
622 deserialize_variable (jit->gsharedvt_info_var, p, &p);
623 deserialize_variable (jit->gsharedvt_locals_var, p, &p);
626 jit->num_line_numbers = decode_value (p, &p);
627 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
630 prev_native_offset = 0;
631 for (i = 0; i < jit->num_line_numbers; ++i) {
632 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
634 offset = prev_offset + decode_value (p, &p);
635 native_offset = prev_native_offset + decode_value (p, &p);
637 lne->native_offset = native_offset;
638 lne->il_offset = offset;
640 prev_offset = offset;
641 prev_native_offset = native_offset;
644 mono_metadata_free_mh (header);
649 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
650 guint8 *debug_info, guint32 debug_info_len)
652 MonoDebugMethodJitInfo *jit;
654 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
657 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
658 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
659 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
660 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
661 (method->wrapper_type != MONO_WRAPPER_NONE))
664 if (debug_info_len == 0)
667 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
669 mono_debug_add_method (method, jit, domain);
671 mono_debug_add_vg_method (method, jit);
673 mono_debug_free_method_jit_info (jit);
677 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
679 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
682 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
686 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
688 switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
689 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
690 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
692 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
693 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);
695 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
696 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);
698 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
699 g_print ("%s %s (%d) gsharedvt local.\n", type, name, idx);
701 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
702 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);
704 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
706 g_assert_not_reached ();
711 * mono_debug_print_locals:
713 * Prints to stdout the information about the local variables in
714 * a method (if @only_arguments is false) or about the arguments.
715 * The information includes the storage info (where the variable
716 * lives, in a register or in memory).
717 * The method is found by looking up what method has been emitted at
718 * the instruction address @ip.
719 * This is for use inside a debugger.
722 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
724 MonoDomain *domain = mono_domain_get ();
725 MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
726 MonoDebugMethodJitInfo *jit;
732 jit = mono_debug_find_method (mono_jit_info_get_method (ji), domain);
736 if (only_arguments) {
738 names = g_new (char *, jit->num_params);
739 mono_method_get_param_names (mono_jit_info_get_method (ji), (const char **) names);
741 print_var_info (jit->this_var, 0, "this", "Arg");
742 for (i = 0; i < jit->num_params; ++i) {
743 print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
747 for (i = 0; i < jit->num_locals; ++i) {
748 print_var_info (&jit->locals [i], i, "", "Local");
751 mono_debug_free_method_jit_info (jit);
755 * The old Debugger breakpoint interface.
757 * This interface is used to insert breakpoints on methods which are not yet JITed.
758 * The debugging code keeps a list of all such breakpoints and automatically inserts the
759 * breakpoint when the method is JITed.
762 static GPtrArray *breakpoints = NULL;
765 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
767 static int last_breakpoint_id = 0;
768 MiniDebugBreakpointInfo *info;
770 info = g_new0 (MiniDebugBreakpointInfo, 1);
772 info->index = ++last_breakpoint_id;
775 breakpoints = g_ptr_array_new ();
777 g_ptr_array_add (breakpoints, info);
783 mono_debugger_remove_breakpoint (int breakpoint_id)
790 for (i = 0; i < breakpoints->len; i++) {
791 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
793 if (info->index != breakpoint_id)
796 mono_method_desc_free (info->desc);
797 g_ptr_array_remove (breakpoints, info);
806 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
808 MonoMethodDesc *desc;
810 desc = mono_method_desc_new (method_name, include_namespace);
814 return mono_debugger_insert_breakpoint_full (desc);
818 mono_debugger_method_has_breakpoint (MonoMethod *method)
825 for (i = 0; i < breakpoints->len; i++) {
826 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
828 if (!mono_method_desc_full_match (info->desc, method))
838 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
840 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
844 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls, gpointer func)
846 #ifdef MONO_DEBUGGER_SUPPORTED
848 guint8 *staddr = NULL;
849 MonoDebuggerThreadInfo *info;
851 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
854 mono_debugger_lock ();
856 mono_thread_get_stack_bounds (&staddr, &stsize);
858 info = g_new0 (MonoDebuggerThreadInfo, 1);
860 info->thread = thread->internal_thread;
861 info->stack_start = (guint64) (gsize) staddr;
862 info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
863 info->stack_size = stsize;
864 info->signal_stack_size = jit_tls->signal_stack_size;
865 info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
866 info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
867 info->jit_tls = jit_tls;
870 info->thread_flags = MONO_DEBUGGER_THREAD_FLAGS_INTERNAL;
871 if (thread->internal_thread->threadpool_thread)
872 info->thread_flags |= MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL;
874 info->next = mono_debugger_thread_table;
875 mono_debugger_thread_table = info;
877 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
878 tid, (guint64) (gsize) info);
880 mono_debugger_unlock ();
881 #endif /* MONO_DEBUGGER_SUPPORTED */
885 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
887 #ifdef MONO_DEBUGGER_SUPPORTED
888 MonoDebuggerThreadInfo **ptr;
890 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
893 mono_debugger_lock ();
895 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
896 MonoDebuggerThreadInfo *info = *ptr;
898 if (info->jit_tls != jit_tls)
901 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
902 info->tid, (guint64) (gsize) info);
909 mono_debugger_unlock ();
914 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
916 #ifdef MONO_DEBUGGER_SUPPORTED
917 MonoDebuggerThreadInfo **ptr;
918 MonoInternalThread *thread = mono_thread_internal_current ();
920 if (!mono_debug_using_mono_debugger ())
923 mono_debugger_lock ();
925 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
926 MonoDebuggerThreadInfo *info = *ptr;
928 if (info->thread != thread)
931 if ((info->extended_notifications & (int) event) == 0)
934 mono_debugger_event (event, data, arg);
937 mono_debugger_unlock ();
942 mono_debugger_trampoline_compiled (const guint8 *trampoline, MonoMethod *method, const guint8 *code)
944 #ifdef MONO_DEBUGGER_SUPPORTED
946 const guint8 * trampoline;
949 } info = { trampoline, method, code };
951 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_OLD_TRAMPOLINE,
952 (guint64) (gsize) method, (guint64) (gsize) code);
953 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
954 (guint64) (gsize) &info, 0);
958 #if MONO_DEBUGGER_SUPPORTED
959 static MonoDebuggerThreadInfo *
960 find_debugger_thread_info (MonoInternalThread *thread)
962 MonoDebuggerThreadInfo **ptr;
964 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
965 MonoDebuggerThreadInfo *info = *ptr;
967 if (info->thread == thread)
975 MonoDebuggerExceptionAction
976 _mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
978 #ifdef MONO_DEBUGGER_SUPPORTED
979 MonoDebuggerExceptionInfo exc_info;
980 MonoDebuggerThreadInfo *thread_info;
982 if (!mono_debug_using_mono_debugger ())
983 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
985 mono_debugger_lock ();
987 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
989 mono_debugger_unlock ();
990 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
993 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
994 mono_debugger_unlock ();
995 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
998 if (thread_info->exception_state.stopped_on_exception ||
999 thread_info->exception_state.stopped_on_unhandled) {
1000 thread_info->exception_state.stopped_on_exception = 0;
1001 mono_debugger_unlock ();
1002 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
1005 /* Protect the exception object from being garbage collected. */
1007 thread_info->exception_state.stopped_on_unhandled = 0;
1008 thread_info->exception_state.stopped_on_exception = 1;
1009 thread_info->exception_state.last_exception = exc;
1012 * Backwards compatibility:
1014 * Older debugger versions only know `exc_info.stop' and older runtime versions check
1015 * `exc_info.stop != 0'.
1017 * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
1018 * `stop_unhandled' field.
1021 exc_info.stack_pointer = stack;
1022 exc_info.exception_obj = exc;
1024 exc_info.stop_unhandled = 0;
1026 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &exc_info,
1027 (guint64) (gsize) addr);
1029 if (!exc_info.stop) {
1030 thread_info->exception_state.stopped_on_exception = 0;
1031 thread_info->exception_state.last_exception = NULL;
1034 mono_debugger_unlock ();
1037 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP;
1038 else if (exc_info.stop_unhandled)
1039 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED;
1042 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
1046 _mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
1048 #ifdef MONO_DEBUGGER_SUPPORTED
1049 MonoDebuggerThreadInfo *thread_info;
1051 if (!mono_debug_using_mono_debugger ())
1055 const gchar *name = mono_class_get_name (mono_object_get_class (exc));
1056 if (!strcmp (name, "ThreadAbortException"))
1060 mono_debugger_lock ();
1062 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1064 mono_debugger_unlock ();
1068 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1069 mono_debugger_unlock ();
1073 if (thread_info->exception_state.stopped_on_unhandled) {
1074 thread_info->exception_state.stopped_on_unhandled = 0;
1075 mono_debugger_unlock ();
1079 thread_info->exception_state.stopped_on_unhandled = 1;
1080 thread_info->exception_state.last_exception = exc;
1082 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
1083 (guint64) (gsize) exc, (guint64) (gsize) addr);
1092 * mono_debugger_call_exception_handler:
1094 * Called from mono_handle_exception_internal() to tell the debugger that we're about
1095 * to invoke an exception handler.
1097 * The debugger may choose to set a breakpoint at @addr. This is used if the user is
1098 * single-stepping from a `try' into a `catch' block, for instance.
1102 mono_debugger_call_exception_handler (gpointer addr, gpointer stack, MonoObject *exc)
1104 #ifdef MONO_DEBUGGER_SUPPORTED
1105 MonoDebuggerThreadInfo *thread_info;
1106 MonoDebuggerExceptionInfo exc_info;
1108 if (!mono_debug_using_mono_debugger ())
1111 mono_debugger_lock ();
1113 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1115 mono_debugger_unlock ();
1119 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1120 mono_debugger_unlock ();
1124 // Prevent the object from being finalized.
1125 thread_info->exception_state.last_exception = exc;
1127 exc_info.stack_pointer = stack;
1128 exc_info.exception_obj = exc;
1130 exc_info.stop_unhandled = 0;
1132 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &exc_info,
1133 (guint64) (gsize) addr);
1135 mono_debugger_unlock ();
1139 #ifdef MONO_DEBUGGER_SUPPORTED
1142 get_exception_message (MonoObject *exc)
1144 char *message = NULL;
1150 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1151 klass = exc->vtable->klass;
1153 while (klass && method == NULL) {
1154 for (i = 0; i < klass->method.count; ++i) {
1155 method = klass->methods [i];
1156 if (!strcmp ("ToString", method->name) &&
1157 mono_method_signature (method)->param_count == 0 &&
1158 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1159 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1166 klass = klass->parent;
1171 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1173 message = mono_string_to_utf8 (str);
1180 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1182 MonoDebuggerThreadInfo *thread_info;
1183 MonoDebuggerExceptionState saved_exception_state;
1187 mono_debugger_lock ();
1189 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1191 mono_debugger_unlock ();
1195 saved_exception_state = thread_info->exception_state;
1197 thread_info->exception_state.last_exception = NULL;
1198 thread_info->exception_state.stopped_on_unhandled = 0;
1199 thread_info->exception_state.stopped_on_exception = 0;
1201 thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1203 mono_debugger_unlock ();
1205 if (!strcmp (method->name, ".ctor")) {
1206 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1208 mono_runtime_invoke (method, obj, params, exc);
1210 retval = mono_runtime_invoke (method, obj, params, exc);
1212 mono_debugger_lock ();
1214 thread_info->exception_state = saved_exception_state;
1215 thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1217 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1218 thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1219 mono_thread_internal_reset_abort (thread_info->thread);
1221 mono_debugger_unlock ();
1227 mono_debugger_unlock ();
1229 if (!exc || (*exc == NULL))
1233 message = get_exception_message (*exc);
1235 *exc = (MonoObject *) mono_string_new_wrapper (message);
1243 mono_debugger_abort_runtime_invoke ()
1245 MonoInternalThread *thread = mono_thread_internal_current ();
1246 MonoDebuggerThreadInfo *thread_info;
1248 mono_debugger_lock ();
1250 thread_info = find_debugger_thread_info (thread);
1252 mono_debugger_unlock ();
1256 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE) == 0) {
1257 mono_debugger_unlock ();
1261 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1262 mono_debugger_unlock ();
1266 thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1267 ves_icall_System_Threading_Thread_Abort (thread_info->thread, NULL);
1269 mono_debugger_unlock ();