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 if ((method
->wrapper_type
!= MONO_WRAPPER_NONE
) &&
779 (method
->wrapper_type
!= MONO_WRAPPER_DYNAMIC_METHOD
))
782 for (i
= 0; i
< breakpoints
->len
; i
++) {
783 MiniDebugBreakpointInfo
*info
= g_ptr_array_index (breakpoints
, i
);
785 if (!mono_method_desc_full_match (info
->desc
, method
))
795 mono_debugger_breakpoint_callback (MonoMethod
*method
, guint32 index
)
797 mono_debugger_event (MONO_DEBUGGER_EVENT_JIT_BREAKPOINT
, (guint64
) (gsize
) method
, index
);
801 mono_debugger_thread_created (gsize tid
, MonoThread
*thread
, MonoJitTlsData
*jit_tls
, gpointer func
)
803 #ifdef MONO_DEBUGGER_SUPPORTED
805 guint8
*staddr
= NULL
;
806 MonoDebuggerThreadInfo
*info
;
808 if (mono_debug_format
== MONO_DEBUG_FORMAT_NONE
)
811 mono_debugger_lock ();
813 mono_thread_get_stack_bounds (&staddr
, &stsize
);
815 info
= g_new0 (MonoDebuggerThreadInfo
, 1);
817 info
->thread
= thread
->internal_thread
;
818 info
->stack_start
= (guint64
) (gsize
) staddr
;
819 info
->signal_stack_start
= (guint64
) (gsize
) jit_tls
->signal_stack
;
820 info
->stack_size
= stsize
;
821 info
->signal_stack_size
= jit_tls
->signal_stack_size
;
822 info
->end_stack
= (guint64
) (gsize
) GC_mono_debugger_get_stack_ptr ();
823 info
->lmf_addr
= (guint64
) (gsize
) mono_get_lmf_addr ();
824 info
->jit_tls
= jit_tls
;
827 info
->thread_flags
= MONO_DEBUGGER_THREAD_FLAGS_INTERNAL
;
828 if (thread
->internal_thread
->threadpool_thread
)
829 info
->thread_flags
|= MONO_DEBUGGER_THREAD_FLAGS_THREADPOOL
;
831 info
->next
= mono_debugger_thread_table
;
832 mono_debugger_thread_table
= info
;
834 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CREATED
,
835 tid
, (guint64
) (gsize
) info
);
837 mono_debugger_unlock ();
838 #endif /* MONO_DEBUGGER_SUPPORTED */
842 mono_debugger_thread_cleanup (MonoJitTlsData
*jit_tls
)
844 #ifdef MONO_DEBUGGER_SUPPORTED
845 MonoDebuggerThreadInfo
**ptr
;
847 if (mono_debug_format
== MONO_DEBUG_FORMAT_NONE
)
850 mono_debugger_lock ();
852 for (ptr
= &mono_debugger_thread_table
; *ptr
; ptr
= &(*ptr
)->next
) {
853 MonoDebuggerThreadInfo
*info
= *ptr
;
855 if (info
->jit_tls
!= jit_tls
)
858 mono_debugger_event (MONO_DEBUGGER_EVENT_THREAD_CLEANUP
,
859 info
->tid
, (guint64
) (gsize
) info
);
866 mono_debugger_unlock ();
871 mono_debugger_extended_notification (MonoDebuggerEvent event
, guint64 data
, guint64 arg
)
873 #ifdef MONO_DEBUGGER_SUPPORTED
874 MonoDebuggerThreadInfo
**ptr
;
875 MonoInternalThread
*thread
= mono_thread_internal_current ();
877 if (!mono_debug_using_mono_debugger ())
880 mono_debugger_lock ();
882 for (ptr
= &mono_debugger_thread_table
; *ptr
; ptr
= &(*ptr
)->next
) {
883 MonoDebuggerThreadInfo
*info
= *ptr
;
885 if (info
->thread
!= thread
)
888 if ((info
->extended_notifications
& (int) event
) == 0)
891 mono_debugger_event (event
, data
, arg
);
894 mono_debugger_unlock ();
899 mono_debugger_trampoline_compiled (const guint8
*trampoline
, MonoMethod
*method
, const guint8
*code
)
901 #ifdef MONO_DEBUGGER_SUPPORTED
903 const guint8
* trampoline
;
906 } info
= { trampoline
, method
, code
};
908 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_OLD_TRAMPOLINE
,
909 (guint64
) (gsize
) method
, (guint64
) (gsize
) code
);
910 mono_debugger_extended_notification (MONO_DEBUGGER_EVENT_TRAMPOLINE
,
911 (guint64
) (gsize
) &info
, 0);
915 #if MONO_DEBUGGER_SUPPORTED
916 static MonoDebuggerThreadInfo
*
917 find_debugger_thread_info (MonoInternalThread
*thread
)
919 MonoDebuggerThreadInfo
**ptr
;
921 for (ptr
= &mono_debugger_thread_table
; *ptr
; ptr
= &(*ptr
)->next
) {
922 MonoDebuggerThreadInfo
*info
= *ptr
;
924 if (info
->thread
== thread
)
932 MonoDebuggerExceptionAction
933 _mono_debugger_throw_exception (gpointer addr
, gpointer stack
, MonoObject
*exc
)
935 #ifdef MONO_DEBUGGER_SUPPORTED
936 MonoDebuggerExceptionInfo exc_info
;
937 MonoDebuggerThreadInfo
*thread_info
;
939 if (!mono_debug_using_mono_debugger ())
940 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
942 mono_debugger_lock ();
944 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
946 mono_debugger_unlock ();
947 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
950 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
951 mono_debugger_unlock ();
952 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
955 if (thread_info
->exception_state
.stopped_on_exception
||
956 thread_info
->exception_state
.stopped_on_unhandled
) {
957 thread_info
->exception_state
.stopped_on_exception
= 0;
958 mono_debugger_unlock ();
959 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
962 /* Protect the exception object from being garbage collected. */
964 thread_info
->exception_state
.stopped_on_unhandled
= 0;
965 thread_info
->exception_state
.stopped_on_exception
= 1;
966 thread_info
->exception_state
.last_exception
= exc
;
969 * Backwards compatibility:
971 * Older debugger versions only know `exc_info.stop' and older runtime versions check
972 * `exc_info.stop != 0'.
974 * The debugger must check for `mono_debug_debugger_version >= 5' before accessing the
975 * `stop_unhandled' field.
978 exc_info
.stack_pointer
= stack
;
979 exc_info
.exception_obj
= exc
;
981 exc_info
.stop_unhandled
= 0;
983 mono_debugger_event (MONO_DEBUGGER_EVENT_THROW_EXCEPTION
, (guint64
) (gsize
) &exc_info
,
984 (guint64
) (gsize
) addr
);
986 if (!exc_info
.stop
) {
987 thread_info
->exception_state
.stopped_on_exception
= 0;
988 thread_info
->exception_state
.last_exception
= NULL
;
991 mono_debugger_unlock ();
994 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP
;
995 else if (exc_info
.stop_unhandled
)
996 return MONO_DEBUGGER_EXCEPTION_ACTION_STOP_UNHANDLED
;
999 return MONO_DEBUGGER_EXCEPTION_ACTION_NONE
;
1003 _mono_debugger_unhandled_exception (gpointer addr
, gpointer stack
, MonoObject
*exc
)
1005 #ifdef MONO_DEBUGGER_SUPPORTED
1006 MonoDebuggerThreadInfo
*thread_info
;
1008 if (!mono_debug_using_mono_debugger ())
1012 const gchar
*name
= mono_class_get_name (mono_object_get_class (exc
));
1013 if (!strcmp (name
, "ThreadAbortException"))
1017 mono_debugger_lock ();
1019 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
1021 mono_debugger_unlock ();
1025 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1026 mono_debugger_unlock ();
1030 if (thread_info
->exception_state
.stopped_on_unhandled
) {
1031 thread_info
->exception_state
.stopped_on_unhandled
= 0;
1032 mono_debugger_unlock ();
1036 thread_info
->exception_state
.stopped_on_unhandled
= 1;
1037 thread_info
->exception_state
.last_exception
= exc
;
1039 mono_debugger_event (MONO_DEBUGGER_EVENT_UNHANDLED_EXCEPTION
,
1040 (guint64
) (gsize
) exc
, (guint64
) (gsize
) addr
);
1049 * mono_debugger_call_exception_handler:
1051 * Called from mono_handle_exception_internal() to tell the debugger that we're about
1052 * to invoke an exception handler.
1054 * The debugger may choose to set a breakpoint at @addr. This is used if the user is
1055 * single-stepping from a `try' into a `catch' block, for instance.
1059 mono_debugger_call_exception_handler (gpointer addr
, gpointer stack
, MonoObject
*exc
)
1061 #ifdef MONO_DEBUGGER_SUPPORTED
1062 MonoDebuggerThreadInfo
*thread_info
;
1063 MonoDebuggerExceptionInfo exc_info
;
1065 if (!mono_debug_using_mono_debugger ())
1068 mono_debugger_lock ();
1070 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
1072 mono_debugger_unlock ();
1076 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1077 mono_debugger_unlock ();
1081 // Prevent the object from being finalized.
1082 thread_info
->exception_state
.last_exception
= exc
;
1084 exc_info
.stack_pointer
= stack
;
1085 exc_info
.exception_obj
= exc
;
1087 exc_info
.stop_unhandled
= 0;
1089 mono_debugger_event (MONO_DEBUGGER_EVENT_HANDLE_EXCEPTION
, (guint64
) (gsize
) &exc_info
,
1090 (guint64
) (gsize
) addr
);
1092 mono_debugger_unlock ();
1096 #ifdef MONO_DEBUGGER_SUPPORTED
1099 get_exception_message (MonoObject
*exc
)
1101 char *message
= NULL
;
1107 if (mono_object_isinst (exc
, mono_defaults
.exception_class
)) {
1108 klass
= exc
->vtable
->klass
;
1110 while (klass
&& method
== NULL
) {
1111 for (i
= 0; i
< klass
->method
.count
; ++i
) {
1112 method
= klass
->methods
[i
];
1113 if (!strcmp ("ToString", method
->name
) &&
1114 mono_method_signature (method
)->param_count
== 0 &&
1115 method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
&&
1116 method
->flags
& METHOD_ATTRIBUTE_PUBLIC
) {
1123 klass
= klass
->parent
;
1128 str
= (MonoString
*) mono_runtime_invoke (method
, exc
, NULL
, NULL
);
1130 message
= mono_string_to_utf8 (str
);
1137 mono_debugger_runtime_invoke (MonoMethod
*method
, void *obj
, void **params
, MonoObject
**exc
)
1139 MonoDebuggerThreadInfo
*thread_info
;
1140 MonoDebuggerExceptionState saved_exception_state
;
1144 mono_debugger_lock ();
1146 thread_info
= find_debugger_thread_info (mono_thread_internal_current ());
1148 mono_debugger_unlock ();
1152 saved_exception_state
= thread_info
->exception_state
;
1154 thread_info
->exception_state
.last_exception
= NULL
;
1155 thread_info
->exception_state
.stopped_on_unhandled
= 0;
1156 thread_info
->exception_state
.stopped_on_exception
= 0;
1158 thread_info
->internal_flags
|= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE
;
1160 mono_debugger_unlock ();
1162 if (!strcmp (method
->name
, ".ctor")) {
1163 retval
= obj
= mono_object_new (mono_domain_get (), method
->klass
);
1165 mono_runtime_invoke (method
, obj
, params
, exc
);
1167 retval
= mono_runtime_invoke (method
, obj
, params
, exc
);
1169 mono_debugger_lock ();
1171 thread_info
->exception_state
= saved_exception_state
;
1172 thread_info
->internal_flags
&= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE
;
1174 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1175 thread_info
->internal_flags
&= ~MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
;
1176 mono_thread_internal_reset_abort (thread_info
->thread
);
1178 mono_debugger_unlock ();
1184 mono_debugger_unlock ();
1186 if (!exc
|| (*exc
== NULL
))
1190 message
= get_exception_message (*exc
);
1192 *exc
= (MonoObject
*) mono_string_new_wrapper (message
);
1200 mono_debugger_abort_runtime_invoke ()
1202 MonoInternalThread
*thread
= mono_thread_internal_current ();
1203 MonoDebuggerThreadInfo
*thread_info
;
1205 mono_debugger_lock ();
1207 thread_info
= find_debugger_thread_info (thread
);
1209 mono_debugger_unlock ();
1213 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_IN_RUNTIME_INVOKE
) == 0) {
1214 mono_debugger_unlock ();
1218 if ((thread_info
->internal_flags
& MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
) != 0) {
1219 mono_debugger_unlock ();
1223 thread_info
->internal_flags
|= MONO_DEBUGGER_INTERNAL_THREAD_FLAGS_ABORT_REQUESTED
;
1224 ves_icall_System_Threading_Thread_Abort (thread_info
->thread
, NULL
);
1226 mono_debugger_unlock ();