3 * Mono support for LLDB.
6 * Zoltan Varga (vargaz@gmail.com)
8 * Copyright 2016 Xamarin, Inc (http://www.xamarin.com)
14 #include "seq-points.h"
16 #include <mono/metadata/debug-internals.h>
17 #include <mono/utils/mono-counters.h>
19 #if !defined(DISABLE_JIT) && !defined(DISABLE_LLDB)
22 ENTRY_CODE_REGION
= 1,
25 ENTRY_UNLOAD_CODE_REGION
= 4
29 * Need to make sure these structures have the same size and alignment on
33 /* One data packet sent from the runtime to the debugger */
35 /* Pointer to the next entry */
37 /* The type of data pointed to by ADDR */
38 /* One of the ENTRY_ constants */
48 /* (MAJOR << 16) | MINOR */
53 /* List of all entries */
54 /* Keep this as a pointer so accessing it is atomic */
55 DebugEntry
*all_entries
;
56 /* The current entry embedded here to reduce the amount of roundtrips */
64 * Represents a memory region used for code.
68 * OBJFILE_MAGIC. This is needed to make it easier for lldb to
69 * create object files from this packet.
79 } UnloadCodeRegionEntry
;
82 * Represents a managed method
87 /* The id of the codegen region which contains CODE */
92 /* Followed by variable size data */
96 * Represents a trampoline
101 /* The id of the codegen region which contains CODE */
106 /* Followed by variable size data */
109 #define MAJOR_VERSION 1
110 #define MINOR_VERSION 0
112 static const char* OBJFILE_MAGIC
= { "MONO_JIT_OBJECT_FILE" };
114 JitDescriptor __mono_jit_debug_descriptor
= { (MAJOR_VERSION
<< 16) | MINOR_VERSION
};
116 static gboolean enabled
;
117 static int id_generator
;
118 static GHashTable
*codegen_regions
;
119 static DebugEntry
*last_entry
;
120 static mono_mutex_t mutex
;
121 static GHashTable
*dyn_codegen_regions
;
122 static gint64 register_time
;
123 static int num_entries
;
125 #define lldb_lock() mono_os_mutex_lock (&mutex)
126 #define lldb_unlock() mono_os_mutex_unlock (&mutex)
130 void MONO_NEVER_INLINE
__mono_jit_debug_register_code (void);
132 /* The native debugger puts a breakpoint in this function. */
133 void MONO_NEVER_INLINE
134 __mono_jit_debug_register_code (void)
136 /* Make sure that even compilers that ignore __noinline__ don't inline this */
137 #if defined(__GNUC__)
145 * Functions to encode protocol data
149 guint8
*buf
, *p
, *end
;
153 buffer_init (Buffer
*buf
, int size
)
155 buf
->buf
= (guint8
*)g_malloc (size
);
157 buf
->end
= buf
->buf
+ size
;
161 buffer_len (Buffer
*buf
)
163 return buf
->p
- buf
->buf
;
167 buffer_make_room (Buffer
*buf
, int size
)
169 if (buf
->end
- buf
->p
< size
) {
170 int new_size
= buf
->end
- buf
->buf
+ size
+ 32;
171 guint8
*p
= (guint8
*)g_realloc (buf
->buf
, new_size
);
172 size
= buf
->p
- buf
->buf
;
175 buf
->end
= buf
->buf
+ new_size
;
180 buffer_add_byte (Buffer
*buf
, guint8 val
)
182 buffer_make_room (buf
, 1);
188 buffer_add_int (Buffer
*buf
, guint32 val
)
190 buffer_make_room (buf
, 4);
191 buf
->p
[0] = (val
>> 24) & 0xff;
192 buf
->p
[1] = (val
>> 16) & 0xff;
193 buf
->p
[2] = (val
>> 8) & 0xff;
194 buf
->p
[3] = (val
>> 0) & 0xff;
199 buffer_add_data (Buffer
*buf
, guint8
*data
, int len
)
201 buffer_make_room (buf
, len
);
202 memcpy (buf
->p
, data
, len
);
207 buffer_add_string (Buffer
*buf
, const char *str
)
212 buffer_add_int (buf
, 0);
215 buffer_add_int (buf
, len
);
216 buffer_add_data (buf
, (guint8
*)str
, len
);
221 buffer_free (Buffer
*buf
)
228 gpointer region_start
;
234 find_code_region (void *data
, int csize
, int size
, void *user_data
)
236 UserData
*ud
= (UserData
*)user_data
;
238 if ((char*)ud
->code
>= (char*)data
&& (char*)ud
->code
< (char*)data
+ csize
) {
239 ud
->region_start
= data
;
240 ud
->region_size
= csize
;
248 add_entry (EntryType type
, Buffer
*buf
)
252 int size
= buffer_len (buf
);
254 data
= g_malloc (size
);
255 memcpy (data
, buf
->buf
, size
);
257 entry
= g_malloc0 (sizeof (DebugEntry
));
259 entry
->addr
= (guint64
)(gsize
)data
;
262 mono_memory_barrier ();
266 /* The debugger can read the list of entries asynchronously, so this has to be async safe */
267 // FIXME: Make sure this is async safe
269 last_entry
->next_addr
= (guint64
)(gsize
) (entry
);
273 __mono_jit_debug_descriptor
.all_entries
= entry
;
276 __mono_jit_debug_descriptor
.entry
= entry
;
278 __mono_jit_debug_descriptor
.type
= entry
->type
;
279 __mono_jit_debug_descriptor
.size
= entry
->size
;
280 __mono_jit_debug_descriptor
.addr
= entry
->addr
;
281 mono_memory_barrier ();
283 gint64 start
= mono_time_track_start ();
284 __mono_jit_debug_register_code ();
285 mono_time_track_end (®ister_time
, start
);
287 //printf ("%lf %d %d\n", register_time, num_entries, entry->type);
293 * register_codegen_region:
295 * Register a codegen region with the debugger if needed.
296 * Return a region id.
299 register_codegen_region (gpointer region_start
, int region_size
, gboolean dynamic
)
301 CodeRegionEntry
*region_entry
;
304 Buffer
*buf
= &tmp_buf
;
312 if (!codegen_regions
)
313 codegen_regions
= g_hash_table_new (NULL
, NULL
);
314 id
= GPOINTER_TO_INT (g_hash_table_lookup (codegen_regions
, region_start
));
320 g_hash_table_insert (codegen_regions
, region_start
, GINT_TO_POINTER (id
));
324 buffer_init (buf
, 128);
326 region_entry
= (CodeRegionEntry
*)buf
->p
;
327 buf
->p
+= sizeof (CodeRegionEntry
);
328 memset (region_entry
, 0, sizeof (CodeRegionEntry
));
329 strcpy (region_entry
->magic
, OBJFILE_MAGIC
);
330 region_entry
->id
= id
;
331 region_entry
->start
= (gsize
)region_start
;
332 region_entry
->size
= (gsize
)region_size
;
334 add_entry (ENTRY_CODE_REGION
, buf
);
340 emit_unwind_info (GSList
*unwind_ops
, Buffer
*buf
)
346 ret_reg
= mono_unwind_get_dwarf_pc_reg ();
347 g_assert (ret_reg
< 256);
349 /* We use the unencoded version of the unwind info to make it easier to decode */
351 for (l
= unwind_ops
; l
; l
= l
->next
) {
352 MonoUnwindOp
*op
= (MonoUnwindOp
*)l
->data
;
354 /* lldb can't handle these */
355 if (op
->op
== DW_CFA_mono_advance_loc
)
360 buffer_add_byte (buf
, ret_reg
);
361 buffer_add_int (buf
, nunwind_ops
);
362 for (l
= unwind_ops
; l
; l
= l
->next
) {
363 MonoUnwindOp
*op
= (MonoUnwindOp
*)l
->data
;
365 if (op
->op
== DW_CFA_mono_advance_loc
)
367 buffer_add_int (buf
, op
->op
);
368 buffer_add_int (buf
, op
->when
);
371 // LLDB doesn't see to use the switched esp/ebp
372 if (op
->reg
== X86_ESP
)
374 else if (op
->reg
== X86_EBP
)
377 dreg
= mono_hw_reg_to_dwarf_reg (op
->reg
);
379 dreg
= mono_hw_reg_to_dwarf_reg (op
->reg
);
381 buffer_add_int (buf
, dreg
);
382 buffer_add_int (buf
, op
->val
);
387 mono_lldb_init (const char *options
)
390 mono_os_mutex_init_recursive (&mutex
);
392 mono_counters_register ("Time spent in LLDB", MONO_COUNTER_JIT
| MONO_COUNTER_LONG
| MONO_COUNTER_TIME
, ®ister_time
);
402 compare_by_addr (const void *arg1
, const void *arg2
)
404 const FullSeqPoint
*sp1
= (const FullSeqPoint
*)arg1
;
405 const FullSeqPoint
*sp2
= (const FullSeqPoint
*)arg2
;
407 return sp1
->native_offset
- sp2
->native_offset
;
411 mono_lldb_save_method_info (MonoCompile
*cfg
)
417 Buffer
*buf
= &tmpbuf
;
418 MonoDebugMethodInfo
*minfo
;
419 int i
, j
, n_il_offsets
;
421 GPtrArray
*source_file_list
;
422 MonoSymSeqPoint
*sym_seq_points
;
428 /* Find the codegen region which contains the code */
429 memset (&udata
, 0, sizeof (udata
));
430 udata
.code
= cfg
->native_code
;
431 if (cfg
->method
->dynamic
) {
432 mono_code_manager_foreach (cfg
->dynamic_info
->code_mp
, find_code_region
, &udata
);
433 g_assert (udata
.found
);
435 region_id
= register_codegen_region (udata
.region_start
, udata
.region_size
, TRUE
);
438 if (!dyn_codegen_regions
)
439 dyn_codegen_regions
= g_hash_table_new (NULL
, NULL
);
440 g_hash_table_insert (dyn_codegen_regions
, cfg
->method
, GINT_TO_POINTER (region_id
));
443 mono_domain_code_foreach (cfg
->domain
, find_code_region
, &udata
);
444 g_assert (udata
.found
);
446 region_id
= register_codegen_region (udata
.region_start
, udata
.region_size
, FALSE
);
449 buffer_init (buf
, 256);
451 entry
= (MethodEntry
*)buf
->p
;
452 buf
->p
+= sizeof (MethodEntry
);
453 entry
->id
= ++id_generator
;
454 entry
->region_id
= region_id
;
455 entry
->code
= (gsize
)cfg
->native_code
;
456 entry
->code_size
= cfg
->code_size
;
458 emit_unwind_info (cfg
->unwind_ops
, buf
);
460 char *s
= mono_method_full_name (cfg
->method
, TRUE
);
461 buffer_add_string (buf
, s
);
464 minfo
= mono_debug_lookup_method (cfg
->method
);
465 MonoSeqPointInfo
*seq_points
= cfg
->seq_point_info
;
466 if (minfo
&& seq_points
) {
467 mono_debug_get_seq_points (minfo
, NULL
, &source_file_list
, &source_files
, &sym_seq_points
, &n_il_offsets
);
468 buffer_add_int (buf
, source_file_list
->len
);
469 for (i
= 0; i
< source_file_list
->len
; ++i
) {
470 MonoDebugSourceInfo
*sinfo
= (MonoDebugSourceInfo
*)g_ptr_array_index (source_file_list
, i
);
471 buffer_add_string (buf
, sinfo
->source_file
);
472 for (j
= 0; j
< 16; ++j
)
473 buffer_add_byte (buf
, sinfo
->hash
[j
]);
476 // The sym seq points are ordered by il offset, need to order them by address
478 locs
= g_new0 (FullSeqPoint
, n_il_offsets
);
479 for (i
= 0; i
< n_il_offsets
; ++i
) {
480 locs
[i
].sp
= sym_seq_points
[i
];
484 if (mono_seq_point_find_by_il_offset (seq_points
, sym_seq_points
[i
].il_offset
, &seq_point
)) {
485 locs
[i
].native_offset
= seq_point
.native_offset
;
487 locs
[i
].native_offset
= 0xffffff;
491 qsort (locs
, n_il_offsets
, sizeof (FullSeqPoint
), compare_by_addr
);
493 n_il_offsets
-= skipped
;
494 buffer_add_int (buf
, n_il_offsets
);
495 for (i
= 0; i
< n_il_offsets
; ++i
) {
496 MonoSymSeqPoint
*sp
= &locs
[i
].sp
;
498 //printf ("%s %x %d %d\n", cfg->method->name, locs [i].native_offset, sp->il_offset, sp->line);
499 buffer_add_int (buf
, locs
[i
].native_offset
);
500 buffer_add_int (buf
, sp
->il_offset
);
501 buffer_add_int (buf
, sp
->line
);
502 buffer_add_int (buf
, source_files
[i
]);
503 buffer_add_int (buf
, sp
->column
);
504 buffer_add_int (buf
, sp
->end_line
);
505 buffer_add_int (buf
, sp
->end_column
);
508 g_free (source_files
);
509 g_free (sym_seq_points
);
510 g_ptr_array_free (source_file_list
, TRUE
);
512 buffer_add_int (buf
, 0);
513 buffer_add_int (buf
, 0);
516 add_entry (ENTRY_METHOD
, buf
);
521 mono_lldb_remove_method (MonoDomain
*domain
, MonoMethod
*method
, MonoJitDynamicMethodInfo
*info
)
524 UnloadCodeRegionEntry
*entry
;
526 Buffer
*buf
= &tmpbuf
;
531 g_assert (method
->dynamic
);
534 region_id
= GPOINTER_TO_INT (g_hash_table_lookup (dyn_codegen_regions
, method
));
535 g_hash_table_remove (dyn_codegen_regions
, method
);
538 buffer_init (buf
, 256);
540 entry
= (UnloadCodeRegionEntry
*)buf
->p
;
541 buf
->p
+= sizeof (UnloadCodeRegionEntry
);
542 entry
->id
= region_id
;
544 add_entry (ENTRY_UNLOAD_CODE_REGION
, buf
);
547 /* The method is associated with the code region, so it doesn't have to be unloaded */
551 mono_lldb_save_trampoline_info (MonoTrampInfo
*info
)
553 TrampolineEntry
*entry
;
557 Buffer
*buf
= &tmpbuf
;
562 /* Find the codegen region which contains the code */
563 memset (&udata
, 0, sizeof (udata
));
564 udata
.code
= info
->code
;
565 mono_global_codeman_foreach (find_code_region
, &udata
);
567 mono_domain_code_foreach (mono_get_root_domain (), find_code_region
, &udata
);
569 /* Can happen with AOT */
572 region_id
= register_codegen_region (udata
.region_start
, udata
.region_size
, FALSE
);
574 buffer_init (buf
, 1024);
576 entry
= (TrampolineEntry
*)buf
->p
;
577 buf
->p
+= sizeof (TrampolineEntry
);
578 entry
->id
= ++id_generator
;
579 entry
->region_id
= region_id
;
580 entry
->code
= (gsize
)info
->code
;
581 entry
->code_size
= info
->code_size
;
583 emit_unwind_info (info
->unwind_ops
, buf
);
585 buffer_add_string (buf
, info
->name
);
587 add_entry (ENTRY_TRAMPOLINE
, buf
);
592 mono_lldb_save_specific_trampoline_info (gpointer arg1
, MonoTrampolineType tramp_type
, MonoDomain
*domain
, gpointer code
, guint32 code_len
)
595 * Avoid emitting these for now,
596 * they slow down execution too much, and they are
597 * only needed during single stepping which doesn't
601 TrampolineEntry
*entry
;
605 Buffer
*buf
= &tmpbuf
;
610 /* Find the codegen region which contains the code */
611 memset (&udata
, 0, sizeof (udata
));
613 mono_global_codeman_foreach (find_code_region
, &udata
);
615 mono_domain_code_foreach (mono_get_root_domain (), find_code_region
, &udata
);
616 g_assert (udata
.found
);
618 region_id
= register_codegen_region (udata
.region_start
, udata
.region_size
, FALSE
);
620 buffer_init (buf
, 1024);
622 entry
= (TrampolineEntry
*)buf
->p
;
623 buf
->p
+= sizeof (TrampolineEntry
);
624 entry
->id
= ++id_generator
;
625 entry
->region_id
= region_id
;
626 entry
->code
= (gsize
)code
;
627 entry
->code_size
= code_len
;
629 GSList
*unwind_ops
= mono_unwind_get_cie_program ();
630 emit_unwind_info (unwind_ops
, buf
);
632 buffer_add_string (buf
, "");
634 add_entry (ENTRY_TRAMPOLINE
, buf
);
643 Similar to the gdb jit interface. The runtime communicates with a plugin running inside lldb.
644 - The runtime allocates a data packet, points a symbol with a well known name at it.
645 - It calls a dummy function with a well known name.
646 - The plugin sets a breakpoint at this function, causing the runtime to be suspended.
647 - The plugin reads the data pointed to by the other symbol and processes it.
649 The data packets are kept in a list, so lldb can read all of them after attaching.
650 Lldb will associate an object file with each mono codegen region.
653 - use a flat byte array so the whole data can be read in one operation.
654 - use 64 bit ints for pointers.
660 mono_lldb_init (const char *options
)
662 g_error ("lldb support has been disabled at configure time.");
666 mono_lldb_save_method_info (MonoCompile
*cfg
)
671 mono_lldb_save_trampoline_info (MonoTrampInfo
*info
)
676 mono_lldb_remove_method (MonoDomain
*domain
, MonoMethod
*method
, MonoJitDynamicMethodInfo
*info
)
681 mono_lldb_save_specific_trampoline_info (gpointer arg1
, MonoTrampolineType tramp_type
, MonoDomain
*domain
, gpointer code
, guint32 code_len
)