In mono/metadata:
[mono.git] / mono / mini / debug-mini.c
blob6df93fe2541bd60190e2031c77163f9b3abc9a55
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 #ifdef HAVE_VALGRIND_H
24 #include <valgrind/valgrind.h>
25 #endif
27 #ifdef MONO_DEBUGGER_SUPPORTED
28 #include <libgc/include/libgc-mono-debugger.h>
29 #endif
31 typedef struct {
32 guint32 index;
33 MonoMethodDesc *desc;
34 } MiniDebugBreakpointInfo;
36 typedef struct
38 MonoDebugMethodJitInfo *jit;
39 GArray *line_numbers;
40 guint32 has_line_numbers;
41 guint32 breakpoint_id;
42 } MiniDebugMethodInfo;
44 typedef struct {
45 MonoObject *last_exception;
46 guint32 stopped_on_exception : 1;
47 guint32 stopped_on_unhandled : 1;
48 } MonoDebuggerExceptionState;
50 typedef enum {
51 MONO_DEBUGGER_THREAD_FLAGS_NONE = 0,
52 MONO_DEBUGGER_THREAD_FLAGS_INTERNAL = 1,
53 MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL = 2
54 } MonoDebuggerThreadFlags;
56 struct _MonoDebuggerThreadInfo {
57 guint64 tid;
58 guint64 lmf_addr;
59 guint64 end_stack;
61 guint64 extended_notifications;
63 /* Next pointer. */
64 MonoDebuggerThreadInfo *next;
67 * The stack bounds are only used when reading a core file.
69 guint64 stack_start;
70 guint64 signal_stack_start;
71 guint32 stack_size;
72 guint32 signal_stack_size;
74 guint32 thread_flags;
77 * The debugger doesn't access anything beyond this point.
79 MonoDebuggerExceptionState exception_state;
81 MonoJitTlsData *jit_tls;
82 MonoThread *thread;
85 typedef struct {
86 gpointer stack_pointer;
87 MonoObject *exception_obj;
88 guint32 stop;
89 guint32 stop_unhandled;
90 } MonoDebuggerExceptionInfo;
92 MonoDebuggerThreadInfo *mono_debugger_thread_table = NULL;
94 static inline void
95 record_line_number (MiniDebugMethodInfo *info, guint32 address, guint32 offset)
97 MonoDebugLineNumberEntry lne;
99 lne.native_offset = address;
100 lne.il_offset = offset;
102 g_array_append_val (info->line_numbers, lne);
106 void
107 mono_debug_init_method (MonoCompile *cfg, MonoBasicBlock *start_block, guint32 breakpoint_id)
109 MiniDebugMethodInfo *info;
111 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
112 return;
114 info = g_new0 (MiniDebugMethodInfo, 1);
115 info->breakpoint_id = breakpoint_id;
117 cfg->debug_info = info;
120 void
121 mono_debug_open_method (MonoCompile *cfg)
123 MiniDebugMethodInfo *info;
124 MonoDebugMethodJitInfo *jit;
125 MonoMethodHeader *header;
127 info = (MiniDebugMethodInfo *) cfg->debug_info;
128 if (!info)
129 return;
131 mono_class_init (cfg->method->klass);
133 header = mono_method_get_header (cfg->method);
134 g_assert (header);
136 info->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
137 info->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
138 jit->num_locals = header->num_locals;
139 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
142 static void
143 write_variable (MonoInst *inst, MonoDebugVarInfo *var)
145 var->type = inst->inst_vtype;
147 if (inst->opcode == OP_REGVAR)
148 var->index = inst->dreg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
149 else if (inst->flags & MONO_INST_IS_DEAD)
150 var->index = MONO_DEBUG_VAR_ADDRESS_MODE_DEAD;
151 else {
152 /* the debug interface needs fixing to allow 0(%base) address */
153 var->index = inst->inst_basereg | MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET;
154 var->offset = inst->inst_offset;
159 * mono_debug_add_vg_method:
161 * Register symbol information for the method with valgrind
163 static void
164 mono_debug_add_vg_method (MonoMethod *method, MonoDebugMethodJitInfo *jit)
166 #ifdef VALGRIND_ADD_LINE_INFO
167 MonoMethodHeader *header;
168 MonoDebugMethodInfo *minfo;
169 int i;
170 char *filename = NULL;
171 guint32 address, line_number;
172 const char *full_name;
173 guint32 *addresses;
174 guint32 *lines;
176 if (!RUNNING_ON_VALGRIND)
177 return;
179 header = mono_method_get_header (method);
181 full_name = mono_method_full_name (method, TRUE);
183 addresses = g_new0 (guint32, header->code_size + 1);
184 lines = g_new0 (guint32, header->code_size + 1);
187 * Very simple code to convert the addr->offset mappings that mono has
188 * into [addr-addr] ->line number mappings.
191 minfo = mono_debug_lookup_method (method);
192 if (minfo) {
193 /* Create offset->line number mapping */
194 for (i = 0; i < header->code_size; ++i) {
195 MonoDebugSourceLocation *location;
197 location = mono_debug_symfile_lookup_location (minfo, i);
198 if (!location)
199 continue;
201 lines [i] = location.row;
202 if (!filename)
203 filename = location.source_file;
205 mono_debug_free_source_location (location);
209 /* Create address->offset mapping */
210 for (i = 0; i < jit->num_line_numbers; ++i) {
211 MonoDebugLineNumberEntry *lne = jit->line_numbers [i];
213 g_assert (lne->offset <= header->code_size);
215 if ((addresses [lne->offset] == 0) || (lne->address < addresses [lne->offset]))
216 addresses [lne->offset] = lne->address;
218 /* Fill out missing addresses */
219 address = 0;
220 for (i = 0; i < header->code_size; ++i) {
221 if (addresses [i] == 0)
222 addresses [i] = address;
223 else
224 address = addresses [i];
227 address = 0;
228 line_number = 0;
229 i = 0;
230 while (i < header->code_size) {
231 if (lines [i] == line_number)
232 i ++;
233 else {
234 if (line_number > 0) {
235 //g_assert (addresses [i] - 1 >= address);
237 if (addresses [i] - 1 >= address) {
238 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + addresses [i] - 1, filename, line_number);
239 //printf ("[%d-%d] -> %d.\n", address, addresses [i] - 1, line_number);
242 address = addresses [i];
243 line_number = lines [i];
247 if (line_number > 0) {
248 VALGRIND_ADD_LINE_INFO (jit->code_start + address, jit->code_start + jit->code_size - 1, filename, line_number);
249 //printf ("[%d-%d] -> %d.\n", address, jit->code_size - 1, line_number);
252 VALGRIND_ADD_SYMBOL (jit->code_start, jit->code_size, full_name);
254 g_free (addresses);
255 g_free (lines);
256 #endif /* VALGRIND_ADD_LINE_INFO */
259 void
260 mono_debug_close_method (MonoCompile *cfg)
262 MiniDebugMethodInfo *info;
263 MonoDebugMethodJitInfo *jit;
264 MonoMethodHeader *header;
265 MonoMethodSignature *sig;
266 MonoDebugMethodAddress *debug_info;
267 MonoMethod *method;
268 int i;
270 info = (MiniDebugMethodInfo *) cfg->debug_info;
271 if (!info || !info->jit) {
272 if (info)
273 g_free (info);
274 return;
277 method = cfg->method;
278 header = mono_method_get_header (method);
279 sig = mono_method_signature (method);
281 jit = info->jit;
282 jit->code_start = cfg->native_code;
283 jit->epilogue_begin = cfg->epilog_begin;
284 jit->code_size = cfg->code_len;
286 if (jit->epilogue_begin)
287 record_line_number (info, jit->epilogue_begin, header->code_size);
289 jit->num_params = sig->param_count;
290 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
292 for (i = 0; i < jit->num_locals; i++)
293 write_variable (cfg->locals [i], &jit->locals [i]);
295 if (sig->hasthis) {
296 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
297 write_variable (cfg->args [0], jit->this_var);
300 for (i = 0; i < jit->num_params; i++)
301 write_variable (cfg->args [i + sig->hasthis], &jit->params [i]);
303 jit->num_line_numbers = info->line_numbers->len;
304 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
306 for (i = 0; i < jit->num_line_numbers; i++)
307 jit->line_numbers [i] = g_array_index (info->line_numbers, MonoDebugLineNumberEntry, i);
309 debug_info = mono_debug_add_method (cfg->method_to_register, jit, cfg->domain);
311 mono_debug_add_vg_method (method, jit);
313 mono_debugger_check_breakpoints (method, debug_info);
315 mono_debug_free_method_jit_info (jit);
316 g_array_free (info->line_numbers, TRUE);
317 g_free (info);
320 void
321 mono_debug_record_line_number (MonoCompile *cfg, MonoInst *ins, guint32 address)
323 MiniDebugMethodInfo *info;
324 MonoMethodHeader *header;
325 guint32 offset;
327 info = (MiniDebugMethodInfo *) cfg->debug_info;
328 if (!info || !info->jit || !ins->cil_code)
329 return;
331 header = mono_method_get_header (cfg->method);
332 g_assert (header);
334 if ((ins->cil_code < header->code) ||
335 (ins->cil_code > header->code + header->code_size))
336 return;
338 offset = ins->cil_code - header->code;
339 if (!info->has_line_numbers) {
340 info->jit->prologue_end = address;
341 info->has_line_numbers = TRUE;
344 record_line_number (info, address, offset);
347 void
348 mono_debug_open_block (MonoCompile *cfg, MonoBasicBlock *bb, guint32 address)
350 MiniDebugMethodInfo *info;
351 MonoMethodHeader *header;
352 guint32 offset;
354 info = (MiniDebugMethodInfo *) cfg->debug_info;
355 if (!info || !info->jit || !bb->cil_code)
356 return;
358 header = mono_method_get_header (cfg->method);
359 g_assert (header);
361 if ((bb->cil_code < header->code) ||
362 (bb->cil_code > header->code + header->code_size))
363 return;
365 offset = bb->cil_code - header->code;
366 if (!info->has_line_numbers) {
367 info->jit->prologue_end = address;
368 info->has_line_numbers = TRUE;
371 record_line_number (info, address, offset);
374 static inline void
375 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
377 guint8 *p = buf;
379 //printf ("ENCODE: %d 0x%x.\n", value, value);
382 * Same encoding as the one used in the metadata, extended to handle values
383 * greater than 0x1fffffff.
385 if ((value >= 0) && (value <= 127))
386 *p++ = value;
387 else if ((value >= 0) && (value <= 16383)) {
388 p [0] = 0x80 | (value >> 8);
389 p [1] = value & 0xff;
390 p += 2;
391 } else if ((value >= 0) && (value <= 0x1fffffff)) {
392 p [0] = (value >> 24) | 0xc0;
393 p [1] = (value >> 16) & 0xff;
394 p [2] = (value >> 8) & 0xff;
395 p [3] = value & 0xff;
396 p += 4;
398 else {
399 p [0] = 0xff;
400 p [1] = (value >> 24) & 0xff;
401 p [2] = (value >> 16) & 0xff;
402 p [3] = (value >> 8) & 0xff;
403 p [4] = value & 0xff;
404 p += 5;
406 if (endbuf)
407 *endbuf = p;
410 static inline gint32
411 decode_value (guint8 *ptr, guint8 **rptr)
413 guint8 b = *ptr;
414 gint32 len;
416 if ((b & 0x80) == 0){
417 len = b;
418 ++ptr;
419 } else if ((b & 0x40) == 0){
420 len = ((b & 0x3f) << 8 | ptr [1]);
421 ptr += 2;
422 } else if (b != 0xff) {
423 len = ((b & 0x1f) << 24) |
424 (ptr [1] << 16) |
425 (ptr [2] << 8) |
426 ptr [3];
427 ptr += 4;
429 else {
430 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
431 ptr += 5;
433 if (rptr)
434 *rptr = ptr;
436 //printf ("DECODE: %d.\n", len);
437 return len;
440 static void
441 serialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
443 guint32 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
445 encode_value (var->index, p, &p);
447 switch (flags) {
448 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
449 break;
450 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
451 encode_value (var->offset, p, &p);
452 break;
453 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
454 break;
455 default:
456 g_assert_not_reached ();
458 *endbuf = p;
461 void
462 mono_debug_serialize_debug_info (MonoCompile *cfg, guint8 **out_buf, guint32 *buf_len)
464 MonoDebugMethodJitInfo *jit;
465 guint32 size, prev_offset, prev_native_offset;
466 guint8 *buf, *p;
467 int i;
469 /* Can't use cfg->debug_info as it is freed by close_method () */
470 jit = mono_debug_find_method (cfg->method, mono_domain_get ());
471 if (!jit) {
472 *buf_len = 0;
473 return;
476 size = ((jit->num_params + jit->num_locals + 1) * 10) + (jit->num_line_numbers * 10) + 64;
477 p = buf = g_malloc (size);
478 encode_value (jit->epilogue_begin, p, &p);
479 encode_value (jit->prologue_end, p, &p);
480 encode_value (jit->code_size, p, &p);
482 for (i = 0; i < jit->num_params; ++i)
483 serialize_variable (&jit->params [i], p, &p);
485 if (mono_method_signature (cfg->method)->hasthis)
486 serialize_variable (jit->this_var, p, &p);
488 for (i = 0; i < jit->num_locals; i++)
489 serialize_variable (&jit->locals [i], p, &p);
491 encode_value (jit->num_line_numbers, p, &p);
493 prev_offset = 0;
494 prev_native_offset = 0;
495 for (i = 0; i < jit->num_line_numbers; ++i) {
496 /* Sometimes, the offset values are not in increasing order */
497 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
498 encode_value (lne->il_offset - prev_offset, p, &p);
499 encode_value (lne->native_offset - prev_native_offset, p, &p);
500 prev_offset = lne->il_offset;
501 prev_native_offset = lne->native_offset;
504 g_assert (p - buf < size);
506 *out_buf = buf;
507 *buf_len = p - buf;
510 static void
511 deserialize_variable (MonoDebugVarInfo *var, guint8 *p, guint8 **endbuf)
513 guint32 flags;
515 var->index = decode_value (p, &p);
517 flags = var->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS;
519 switch (flags) {
520 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
521 break;
522 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
523 var->offset = decode_value (p, &p);
524 break;
525 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD:
526 break;
527 default:
528 g_assert_not_reached ();
530 *endbuf = p;
533 static MonoDebugMethodJitInfo *
534 deserialize_debug_info (MonoMethod *method, guint8 *code_start, guint8 *buf, guint32 buf_len)
536 MonoMethodHeader *header;
537 gint32 offset, native_offset, prev_offset, prev_native_offset;
538 MonoDebugMethodJitInfo *jit;
539 guint8 *p;
540 int i;
542 header = mono_method_get_header (method);
543 g_assert (header);
545 jit = g_new0 (MonoDebugMethodJitInfo, 1);
546 jit->code_start = code_start;
547 jit->num_locals = header->num_locals;
548 jit->locals = g_new0 (MonoDebugVarInfo, jit->num_locals);
549 jit->num_params = mono_method_signature (method)->param_count;
550 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
552 p = buf;
553 jit->epilogue_begin = decode_value (p, &p);
554 jit->prologue_end = decode_value (p, &p);
555 jit->code_size = decode_value (p, &p);
557 for (i = 0; i < jit->num_params; ++i)
558 deserialize_variable (&jit->params [i], p, &p);
560 if (mono_method_signature (method)->hasthis) {
561 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
562 deserialize_variable (jit->this_var, p, &p);
565 for (i = 0; i < jit->num_locals; i++)
566 deserialize_variable (&jit->locals [i], p, &p);
568 jit->num_line_numbers = decode_value (p, &p);
569 jit->line_numbers = g_new0 (MonoDebugLineNumberEntry, jit->num_line_numbers);
571 prev_offset = 0;
572 prev_native_offset = 0;
573 for (i = 0; i < jit->num_line_numbers; ++i) {
574 MonoDebugLineNumberEntry *lne = &jit->line_numbers [i];
576 offset = prev_offset + decode_value (p, &p);
577 native_offset = prev_native_offset + decode_value (p, &p);
579 lne->native_offset = native_offset;
580 lne->il_offset = offset;
582 prev_offset = offset;
583 prev_native_offset = native_offset;
586 return jit;
589 void
590 mono_debug_add_aot_method (MonoDomain *domain, MonoMethod *method, guint8 *code_start,
591 guint8 *debug_info, guint32 debug_info_len)
593 MonoDebugMethodJitInfo *jit;
595 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
596 return;
598 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
599 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
600 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
601 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
602 (method->wrapper_type != MONO_WRAPPER_NONE))
603 return;
605 if (debug_info_len == 0)
606 return;
608 jit = deserialize_debug_info (method, code_start, debug_info, debug_info_len);
610 mono_debug_add_method (method, jit, domain);
612 mono_debug_add_vg_method (method, jit);
614 mono_debug_free_method_jit_info (jit);
617 void
618 mono_debug_add_icall_wrapper (MonoMethod *method, MonoJitICallInfo* callinfo)
620 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
621 return;
623 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
626 static void
627 print_var_info (MonoDebugVarInfo *info, int idx, const char *name, const char *type)
629 switch (info->index & MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS) {
630 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER:
631 g_print ("%s %s (%d) in register %s\n", type, name, idx, mono_arch_regname (info->index & (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS)));
632 break;
633 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET:
634 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);
635 break;
636 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS:
637 default:
638 g_assert_not_reached ();
643 * mono_debug_print_locals:
645 * Prints to stdout the information about the local variables in
646 * a method (if @only_arguments is false) or about the arguments.
647 * The information includes the storage info (where the variable
648 * lives, in a register or in memory).
649 * The method is found by looking up what method has been emitted at
650 * the instruction address @ip.
651 * This is for use inside a debugger.
653 void
654 mono_debug_print_vars (gpointer ip, gboolean only_arguments)
656 MonoDomain *domain = mono_domain_get ();
657 MonoJitInfo *ji = mono_jit_info_table_find (domain, ip);
658 MonoDebugMethodJitInfo *jit;
659 int i;
661 if (!ji)
662 return;
664 jit = mono_debug_find_method (mono_jit_info_get_method (ji), domain);
665 if (!jit)
666 return;
668 if (only_arguments) {
669 char **names;
670 names = g_new (char *, jit->num_params);
671 mono_method_get_param_names (mono_jit_info_get_method (ji), (const char **) names);
672 if (jit->this_var)
673 print_var_info (jit->this_var, 0, "this", "Arg");
674 for (i = 0; i < jit->num_params; ++i) {
675 print_var_info (&jit->params [i], i, names [i]? names [i]: "unknown name", "Arg");
677 g_free (names);
678 } else {
679 for (i = 0; i < jit->num_locals; ++i) {
680 print_var_info (&jit->locals [i], i, "", "Local");
683 mono_debug_free_method_jit_info (jit);
687 * The old Debugger breakpoint interface.
689 * This interface is used to insert breakpoints on methods which are not yet JITed.
690 * The debugging code keeps a list of all such breakpoints and automatically inserts the
691 * breakpoint when the method is JITed.
694 static GPtrArray *breakpoints = NULL;
697 mono_debugger_insert_breakpoint_full (MonoMethodDesc *desc)
699 static int last_breakpoint_id = 0;
700 MiniDebugBreakpointInfo *info;
702 info = g_new0 (MiniDebugBreakpointInfo, 1);
703 info->desc = desc;
704 info->index = ++last_breakpoint_id;
706 if (!breakpoints)
707 breakpoints = g_ptr_array_new ();
709 g_ptr_array_add (breakpoints, info);
711 return info->index;
715 mono_debugger_remove_breakpoint (int breakpoint_id)
717 int i;
719 if (!breakpoints)
720 return 0;
722 for (i = 0; i < breakpoints->len; i++) {
723 MiniDebugBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
725 if (info->index != breakpoint_id)
726 continue;
728 mono_method_desc_free (info->desc);
729 g_ptr_array_remove (breakpoints, info);
730 g_free (info);
731 return 1;
734 return 0;
738 mono_debugger_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
740 MonoMethodDesc *desc;
742 desc = mono_method_desc_new (method_name, include_namespace);
743 if (!desc)
744 return 0;
746 return mono_debugger_insert_breakpoint_full (desc);
750 mono_debugger_method_has_breakpoint (MonoMethod *method)
752 int i;
754 if (!breakpoints || ((method->wrapper_type != MONO_WRAPPER_NONE) &&
755 (method->wrapper_type != MONO_WRAPPER_DYNAMIC_METHOD)))
756 return 0;
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))
762 continue;
764 return info->index;
767 return 0;
770 void
771 mono_debugger_breakpoint_callback (MonoMethod *method, guint32 index)
773 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT, (guint64) (gsize) method, index);
776 void
777 mono_debugger_thread_created (gsize tid, MonoThread *thread, MonoJitTlsData *jit_tls, gpointer func)
779 #ifdef MONO_DEBUGGER_SUPPORTED
780 size_t stsize = 0;
781 guint8 *staddr = NULL;
782 MonoDebuggerThreadInfo *info;
784 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
785 return;
787 mono_debugger_lock ();
789 mono_thread_get_stack_bounds (&staddr, &stsize);
791 info = g_new0 (MonoDebuggerThreadInfo, 1);
792 info->tid = tid;
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 if (func)
803 info->thread_flags = MONO_DEBUGGER_THREAD_FLAGS_INTERNAL;
804 if (thread->internal_thread->threadpool_thread)
805 info->thread_flags |= MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL;
807 info->next = mono_debugger_thread_table;
808 mono_debugger_thread_table = info;
810 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED,
811 tid, (guint64) (gsize) info);
813 mono_debugger_unlock ();
814 #endif /* MONO_DEBUGGER_SUPPORTED */
817 void
818 mono_debugger_thread_cleanup (MonoJitTlsData *jit_tls)
820 #ifdef MONO_DEBUGGER_SUPPORTED
821 MonoDebuggerThreadInfo **ptr;
823 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE)
824 return;
826 mono_debugger_lock ();
828 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
829 MonoDebuggerThreadInfo *info = *ptr;
831 if (info->jit_tls != jit_tls)
832 continue;
834 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP,
835 info->tid, (guint64) (gsize) info);
837 *ptr = info->next;
838 g_free (info);
839 break;
842 mono_debugger_unlock ();
843 #endif
846 void
847 mono_debugger_extended_notification (MonoDebuggerEvent event, guint64 data, guint64 arg)
849 #ifdef MONO_DEBUGGER_SUPPORTED
850 MonoDebuggerThreadInfo **ptr;
851 MonoThread *thread = mono_thread_current ();
853 if (!mono_debug_using_mono_debugger ())
854 return;
856 mono_debugger_lock ();
858 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
859 MonoDebuggerThreadInfo *info = *ptr;
861 if (info->thread != thread)
862 continue;
864 if ((info->extended_notifications & (int) event) == 0)
865 continue;
867 mono_debugger_event (event, data, arg);
870 mono_debugger_unlock ();
871 #endif
874 void
875 mono_debugger_trampoline_compiled (const guint8 *trampoline, MonoMethod *method, const guint8 *code)
877 #ifdef MONO_DEBUGGER_SUPPORTED
878 struct {
879 const guint8 * trampoline;
880 MonoMethod *method;
881 const guint8 *code;
882 } info = { trampoline, method, code };
884 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_OLD_TRAMPOLINE,
885 (guint64) (gsize) method, (guint64) (gsize) code);
886 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE,
887 (guint64) (gsize) &info, 0);
888 #endif
891 #if MONO_DEBUGGER_SUPPORTED
892 static MonoDebuggerThreadInfo *
893 find_debugger_thread_info (MonoThread *thread)
895 MonoDebuggerThreadInfo **ptr;
897 for (ptr = &mono_debugger_thread_table; *ptr; ptr = &(*ptr)->next) {
898 MonoDebuggerThreadInfo *info = *ptr;
900 if (info->thread == thread)
901 return info;
904 return NULL;
906 #endif
908 MonoDebuggerExceptionAction
909 _mono_debugger_throw_exception (gpointer addr, gpointer stack, MonoObject *exc)
911 #ifdef MONO_DEBUGGER_SUPPORTED
912 MonoDebuggerExceptionInfo exc_info;
913 MonoDebuggerThreadInfo *thread_info;
915 if (!mono_debug_using_mono_debugger ())
916 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
918 mono_debugger_lock ();
920 thread_info = find_debugger_thread_info (mono_thread_current ());
921 if (!thread_info) {
922 mono_debugger_unlock ();
923 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
926 if (thread_info->exception_state.stopped_on_exception ||
927 thread_info->exception_state.stopped_on_unhandled) {
928 thread_info->exception_state.stopped_on_exception = 0;
929 mono_debugger_unlock ();
930 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
933 /* Protect the exception object from being garbage collected. */
935 thread_info->exception_state.stopped_on_unhandled = 0;
936 thread_info->exception_state.stopped_on_exception = 1;
937 thread_info->exception_state.last_exception = exc;
940 * Backwards compatibility:
942 * Older debugger versions only know `exc_info.stop' and older runtime versions check
943 * `exc_info.stop != 0'.
945 * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
946 * `stop_unhandled' field.
949 exc_info.stack_pointer = stack;
950 exc_info.exception_obj = exc;
951 exc_info.stop = 0;
952 exc_info.stop_unhandled = 0;
954 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION, (guint64) (gsize) &exc_info,
955 (guint64) (gsize) addr);
957 if (!exc_info.stop) {
958 thread_info->exception_state.stopped_on_exception = 0;
959 thread_info->exception_state.last_exception = NULL;
962 mono_debugger_unlock ();
964 if (exc_info.stop)
965 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP;
966 else if (exc_info.stop_unhandled)
967 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED;
968 #endif
970 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE;
973 gboolean
974 _mono_debugger_unhandled_exception (gpointer addr, gpointer stack, MonoObject *exc)
976 #ifdef MONO_DEBUGGER_SUPPORTED
977 MonoDebuggerThreadInfo *thread_info;
979 if (!mono_debug_using_mono_debugger ())
980 return FALSE;
982 if (exc) {
983 const gchar *name = mono_class_get_name (mono_object_get_class (exc));
984 if (!strcmp (name, "ThreadAbortException"))
985 return FALSE;
988 mono_debugger_lock ();
990 thread_info = find_debugger_thread_info (mono_thread_current ());
991 if (!thread_info) {
992 mono_debugger_unlock ();
993 return FALSE;
996 if (thread_info->exception_state.stopped_on_unhandled) {
997 thread_info->exception_state.stopped_on_unhandled = 0;
998 mono_debugger_unlock ();
999 return FALSE;
1002 thread_info->exception_state.stopped_on_unhandled = 1;
1003 thread_info->exception_state.last_exception = exc;
1005 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION,
1006 (guint64) (gsize) exc, (guint64) (gsize) addr);
1008 return TRUE;
1009 #else
1010 return FALSE;
1011 #endif
1015 * mono_debugger_call_exception_handler:
1017 * Called from mono_handle_exception_internal() to tell the debugger that we're about
1018 * to invoke an exception handler.
1020 * The debugger may choose to set a breakpoint at @addr. This is used if the user is
1021 * single-stepping from a `try' into a `catch' block, for instance.
1024 void
1025 mono_debugger_call_exception_handler (gpointer addr, gpointer stack, MonoObject *exc)
1027 #ifdef MONO_DEBUGGER_SUPPORTED
1028 MonoDebuggerThreadInfo *thread_info;
1029 MonoDebuggerExceptionInfo exc_info;
1031 if (!mono_debug_using_mono_debugger ())
1032 return;
1034 mono_debugger_lock ();
1036 thread_info = find_debugger_thread_info (mono_thread_current ());
1037 if (!thread_info) {
1038 mono_debugger_unlock ();
1039 return;
1042 // Prevent the object from being finalized.
1043 thread_info->exception_state.last_exception = exc;
1045 exc_info.stack_pointer = stack;
1046 exc_info.exception_obj = exc;
1047 exc_info.stop = 0;
1048 exc_info.stop_unhandled = 0;
1050 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION, (guint64) (gsize) &exc_info,
1051 (guint64) (gsize) addr);
1053 mono_debugger_unlock ();
1054 #endif
1057 #ifdef MONO_DEBUGGER_SUPPORTED
1059 static gchar *
1060 get_exception_message (MonoObject *exc)
1062 char *message = NULL;
1063 MonoString *str;
1064 MonoMethod *method;
1065 MonoClass *klass;
1066 gint i;
1068 if (mono_object_isinst (exc, mono_defaults.exception_class)) {
1069 klass = exc->vtable->klass;
1070 method = NULL;
1071 while (klass && method == NULL) {
1072 for (i = 0; i < klass->method.count; ++i) {
1073 method = klass->methods [i];
1074 if (!strcmp ("ToString", method->name) &&
1075 mono_method_signature (method)->param_count == 0 &&
1076 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1077 method->flags & METHOD_ATTRIBUTE_PUBLIC) {
1078 break;
1080 method = NULL;
1083 if (method == NULL)
1084 klass = klass->parent;
1087 g_assert (method);
1089 str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
1090 if (str)
1091 message = mono_string_to_utf8 (str);
1094 return message;
1097 MonoObject *
1098 mono_debugger_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1100 MonoDebuggerThreadInfo *thread_info;
1101 MonoDebuggerExceptionState saved_exception_state;
1102 MonoObject *retval;
1103 gchar *message;
1105 mono_debugger_lock ();
1107 thread_info = find_debugger_thread_info (mono_thread_current ());
1108 if (!thread_info) {
1109 mono_debugger_unlock ();
1110 return NULL;
1113 saved_exception_state = thread_info->exception_state;
1115 thread_info->exception_state.last_exception = NULL;
1116 thread_info->exception_state.stopped_on_unhandled = 0;
1117 thread_info->exception_state.stopped_on_exception = 0;
1119 mono_debugger_unlock ();
1121 if (!strcmp (method->name, ".ctor")) {
1122 retval = obj = mono_object_new (mono_domain_get (), method->klass);
1124 mono_runtime_invoke (method, obj, params, exc);
1125 } else
1126 retval = mono_runtime_invoke (method, obj, params, exc);
1128 mono_debugger_lock ();
1130 thread_info = find_debugger_thread_info (mono_thread_current ());
1131 if (thread_info)
1132 thread_info->exception_state = saved_exception_state;
1134 mono_debugger_unlock ();
1136 if (!exc || (*exc == NULL))
1137 return retval;
1139 retval = *exc;
1140 message = get_exception_message (*exc);
1141 if (message) {
1142 *exc = (MonoObject *) mono_string_new_wrapper (message);
1143 g_free (message);
1146 return retval;
1149 #endif