2 * debug-mini.c: Mini-specific debugging stuff.
5 * Martin Baulig (martin@ximian.com)
7 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/verify.h>
14 #include <mono/metadata/mono-config.h>
15 #include <mono/metadata/mono-debug.h>
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/threads-types.h>
19 #define _IN_THE_MONO_DEBUGGER
20 #include <mono/metadata/mono-debug-debugger.h>
21 #include "debug-mini.h"
23 #include <mono/utils/valgrind.h>
25 #ifdef MONO_DEBUGGER_SUPPORTED
26 #include <libgc/include/libgc-mono-debugger.h>
32 } MiniDebugBreakpointInfo
;
36 MonoDebugMethodJitInfo
*jit
;
38 guint32 has_line_numbers
;
39 guint32 breakpoint_id
;
40 } MiniDebugMethodInfo
;
43 MonoObject
*last_exception
;
44 guint32 stopped_on_exception
: 1;
45 guint32 stopped_on_unhandled
: 1;
46 } MonoDebuggerExceptionState
;
49 MONO_DEBUGGER_THREAD_FLAGS_NONE
= 0,
50 MONO_DEBUGGER_THREAD_FLAGS_INTERNAL
= 1,
51 MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL
= 2
52 } MonoDebuggerThreadFlags
;
55 MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_NONE
= 0,
56 MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE
= 1,
57 MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
= 2
58 } MonoDebuggerInternalThreadFlags
;
60 struct _MonoDebuggerThreadInfo
{
65 guint64 extended_notifications
;
68 MonoDebuggerThreadInfo
*next
;
71 * The stack bounds are only used when reading a core file.
74 guint64 signal_stack_start
;
76 guint32 signal_stack_size
;
81 * The debugger doesn't access anything beyond this point.
83 MonoDebuggerExceptionState exception_state
;
85 guint32 internal_flags
;
87 MonoJitTlsData
*jit_tls
;
88 MonoInternalThread
*thread
;
92 gpointer stack_pointer
;
93 MonoObject
*exception_obj
;
95 guint32 stop_unhandled
;
96 } MonoDebuggerExceptionInfo
;
98 MonoDebuggerThreadInfo
*mono_debugger_thread_table
= NULL
;
101 record_line_number (MiniDebugMethodInfo
*info
, guint32 address
, guint32 offset
)
103 MonoDebugLineNumberEntry lne
;
105 lne
.native_offset
= address
;
106 lne
.il_offset
= offset
;
108 g_array_append_val (info
->line_numbers
, lne
);
113 mono_debug_init_method (MonoCompile
*cfg
, MonoBasicBlock
*start_block
, guint32 breakpoint_id
)
115 MiniDebugMethodInfo
*info
;
117 if (mono_debug_format
== MONO_DEBUG_FORMAT_NONE
)
120 info
= g_new0 (MiniDebugMethodInfo
, 1);
121 info
->breakpoint_id
= breakpoint_id
;
123 cfg
->debug_info
= info
;
127 mono_debug_open_method (MonoCompile
*cfg
)
129 MiniDebugMethodInfo
*info
;
130 MonoDebugMethodJitInfo
*jit
;
131 MonoMethodHeader
*header
;
133 info
= (MiniDebugMethodInfo
*) cfg
->debug_info
;
137 mono_class_init (cfg
->method
->klass
);
139 header
= cfg
->header
;
142 info
->jit
= jit
= g_new0 (MonoDebugMethodJitInfo
, 1);
143 info
->line_numbers
= g_array_new (FALSE
, TRUE
, sizeof (MonoDebugLineNumberEntry
));
144 jit
->num_locals
= header
->num_locals
;
145 jit
->locals
= g_new0 (MonoDebugVarInfo
, jit
->num_locals
);
149 write_variable (MonoInst
*inst
, MonoDebugVarInfo
*var
)
151 var
->type
= inst
->inst_vtype
;
153 if (inst
->opcode
== OP_REGVAR
)
154 var
->index
= inst
->dreg
| MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
;
155 else if (inst
->flags
& MONO_INST_IS_DEAD
)
156 var
->index
= MONO_DEBUG_VAR_ADDRESS_MODE_DEAD
;
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
170 mono_debug_add_vg_method (MonoMethod
*method
, MonoDebugMethodJitInfo
*jit
)
172 #ifdef VALGRIND_ADD_LINE_INFO
173 MonoMethodHeader
*header
;
174 MonoDebugMethodInfo
*minfo
;
176 char *filename
= NULL
;
177 guint32 address
, line_number
;
178 const char *full_name
;
182 if (!RUNNING_ON_VALGRIND
)
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
);
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
);
207 lines
[i
] = location
.row
;
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 */
226 for (i
= 0; i
< header
->code_size
; ++i
) {
227 if (addresses
[i
] == 0)
228 addresses
[i
] = address
;
230 address
= addresses
[i
];
236 while (i
< header
->code_size
) {
237 if (lines
[i
] == line_number
)
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
);
262 mono_metadata_free_mh (header
);
263 #endif /* VALGRIND_ADD_LINE_INFO */
267 mono_debug_close_method (MonoCompile
*cfg
)
269 MiniDebugMethodInfo
*info
;
270 MonoDebugMethodJitInfo
*jit
;
271 MonoMethodHeader
*header
;
272 MonoMethodSignature
*sig
;
273 MonoDebugMethodAddress
*debug_info
;
277 info
= (MiniDebugMethodInfo
*) cfg
->debug_info
;
278 if (!info
|| !info
->jit
) {
284 method
= cfg
->method
;
285 header
= cfg
->header
;
286 sig
= mono_method_signature (method
);
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
]);
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 g_array_free (info
->line_numbers
, TRUE
);
328 mono_debug_record_line_number (MonoCompile
*cfg
, MonoInst
*ins
, guint32 address
)
330 MiniDebugMethodInfo
*info
;
331 MonoMethodHeader
*header
;
334 info
= (MiniDebugMethodInfo
*) cfg
->debug_info
;
335 if (!info
|| !info
->jit
|| !ins
->cil_code
)
338 header
= cfg
->header
;
341 if ((ins
->cil_code
< header
->code
) ||
342 (ins
->cil_code
> header
->code
+ header
->code_size
))
345 offset
= ins
->cil_code
- header
->code
;
346 if (!info
->has_line_numbers
) {
347 info
->jit
->prologue_end
= address
;
348 info
->has_line_numbers
= TRUE
;
351 record_line_number (info
, address
, offset
);
355 mono_debug_open_block (MonoCompile
*cfg
, MonoBasicBlock
*bb
, guint32 address
)
357 MiniDebugMethodInfo
*info
;
358 MonoMethodHeader
*header
;
361 info
= (MiniDebugMethodInfo
*) cfg
->debug_info
;
362 if (!info
|| !info
->jit
|| !bb
->cil_code
)
365 header
= cfg
->header
;
368 if ((bb
->cil_code
< header
->code
) ||
369 (bb
->cil_code
> header
->code
+ header
->code_size
))
372 offset
= bb
->cil_code
- header
->code
;
373 if (!info
->has_line_numbers
) {
374 info
->jit
->prologue_end
= address
;
375 info
->has_line_numbers
= TRUE
;
378 record_line_number (info
, address
, offset
);
382 encode_value (gint32 value
, guint8
*buf
, guint8
**endbuf
)
386 //printf ("ENCODE: %d 0x%x.\n", value, value);
389 * Same encoding as the one used in the metadata, extended to handle values
390 * greater than 0x1fffffff.
392 if ((value
>= 0) && (value
<= 127))
394 else if ((value
>= 0) && (value
<= 16383)) {
395 p
[0] = 0x80 | (value
>> 8);
396 p
[1] = value
& 0xff;
398 } else if ((value
>= 0) && (value
<= 0x1fffffff)) {
399 p
[0] = (value
>> 24) | 0xc0;
400 p
[1] = (value
>> 16) & 0xff;
401 p
[2] = (value
>> 8) & 0xff;
402 p
[3] = value
& 0xff;
407 p
[1] = (value
>> 24) & 0xff;
408 p
[2] = (value
>> 16) & 0xff;
409 p
[3] = (value
>> 8) & 0xff;
410 p
[4] = value
& 0xff;
418 decode_value (guint8
*ptr
, guint8
**rptr
)
423 if ((b
& 0x80) == 0){
426 } else if ((b
& 0x40) == 0){
427 len
= ((b
& 0x3f) << 8 | ptr
[1]);
429 } else if (b
!= 0xff) {
430 len
= ((b
& 0x1f) << 24) |
437 len
= (ptr
[1] << 24) | (ptr
[2] << 16) | (ptr
[3] << 8) | ptr
[4];
443 //printf ("DECODE: %d.\n", len);
448 serialize_variable (MonoDebugVarInfo
*var
, guint8
*p
, guint8
**endbuf
)
450 guint32 flags
= var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
452 encode_value (var
->index
, p
, &p
);
455 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
:
457 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
:
458 encode_value (var
->offset
, p
, &p
);
460 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD
:
463 g_assert_not_reached ();
469 mono_debug_serialize_debug_info (MonoCompile
*cfg
, guint8
**out_buf
, guint32
*buf_len
)
471 MonoDebugMethodJitInfo
*jit
;
472 guint32 size
, prev_offset
, prev_native_offset
;
476 /* Can't use cfg->debug_info as it is freed by close_method () */
477 jit
= mono_debug_find_method (cfg
->method
, mono_domain_get ());
483 size
= ((jit
->num_params
+ jit
->num_locals
+ 1) * 10) + (jit
->num_line_numbers
* 10) + 64;
484 p
= buf
= g_malloc (size
);
485 encode_value (jit
->epilogue_begin
, p
, &p
);
486 encode_value (jit
->prologue_end
, p
, &p
);
487 encode_value (jit
->code_size
, p
, &p
);
489 for (i
= 0; i
< jit
->num_params
; ++i
)
490 serialize_variable (&jit
->params
[i
], p
, &p
);
492 if (mono_method_signature (cfg
->method
)->hasthis
)
493 serialize_variable (jit
->this_var
, p
, &p
);
495 for (i
= 0; i
< jit
->num_locals
; i
++)
496 serialize_variable (&jit
->locals
[i
], p
, &p
);
498 encode_value (jit
->num_line_numbers
, p
, &p
);
501 prev_native_offset
= 0;
502 for (i
= 0; i
< jit
->num_line_numbers
; ++i
) {
503 /* Sometimes, the offset values are not in increasing order */
504 MonoDebugLineNumberEntry
*lne
= &jit
->line_numbers
[i
];
505 encode_value (lne
->il_offset
- prev_offset
, p
, &p
);
506 encode_value (lne
->native_offset
- prev_native_offset
, p
, &p
);
507 prev_offset
= lne
->il_offset
;
508 prev_native_offset
= lne
->native_offset
;
511 g_assert (p
- buf
< size
);
518 deserialize_variable (MonoDebugVarInfo
*var
, guint8
*p
, guint8
**endbuf
)
522 var
->index
= decode_value (p
, &p
);
524 flags
= var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
527 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
:
529 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
:
530 var
->offset
= decode_value (p
, &p
);
532 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD
:
535 g_assert_not_reached ();
540 static MonoDebugMethodJitInfo
*
541 deserialize_debug_info (MonoMethod
*method
, guint8
*code_start
, guint8
*buf
, guint32 buf_len
)
543 MonoMethodHeader
*header
;
544 gint32 offset
, native_offset
, prev_offset
, prev_native_offset
;
545 MonoDebugMethodJitInfo
*jit
;
549 header
= mono_method_get_header (method
);
552 jit
= g_new0 (MonoDebugMethodJitInfo
, 1);
553 jit
->code_start
= code_start
;
554 jit
->num_locals
= header
->num_locals
;
555 jit
->locals
= g_new0 (MonoDebugVarInfo
, jit
->num_locals
);
556 jit
->num_params
= mono_method_signature (method
)->param_count
;
557 jit
->params
= g_new0 (MonoDebugVarInfo
, jit
->num_params
);
560 jit
->epilogue_begin
= decode_value (p
, &p
);
561 jit
->prologue_end
= decode_value (p
, &p
);
562 jit
->code_size
= decode_value (p
, &p
);
564 for (i
= 0; i
< jit
->num_params
; ++i
)
565 deserialize_variable (&jit
->params
[i
], p
, &p
);
567 if (mono_method_signature (method
)->hasthis
) {
568 jit
->this_var
= g_new0 (MonoDebugVarInfo
, 1);
569 deserialize_variable (jit
->this_var
, p
, &p
);
572 for (i
= 0; i
< jit
->num_locals
; i
++)
573 deserialize_variable (&jit
->locals
[i
], p
, &p
);
575 jit
->num_line_numbers
= decode_value (p
, &p
);
576 jit
->line_numbers
= g_new0 (MonoDebugLineNumberEntry
, jit
->num_line_numbers
);
579 prev_native_offset
= 0;
580 for (i
= 0; i
< jit
->num_line_numbers
; ++i
) {
581 MonoDebugLineNumberEntry
*lne
= &jit
->line_numbers
[i
];
583 offset
= prev_offset
+ decode_value (p
, &p
);
584 native_offset
= prev_native_offset
+ decode_value (p
, &p
);
586 lne
->native_offset
= native_offset
;
587 lne
->il_offset
= offset
;
589 prev_offset
= offset
;
590 prev_native_offset
= native_offset
;
593 mono_metadata_free_mh (header
);
598 mono_debug_add_aot_method (MonoDomain
*domain
, MonoMethod
*method
, guint8
*code_start
,
599 guint8
*debug_info
, guint32 debug_info_len
)
601 MonoDebugMethodJitInfo
*jit
;
603 if (mono_debug_format
== MONO_DEBUG_FORMAT_NONE
)
606 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
607 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
608 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
609 (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
) ||
610 (method
->wrapper_type
!= MONO_WRAPPER_NONE
))
613 if (debug_info_len
== 0)
616 jit
= deserialize_debug_info (method
, code_start
, debug_info
, debug_info_len
);
618 mono_debug_add_method (method
, jit
, domain
);
620 mono_debug_add_vg_method (method
, jit
);
622 mono_debug_free_method_jit_info (jit
);
626 mono_debug_add_icall_wrapper (MonoMethod
*method
, MonoJitICallInfo
* callinfo
)
628 if (mono_debug_format
== MONO_DEBUG_FORMAT_NONE
)
631 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
635 print_var_info (MonoDebugVarInfo
*info
, int idx
, const char *name
, const char *type
)
637 switch (info
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
) {
638 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
:
639 g_print ("%s %s (%d) in register %s\n", type
, name
, idx
, mono_arch_regname (info
->index
& (~MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
)));
641 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
:
642 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
);
644 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS
:
646 g_assert_not_reached ();
651 * mono_debug_print_locals:
653 * Prints to stdout the information about the local variables in
654 * a method (if @only_arguments is false) or about the arguments.
655 * The information includes the storage info (where the variable
656 * lives, in a register or in memory).
657 * The method is found by looking up what method has been emitted at
658 * the instruction address @ip.
659 * This is for use inside a debugger.
662 mono_debug_print_vars (gpointer ip
, gboolean only_arguments
)
664 MonoDomain
*domain
= mono_domain_get ();
665 MonoJitInfo
*ji
= mono_jit_info_table_find (domain
, ip
);
666 MonoDebugMethodJitInfo
*jit
;
672 jit
= mono_debug_find_method (mono_jit_info_get_method (ji
), domain
);
676 if (only_arguments
) {
678 names
= g_new (char *, jit
->num_params
);
679 mono_method_get_param_names (mono_jit_info_get_method (ji
), (const char **) names
);
681 print_var_info (jit
->this_var
, 0, "this", "Arg");
682 for (i
= 0; i
< jit
->num_params
; ++i
) {
683 print_var_info (&jit
->params
[i
], i
, names
[i
]? names
[i
]: "unknown name", "Arg");
687 for (i
= 0; i
< jit
->num_locals
; ++i
) {
688 print_var_info (&jit
->locals
[i
], i
, "", "Local");
691 mono_debug_free_method_jit_info (jit
);
695 * The old Debugger breakpoint interface.
697 * This interface is used to insert breakpoints on methods which are not yet JITed.
698 * The debugging code keeps a list of all such breakpoints and automatically inserts the
699 * breakpoint when the method is JITed.
702 static GPtrArray
*breakpoints
= NULL
;
705 mono_debugger_insert_breakpoint_full (MonoMethodDesc
*desc
)
707 static int last_breakpoint_id
= 0;
708 MiniDebugBreakpointInfo
*info
;
710 info
= g_new0 (MiniDebugBreakpointInfo
, 1);
712 info
->index
= ++last_breakpoint_id
;
715 breakpoints
= g_ptr_array_new ();
717 g_ptr_array_add (breakpoints
, info
);
723 mono_debugger_remove_breakpoint (int breakpoint_id
)
730 for (i
= 0; i
< breakpoints
->len
; i
++) {
731 MiniDebugBreakpointInfo
*info
= g_ptr_array_index (breakpoints
, i
);
733 if (info
->index
!= breakpoint_id
)
736 mono_method_desc_free (info
->desc
);
737 g_ptr_array_remove (breakpoints
, info
);
746 mono_debugger_insert_breakpoint (const gchar
*method_name
, gboolean include_namespace
)
748 MonoMethodDesc
*desc
;
750 desc
= mono_method_desc_new (method_name
, include_namespace
);
754 return mono_debugger_insert_breakpoint_full (desc
);
758 mono_debugger_method_has_breakpoint (MonoMethod
*method
)
762 if (!breakpoints
|| ((method
->wrapper_type
!= MONO_WRAPPER_NONE
) &&
763 (method
->wrapper_type
!= MONO_WRAPPER_DYNAMIC_METHOD
)))
766 for (i
= 0; i
< breakpoints
->len
; i
++) {
767 MiniDebugBreakpointInfo
*info
= g_ptr_array_index (breakpoints
, i
);
769 if (!mono_method_desc_full_match (info
->desc
, method
))
779 mono_debugger_breakpoint_callback (MonoMethod
*method
, guint32 index
)
781 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT
, (guint64
) (gsize
) method
, index
);
785 mono_debugger_thread_created (gsize tid
, MonoThread
*thread
, MonoJitTlsData
*jit_tls
, gpointer func
)
787 #ifdef MONO_DEBUGGER_SUPPORTED
789 guint8
*staddr
= NULL
;
790 MonoDebuggerThreadInfo
*info
;
792 if (mono_debug_format
== MONO_DEBUG_FORMAT_NONE
)
795 mono_debugger_lock ();
797 mono_thread_get_stack_bounds (&staddr
, &stsize
);
799 info
= g_new0 (MonoDebuggerThreadInfo
, 1);
801 info
->thread
= thread
->internal_thread
;
802 info
->stack_start
= (guint64
) (gsize
) staddr
;
803 info
->signal_stack_start
= (guint64
) (gsize
) jit_tls
->signal_stack
;
804 info
->stack_size
= stsize
;
805 info
->signal_stack_size
= jit_tls
->signal_stack_size
;
806 info
->end_stack
= (guint64
) (gsize
) GC_mono_debugger_get_stack_ptr ();
807 info
->lmf_addr
= (guint64
) (gsize
) mono_get_lmf_addr ();
808 info
->jit_tls
= jit_tls
;
811 info
->thread_flags
= MONO_DEBUGGER_THREAD_FLAGS_INTERNAL
;
812 if (thread
->internal_thread
->threadpool_thread
)
813 info
->thread_flags
|= MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL
;
815 info
->next
= mono_debugger_thread_table
;
816 mono_debugger_thread_table
= info
;
818 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED
,
819 tid
, (guint64
) (gsize
) info
);
821 mono_debugger_unlock ();
822 #endif /* MONO_DEBUGGER_SUPPORTED */
826 mono_debugger_thread_cleanup (MonoJitTlsData
*jit_tls
)
828 #ifdef MONO_DEBUGGER_SUPPORTED
829 MonoDebuggerThreadInfo
**ptr
;
831 if (mono_debug_format
== MONO_DEBUG_FORMAT_NONE
)
834 mono_debugger_lock ();
836 for (ptr
= &mono_debugger_thread_table
; *ptr
; ptr
= &(*ptr
)->next
) {
837 MonoDebuggerThreadInfo
*info
= *ptr
;
839 if (info
->jit_tls
!= jit_tls
)
842 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP
,
843 info
->tid
, (guint64
) (gsize
) info
);
850 mono_debugger_unlock ();
855 mono_debugger_extended_notification (MonoDebuggerEvent event
, guint64 data
, guint64 arg
)
857 #ifdef MONO_DEBUGGER_SUPPORTED
858 MonoDebuggerThreadInfo
**ptr
;
859 MonoInternalThread
*thread
= mono_thread_internal_current ();
861 if (!mono_debug_using_mono_debugger ())
864 mono_debugger_lock ();
866 for (ptr
= &mono_debugger_thread_table
; *ptr
; ptr
= &(*ptr
)->next
) {
867 MonoDebuggerThreadInfo
*info
= *ptr
;
869 if (info
->thread
!= thread
)
872 if ((info
->extended_notifications
& (int) event
) == 0)
875 mono_debugger_event (event
, data
, arg
);
878 mono_debugger_unlock ();
883 mono_debugger_trampoline_compiled (const guint8
*trampoline
, MonoMethod
*method
, const guint8
*code
)
885 #ifdef MONO_DEBUGGER_SUPPORTED
887 const guint8
* trampoline
;
890 } info
= { trampoline
, method
, code
};
892 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_OLD_TRAMPOLINE
,
893 (guint64
) (gsize
) method
, (guint64
) (gsize
) code
);
894 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE
,
895 (guint64
) (gsize
) &info
, 0);
899 #if MONO_DEBUGGER_SUPPORTED
900 static MonoDebuggerThreadInfo
*
901 find_debugger_thread_info (MonoInternalThread
*thread
)
903 MonoDebuggerThreadInfo
**ptr
;
905 for (ptr
= &mono_debugger_thread_table
; *ptr
; ptr
= &(*ptr
)->next
) {
906 MonoDebuggerThreadInfo
*info
= *ptr
;
908 if (info
->thread
== thread
)
916 MonoDebuggerExceptionAction
917 _mono_debugger_throw_exception (gpointer addr
, gpointer stack
, MonoObject
*exc
)
919 #ifdef MONO_DEBUGGER_SUPPORTED
920 MonoDebuggerExceptionInfo exc_info
;
921 MonoDebuggerThreadInfo
*thread_info
;
923 if (!mono_debug_using_mono_debugger ())
924 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
926 mono_debugger_lock ();
928 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
930 mono_debugger_unlock ();
931 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
934 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
935 mono_debugger_unlock ();
936 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
939 if (thread_info
->exception_state
.stopped_on_exception
||
940 thread_info
->exception_state
.stopped_on_unhandled
) {
941 thread_info
->exception_state
.stopped_on_exception
= 0;
942 mono_debugger_unlock ();
943 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
946 /* Protect the exception object from being garbage collected. */
948 thread_info
->exception_state
.stopped_on_unhandled
= 0;
949 thread_info
->exception_state
.stopped_on_exception
= 1;
950 thread_info
->exception_state
.last_exception
= exc
;
953 * Backwards compatibility:
955 * Older debugger versions only know `exc_info.stop' and older runtime versions check
956 * `exc_info.stop != 0'.
958 * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
959 * `stop_unhandled' field.
962 exc_info
.stack_pointer
= stack
;
963 exc_info
.exception_obj
= exc
;
965 exc_info
.stop_unhandled
= 0;
967 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION
, (guint64
) (gsize
) &exc_info
,
968 (guint64
) (gsize
) addr
);
970 if (!exc_info
.stop
) {
971 thread_info
->exception_state
.stopped_on_exception
= 0;
972 thread_info
->exception_state
.last_exception
= NULL
;
975 mono_debugger_unlock ();
978 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP
;
979 else if (exc_info
.stop_unhandled
)
980 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED
;
983 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
987 _mono_debugger_unhandled_exception (gpointer addr
, gpointer stack
, MonoObject
*exc
)
989 #ifdef MONO_DEBUGGER_SUPPORTED
990 MonoDebuggerThreadInfo
*thread_info
;
992 if (!mono_debug_using_mono_debugger ())
996 const gchar
*name
= mono_class_get_name (mono_object_get_class (exc
));
997 if (!strcmp (name
, "ThreadAbortException"))
1001 mono_debugger_lock ();
1003 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
1005 mono_debugger_unlock ();
1009 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1010 mono_debugger_unlock ();
1014 if (thread_info
->exception_state
.stopped_on_unhandled
) {
1015 thread_info
->exception_state
.stopped_on_unhandled
= 0;
1016 mono_debugger_unlock ();
1020 thread_info
->exception_state
.stopped_on_unhandled
= 1;
1021 thread_info
->exception_state
.last_exception
= exc
;
1023 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION
,
1024 (guint64
) (gsize
) exc
, (guint64
) (gsize
) addr
);
1033 * mono_debugger_call_exception_handler:
1035 * Called from mono_handle_exception_internal() to tell the debugger that we're about
1036 * to invoke an exception handler.
1038 * The debugger may choose to set a breakpoint at @addr. This is used if the user is
1039 * single-stepping from a `try' into a `catch' block, for instance.
1043 mono_debugger_call_exception_handler (gpointer addr
, gpointer stack
, MonoObject
*exc
)
1045 #ifdef MONO_DEBUGGER_SUPPORTED
1046 MonoDebuggerThreadInfo
*thread_info
;
1047 MonoDebuggerExceptionInfo exc_info
;
1049 if (!mono_debug_using_mono_debugger ())
1052 mono_debugger_lock ();
1054 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
1056 mono_debugger_unlock ();
1060 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1061 mono_debugger_unlock ();
1065 // Prevent the object from being finalized.
1066 thread_info
->exception_state
.last_exception
= exc
;
1068 exc_info
.stack_pointer
= stack
;
1069 exc_info
.exception_obj
= exc
;
1071 exc_info
.stop_unhandled
= 0;
1073 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION
, (guint64
) (gsize
) &exc_info
,
1074 (guint64
) (gsize
) addr
);
1076 mono_debugger_unlock ();
1080 #ifdef MONO_DEBUGGER_SUPPORTED
1083 get_exception_message (MonoObject
*exc
)
1085 char *message
= NULL
;
1091 if (mono_object_isinst (exc
, mono_defaults
.exception_class
)) {
1092 klass
= exc
->vtable
->klass
;
1094 while (klass
&& method
== NULL
) {
1095 for (i
= 0; i
< klass
->method
.count
; ++i
) {
1096 method
= klass
->methods
[i
];
1097 if (!strcmp ("ToString", method
->name
) &&
1098 mono_method_signature (method
)->param_count
== 0 &&
1099 method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
&&
1100 method
->flags
& METHOD_ATTRIBUTE_PUBLIC
) {
1107 klass
= klass
->parent
;
1112 str
= (MonoString
*) mono_runtime_invoke (method
, exc
, NULL
, NULL
);
1114 message
= mono_string_to_utf8 (str
);
1121 mono_debugger_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
)
1123 MonoDebuggerThreadInfo
*thread_info
;
1124 MonoDebuggerExceptionState saved_exception_state
;
1128 mono_debugger_lock ();
1130 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
1132 mono_debugger_unlock ();
1136 saved_exception_state
= thread_info
->exception_state
;
1138 thread_info
->exception_state
.last_exception
= NULL
;
1139 thread_info
->exception_state
.stopped_on_unhandled
= 0;
1140 thread_info
->exception_state
.stopped_on_exception
= 0;
1142 thread_info
->internal_flags
|= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE
;
1144 mono_debugger_unlock ();
1146 if (!strcmp (method
->name
, ".ctor")) {
1147 retval
= obj
= mono_object_new (mono_domain_get (), method
->klass
);
1149 mono_runtime_invoke (method
, obj
, params
, exc
);
1151 retval
= mono_runtime_invoke (method
, obj
, params
, exc
);
1153 mono_debugger_lock ();
1155 thread_info
->exception_state
= saved_exception_state
;
1156 thread_info
->internal_flags
&= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE
;
1158 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1159 thread_info
->internal_flags
&= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
;
1160 mono_thread_internal_reset_abort (thread_info
->thread
);
1162 mono_debugger_unlock ();
1168 mono_debugger_unlock ();
1170 if (!exc
|| (*exc
== NULL
))
1174 message
= get_exception_message (*exc
);
1176 *exc
= (MonoObject
*) mono_string_new_wrapper (message
);
1184 mono_debugger_abort_runtime_invoke ()
1186 MonoInternalThread
*thread
= mono_thread_internal_current ();
1187 MonoDebuggerThreadInfo
*thread_info
;
1189 mono_debugger_lock ();
1191 thread_info
= find_debugger_thread_info (thread
);
1193 mono_debugger_unlock ();
1197 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE
) == 0) {
1198 mono_debugger_unlock ();
1202 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1203 mono_debugger_unlock ();
1207 thread_info
->internal_flags
|= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
;
1208 ves_icall_System_Threading_Thread_Abort (thread_info
->thread
, NULL
);
1210 mono_debugger_unlock ();