Merge pull request #444 from knocte/xbuild_improvements
[mono-project.git] / mono / mini / debug-mini.c
blobf82bf6a9d5f8749cf84513c7d8312a7a4ffd8c25
1 /*
2 * debug-mini.c: Mini-specific debugging stuff.
4 * Author:
5 * Martin Baulig (martin@ximian.com)
7 * (C) 2003 Ximian, Inc.
8 */
10 #include "mini.h"
11 #include "jit.h"
12 #include "config.h"
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>
27 #endif
29 typedef struct {
30 guint32 index;
31 MonoMethodDesc *desc;
32 } MiniDebugBreakpointInfo;
34 typedef struct
36 MonoDebugMethodJitInfo *jit;
37 GArray *line_numbers;
38 guint32 has_line_numbers;
39 guint32 breakpoint_id;
40 } MiniDebugMethodInfo;
42 typedef struct {
43 MonoObject *last_exception;
44 guint32 stopped_on_exception : 1;
45 guint32 stopped_on_unhandled : 1;
46 } MonoDebuggerExceptionState;
48 typedef enum {
49 MONO_DEBUGGER_THREAD_FLAGS_NONE = 0,
50 MONO_DEBUGGER_THREAD_FLAGS_INTERNAL = 1,
51 MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL = 2
52 } MonoDebuggerThreadFlags;
54 typedef enum {
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 {
61 guint64 tid;
62 guint64 lmf_addr;
63 guint64 end_stack;
65 guint64 extended_notifications;
67 /* Next pointer. */
68 MonoDebuggerThreadInfo *next;
71 * The stack bounds are only used when reading a core file.
73 guint64 stack_start;
74 guint64 signal_stack_start;
75 guint32 stack_size;
76 guint32 signal_stack_size;
78 guint32 thread_flags;
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;
91 typedef struct {
92 gpointer stack_pointer;
93 MonoObject *exception_obj;
94 guint32 stop;
95 guint32 stop_unhandled;
96 } MonoDebuggerExceptionInfo;
98 MonoDebuggerThreadInfo *mono_debugger_thread_table = NULL;
100 static inline void
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);
112 void
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)
118 return;
120 info = g_new0 (MiniDebugMethodInfo, 1);
121 info->breakpoint_id = breakpoint_id;
123 cfg->debug_info = info;
126 void
127 mono_debug_open_method (MonoCompile *cfg)
129 MiniDebugMethodInfo *info;
130 MonoDebugMethodJitInfo *jit;
131 MonoMethodHeader *header;
133 info = (MiniDebugMethodInfo *) cfg->debug_info;
134 if (!info)
135 return;
137 mono_class_init (cfg->method->klass);
139 header = cfg->header;
140 g_assert (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);
148 static void
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 {
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;
165 * mono_debug_add_vg_method:
167 * Register symbol information for the method with valgrind
169 static void
170 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
172 #ifdef VALGRIND_ADD_LINE_INFO
173 MonoMethodHeader *header;
174 MonoDebugMethodInfo *minfo;
175 int i;
176 char *filename = NULL;
177 guint32 address, line_number;
178 const char *full_name;
179 guint32 *addresses;
180 guint32 *lines;
182 if (!RUNNING_ON_VALGRIND)
183 return;
185 header = mono_method_get_header (method);
187 full_name = mono_method_full_name (method, TRUE);
189 addresses = g_new0 (guint32, header->code_size + 1);
190 lines = g_new0 (guint32, header->code_size + 1);
193 * Very simple code to convert the addr->offset mappings that mono has
194 * into [addr-addr] ->line number mappings.
197 minfo = mono_debug_lookup_method (method);
198 if (minfo) {
199 /* Create offset->line number mapping */
200 for (i = 0; i < header->code_size; ++i) {
201 MonoDebugSourceLocation *location;
203 location = mono_debug_symfile_lookup_location (minfo, i);
204 if (!location)
205 continue;
207 lines [i] = location.row;
208 if (!filename)
209 filename = location.source_file;
211 mono_debug_free_source_location (location);
215 /* Create address->offset mapping */
216 for (i = 0; i < jit->num_line_numbers; ++i) {
217 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
219 g_assert (lne->offset <= header->code_size);
221 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
222 addresses [lne->offset] = lne->address;
224 /* Fill out missing addresses */
225 address = 0;
226 for (i = 0; i < header->code_size; ++i) {
227 if (addresses [i] == 0)
228 addresses [i] = address;
229 else
230 address = addresses [i];
233 address = 0;
234 line_number = 0;
235 i = 0;
236 while (i < header->code_size) {
237 if (lines [i] == line_number)
238 i ++;
239 else {
240 if (line_number > 0) {
241 //g_assert (addresses [i] - 1 >= address);
243 if (addresses [i] - 1 >= address) {
244 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
245 //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
248 address = addresses [i];
249 line_number = lines [i];
253 if (line_number > 0) {
254 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
255 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
258 VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
260 g_free (addresses);
261 g_free (lines);
262 mono_metadata_free_mh (header);
263 #endif /* VALGRIND_ADD_LINE_INFO */
266 void
267 mono_debug_close_method (MonoCompile *cfg)
269 MiniDebugMethodInfo *info;
270 MonoDebugMethodJitInfo *jit;
271 MonoMethodHeader *header;
272 MonoMethodSignature *sig;
273 MonoDebugMethodAddress *debug_info;
274 MonoMethod *method;
275 int i;
277 info = (MiniDebugMethodInfo *) cfg->debug_info;
278 if (!info || !info->jit) {
279 if (info)
280 g_free (info);
281 return;
284 method = cfg->method;
285 header = cfg->header;
286 sig = mono_method_signature (method);
288 jit = info->jit;
289 jit->code_start = cfg->native_code;
290 jit->epilogue_begin = cfg->epilog_begin;
291 jit->code_size = cfg->code_len;
293 if (jit->epilogue_begin)
294 record_line_number (info, jit->epilogue_begin, header->code_size);
296 jit->num_params = sig->param_count;
297 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
299 for (i = 0; i < jit->num_locals; i++)
300 write_variable (cfg->locals [i], &jit->locals [i]);
302 if (sig->hasthis) {
303 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
304 write_variable (cfg->args [0], jit->this_var);
307 for (i = 0; i < jit->num_params; i++)
308 write_variable (cfg->args [i + sig->hasthis], &jit->params [i]);
310 jit->num_line_numbers = info->line_numbers->len;
311 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
313 for (i = 0; i < jit->num_line_numbers; i++)
314 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
316 debug_info = mono_debug_add_method (cfg->method_to_register, jit, cfg->domain);
318 mono_debug_add_vg_method (method, jit);
320 mono_debugger_check_breakpoints (method, debug_info);
322 mono_debug_free_method_jit_info (jit);
323 mono_debug_free_method (cfg);
326 void
327 mono_debug_free_method (MonoCompile *cfg)
329 MiniDebugMethodInfo *info;
331 info = (MiniDebugMethodInfo *) cfg->debug_info;
332 if (info) {
333 if (info->line_numbers)
334 g_array_free (info->line_numbers, TRUE);
335 g_free (info);
336 cfg->debug_info = NULL;
340 void
341 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
343 MiniDebugMethodInfo *info;
344 MonoMethodHeader *header;
345 guint32 offset;
347 info = (MiniDebugMethodInfo *) cfg->debug_info;
348 if (!info || !info->jit || !ins->cil_code)
349 return;
351 header = cfg->header;
352 g_assert (header);
354 if ((ins->cil_code < header->code) ||
355 (ins->cil_code > header->code + header->code_size))
356 return;
358 offset = ins->cil_code - header->code;
359 if (!info->has_line_numbers) {
360 info->jit->prologue_end = address;
361 info->has_line_numbers = TRUE;
364 record_line_number (info, address, offset);
367 void
368 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
370 MiniDebugMethodInfo *info;
371 MonoMethodHeader *header;
372 guint32 offset;
374 info = (MiniDebugMethodInfo *) cfg->debug_info;
375 if (!info || !info->jit || !bb->cil_code)
376 return;
378 header = cfg->header;
379 g_assert (header);
381 if ((bb->cil_code < header->code) ||
382 (bb->cil_code > header->code + header->code_size))
383 return;
385 offset = bb->cil_code - header->code;
386 if (!info->has_line_numbers) {
387 info->jit->prologue_end = address;
388 info->has_line_numbers = TRUE;
391 record_line_number (info, address, offset);
394 static inline void
395 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
397 guint8 *p = buf;
399 //printf ("ENCODE: %d 0x%x.\n", value, value);
402 * Same encoding as the one used in the metadata, extended to handle values
403 * greater than 0x1fffffff.
405 if ((value >= 0) && (value <= 127))
406 *p++ = value;
407 else if ((value >= 0) && (value <= 16383)) {
408 p [0] = 0x80 | (value >> 8);
409 p [1] = value & 0xff;
410 p += 2;
411 } else if ((value >= 0) && (value <= 0x1fffffff)) {
412 p [0] = (value >> 24) | 0xc0;
413 p [1] = (value >> 16) & 0xff;
414 p [2] = (value >> 8) & 0xff;
415 p [3] = value & 0xff;
416 p += 4;
418 else {
419 p [0] = 0xff;
420 p [1] = (value >> 24) & 0xff;
421 p [2] = (value >> 16) & 0xff;
422 p [3] = (value >> 8) & 0xff;
423 p [4] = value & 0xff;
424 p += 5;
426 if (endbuf)
427 *endbuf = p;
430 static inline gint32
431 decode_value (guint8 *ptr, guint8 **rptr)
433 guint8 b = *ptr;
434 gint32 len;
436 if ((b & 0x80) == 0){
437 len = b;
438 ++ptr;
439 } else if ((b & 0x40) == 0){
440 len = ((b & 0x3f) << 8 | ptr [1]);
441 ptr += 2;
442 } else if (b != 0xff) {
443 len = ((b & 0x1f) << 24) |
444 (ptr [1] << 16) |
445 (ptr [2] << 8) |
446 ptr [3];
447 ptr += 4;
449 else {
450 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
451 ptr += 5;
453 if (rptr)
454 *rptr = ptr;
456 //printf ("DECODE: %d.\n", len);
457 return len;
460 static void
461 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
463 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
465 encode_value (var->index, p, &p);
467 switch (flags) {
468 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
469 break;
470 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
471 encode_value (var->offset, p, &p);
472 break;
473 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
474 break;
475 default:
476 g_assert_not_reached ();
478 *endbuf = p;
481 void
482 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
484 MonoDebugMethodJitInfo *jit;
485 guint32 size, prev_offset, prev_native_offset;
486 guint8 *buf, *p;
487 int i;
489 /* Can't use cfg->debug_info as it is freed by close_method () */
490 jit = mono_debug_find_method (cfg->method, mono_domain_get ());
491 if (!jit) {
492 *buf_len = 0;
493 return;
496 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
497 p = buf = g_malloc (size);
498 encode_value (jit->epilogue_begin, p, &p);
499 encode_value (jit->prologue_end, p, &p);
500 encode_value (jit->code_size, p, &p);
502 for (i = 0; i < jit->num_params; ++i)
503 serialize_variable (&jit->params [i], p, &p);
505 if (mono_method_signature (cfg->method)->hasthis)
506 serialize_variable (jit->this_var, p, &p);
508 for (i = 0; i < jit->num_locals; i++)
509 serialize_variable (&jit->locals [i], p, &p);
511 encode_value (jit->num_line_numbers, p, &p);
513 prev_offset = 0;
514 prev_native_offset = 0;
515 for (i = 0; i < jit->num_line_numbers; ++i) {
516 /* Sometimes, the offset values are not in increasing order */
517 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
518 encode_value (lne->il_offset - prev_offset, p, &p);
519 encode_value (lne->native_offset - prev_native_offset, p, &p);
520 prev_offset = lne->il_offset;
521 prev_native_offset = lne->native_offset;
524 g_assert (p - buf < size);
526 *out_buf = buf;
527 *buf_len = p - buf;
530 static void
531 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
533 guint32 flags;
535 var->index = decode_value (p, &p);
537 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
539 switch (flags) {
540 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
541 break;
542 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
543 var->offset = decode_value (p, &p);
544 break;
545 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
546 break;
547 default:
548 g_assert_not_reached ();
550 *endbuf = p;
553 static MonoDebugMethodJitInfo *
554 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
556 MonoMethodHeader *header;
557 gint32 offset, native_offset, prev_offset, prev_native_offset;
558 MonoDebugMethodJitInfo *jit;
559 guint8 *p;
560 int i;
562 header = mono_method_get_header (method);
563 g_assert (header);
565 jit = g_new0 (MonoDebugMethodJitInfo, 1);
566 jit->code_start = code_start;
567 jit->num_locals = header->num_locals;
568 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
569 jit->num_params = mono_method_signature (method)->param_count;
570 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
572 p = buf;
573 jit->epilogue_begin = decode_value (p, &p);
574 jit->prologue_end = decode_value (p, &p);
575 jit->code_size = decode_value (p, &p);
577 for (i = 0; i < jit->num_params; ++i)
578 deserialize_variable (&jit->params [i], p, &p);
580 if (mono_method_signature (method)->hasthis) {
581 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
582 deserialize_variable (jit->this_var, p, &p);
585 for (i = 0; i < jit->num_locals; i++)
586 deserialize_variable (&jit->locals [i], p, &p);
588 jit->num_line_numbers = decode_value (p, &p);
589 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
591 prev_offset = 0;
592 prev_native_offset = 0;
593 for (i = 0; i < jit->num_line_numbers; ++i) {
594 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
596 offset = prev_offset + decode_value (p, &p);
597 native_offset = prev_native_offset + decode_value (p, &p);
599 lne->native_offset = native_offset;
600 lne->il_offset = offset;
602 prev_offset = offset;
603 prev_native_offset = native_offset;
606 mono_metadata_free_mh (header);
607 return jit;
610 void
611 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
612 guint8 *debug_info, guint32 debug_info_len)
614 MonoDebugMethodJitInfo *jit;
616 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
617 return;
619 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
620 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
621 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
622 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
623 (method->wrapper_type != MONO_WRAPPER_NONE))
624 return;
626 if (debug_info_len == 0)
627 return;
629 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
631 mono_debug_add_method (method, jit, domain);
633 mono_debug_add_vg_method (method, jit);
635 mono_debug_free_method_jit_info (jit);
638 void
639 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
641 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
642 return;
644 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
647 static void
648 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
650 switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
651 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
652 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
653 break;
654 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
655 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);
656 break;
657 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
658 default:
659 g_assert_not_reached ();
664 * mono_debug_print_locals:
666 * Prints to stdout the information about the local variables in
667 * a method (if @only_arguments is false) or about the arguments.
668 * The information includes the storage info (where the variable
669 * lives, in a register or in memory).
670 * The method is found by looking up what method has been emitted at
671 * the instruction address @ip.
672 * This is for use inside a debugger.
674 void
675 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
677 MonoDomain *domain = mono_domain_get ();
678 MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
679 MonoDebugMethodJitInfo *jit;
680 int i;
682 if (!ji)
683 return;
685 jit = mono_debug_find_method (mono_jit_info_get_method (ji), domain);
686 if (!jit)
687 return;
689 if (only_arguments) {
690 char **names;
691 names = g_new (char *, jit->num_params);
692 mono_method_get_param_names (mono_jit_info_get_method (ji), (const char **) names);
693 if (jit->this_var)
694 print_var_info (jit->this_var, 0, "this", "Arg");
695 for (i = 0; i < jit->num_params; ++i) {
696 print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
698 g_free (names);
699 } else {
700 for (i = 0; i < jit->num_locals; ++i) {
701 print_var_info (&jit->locals [i], i, "", "Local");
704 mono_debug_free_method_jit_info (jit);
708 * The old Debugger breakpoint interface.
710 * This interface is used to insert breakpoints on methods which are not yet JITed.
711 * The debugging code keeps a list of all such breakpoints and automatically inserts the
712 * breakpoint when the method is JITed.
715 static GPtrArray *breakpoints = NULL;
718 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
720 static int last_breakpoint_id = 0;
721 MiniDebugBreakpointInfo *info;
723 info = g_new0 (MiniDebugBreakpointInfo, 1);
724 info->desc = desc;
725 info->index = ++last_breakpoint_id;
727 if (!breakpoints)
728 breakpoints = g_ptr_array_new ();
730 g_ptr_array_add (breakpoints, info);
732 return info->index;
736 mono_debugger_remove_breakpoint (int breakpoint_id)
738 int i;
740 if (!breakpoints)
741 return 0;
743 for (i = 0; i < breakpoints->len; i++) {
744 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
746 if (info->index != breakpoint_id)
747 continue;
749 mono_method_desc_free (info->desc);
750 g_ptr_array_remove (breakpoints, info);
751 g_free (info);
752 return 1;
755 return 0;
759 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
761 MonoMethodDesc *desc;
763 desc = mono_method_desc_new (method_name, include_namespace);
764 if (!desc)
765 return 0;
767 return mono_debugger_insert_breakpoint_full (desc);
771 mono_debugger_method_has_breakpoint (MonoMethod *method)
773 int i;
775 if (!breakpoints)
776 return 0;
778 for (i = 0; i < breakpoints->len; i++) {
779 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
781 if (!mono_method_desc_full_match (info->desc, method))
782 continue;
784 return info->index;
787 return 0;
790 void
791 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
793 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
796 void
797 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls, gpointer func)
799 #ifdef MONO_DEBUGGER_SUPPORTED
800 size_t stsize = 0;
801 guint8 *staddr = NULL;
802 MonoDebuggerThreadInfo *info;
804 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
805 return;
807 mono_debugger_lock ();
809 mono_thread_get_stack_bounds (&staddr, &stsize);
811 info = g_new0 (MonoDebuggerThreadInfo, 1);
812 info->tid = tid;
813 info->thread = thread->internal_thread;
814 info->stack_start = (guint64) (gsize) staddr;
815 info->signal_stack_start = (guint64) (gsize) jit_tls->signal_stack;
816 info->stack_size = stsize;
817 info->signal_stack_size = jit_tls->signal_stack_size;
818 info->end_stack = (guint64) (gsize) GC_mono_debugger_get_stack_ptr ();
819 info->lmf_addr = (guint64) (gsize) mono_get_lmf_addr ();
820 info->jit_tls = jit_tls;
822 if (func)
823 info->thread_flags = MONO_DEBUGGER_THREAD_FLAGS_INTERNAL;
824 if (thread->internal_thread->threadpool_thread)
825 info->thread_flags |= MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL;
827 info->next = mono_debugger_thread_table;
828 mono_debugger_thread_table = info;
830 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
831 tid, (guint64) (gsize) info);
833 mono_debugger_unlock ();
834 #endif /* MONO_DEBUGGER_SUPPORTED */
837 void
838 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
840 #ifdef MONO_DEBUGGER_SUPPORTED
841 MonoDebuggerThreadInfo **ptr;
843 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
844 return;
846 mono_debugger_lock ();
848 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
849 MonoDebuggerThreadInfo *info = *ptr;
851 if (info->jit_tls != jit_tls)
852 continue;
854 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
855 info->tid, (guint64) (gsize) info);
857 *ptr = info->next;
858 g_free (info);
859 break;
862 mono_debugger_unlock ();
863 #endif
866 void
867 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
869 #ifdef MONO_DEBUGGER_SUPPORTED
870 MonoDebuggerThreadInfo **ptr;
871 MonoInternalThread *thread = mono_thread_internal_current ();
873 if (!mono_debug_using_mono_debugger ())
874 return;
876 mono_debugger_lock ();
878 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
879 MonoDebuggerThreadInfo *info = *ptr;
881 if (info->thread != thread)
882 continue;
884 if ((info->extended_notifications & (int) event) == 0)
885 continue;
887 mono_debugger_event (event, data, arg);
890 mono_debugger_unlock ();
891 #endif
894 void
895 mono_debugger_trampoline_compiled (const guint8 *trampoline, MonoMethod *method, const guint8 *code)
897 #ifdef MONO_DEBUGGER_SUPPORTED
898 struct {
899 const guint8 * trampoline;
900 MonoMethod *method;
901 const guint8 *code;
902 } info = { trampoline, method, code };
904 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_OLD_TRAMPOLINE,
905 (guint64) (gsize) method, (guint64) (gsize) code);
906 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
907 (guint64) (gsize) &info, 0);
908 #endif
911 #if MONO_DEBUGGER_SUPPORTED
912 static MonoDebuggerThreadInfo *
913 find_debugger_thread_info (MonoInternalThread *thread)
915 MonoDebuggerThreadInfo **ptr;
917 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
918 MonoDebuggerThreadInfo *info = *ptr;
920 if (info->thread == thread)
921 return info;
924 return NULL;
926 #endif
928 MonoDebuggerExceptionAction
929 _mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
931 #ifdef MONO_DEBUGGER_SUPPORTED
932 MonoDebuggerExceptionInfo exc_info;
933 MonoDebuggerThreadInfo *thread_info;
935 if (!mono_debug_using_mono_debugger ())
936 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
938 mono_debugger_lock ();
940 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
941 if (!thread_info) {
942 mono_debugger_unlock ();
943 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
946 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
947 mono_debugger_unlock ();
948 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
951 if (thread_info->exception_state.stopped_on_exception ||
952 thread_info->exception_state.stopped_on_unhandled) {
953 thread_info->exception_state.stopped_on_exception = 0;
954 mono_debugger_unlock ();
955 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
958 /* Protect the exception object from being garbage collected. */
960 thread_info->exception_state.stopped_on_unhandled = 0;
961 thread_info->exception_state.stopped_on_exception = 1;
962 thread_info->exception_state.last_exception = exc;
965 * Backwards compatibility:
967 * Older debugger versions only know `exc_info.stop' and older runtime versions check
968 * `exc_info.stop != 0'.
970 * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
971 * `stop_unhandled' field.
974 exc_info.stack_pointer = stack;
975 exc_info.exception_obj = exc;
976 exc_info.stop = 0;
977 exc_info.stop_unhandled = 0;
979 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &exc_info,
980 (guint64) (gsize) addr);
982 if (!exc_info.stop) {
983 thread_info->exception_state.stopped_on_exception = 0;
984 thread_info->exception_state.last_exception = NULL;
987 mono_debugger_unlock ();
989 if (exc_info.stop)
990 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP;
991 else if (exc_info.stop_unhandled)
992 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED;
993 #endif
995 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
998 gboolean
999 _mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
1001 #ifdef MONO_DEBUGGER_SUPPORTED
1002 MonoDebuggerThreadInfo *thread_info;
1004 if (!mono_debug_using_mono_debugger ())
1005 return FALSE;
1007 if (exc) {
1008 const gchar *name = mono_class_get_name (mono_object_get_class (exc));
1009 if (!strcmp (name, "ThreadAbortException"))
1010 return FALSE;
1013 mono_debugger_lock ();
1015 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1016 if (!thread_info) {
1017 mono_debugger_unlock ();
1018 return FALSE;
1021 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1022 mono_debugger_unlock ();
1023 return FALSE;
1026 if (thread_info->exception_state.stopped_on_unhandled) {
1027 thread_info->exception_state.stopped_on_unhandled = 0;
1028 mono_debugger_unlock ();
1029 return FALSE;
1032 thread_info->exception_state.stopped_on_unhandled = 1;
1033 thread_info->exception_state.last_exception = exc;
1035 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
1036 (guint64) (gsize) exc, (guint64) (gsize) addr);
1038 return TRUE;
1039 #else
1040 return FALSE;
1041 #endif
1045 * mono_debugger_call_exception_handler:
1047 * Called from mono_handle_exception_internal() to tell the debugger that we're about
1048 * to invoke an exception handler.
1050 * The debugger may choose to set a breakpoint at @addr. This is used if the user is
1051 * single-stepping from a `try' into a `catch' block, for instance.
1054 void
1055 mono_debugger_call_exception_handler (gpointer addr, gpointer stack, MonoObject *exc)
1057 #ifdef MONO_DEBUGGER_SUPPORTED
1058 MonoDebuggerThreadInfo *thread_info;
1059 MonoDebuggerExceptionInfo exc_info;
1061 if (!mono_debug_using_mono_debugger ())
1062 return;
1064 mono_debugger_lock ();
1066 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1067 if (!thread_info) {
1068 mono_debugger_unlock ();
1069 return;
1072 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1073 mono_debugger_unlock ();
1074 return;
1077 // Prevent the object from being finalized.
1078 thread_info->exception_state.last_exception = exc;
1080 exc_info.stack_pointer = stack;
1081 exc_info.exception_obj = exc;
1082 exc_info.stop = 0;
1083 exc_info.stop_unhandled = 0;
1085 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &exc_info,
1086 (guint64) (gsize) addr);
1088 mono_debugger_unlock ();
1089 #endif
1092 #ifdef MONO_DEBUGGER_SUPPORTED
1094 static gchar *
1095 get_exception_message (MonoObject *exc)
1097 char *message = NULL;
1098 MonoString *str;
1099 MonoMethod *method;
1100 MonoClass *klass;
1101 gint i;
1103 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1104 klass = exc->vtable->klass;
1105 method = NULL;
1106 while (klass && method == NULL) {
1107 for (i = 0; i < klass->method.count; ++i) {
1108 method = klass->methods [i];
1109 if (!strcmp ("ToString", method->name) &&
1110 mono_method_signature (method)->param_count == 0 &&
1111 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1112 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1113 break;
1115 method = NULL;
1118 if (method == NULL)
1119 klass = klass->parent;
1122 g_assert (method);
1124 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1125 if (str)
1126 message = mono_string_to_utf8 (str);
1129 return message;
1132 MonoObject *
1133 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1135 MonoDebuggerThreadInfo *thread_info;
1136 MonoDebuggerExceptionState saved_exception_state;
1137 MonoObject *retval;
1138 gchar *message;
1140 mono_debugger_lock ();
1142 thread_info = find_debugger_thread_info (mono_thread_internal_current ());
1143 if (!thread_info) {
1144 mono_debugger_unlock ();
1145 return NULL;
1148 saved_exception_state = thread_info->exception_state;
1150 thread_info->exception_state.last_exception = NULL;
1151 thread_info->exception_state.stopped_on_unhandled = 0;
1152 thread_info->exception_state.stopped_on_exception = 0;
1154 thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1156 mono_debugger_unlock ();
1158 if (!strcmp (method->name, ".ctor")) {
1159 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1161 mono_runtime_invoke (method, obj, params, exc);
1162 } else
1163 retval = mono_runtime_invoke (method, obj, params, exc);
1165 mono_debugger_lock ();
1167 thread_info->exception_state = saved_exception_state;
1168 thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE;
1170 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1171 thread_info->internal_flags &= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1172 mono_thread_internal_reset_abort (thread_info->thread);
1174 mono_debugger_unlock ();
1176 *exc = NULL;
1177 return NULL;
1180 mono_debugger_unlock ();
1182 if (!exc || (*exc == NULL))
1183 return retval;
1185 retval = *exc;
1186 message = get_exception_message (*exc);
1187 if (message) {
1188 *exc = (MonoObject *) mono_string_new_wrapper (message);
1189 g_free (message);
1192 return retval;
1195 gboolean
1196 mono_debugger_abort_runtime_invoke ()
1198 MonoInternalThread *thread = mono_thread_internal_current ();
1199 MonoDebuggerThreadInfo *thread_info;
1201 mono_debugger_lock ();
1203 thread_info = find_debugger_thread_info (thread);
1204 if (!thread_info) {
1205 mono_debugger_unlock ();
1206 return FALSE;
1209 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE) == 0) {
1210 mono_debugger_unlock ();
1211 return FALSE;
1214 if ((thread_info->internal_flags & MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED) != 0) {
1215 mono_debugger_unlock ();
1216 return TRUE;
1219 thread_info->internal_flags |= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED;
1220 ves_icall_System_Threading_Thread_Abort (thread_info->thread, NULL);
1222 mono_debugger_unlock ();
1223 return TRUE;
1226 #endif