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;
54 struct _MonoDebuggerThreadInfo {
59 guint64 extended_notifications;
62 MonoDebuggerThreadInfo *next;
65 * The stack bounds are only used when reading a core file.
68 guint64 signal_stack_start;
70 guint32 signal_stack_size;
75 * The debugger doesn't access anything beyond this point.
77 MonoDebuggerExceptionState exception_state;
79 MonoJitTlsData *jit_tls;
84 gpointer stack_pointer;
85 MonoObject *exception_obj;
87 guint32 stop_unhandled;
88 } MonoDebuggerExceptionInfo;
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 (cfg->method_to_register, jit, cfg->domain);
309 mono_debug_add_vg_method (method, jit);
311 mono_debugger_check_breakpoints (method, debug_info);
313 mono_debug_free_method_jit_info (jit);
314 g_array_free (info->line_numbers, TRUE);
319 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
321 MiniDebugMethodInfo *info;
322 MonoMethodHeader *header;
325 info = (MiniDebugMethodInfo *) cfg->debug_info;
326 if (!info || !info->jit || !ins->cil_code)
329 header = mono_method_get_header (cfg->method);
332 if ((ins->cil_code < header->code) ||
333 (ins->cil_code > header->code + header->code_size))
336 offset = ins->cil_code - header->code;
337 if (!info->has_line_numbers) {
338 info->jit->prologue_end = address;
339 info->has_line_numbers = TRUE;
342 record_line_number (info, address, offset);
346 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
348 MiniDebugMethodInfo *info;
349 MonoMethodHeader *header;
352 info = (MiniDebugMethodInfo *) cfg->debug_info;
353 if (!info || !info->jit || !bb->cil_code)
356 header = mono_method_get_header (cfg->method);
359 if ((bb->cil_code < header->code) ||
360 (bb->cil_code > header->code + header->code_size))
363 offset = bb->cil_code - header->code;
364 if (!info->has_line_numbers) {
365 info->jit->prologue_end = address;
366 info->has_line_numbers = TRUE;
369 record_line_number (info, address, offset);
373 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
377 //printf ("ENCODE: %d 0x%x.\n", value, value);
380 * Same encoding as the one used in the metadata, extended to handle values
381 * greater than 0x1fffffff.
383 if ((value >= 0) && (value <= 127))
385 else if ((value >= 0) && (value <= 16383)) {
386 p [0] = 0x80 | (value >> 8);
387 p [1] = value & 0xff;
389 } else if ((value >= 0) && (value <= 0x1fffffff)) {
390 p [0] = (value >> 24) | 0xc0;
391 p [1] = (value >> 16) & 0xff;
392 p [2] = (value >> 8) & 0xff;
393 p [3] = value & 0xff;
398 p [1] = (value >> 24) & 0xff;
399 p [2] = (value >> 16) & 0xff;
400 p [3] = (value >> 8) & 0xff;
401 p [4] = value & 0xff;
409 decode_value (guint8 *ptr, guint8 **rptr)
414 if ((b & 0x80) == 0){
417 } else if ((b & 0x40) == 0){
418 len = ((b & 0x3f) << 8 | ptr [1]);
420 } else if (b != 0xff) {
421 len = ((b & 0x1f) << 24) |
428 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
434 //printf ("DECODE: %d.\n", len);
439 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
441 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
443 encode_value (var->index, p, &p);
446 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
448 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
449 encode_value (var->offset, p, &p);
451 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
454 g_assert_not_reached ();
460 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
462 MonoDebugMethodJitInfo *jit;
463 guint32 size, prev_offset, prev_native_offset;
467 /* Can't use cfg->debug_info as it is freed by close_method () */
468 jit = mono_debug_find_method (cfg->method, mono_domain_get ());
474 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
475 p = buf = g_malloc (size);
476 encode_value (jit->epilogue_begin, p, &p);
477 encode_value (jit->prologue_end, p, &p);
478 encode_value (jit->code_size, p, &p);
480 for (i = 0; i < jit->num_params; ++i)
481 serialize_variable (&jit->params [i], p, &p);
483 if (mono_method_signature (cfg->method)->hasthis)
484 serialize_variable (jit->this_var, p, &p);
486 for (i = 0; i < jit->num_locals; i++)
487 serialize_variable (&jit->locals [i], p, &p);
489 encode_value (jit->num_line_numbers, p, &p);
492 prev_native_offset = 0;
493 for (i = 0; i < jit->num_line_numbers; ++i) {
494 /* Sometimes, the offset values are not in increasing order */
495 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
496 encode_value (lne->il_offset - prev_offset, p, &p);
497 encode_value (lne->native_offset - prev_native_offset, p, &p);
498 prev_offset = lne->il_offset;
499 prev_native_offset = lne->native_offset;
502 g_assert (p - buf < size);
509 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
513 var->index = decode_value (p, &p);
515 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
518 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
520 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
521 var->offset = decode_value (p, &p);
523 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
526 g_assert_not_reached ();
531 static MonoDebugMethodJitInfo *
532 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
534 MonoMethodHeader *header;
535 gint32 offset, native_offset, prev_offset, prev_native_offset;
536 MonoDebugMethodJitInfo *jit;
540 header = mono_method_get_header (method);
543 jit = g_new0 (MonoDebugMethodJitInfo, 1);
544 jit->code_start = code_start;
545 jit->num_locals = header->num_locals;
546 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
547 jit->num_params = mono_method_signature (method)->param_count;
548 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
551 jit->epilogue_begin = decode_value (p, &p);
552 jit->prologue_end = decode_value (p, &p);
553 jit->code_size = decode_value (p, &p);
555 for (i = 0; i < jit->num_params; ++i)
556 deserialize_variable (&jit->params [i], p, &p);
558 if (mono_method_signature (method)->hasthis) {
559 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
560 deserialize_variable (jit->this_var, p, &p);
563 for (i = 0; i < jit->num_locals; i++)
564 deserialize_variable (&jit->locals [i], p, &p);
566 jit->num_line_numbers = decode_value (p, &p);
567 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
570 prev_native_offset = 0;
571 for (i = 0; i < jit->num_line_numbers; ++i) {
572 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
574 offset = prev_offset + decode_value (p, &p);
575 native_offset = prev_native_offset + decode_value (p, &p);
577 lne->native_offset = native_offset;
578 lne->il_offset = offset;
580 prev_offset = offset;
581 prev_native_offset = native_offset;
588 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
589 guint8 *debug_info, guint32 debug_info_len)
591 MonoDebugMethodJitInfo *jit;
593 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
596 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
597 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
598 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
599 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
600 (method->wrapper_type != MONO_WRAPPER_NONE))
603 if (debug_info_len == 0)
606 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
608 mono_debug_add_method (method, jit, domain);
610 mono_debug_add_vg_method (method, jit);
612 mono_debug_free_method_jit_info (jit);
616 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
618 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
621 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
625 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
627 switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
628 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
629 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
631 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
632 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);
634 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
636 g_assert_not_reached ();
641 * mono_debug_print_locals:
643 * Prints to stdout the information about the local variables in
644 * a method (if @only_arguments is false) or about the arguments.
645 * The information includes the storage info (where the variable
646 * lives, in a register or in memory).
647 * The method is found by looking up what method has been emitted at
648 * the instruction address @ip.
649 * This is for use inside a debugger.
652 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
654 MonoDomain *domain = mono_domain_get ();
655 MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
656 MonoDebugMethodJitInfo *jit;
662 jit = mono_debug_find_method (mono_jit_info_get_method (ji), domain);
666 if (only_arguments) {
668 names = g_new (char *, jit->num_params);
669 mono_method_get_param_names (mono_jit_info_get_method (ji), (const char **) names);
671 print_var_info (jit->this_var, 0, "this", "Arg");
672 for (i = 0; i < jit->num_params; ++i) {
673 print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
677 for (i = 0; i < jit->num_locals; ++i) {
678 print_var_info (&jit->locals [i], i, "", "Local");
681 mono_debug_free_method_jit_info (jit);
685 * The old Debugger breakpoint interface.
687 * This interface is used to insert breakpoints on methods which are not yet JITed.
688 * The debugging code keeps a list of all such breakpoints and automatically inserts the
689 * breakpoint when the method is JITed.
692 static GPtrArray *breakpoints = NULL;
695 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
697 static int last_breakpoint_id = 0;
698 MiniDebugBreakpointInfo *info;
700 info = g_new0 (MiniDebugBreakpointInfo, 1);
702 info->index = ++last_breakpoint_id;
705 breakpoints = g_ptr_array_new ();
707 g_ptr_array_add (breakpoints, info);
713 mono_debugger_remove_breakpoint (int breakpoint_id)
720 for (i = 0; i < breakpoints->len; i++) {
721 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
723 if (info->index != breakpoint_id)
726 mono_method_desc_free (info->desc);
727 g_ptr_array_remove (breakpoints, info);
736 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
738 MonoMethodDesc *desc;
740 desc = mono_method_desc_new (method_name, include_namespace);
744 return mono_debugger_insert_breakpoint_full (desc);
748 mono_debugger_method_has_breakpoint (MonoMethod *method)
752 if (!breakpoints || ((method->wrapper_type != MONO_WRAPPER_NONE) &&
753 (method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)))
756 for (i = 0; i < breakpoints->len; i++) {
757 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
759 if (!mono_method_desc_full_match (info->desc, method))
769 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
771 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
775 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls, gpointer func)
777 #ifdef MONO_DEBUGGER_SUPPORTED
779 guint8 *staddr = NULL;
780 MonoDebuggerThreadInfo *info;
782 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
785 mono_debugger_lock ();
787 mono_thread_get_stack_bounds (&staddr, &stsize);
789 info = g_new0 (MonoDebuggerThreadInfo, 1);
791 info->thread = thread;
792 info->stack_start = (guint64) (gsize) staddr;
793 info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
794 info->stack_size = stsize;
795 info->signal_stack_size = jit_tls->signal_stack_size;
796 info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
797 info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
798 info->jit_tls = jit_tls;
801 info->thread_flags = MONO_DEBUGGER_THREAD_FLAGS_INTERNAL;
802 if (thread->internal_thread->threadpool_thread)
803 info->thread_flags |= MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL;
805 info->next = mono_debugger_thread_table;
806 mono_debugger_thread_table = info;
808 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
809 tid, (guint64) (gsize) info);
811 mono_debugger_unlock ();
812 #endif /* MONO_DEBUGGER_SUPPORTED */
816 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
818 #ifdef MONO_DEBUGGER_SUPPORTED
819 MonoDebuggerThreadInfo **ptr;
821 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
824 mono_debugger_lock ();
826 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
827 MonoDebuggerThreadInfo *info = *ptr;
829 if (info->jit_tls != jit_tls)
832 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
833 info->tid, (guint64) (gsize) info);
840 mono_debugger_unlock ();
845 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
847 #ifdef MONO_DEBUGGER_SUPPORTED
848 MonoDebuggerThreadInfo **ptr;
849 MonoThread *thread = mono_thread_current ();
851 if (!mono_debug_using_mono_debugger ())
854 mono_debugger_lock ();
856 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
857 MonoDebuggerThreadInfo *info = *ptr;
859 if (info->thread != thread)
862 if ((info->extended_notifications & (int) event) == 0)
865 mono_debugger_event (event, data, arg);
868 mono_debugger_unlock ();
873 mono_debugger_trampoline_compiled (const guint8 *trampoline, MonoMethod *method, const guint8 *code)
875 #ifdef MONO_DEBUGGER_SUPPORTED
877 const guint8 * trampoline;
880 } info = { trampoline, method, code };
882 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_OLD_TRAMPOLINE,
883 (guint64) (gsize) method, (guint64) (gsize) code);
884 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
885 (guint64) (gsize) &info, 0);
889 #if MONO_DEBUGGER_SUPPORTED
890 static MonoDebuggerThreadInfo *
891 find_debugger_thread_info (MonoThread *thread)
893 MonoDebuggerThreadInfo **ptr;
895 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
896 MonoDebuggerThreadInfo *info = *ptr;
898 if (info->thread == thread)
906 MonoDebuggerExceptionAction
907 _mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
909 #ifdef MONO_DEBUGGER_SUPPORTED
910 MonoDebuggerExceptionInfo exc_info;
911 MonoDebuggerThreadInfo *thread_info;
913 if (!mono_debug_using_mono_debugger ())
914 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
916 mono_debugger_lock ();
918 thread_info = find_debugger_thread_info (mono_thread_current ());
920 mono_debugger_unlock ();
921 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
924 if (thread_info->exception_state.stopped_on_exception ||
925 thread_info->exception_state.stopped_on_unhandled) {
926 thread_info->exception_state.stopped_on_exception = 0;
927 mono_debugger_unlock ();
928 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
931 /* Protect the exception object from being garbage collected. */
933 thread_info->exception_state.stopped_on_unhandled = 0;
934 thread_info->exception_state.stopped_on_exception = 1;
935 thread_info->exception_state.last_exception = exc;
938 * Backwards compatibility:
940 * Older debugger versions only know `exc_info.stop' and older runtime versions check
941 * `exc_info.stop != 0'.
943 * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
944 * `stop_unhandled' field.
947 exc_info.stack_pointer = stack;
948 exc_info.exception_obj = exc;
950 exc_info.stop_unhandled = 0;
952 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &exc_info,
953 (guint64) (gsize) addr);
955 if (!exc_info.stop) {
956 thread_info->exception_state.stopped_on_exception = 0;
957 thread_info->exception_state.last_exception = NULL;
960 mono_debugger_unlock ();
963 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP;
964 else if (exc_info.stop_unhandled)
965 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED;
968 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
972 _mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
974 #ifdef MONO_DEBUGGER_SUPPORTED
975 MonoDebuggerThreadInfo *thread_info;
977 if (!mono_debug_using_mono_debugger ())
981 const gchar *name = mono_class_get_name (mono_object_get_class (exc));
982 if (!strcmp (name, "ThreadAbortException"))
986 mono_debugger_lock ();
988 thread_info = find_debugger_thread_info (mono_thread_current ());
990 mono_debugger_unlock ();
994 if (thread_info->exception_state.stopped_on_unhandled) {
995 thread_info->exception_state.stopped_on_unhandled = 0;
996 mono_debugger_unlock ();
1000 thread_info->exception_state.stopped_on_unhandled = 1;
1001 thread_info->exception_state.last_exception = exc;
1003 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
1004 (guint64) (gsize) exc, (guint64) (gsize) addr);
1013 * mono_debugger_call_exception_handler:
1015 * Called from mono_handle_exception_internal() to tell the debugger that we're about
1016 * to invoke an exception handler.
1018 * The debugger may choose to set a breakpoint at @addr. This is used if the user is
1019 * single-stepping from a `try' into a `catch' block, for instance.
1023 mono_debugger_call_exception_handler (gpointer addr, gpointer stack, MonoObject *exc)
1025 #ifdef MONO_DEBUGGER_SUPPORTED
1026 MonoDebuggerThreadInfo *thread_info;
1027 MonoDebuggerExceptionInfo exc_info;
1029 if (!mono_debug_using_mono_debugger ())
1032 mono_debugger_lock ();
1034 thread_info = find_debugger_thread_info (mono_thread_current ());
1036 mono_debugger_unlock ();
1040 // Prevent the object from being finalized.
1041 thread_info->exception_state.last_exception = exc;
1043 exc_info.stack_pointer = stack;
1044 exc_info.exception_obj = exc;
1046 exc_info.stop_unhandled = 0;
1048 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &exc_info,
1049 (guint64) (gsize) addr);
1051 mono_debugger_unlock ();
1055 #ifdef MONO_DEBUGGER_SUPPORTED
1058 get_exception_message (MonoObject *exc)
1060 char *message = NULL;
1066 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1067 klass = exc->vtable->klass;
1069 while (klass && method == NULL) {
1070 for (i = 0; i < klass->method.count; ++i) {
1071 method = klass->methods [i];
1072 if (!strcmp ("ToString", method->name) &&
1073 mono_method_signature (method)->param_count == 0 &&
1074 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1075 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1082 klass = klass->parent;
1087 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1089 message = mono_string_to_utf8 (str);
1096 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1098 MonoDebuggerThreadInfo *thread_info;
1099 MonoDebuggerExceptionState saved_exception_state;
1103 mono_debugger_lock ();
1105 thread_info = find_debugger_thread_info (mono_thread_current ());
1107 mono_debugger_unlock ();
1111 saved_exception_state = thread_info->exception_state;
1113 thread_info->exception_state.last_exception = NULL;
1114 thread_info->exception_state.stopped_on_unhandled = 0;
1115 thread_info->exception_state.stopped_on_exception = 0;
1117 mono_debugger_unlock ();
1119 if (!strcmp (method->name, ".ctor")) {
1120 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1122 mono_runtime_invoke (method, obj, params, exc);
1124 retval = mono_runtime_invoke (method, obj, params, exc);
1126 mono_debugger_lock ();
1128 thread_info = find_debugger_thread_info (mono_thread_current ());
1130 thread_info->exception_state = saved_exception_state;
1132 mono_debugger_unlock ();
1134 if (!exc || (*exc == NULL))
1138 message = get_exception_message (*exc);
1140 *exc = (MonoObject *) mono_string_new_wrapper (message);