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;
45 MonoObject *last_exception;
46 guint32 stopped_on_exception : 1;
47 guint32 stopped_on_unhandled : 1;
48 } MonoDebuggerExceptionState;
50 struct _MonoDebuggerThreadInfo {
55 guint64 extended_notifications;
58 MonoDebuggerThreadInfo *next;
61 * The stack bounds are only used when reading a core file.
64 guint64 signal_stack_start;
66 guint32 signal_stack_size;
68 MonoDebuggerExceptionState exception_state;
71 * The debugger doesn't access anything beyond this point.
73 MonoJitTlsData *jit_tls;
78 gpointer stack_pointer;
79 MonoObject *exception_obj;
81 guint32 stop_unhandled;
82 } MonoDebuggerExceptionInfo;
85 MONO_DEBUGGER_EXCEPTION_ACTION_NONE = 0,
86 MONO_DEBUGGER_EXCEPTION_ACTION_STOP = 1,
87 MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED = 2
88 } MonoDebuggerExceptionAction;
90 MonoDebuggerThreadInfo *mono_debugger_thread_table = NULL;
93 record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
95 MonoDebugLineNumberEntry lne;
97 lne.native_offset = address;
98 lne.il_offset = offset;
100 g_array_append_val (info->line_numbers, lne);
105 mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block, guint32 breakpoint_id)
107 MiniDebugMethodInfo *info;
109 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
112 info = g_new0 (MiniDebugMethodInfo, 1);
113 info->breakpoint_id = breakpoint_id;
115 cfg->debug_info = info;
119 mono_debug_open_method (MonoCompile *cfg)
121 MiniDebugMethodInfo *info;
122 MonoDebugMethodJitInfo *jit;
123 MonoMethodHeader *header;
125 info = (MiniDebugMethodInfo *) cfg->debug_info;
129 mono_class_init (cfg->method->klass);
131 header = mono_method_get_header (cfg->method);
134 info->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
135 info->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
136 jit->num_locals = header->num_locals;
137 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
141 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
143 var->type = inst->inst_vtype;
145 if (inst->opcode == OP_REGVAR)
146 var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
147 else if (inst->flags & MONO_INST_IS_DEAD)
148 var->index = MONO_DEBUG_VAR_ADDRESS_MODE_DEAD;
150 /* the debug interface needs fixing to allow 0(%base) address */
151 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET;
152 var->offset = inst->inst_offset;
157 * mono_debug_add_vg_method:
159 * Register symbol information for the method with valgrind
162 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
164 #ifdef VALGRIND_ADD_LINE_INFO
165 MonoMethodHeader *header;
166 MonoDebugMethodInfo *minfo;
168 char *filename = NULL;
169 guint32 address, line_number;
170 const char *full_name;
174 if (!RUNNING_ON_VALGRIND)
177 header = mono_method_get_header (method);
179 full_name = mono_method_full_name (method, TRUE);
181 addresses = g_new0 (guint32, header->code_size + 1);
182 lines = g_new0 (guint32, header->code_size + 1);
185 * Very simple code to convert the addr->offset mappings that mono has
186 * into [addr-addr] ->line number mappings.
189 minfo = mono_debug_lookup_method (method);
191 /* Create offset->line number mapping */
192 for (i = 0; i < header->code_size; ++i) {
193 MonoDebugSourceLocation *location;
195 location = mono_debug_symfile_lookup_location (minfo, i);
199 lines [i] = location.row;
201 filename = location.source_file;
203 mono_debug_free_source_location (location);
207 /* Create address->offset mapping */
208 for (i = 0; i < jit->num_line_numbers; ++i) {
209 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
211 g_assert (lne->offset <= header->code_size);
213 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
214 addresses [lne->offset] = lne->address;
216 /* Fill out missing addresses */
218 for (i = 0; i < header->code_size; ++i) {
219 if (addresses [i] == 0)
220 addresses [i] = address;
222 address = addresses [i];
228 while (i < header->code_size) {
229 if (lines [i] == line_number)
232 if (line_number > 0) {
233 //g_assert (addresses [i] - 1 >= address);
235 if (addresses [i] - 1 >= address) {
236 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
237 //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
240 address = addresses [i];
241 line_number = lines [i];
245 if (line_number > 0) {
246 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
247 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
250 VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
254 #endif /* VALGRIND_ADD_LINE_INFO */
258 mono_debug_close_method (MonoCompile *cfg)
260 MiniDebugMethodInfo *info;
261 MonoDebugMethodJitInfo *jit;
262 MonoMethodHeader *header;
263 MonoMethodSignature *sig;
264 MonoDebugMethodAddress *debug_info;
268 info = (MiniDebugMethodInfo *) cfg->debug_info;
269 if (!info || !info->jit) {
275 method = cfg->method;
276 header = mono_method_get_header (method);
277 sig = mono_method_signature (method);
280 jit->code_start = cfg->native_code;
281 jit->epilogue_begin = cfg->epilog_begin;
282 jit->code_size = cfg->code_len;
284 if (jit->epilogue_begin)
285 record_line_number (info, jit->epilogue_begin, header->code_size);
287 jit->num_params = sig->param_count;
288 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
290 for (i = 0; i < jit->num_locals; i++)
291 write_variable (cfg->locals [i], &jit->locals [i]);
294 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
295 write_variable (cfg->args [0], jit->this_var);
298 for (i = 0; i < jit->num_params; i++)
299 write_variable (cfg->args [i + sig->hasthis], &jit->params [i]);
301 jit->num_line_numbers = info->line_numbers->len;
302 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
304 for (i = 0; i < jit->num_line_numbers; i++)
305 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
307 debug_info = mono_debug_add_method (method, jit, cfg->domain);
309 mono_debug_add_vg_method (method, jit);
311 if (info->breakpoint_id)
312 mono_debugger_breakpoint_callback (method, info->breakpoint_id);
314 mono_debugger_check_breakpoints (method, debug_info);
316 mono_debug_free_method_jit_info (jit);
317 g_array_free (info->line_numbers, TRUE);
322 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
324 MiniDebugMethodInfo *info;
325 MonoMethodHeader *header;
328 info = (MiniDebugMethodInfo *) cfg->debug_info;
329 if (!info || !info->jit || !ins->cil_code)
332 header = mono_method_get_header (cfg->method);
335 if ((ins->cil_code < header->code) ||
336 (ins->cil_code > header->code + header->code_size))
339 offset = ins->cil_code - header->code;
340 if (!info->has_line_numbers) {
341 info->jit->prologue_end = address;
342 info->has_line_numbers = TRUE;
345 record_line_number (info, address, offset);
349 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
351 MiniDebugMethodInfo *info;
352 MonoMethodHeader *header;
355 info = (MiniDebugMethodInfo *) cfg->debug_info;
356 if (!info || !info->jit || !bb->cil_code)
359 header = mono_method_get_header (cfg->method);
362 if ((bb->cil_code < header->code) ||
363 (bb->cil_code > header->code + header->code_size))
366 offset = bb->cil_code - header->code;
367 if (!info->has_line_numbers) {
368 info->jit->prologue_end = address;
369 info->has_line_numbers = TRUE;
372 record_line_number (info, address, offset);
376 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
380 //printf ("ENCODE: %d 0x%x.\n", value, value);
383 * Same encoding as the one used in the metadata, extended to handle values
384 * greater than 0x1fffffff.
386 if ((value >= 0) && (value <= 127))
388 else if ((value >= 0) && (value <= 16383)) {
389 p [0] = 0x80 | (value >> 8);
390 p [1] = value & 0xff;
392 } else if ((value >= 0) && (value <= 0x1fffffff)) {
393 p [0] = (value >> 24) | 0xc0;
394 p [1] = (value >> 16) & 0xff;
395 p [2] = (value >> 8) & 0xff;
396 p [3] = value & 0xff;
401 p [1] = (value >> 24) & 0xff;
402 p [2] = (value >> 16) & 0xff;
403 p [3] = (value >> 8) & 0xff;
404 p [4] = value & 0xff;
412 decode_value (guint8 *ptr, guint8 **rptr)
417 if ((b & 0x80) == 0){
420 } else if ((b & 0x40) == 0){
421 len = ((b & 0x3f) << 8 | ptr [1]);
423 } else if (b != 0xff) {
424 len = ((b & 0x1f) << 24) |
431 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
437 //printf ("DECODE: %d.\n", len);
442 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
444 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
446 encode_value (var->index, p, &p);
449 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
451 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
452 encode_value (var->offset, p, &p);
454 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
457 g_assert_not_reached ();
463 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
465 MonoDebugMethodJitInfo *jit;
466 guint32 size, prev_offset, prev_native_offset;
470 /* Can't use cfg->debug_info as it is freed by close_method () */
471 jit = mono_debug_find_method (cfg->method, mono_domain_get ());
477 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
478 p = buf = g_malloc (size);
479 encode_value (jit->epilogue_begin, p, &p);
480 encode_value (jit->prologue_end, p, &p);
481 encode_value (jit->code_size, p, &p);
483 for (i = 0; i < jit->num_params; ++i)
484 serialize_variable (&jit->params [i], p, &p);
486 if (mono_method_signature (cfg->method)->hasthis)
487 serialize_variable (jit->this_var, p, &p);
489 for (i = 0; i < jit->num_locals; i++)
490 serialize_variable (&jit->locals [i], p, &p);
492 encode_value (jit->num_line_numbers, p, &p);
495 prev_native_offset = 0;
496 for (i = 0; i < jit->num_line_numbers; ++i) {
497 /* Sometimes, the offset values are not in increasing order */
498 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
499 encode_value (lne->il_offset - prev_offset, p, &p);
500 encode_value (lne->native_offset - prev_native_offset, p, &p);
501 prev_offset = lne->il_offset;
502 prev_native_offset = lne->native_offset;
505 g_assert (p - buf < size);
512 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
516 var->index = decode_value (p, &p);
518 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
521 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
523 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
524 var->offset = decode_value (p, &p);
526 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
529 g_assert_not_reached ();
534 static MonoDebugMethodJitInfo *
535 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
537 MonoMethodHeader *header;
538 gint32 offset, native_offset, prev_offset, prev_native_offset;
539 MonoDebugMethodJitInfo *jit;
543 header = mono_method_get_header (method);
546 jit = g_new0 (MonoDebugMethodJitInfo, 1);
547 jit->code_start = code_start;
548 jit->num_locals = header->num_locals;
549 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
550 jit->num_params = mono_method_signature (method)->param_count;
551 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
554 jit->epilogue_begin = decode_value (p, &p);
555 jit->prologue_end = decode_value (p, &p);
556 jit->code_size = decode_value (p, &p);
558 for (i = 0; i < jit->num_params; ++i)
559 deserialize_variable (&jit->params [i], p, &p);
561 if (mono_method_signature (method)->hasthis) {
562 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
563 deserialize_variable (jit->this_var, p, &p);
566 for (i = 0; i < jit->num_locals; i++)
567 deserialize_variable (&jit->locals [i], p, &p);
569 jit->num_line_numbers = decode_value (p, &p);
570 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
573 prev_native_offset = 0;
574 for (i = 0; i < jit->num_line_numbers; ++i) {
575 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
577 offset = prev_offset + decode_value (p, &p);
578 native_offset = prev_native_offset + decode_value (p, &p);
580 lne->native_offset = native_offset;
581 lne->il_offset = offset;
583 prev_offset = offset;
584 prev_native_offset = native_offset;
591 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
592 guint8 *debug_info, guint32 debug_info_len)
594 MonoDebugMethodJitInfo *jit;
596 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
599 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
600 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
601 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
602 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
603 (method->wrapper_type != MONO_WRAPPER_NONE))
606 if (debug_info_len == 0)
609 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
611 mono_debug_add_method (method, jit, domain);
613 mono_debug_add_vg_method (method, jit);
615 mono_debug_free_method_jit_info (jit);
619 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
621 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
624 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
628 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
630 switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
631 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
632 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
634 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
635 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);
637 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
639 g_assert_not_reached ();
644 * mono_debug_print_locals:
646 * Prints to stdout the information about the local variables in
647 * a method (if @only_arguments is false) or about the arguments.
648 * The information includes the storage info (where the variable
649 * lives, in a register or in memory).
650 * The method is found by looking up what method has been emitted at
651 * the instruction address @ip.
652 * This is for use inside a debugger.
655 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
657 MonoDomain *domain = mono_domain_get ();
658 MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
659 MonoDebugMethodJitInfo *jit;
665 jit = mono_debug_find_method (mono_jit_info_get_method (ji), domain);
669 if (only_arguments) {
671 names = g_new (char *, jit->num_params);
672 mono_method_get_param_names (mono_jit_info_get_method (ji), (const char **) names);
674 print_var_info (jit->this_var, 0, "this", "Arg");
675 for (i = 0; i < jit->num_params; ++i) {
676 print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
680 for (i = 0; i < jit->num_locals; ++i) {
681 print_var_info (&jit->locals [i], i, "", "Local");
684 mono_debug_free_method_jit_info (jit);
688 * The old Debugger breakpoint interface.
690 * This interface is used to insert breakpoints on methods which are not yet JITed.
691 * The debugging code keeps a list of all such breakpoints and automatically inserts the
692 * breakpoint when the method is JITed.
695 static GPtrArray *breakpoints = NULL;
698 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
700 static int last_breakpoint_id = 0;
701 MiniDebugBreakpointInfo *info;
703 info = g_new0 (MiniDebugBreakpointInfo, 1);
705 info->index = ++last_breakpoint_id;
708 breakpoints = g_ptr_array_new ();
710 g_ptr_array_add (breakpoints, info);
716 mono_debugger_remove_breakpoint (int breakpoint_id)
723 for (i = 0; i < breakpoints->len; i++) {
724 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
726 if (info->index != breakpoint_id)
729 mono_method_desc_free (info->desc);
730 g_ptr_array_remove (breakpoints, info);
739 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
741 MonoMethodDesc *desc;
743 desc = mono_method_desc_new (method_name, include_namespace);
747 return mono_debugger_insert_breakpoint_full (desc);
751 mono_debugger_method_has_breakpoint (MonoMethod *method)
755 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
758 for (i = 0; i < breakpoints->len; i++) {
759 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
761 if (!mono_method_desc_full_match (info->desc, method))
771 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
773 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
777 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls)
779 #ifdef MONO_DEBUGGER_SUPPORTED
781 guint8 *staddr = NULL;
782 MonoDebuggerThreadInfo *info;
784 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
787 mono_debugger_lock ();
789 mono_thread_get_stack_bounds (&staddr, &stsize);
791 info = g_new0 (MonoDebuggerThreadInfo, 1);
793 info->thread = thread;
794 info->stack_start = (guint64) (gsize) staddr;
795 info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
796 info->stack_size = stsize;
797 info->signal_stack_size = jit_tls->signal_stack_size;
798 info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
799 info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
800 info->jit_tls = jit_tls;
802 info->next = mono_debugger_thread_table;
803 mono_debugger_thread_table = info;
805 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
806 tid, (guint64) (gsize) info);
808 mono_debugger_unlock ();
809 #endif /* MONO_DEBUGGER_SUPPORTED */
813 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
815 #ifdef MONO_DEBUGGER_SUPPORTED
816 MonoDebuggerThreadInfo **ptr;
818 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
821 mono_debugger_lock ();
823 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
824 MonoDebuggerThreadInfo *info = *ptr;
826 if (info->jit_tls != jit_tls)
829 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
830 info->tid, (guint64) (gsize) info);
837 mono_debugger_unlock ();
842 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
844 #ifdef MONO_DEBUGGER_SUPPORTED
845 MonoDebuggerThreadInfo **ptr;
846 MonoThread *thread = mono_thread_current ();
848 if (!mono_debug_using_mono_debugger ())
851 mono_debugger_lock ();
853 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
854 MonoDebuggerThreadInfo *info = *ptr;
856 if (info->thread != thread)
859 if ((info->extended_notifications & (int) event) == 0)
862 mono_debugger_event (event, data, arg);
865 mono_debugger_unlock ();
870 mono_debugger_trampoline_compiled (MonoMethod *method, const guint8 *code)
872 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
873 (guint64) (gsize) method, (guint64) (gsize) code);
876 #if MONO_DEBUGGER_SUPPORTED
877 static MonoDebuggerThreadInfo *
878 find_debugger_thread_info (MonoThread *thread)
880 MonoDebuggerThreadInfo **ptr;
882 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
883 MonoDebuggerThreadInfo *info = *ptr;
885 if (info->thread == thread)
893 static MonoDebuggerExceptionAction
894 _mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
896 #ifdef MONO_DEBUGGER_SUPPORTED
897 MonoDebuggerExceptionInfo exc_info;
898 MonoDebuggerThreadInfo *thread_info;
900 if (!mono_debug_using_mono_debugger ())
901 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
903 mono_debugger_lock ();
905 thread_info = find_debugger_thread_info (mono_thread_current ());
907 mono_debugger_unlock ();
908 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
911 if (thread_info->exception_state.stopped_on_exception ||
912 thread_info->exception_state.stopped_on_unhandled) {
913 thread_info->exception_state.stopped_on_exception = 0;
914 mono_debugger_unlock ();
915 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
918 /* Protect the exception object from being garbage collected. */
920 thread_info->exception_state.stopped_on_unhandled = 0;
921 thread_info->exception_state.stopped_on_exception = 1;
922 thread_info->exception_state.last_exception = exc;
925 * Backwards compatibility:
927 * Older debugger versions only know `exc_info.stop' and older runtime versions check
928 * `exc_info.stop != 0'.
930 * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
931 * `stop_unhandled' field.
934 exc_info.stack_pointer = stack;
935 exc_info.exception_obj = exc;
937 exc_info.stop_unhandled = 0;
939 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &exc_info,
940 (guint64) (gsize) addr);
942 if (!exc_info.stop) {
943 thread_info->exception_state.stopped_on_exception = 0;
944 thread_info->exception_state.last_exception = NULL;
947 mono_debugger_unlock ();
950 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP;
951 else if (exc_info.stop_unhandled)
952 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED;
955 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
959 _mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
961 #ifdef MONO_DEBUGGER_SUPPORTED
962 MonoDebuggerThreadInfo *thread_info;
964 if (!mono_debug_using_mono_debugger ())
968 const gchar *name = mono_class_get_name (mono_object_get_class (exc));
969 if (!strcmp (name, "ThreadAbortException"))
973 mono_debugger_lock ();
975 thread_info = find_debugger_thread_info (mono_thread_current ());
977 mono_debugger_unlock ();
981 if (thread_info->exception_state.stopped_on_unhandled) {
982 thread_info->exception_state.stopped_on_unhandled = 0;
983 mono_debugger_unlock ();
987 thread_info->exception_state.stopped_on_unhandled = 1;
988 thread_info->exception_state.last_exception = exc;
990 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
991 (guint64) (gsize) exc, (guint64) (gsize) addr);
1000 * mono_debugger_call_exception_handler:
1002 * Called from mono_handle_exception_internal() to tell the debugger that we're about
1003 * to invoke an exception handler.
1005 * The debugger may choose to set a breakpoint at @addr. This is used if the user is
1006 * single-stepping from a `try' into a `catch' block, for instance.
1010 mono_debugger_call_exception_handler (gpointer addr, gpointer stack, MonoObject *exc)
1012 #ifdef MONO_DEBUGGER_SUPPORTED
1013 MonoDebuggerThreadInfo *thread_info;
1014 MonoDebuggerExceptionInfo exc_info;
1016 if (!mono_debug_using_mono_debugger ())
1019 mono_debugger_lock ();
1021 thread_info = find_debugger_thread_info (mono_thread_current ());
1023 mono_debugger_unlock ();
1027 // Prevent the object from being finalized.
1028 thread_info->exception_state.last_exception = exc;
1030 exc_info.stack_pointer = stack;
1031 exc_info.exception_obj = exc;
1033 exc_info.stop_unhandled = 0;
1035 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &exc_info,
1036 (guint64) (gsize) addr);
1038 mono_debugger_unlock ();
1043 * mono_debugger_handle_exception:
1045 * Notify the debugger about exceptions. Returns TRUE if the debugger wants us to stop
1046 * at the exception and FALSE to resume with the normal exception handling.
1048 * The arch code is responsible to setup @ctx in a way that MONO_CONTEXT_GET_IP () and
1049 * MONO_CONTEXT_GET_SP () point to the throw instruction; ie. before executing the
1050 * `callq throw' instruction.
1053 mono_debugger_handle_exception (MonoContext *ctx, MonoObject *obj)
1055 MonoDebuggerExceptionAction action;
1057 if (!mono_debug_using_mono_debugger ())
1061 MonoException *ex = mono_get_exception_null_reference ();
1062 MONO_OBJECT_SETREF (ex, message, mono_string_new (mono_domain_get (), "Object reference not set to an instance of an object"));
1063 obj = (MonoObject *)ex;
1066 action = _mono_debugger_throw_exception (MONO_CONTEXT_GET_IP (ctx), MONO_CONTEXT_GET_SP (ctx), obj);
1068 if (action == MONO_DEBUGGER_EXCEPTION_ACTION_STOP) {
1070 * The debugger wants us to stop on the `throw' instruction.
1071 * By the time we get here, it already inserted a breakpoint there.
1074 } else if (action == MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED) {
1075 MonoContext ctx_cp = *ctx;
1078 * The debugger wants us to stop only if this exception is user-unhandled.
1081 if (!mono_handle_exception (&ctx_cp, obj, MONO_CONTEXT_GET_IP (ctx), TRUE)) {
1083 * The exception is user-unhandled - tell the debugger to stop.
1085 return _mono_debugger_unhandled_exception (MONO_CONTEXT_GET_IP (ctx), MONO_CONTEXT_GET_SP (ctx), obj);
1089 * The exception is catched somewhere - resume with the normal exception handling and don't
1090 * stop in the debugger.
1097 #ifdef MONO_DEBUGGER_SUPPORTED
1100 get_exception_message (MonoObject *exc)
1102 char *message = NULL;
1108 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1109 klass = exc->vtable->klass;
1111 while (klass && method == NULL) {
1112 for (i = 0; i < klass->method.count; ++i) {
1113 method = klass->methods [i];
1114 if (!strcmp ("ToString", method->name) &&
1115 mono_method_signature (method)->param_count == 0 &&
1116 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1117 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1124 klass = klass->parent;
1129 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1131 message = mono_string_to_utf8 (str);
1138 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1140 MonoDebuggerThreadInfo *thread_info;
1141 MonoDebuggerExceptionState saved_exception_state;
1145 mono_debugger_lock ();
1147 thread_info = find_debugger_thread_info (mono_thread_current ());
1149 mono_debugger_unlock ();
1153 saved_exception_state = thread_info->exception_state;
1155 thread_info->exception_state.last_exception = NULL;
1156 thread_info->exception_state.stopped_on_unhandled = 0;
1157 thread_info->exception_state.stopped_on_exception = 0;
1159 mono_debugger_unlock ();
1161 if (!strcmp (method->name, ".ctor")) {
1162 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1164 mono_runtime_invoke (method, obj, params, exc);
1166 retval = mono_runtime_invoke (method, obj, params, exc);
1168 mono_debugger_lock ();
1170 thread_info = find_debugger_thread_info (mono_thread_current ());
1172 thread_info->exception_state = saved_exception_state;
1174 mono_debugger_unlock ();
1176 if (!exc || (*exc == NULL))
1180 message = get_exception_message (*exc);
1182 *exc = (MonoObject *) mono_string_new_wrapper (message);