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) {
169 vtaddr = inst->inst_left;
170 g_assert (vtaddr->opcode == OP_REGOFFSET);
171 var->offset = vtaddr->inst_offset;
172 var->index = vtaddr->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR;
174 g_assert_not_reached ();
179 * mono_debug_add_vg_method:
181 * Register symbol information for the method with valgrind
184 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
186 #ifdef VALGRIND_ADD_LINE_INFO
187 MonoMethodHeader *header;
188 MonoDebugMethodInfo *minfo;
190 char *filename = NULL;
191 guint32 address, line_number;
192 const char *full_name;
196 if (!RUNNING_ON_VALGRIND)
199 header = mono_method_get_header (method);
201 full_name = mono_method_full_name (method, TRUE);
203 addresses = g_new0 (guint32, header->code_size + 1);
204 lines = g_new0 (guint32, header->code_size + 1);
207 * Very simple code to convert the addr->offset mappings that mono has
208 * into [addr-addr] ->line number mappings.
211 minfo = mono_debug_lookup_method (method);
213 /* Create offset->line number mapping */
214 for (i = 0; i < header->code_size; ++i) {
215 MonoDebugSourceLocation *location;
217 location = mono_debug_symfile_lookup_location (minfo, i);
221 lines [i] = location.row;
223 filename = location.source_file;
225 mono_debug_free_source_location (location);
229 /* Create address->offset mapping */
230 for (i = 0; i < jit->num_line_numbers; ++i) {
231 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
233 g_assert (lne->offset <= header->code_size);
235 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
236 addresses [lne->offset] = lne->address;
238 /* Fill out missing addresses */
240 for (i = 0; i < header->code_size; ++i) {
241 if (addresses [i] == 0)
242 addresses [i] = address;
244 address = addresses [i];
250 while (i < header->code_size) {
251 if (lines [i] == line_number)
254 if (line_number > 0) {
255 //g_assert (addresses [i] - 1 >= address);
257 if (addresses [i] - 1 >= address) {
258 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
259 //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
262 address = addresses [i];
263 line_number = lines [i];
267 if (line_number > 0) {
268 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
269 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
272 VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
276 mono_metadata_free_mh (header);
277 #endif /* VALGRIND_ADD_LINE_INFO */
281 mono_debug_close_method (MonoCompile *cfg)
283 MiniDebugMethodInfo *info;
284 MonoDebugMethodJitInfo *jit;
285 MonoMethodHeader *header;
286 MonoMethodSignature *sig;
287 MonoDebugMethodAddress *debug_info;
291 info = (MiniDebugMethodInfo *) cfg->debug_info;
292 if (!info || !info->jit) {
298 method = cfg->method;
299 header = cfg->header;
300 sig = mono_method_signature (method);
303 jit->code_start = cfg->native_code;
304 jit->epilogue_begin = cfg->epilog_begin;
305 jit->code_size = cfg->code_len;
307 if (jit->epilogue_begin)
308 record_line_number (info, jit->epilogue_begin, header->code_size);
310 jit->num_params = sig->param_count;
311 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
313 for (i = 0; i < jit->num_locals; i++)
314 write_variable (cfg->locals [i], &jit->locals [i]);
317 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
318 write_variable (cfg->args [0], jit->this_var);
321 for (i = 0; i < jit->num_params; i++)
322 write_variable (cfg->args [i + sig->hasthis], &jit->params [i]);
324 if (cfg->gsharedvt_info_var) {
325 jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
326 jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
327 write_variable (cfg->gsharedvt_info_var, jit->gsharedvt_info_var);
328 write_variable (cfg->gsharedvt_locals_var, jit->gsharedvt_locals_var);
331 jit->num_line_numbers = info->line_numbers->len;
332 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
334 for (i = 0; i < jit->num_line_numbers; i++)
335 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
337 debug_info = mono_debug_add_method (cfg->method_to_register, jit, cfg->domain);
339 mono_debug_add_vg_method (method, jit);
341 mono_debugger_check_breakpoints (method, debug_info);
343 mono_debug_free_method_jit_info (jit);
344 mono_debug_free_method (cfg);
348 mono_debug_free_method (MonoCompile *cfg)
350 MiniDebugMethodInfo *info;
352 info = (MiniDebugMethodInfo *) cfg->debug_info;
354 if (info->line_numbers)
355 g_array_free (info->line_numbers, TRUE);
357 cfg->debug_info = NULL;
362 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
364 MiniDebugMethodInfo *info;
365 MonoMethodHeader *header;
368 info = (MiniDebugMethodInfo *) cfg->debug_info;
369 if (!info || !info->jit || !ins->cil_code)
372 header = cfg->header;
375 if ((ins->cil_code < header->code) ||
376 (ins->cil_code > header->code + header->code_size))
379 offset = ins->cil_code - header->code;
380 if (!info->has_line_numbers) {
381 info->jit->prologue_end = address;
382 info->has_line_numbers = TRUE;
385 record_line_number (info, address, offset);
389 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
391 MiniDebugMethodInfo *info;
392 MonoMethodHeader *header;
395 info = (MiniDebugMethodInfo *) cfg->debug_info;
396 if (!info || !info->jit || !bb->cil_code)
399 header = cfg->header;
402 if ((bb->cil_code < header->code) ||
403 (bb->cil_code > header->code + header->code_size))
406 offset = bb->cil_code - header->code;
407 if (!info->has_line_numbers) {
408 info->jit->prologue_end = address;
409 info->has_line_numbers = TRUE;
412 record_line_number (info, address, offset);
416 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
420 //printf ("ENCODE: %d 0x%x.\n", value, value);
423 * Same encoding as the one used in the metadata, extended to handle values
424 * greater than 0x1fffffff.
426 if ((value >= 0) && (value <= 127))
428 else if ((value >= 0) && (value <= 16383)) {
429 p [0] = 0x80 | (value >> 8);
430 p [1] = value & 0xff;
432 } else if ((value >= 0) && (value <= 0x1fffffff)) {
433 p [0] = (value >> 24) | 0xc0;
434 p [1] = (value >> 16) & 0xff;
435 p [2] = (value >> 8) & 0xff;
436 p [3] = value & 0xff;
441 p [1] = (value >> 24) & 0xff;
442 p [2] = (value >> 16) & 0xff;
443 p [3] = (value >> 8) & 0xff;
444 p [4] = value & 0xff;
452 decode_value (guint8 *ptr, guint8 **rptr)
457 if ((b & 0x80) == 0){
460 } else if ((b & 0x40) == 0){
461 len = ((b & 0x3f) << 8 | ptr [1]);
463 } else if (b != 0xff) {
464 len = ((b & 0x1f) << 24) |
471 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
477 //printf ("DECODE: %d.\n", len);
482 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
484 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
486 encode_value (var->index, p, &p);
489 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
491 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
492 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
493 encode_value (var->offset, p, &p);
495 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
496 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
497 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
500 g_assert_not_reached ();
506 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
508 MonoDebugMethodJitInfo *jit;
509 guint32 size, prev_offset, prev_native_offset;
513 /* Can't use cfg->debug_info as it is freed by close_method () */
514 jit = mono_debug_find_method (cfg->method, mono_domain_get ());
520 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
521 p = buf = g_malloc (size);
522 encode_value (jit->epilogue_begin, p, &p);
523 encode_value (jit->prologue_end, p, &p);
524 encode_value (jit->code_size, p, &p);
526 for (i = 0; i < jit->num_params; ++i)
527 serialize_variable (&jit->params [i], p, &p);
529 if (mono_method_signature (cfg->method)->hasthis)
530 serialize_variable (jit->this_var, p, &p);
532 for (i = 0; i < jit->num_locals; i++)
533 serialize_variable (&jit->locals [i], p, &p);
535 if (jit->gsharedvt_info_var) {
536 encode_value (1, p, &p);
537 serialize_variable (jit->gsharedvt_info_var, p, &p);
538 serialize_variable (jit->gsharedvt_locals_var, p, &p);
540 encode_value (0, p, &p);
543 encode_value (jit->num_line_numbers, p, &p);
546 prev_native_offset = 0;
547 for (i = 0; i < jit->num_line_numbers; ++i) {
548 /* Sometimes, the offset values are not in increasing order */
549 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
550 encode_value (lne->il_offset - prev_offset, p, &p);
551 encode_value (lne->native_offset - prev_native_offset, p, &p);
552 prev_offset = lne->il_offset;
553 prev_native_offset = lne->native_offset;
556 g_assert (p - buf < size);
563 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
567 var->index = decode_value (p, &p);
569 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
572 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
574 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
575 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
576 var->offset = decode_value (p, &p);
578 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
579 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
580 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
583 g_assert_not_reached ();
588 static MonoDebugMethodJitInfo *
589 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
591 MonoMethodHeader *header;
592 gint32 offset, native_offset, prev_offset, prev_native_offset;
593 MonoDebugMethodJitInfo *jit;
597 header = mono_method_get_header (method);
600 jit = g_new0 (MonoDebugMethodJitInfo, 1);
601 jit->code_start = code_start;
602 jit->num_locals = header->num_locals;
603 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
604 jit->num_params = mono_method_signature (method)->param_count;
605 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
608 jit->epilogue_begin = decode_value (p, &p);
609 jit->prologue_end = decode_value (p, &p);
610 jit->code_size = decode_value (p, &p);
612 for (i = 0; i < jit->num_params; ++i)
613 deserialize_variable (&jit->params [i], p, &p);
615 if (mono_method_signature (method)->hasthis) {
616 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
617 deserialize_variable (jit->this_var, p, &p);
620 for (i = 0; i < jit->num_locals; i++)
621 deserialize_variable (&jit->locals [i], p, &p);
623 if (decode_value (p, &p)) {
624 jit->gsharedvt_info_var = g_new0 (MonoDebugVarInfo, 1);
625 jit->gsharedvt_locals_var = g_new0 (MonoDebugVarInfo, 1);
626 deserialize_variable (jit->gsharedvt_info_var, p, &p);
627 deserialize_variable (jit->gsharedvt_locals_var, p, &p);
630 jit->num_line_numbers = decode_value (p, &p);
631 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
634 prev_native_offset = 0;
635 for (i = 0; i < jit->num_line_numbers; ++i) {
636 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
638 offset = prev_offset + decode_value (p, &p);
639 native_offset = prev_native_offset + decode_value (p, &p);
641 lne->native_offset = native_offset;
642 lne->il_offset = offset;
644 prev_offset = offset;
645 prev_native_offset = native_offset;
648 mono_metadata_free_mh (header);
653 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
654 guint8 *debug_info, guint32 debug_info_len)
656 MonoDebugMethodJitInfo *jit;
658 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
661 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
662 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
663 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
664 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
665 (method->wrapper_type != MONO_WRAPPER_NONE))
668 if (debug_info_len == 0)
671 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
673 mono_debug_add_method (method, jit, domain);
675 mono_debug_add_vg_method (method, jit);
677 mono_debug_free_method_jit_info (jit);
681 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
683 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
686 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
690 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
692 switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
693 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
694 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
696 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
697 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);
699 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET_INDIR:
700 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);
702 case MONO_DEBUG_VAR_ADDRESS_MODE_GSHAREDVT_LOCAL:
703 g_print ("%s %s (%d) gsharedvt local.\n", type, name, idx);
705 case MONO_DEBUG_VAR_ADDRESS_MODE_VTADDR:
706 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);
708 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
710 g_assert_not_reached ();
715 * mono_debug_print_locals:
717 * Prints to stdout the information about the local variables in
718 * a method (if @only_arguments is false) or about the arguments.
719 * The information includes the storage info (where the variable
720 * lives, in a register or in memory).
721 * The method is found by looking up what method has been emitted at
722 * the instruction address @ip.
723 * This is for use inside a debugger.
726 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
728 MonoDomain *domain = mono_domain_get ();
729 MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
730 MonoDebugMethodJitInfo *jit;
736 jit = mono_debug_find_method (jinfo_get_method (ji), domain);
740 if (only_arguments) {
742 names = g_new (char *, jit->num_params);
743 mono_method_get_param_names (jinfo_get_method (ji), (const char **) names);
745 print_var_info (jit->this_var, 0, "this", "Arg");
746 for (i = 0; i < jit->num_params; ++i) {
747 print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
751 for (i = 0; i < jit->num_locals; ++i) {
752 print_var_info (&jit->locals [i], i, "", "Local");
755 mono_debug_free_method_jit_info (jit);
759 * The old Debugger breakpoint interface.
761 * This interface is used to insert breakpoints on methods which are not yet JITed.
762 * The debugging code keeps a list of all such breakpoints and automatically inserts the
763 * breakpoint when the method is JITed.
766 static GPtrArray *breakpoints = NULL;
769 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
771 static int last_breakpoint_id = 0;
772 MiniDebugBreakpointInfo *info;
774 info = g_new0 (MiniDebugBreakpointInfo, 1);
776 info->index = ++last_breakpoint_id;
779 breakpoints = g_ptr_array_new ();
781 g_ptr_array_add (breakpoints, info);
787 mono_debugger_remove_breakpoint (int breakpoint_id)
794 for (i = 0; i < breakpoints->len; i++) {
795 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
797 if (info->index != breakpoint_id)
800 mono_method_desc_free (info->desc);
801 g_ptr_array_remove (breakpoints, info);
810 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
812 MonoMethodDesc *desc;
814 desc = mono_method_desc_new (method_name, include_namespace);
818 return mono_debugger_insert_breakpoint_full (desc);
822 mono_debugger_method_has_breakpoint (MonoMethod *method)
829 for (i = 0; i < breakpoints->len; i++) {
830 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
832 if (!mono_method_desc_full_match (info->desc, method))
842 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
844 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
848 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls, gpointer func)
850 #ifdef MONO_DEBUGGER_SUPPORTED
852 guint8 *staddr = NULL;
853 MonoDebuggerThreadInfo *info;
855 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
858 mono_debugger_lock ();
860 mono_thread_get_stack_bounds (&staddr, &stsize);
862 info = g_new0 (MonoDebuggerThreadInfo, 1);
864 info->thread = thread->internal_thread;
865 info->stack_start = (guint64) (gsize) staddr;
866 info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
867 info->stack_size = stsize;
868 info->signal_stack_size = jit_tls->signal_stack_size;
869 info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
870 info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
871 info->jit_tls = jit_tls;
874 info->thread_flags = MONO_DEBUGGER_THREAD_FLAGS_INTERNAL;
875 if (thread->internal_thread->threadpool_thread)
876 info->thread_flags |= MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL;
878 info->next = mono_debugger_thread_table;
879 mono_debugger_thread_table = info;
881 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
882 tid, (guint64) (gsize) info);
884 mono_debugger_unlock ();
885 #endif /* MONO_DEBUGGER_SUPPORTED */
889 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
891 #ifdef MONO_DEBUGGER_SUPPORTED
892 MonoDebuggerThreadInfo **ptr;
894 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
897 mono_debugger_lock ();
899 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
900 MonoDebuggerThreadInfo *info = *ptr;
902 if (info->jit_tls != jit_tls)
905 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
906 info->tid, (guint64) (gsize) info);
913 mono_debugger_unlock ();
918 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
920 #ifdef MONO_DEBUGGER_SUPPORTED
921 MonoDebuggerThreadInfo **ptr;
922 MonoInternalThread *thread = mono_thread_internal_current ();
924 if (!mono_debug_using_mono_debugger ())
927 mono_debugger_lock ();
929 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
930 MonoDebuggerThreadInfo *info = *ptr;
932 if (info->thread != thread)
935 if ((info->extended_notifications & (int) event) == 0)
938 mono_debugger_event (event, data, arg);
941 mono_debugger_unlock ();
946 mono_debugger_trampoline_compiled (const guint8 *trampoline, MonoMethod *method, const guint8 *code)
948 #ifdef MONO_DEBUGGER_SUPPORTED
950 const guint8 * trampoline;
953 } info = { trampoline, method, code };
955 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_OLD_TRAMPOLINE,
956 (guint64) (gsize) method, (guint64) (gsize) code);
957 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
958 (guint64) (gsize) &info, 0);
962 #if MONO_DEBUGGER_SUPPORTED
963 static MonoDebuggerThreadInfo *
964 find_debugger_thread_info (MonoInternalThread *thread)
966 MonoDebuggerThreadInfo **ptr;
968 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
969 MonoDebuggerThreadInfo *info = *ptr;
971 if (info->thread == thread)
979 MonoDebuggerExceptionAction
980 _mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
982 #ifdef MONO_DEBUGGER_SUPPORTED
983 MonoDebuggerExceptionInfo exc_info;
984 MonoDebuggerThreadInfo *thread_info;
986 if (!mono_debug_using_mono_debugger ())
987 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
989 mono_debugger_lock ();
991 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
993 mono_debugger_unlock ();
994 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
997 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
998 mono_debugger_unlock ();
999 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
1002 if (thread_info->exception_state.stopped_on_exception ||
1003 thread_info->exception_state.stopped_on_unhandled) {
1004 thread_info->exception_state.stopped_on_exception = 0;
1005 mono_debugger_unlock ();
1006 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
1009 /* Protect the exception object from being garbage collected. */
1011 thread_info->exception_state.stopped_on_unhandled = 0;
1012 thread_info->exception_state.stopped_on_exception = 1;
1013 thread_info->exception_state.last_exception = exc;
1016 * Backwards compatibility:
1018 * Older debugger versions only know `exc_info.stop' and older runtime versions check
1019 * `exc_info.stop != 0'.
1021 * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
1022 * `stop_unhandled' field.
1025 exc_info.stack_pointer = stack;
1026 exc_info.exception_obj = exc;
1028 exc_info.stop_unhandled = 0;
1030 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &exc_info,
1031 (guint64) (gsize) addr);
1033 if (!exc_info.stop) {
1034 thread_info->exception_state.stopped_on_exception = 0;
1035 thread_info->exception_state.last_exception = NULL;
1038 mono_debugger_unlock ();
1041 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP;
1042 else if (exc_info.stop_unhandled)
1043 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED;
1046 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
1050 _mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
1052 #ifdef MONO_DEBUGGER_SUPPORTED
1053 MonoDebuggerThreadInfo *thread_info;
1055 if (!mono_debug_using_mono_debugger ())
1059 const gchar *name = mono_class_get_name (mono_object_get_class (exc));
1060 if (!strcmp (name, "ThreadAbortException"))
1064 mono_debugger_lock ();
1066 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1068 mono_debugger_unlock ();
1072 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1073 mono_debugger_unlock ();
1077 if (thread_info->exception_state.stopped_on_unhandled) {
1078 thread_info->exception_state.stopped_on_unhandled = 0;
1079 mono_debugger_unlock ();
1083 thread_info->exception_state.stopped_on_unhandled = 1;
1084 thread_info->exception_state.last_exception = exc;
1086 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
1087 (guint64) (gsize) exc, (guint64) (gsize) addr);
1096 * mono_debugger_call_exception_handler:
1098 * Called from mono_handle_exception_internal() to tell the debugger that we're about
1099 * to invoke an exception handler.
1101 * The debugger may choose to set a breakpoint at @addr. This is used if the user is
1102 * single-stepping from a `try' into a `catch' block, for instance.
1106 mono_debugger_call_exception_handler (gpointer addr, gpointer stack, MonoObject *exc)
1108 #ifdef MONO_DEBUGGER_SUPPORTED
1109 MonoDebuggerThreadInfo *thread_info;
1110 MonoDebuggerExceptionInfo exc_info;
1112 if (!mono_debug_using_mono_debugger ())
1115 mono_debugger_lock ();
1117 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1119 mono_debugger_unlock ();
1123 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1124 mono_debugger_unlock ();
1128 // Prevent the object from being finalized.
1129 thread_info->exception_state.last_exception = exc;
1131 exc_info.stack_pointer = stack;
1132 exc_info.exception_obj = exc;
1134 exc_info.stop_unhandled = 0;
1136 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &exc_info,
1137 (guint64) (gsize) addr);
1139 mono_debugger_unlock ();
1143 #ifdef MONO_DEBUGGER_SUPPORTED
1146 get_exception_message (MonoObject *exc)
1148 char *message = NULL;
1154 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1155 klass = exc->vtable->klass;
1157 while (klass && method == NULL) {
1158 for (i = 0; i < klass->method.count; ++i) {
1159 method = klass->methods [i];
1160 if (!strcmp ("ToString", method->name) &&
1161 mono_method_signature (method)->param_count == 0 &&
1162 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1163 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1170 klass = klass->parent;
1175 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1177 message = mono_string_to_utf8 (str);
1184 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1186 MonoDebuggerThreadInfo *thread_info;
1187 MonoDebuggerExceptionState saved_exception_state;
1191 mono_debugger_lock ();
1193 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1195 mono_debugger_unlock ();
1199 saved_exception_state = thread_info->exception_state;
1201 thread_info->exception_state.last_exception = NULL;
1202 thread_info->exception_state.stopped_on_unhandled = 0;
1203 thread_info->exception_state.stopped_on_exception = 0;
1205 thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1207 mono_debugger_unlock ();
1209 if (!strcmp (method->name, ".ctor")) {
1210 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1212 mono_runtime_invoke (method, obj, params, exc);
1214 retval = mono_runtime_invoke (method, obj, params, exc);
1216 mono_debugger_lock ();
1218 thread_info->exception_state = saved_exception_state;
1219 thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1221 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1222 thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1223 mono_thread_internal_reset_abort (thread_info->thread);
1225 mono_debugger_unlock ();
1231 mono_debugger_unlock ();
1233 if (!exc || (*exc == NULL))
1237 message = get_exception_message (*exc);
1239 *exc = (MonoObject *) mono_string_new_wrapper (message);
1247 mono_debugger_abort_runtime_invoke ()
1249 MonoInternalThread *thread = mono_thread_internal_current ();
1250 MonoDebuggerThreadInfo *thread_info;
1252 mono_debugger_lock ();
1254 thread_info = find_debugger_thread_info (thread);
1256 mono_debugger_unlock ();
1260 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE) == 0) {
1261 mono_debugger_unlock ();
1265 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1266 mono_debugger_unlock ();
1270 thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1271 ves_icall_System_Threading_Thread_Abort (thread_info->thread, NULL);
1273 mono_debugger_unlock ();