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 mono_debug_free_method (cfg
);
327 mono_debug_free_method (MonoCompile
*cfg
)
329 MiniDebugMethodInfo
*info
;
331 info
= (MiniDebugMethodInfo
*) cfg
->debug_info
;
333 if (info
->line_numbers
)
334 g_array_free (info
->line_numbers
, TRUE
);
336 cfg
->debug_info
= NULL
;
341 mono_debug_record_line_number (MonoCompile
*cfg
, MonoInst
*ins
, guint32 address
)
343 MiniDebugMethodInfo
*info
;
344 MonoMethodHeader
*header
;
347 info
= (MiniDebugMethodInfo
*) cfg
->debug_info
;
348 if (!info
|| !info
->jit
|| !ins
->cil_code
)
351 header
= cfg
->header
;
354 if ((ins
->cil_code
< header
->code
) ||
355 (ins
->cil_code
> header
->code
+ header
->code_size
))
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
);
368 mono_debug_open_block (MonoCompile
*cfg
, MonoBasicBlock
*bb
, guint32 address
)
370 MiniDebugMethodInfo
*info
;
371 MonoMethodHeader
*header
;
374 info
= (MiniDebugMethodInfo
*) cfg
->debug_info
;
375 if (!info
|| !info
->jit
|| !bb
->cil_code
)
378 header
= cfg
->header
;
381 if ((bb
->cil_code
< header
->code
) ||
382 (bb
->cil_code
> header
->code
+ header
->code_size
))
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
);
395 encode_value (gint32 value
, guint8
*buf
, guint8
**endbuf
)
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))
407 else if ((value
>= 0) && (value
<= 16383)) {
408 p
[0] = 0x80 | (value
>> 8);
409 p
[1] = value
& 0xff;
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;
420 p
[1] = (value
>> 24) & 0xff;
421 p
[2] = (value
>> 16) & 0xff;
422 p
[3] = (value
>> 8) & 0xff;
423 p
[4] = value
& 0xff;
431 decode_value (guint8
*ptr
, guint8
**rptr
)
436 if ((b
& 0x80) == 0){
439 } else if ((b
& 0x40) == 0){
440 len
= ((b
& 0x3f) << 8 | ptr
[1]);
442 } else if (b
!= 0xff) {
443 len
= ((b
& 0x1f) << 24) |
450 len
= (ptr
[1] << 24) | (ptr
[2] << 16) | (ptr
[3] << 8) | ptr
[4];
456 //printf ("DECODE: %d.\n", len);
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
);
468 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
:
470 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
:
471 encode_value (var
->offset
, p
, &p
);
473 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD
:
476 g_assert_not_reached ();
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
;
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 ());
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
);
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
);
531 deserialize_variable (MonoDebugVarInfo
*var
, guint8
*p
, guint8
**endbuf
)
535 var
->index
= decode_value (p
, &p
);
537 flags
= var
->index
& MONO_DEBUG_VAR_ADDRESS_MODE_FLAGS
;
540 case MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER
:
542 case MONO_DEBUG_VAR_ADDRESS_MODE_REGOFFSET
:
543 var
->offset
= decode_value (p
, &p
);
545 case MONO_DEBUG_VAR_ADDRESS_MODE_DEAD
:
548 g_assert_not_reached ();
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
;
562 header
= mono_method_get_header (method
);
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
);
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
);
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
);
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
)
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
))
626 if (debug_info_len
== 0)
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
);
639 mono_debug_add_icall_wrapper (MonoMethod
*method
, MonoJitICallInfo
* callinfo
)
641 if (mono_debug_format
== MONO_DEBUG_FORMAT_NONE
)
644 // mono_debug_add_wrapper (method, callinfo->wrapper, callinfo->func);
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
)));
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
);
657 case MONO_DEBUG_VAR_ADDRESS_MODE_TWO_REGISTERS
:
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.
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
;
685 jit
= mono_debug_find_method (mono_jit_info_get_method (ji
), domain
);
689 if (only_arguments
) {
691 names
= g_new (char *, jit
->num_params
);
692 mono_method_get_param_names (mono_jit_info_get_method (ji
), (const char **) names
);
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");
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);
725 info
->index
= ++last_breakpoint_id
;
728 breakpoints
= g_ptr_array_new ();
730 g_ptr_array_add (breakpoints
, info
);
736 mono_debugger_remove_breakpoint (int breakpoint_id
)
743 for (i
= 0; i
< breakpoints
->len
; i
++) {
744 MiniDebugBreakpointInfo
*info
= g_ptr_array_index (breakpoints
, i
);
746 if (info
->index
!= breakpoint_id
)
749 mono_method_desc_free (info
->desc
);
750 g_ptr_array_remove (breakpoints
, info
);
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
);
767 return mono_debugger_insert_breakpoint_full (desc
);
771 mono_debugger_method_has_breakpoint (MonoMethod
*method
)
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
))
791 mono_debugger_breakpoint_callback (MonoMethod
*method
, guint32 index
)
793 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT
, (guint64
) (gsize
) method
, index
);
797 mono_debugger_thread_created (gsize tid
, MonoThread
*thread
, MonoJitTlsData
*jit_tls
, gpointer func
)
799 #ifdef MONO_DEBUGGER_SUPPORTED
801 guint8
*staddr
= NULL
;
802 MonoDebuggerThreadInfo
*info
;
804 if (mono_debug_format
== MONO_DEBUG_FORMAT_NONE
)
807 mono_debugger_lock ();
809 mono_thread_get_stack_bounds (&staddr
, &stsize
);
811 info
= g_new0 (MonoDebuggerThreadInfo
, 1);
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
;
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 */
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
)
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
)
854 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP
,
855 info
->tid
, (guint64
) (gsize
) info
);
862 mono_debugger_unlock ();
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 ())
876 mono_debugger_lock ();
878 for (ptr
= &mono_debugger_thread_table
; *ptr
; ptr
= &(*ptr
)->next
) {
879 MonoDebuggerThreadInfo
*info
= *ptr
;
881 if (info
->thread
!= thread
)
884 if ((info
->extended_notifications
& (int) event
) == 0)
887 mono_debugger_event (event
, data
, arg
);
890 mono_debugger_unlock ();
895 mono_debugger_trampoline_compiled (const guint8
*trampoline
, MonoMethod
*method
, const guint8
*code
)
897 #ifdef MONO_DEBUGGER_SUPPORTED
899 const guint8
* trampoline
;
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);
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
)
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 ());
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
;
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 ();
990 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP
;
991 else if (exc_info
.stop_unhandled
)
992 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED
;
995 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
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 ())
1008 const gchar
*name
= mono_class_get_name (mono_object_get_class (exc
));
1009 if (!strcmp (name
, "ThreadAbortException"))
1013 mono_debugger_lock ();
1015 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
1017 mono_debugger_unlock ();
1021 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1022 mono_debugger_unlock ();
1026 if (thread_info
->exception_state
.stopped_on_unhandled
) {
1027 thread_info
->exception_state
.stopped_on_unhandled
= 0;
1028 mono_debugger_unlock ();
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
);
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.
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 ())
1064 mono_debugger_lock ();
1066 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
1068 mono_debugger_unlock ();
1072 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1073 mono_debugger_unlock ();
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
;
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 ();
1092 #ifdef MONO_DEBUGGER_SUPPORTED
1095 get_exception_message (MonoObject
*exc
)
1097 char *message
= NULL
;
1103 if (mono_object_isinst (exc
, mono_defaults
.exception_class
)) {
1104 klass
= exc
->vtable
->klass
;
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
) {
1119 klass
= klass
->parent
;
1124 str
= (MonoString
*) mono_runtime_invoke (method
, exc
, NULL
, NULL
);
1126 message
= mono_string_to_utf8 (str
);
1133 mono_debugger_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
)
1135 MonoDebuggerThreadInfo
*thread_info
;
1136 MonoDebuggerExceptionState saved_exception_state
;
1140 mono_debugger_lock ();
1142 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
1144 mono_debugger_unlock ();
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
);
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 ();
1180 mono_debugger_unlock ();
1182 if (!exc
|| (*exc
== NULL
))
1186 message
= get_exception_message (*exc
);
1188 *exc
= (MonoObject
*) mono_string_new_wrapper (message
);
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
);
1205 mono_debugger_unlock ();
1209 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE
) == 0) {
1210 mono_debugger_unlock ();
1214 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1215 mono_debugger_unlock ();
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 ();