2 * lldb.c: Mono support for LLDB.
5 * Zoltan Varga (vargaz@gmail.com)
7 * Copyright 2016 Xamarin, Inc (http://www.xamarin.com)
13 #include "seq-points.h"
15 #include <mono/metadata/debug-internals.h>
16 #include <mono/utils/mono-counters.h>
18 #if !defined(DISABLE_JIT) && !defined(DISABLE_LLDB)
21 ENTRY_CODE_REGION = 1,
24 ENTRY_UNLOAD_CODE_REGION = 4
28 * Need to make sure these structures have the same size and alignment on
32 /* One data packet sent from the runtime to the debugger */
34 /* Pointer to the next entry */
36 /* The type of data pointed to by ADDR */
37 /* One of the ENTRY_ constants */
47 /* (MAJOR << 16) | MINOR */
52 /* List of all entries */
53 /* Keep this as a pointer so accessing it is atomic */
54 DebugEntry *all_entries;
55 /* The current entry embedded here to reduce the amount of roundtrips */
63 * Represents a memory region used for code.
67 * OBJFILE_MAGIC. This is needed to make it easier for lldb to
68 * create object files from this packet.
78 } UnloadCodeRegionEntry;
81 * Represents a managed method
86 /* The id of the codegen region which contains CODE */
91 /* Followed by variable size data */
95 * Represents a trampoline
100 /* The id of the codegen region which contains CODE */
105 /* Followed by variable size data */
108 #define MAJOR_VERSION 1
109 #define MINOR_VERSION 0
111 static const char* OBJFILE_MAGIC = { "MONO_JIT_OBJECT_FILE" };
113 JitDescriptor __mono_jit_debug_descriptor = { (MAJOR_VERSION << 16) | MINOR_VERSION };
115 static gboolean enabled;
116 static int id_generator;
117 static GHashTable *codegen_regions;
118 static DebugEntry *last_entry;
119 static mono_mutex_t mutex;
120 static GHashTable *dyn_codegen_regions;
121 static double register_time;
122 static int num_entries;
124 #define lldb_lock() mono_os_mutex_lock (&mutex)
125 #define lldb_unlock() mono_os_mutex_unlock (&mutex)
127 void MONO_NEVER_INLINE __mono_jit_debug_register_code (void);
129 /* The native debugger puts a breakpoint in this function. */
130 void MONO_NEVER_INLINE
131 __mono_jit_debug_register_code (void)
133 /* Make sure that even compilers that ignore __noinline__ don't inline this */
134 #if defined(__GNUC__)
140 * Functions to encode protocol data
144 guint8 *buf, *p, *end;
148 buffer_init (Buffer *buf, int size)
150 buf->buf = (guint8 *)g_malloc (size);
152 buf->end = buf->buf + size;
156 buffer_len (Buffer *buf)
158 return buf->p - buf->buf;
162 buffer_make_room (Buffer *buf, int size)
164 if (buf->end - buf->p < size) {
165 int new_size = buf->end - buf->buf + size + 32;
166 guint8 *p = (guint8 *)g_realloc (buf->buf, new_size);
167 size = buf->p - buf->buf;
170 buf->end = buf->buf + new_size;
175 buffer_add_byte (Buffer *buf, guint8 val)
177 buffer_make_room (buf, 1);
183 buffer_add_short (Buffer *buf, guint32 val)
185 buffer_make_room (buf, 2);
186 buf->p [0] = (val >> 8) & 0xff;
187 buf->p [1] = (val >> 0) & 0xff;
192 buffer_add_int (Buffer *buf, guint32 val)
194 buffer_make_room (buf, 4);
195 buf->p [0] = (val >> 24) & 0xff;
196 buf->p [1] = (val >> 16) & 0xff;
197 buf->p [2] = (val >> 8) & 0xff;
198 buf->p [3] = (val >> 0) & 0xff;
203 buffer_add_long (Buffer *buf, guint64 l)
205 buffer_add_int (buf, (l >> 32) & 0xffffffff);
206 buffer_add_int (buf, (l >> 0) & 0xffffffff);
210 buffer_add_id (Buffer *buf, int id)
212 buffer_add_int (buf, (guint64)id);
216 buffer_add_data (Buffer *buf, guint8 *data, int len)
218 buffer_make_room (buf, len);
219 memcpy (buf->p, data, len);
224 buffer_add_string (Buffer *buf, const char *str)
229 buffer_add_int (buf, 0);
232 buffer_add_int (buf, len);
233 buffer_add_data (buf, (guint8*)str, len);
238 buffer_add_buffer (Buffer *buf, Buffer *data)
240 buffer_add_data (buf, data->buf, buffer_len (data));
244 buffer_free (Buffer *buf)
251 gpointer region_start;
257 find_code_region (void *data, int csize, int size, void *user_data)
259 UserData *ud = user_data;
261 if ((char*)ud->code >= (char*)data && (char*)ud->code < (char*)data + csize) {
262 ud->region_start = data;
263 ud->region_size = csize;
271 add_entry (EntryType type, Buffer *buf)
275 int size = buffer_len (buf);
277 data = g_malloc (size);
278 memcpy (data, buf->buf, size);
280 entry = g_malloc0 (sizeof (DebugEntry));
282 entry->addr = (guint64)(gsize)data;
285 mono_memory_barrier ();
289 /* The debugger can read the list of entries asynchronously, so this has to be async safe */
290 // FIXME: Make sure this is async safe
292 last_entry->next_addr = (guint64)(gsize) (entry);
296 __mono_jit_debug_descriptor.all_entries = entry;
299 __mono_jit_debug_descriptor.entry = entry;
301 __mono_jit_debug_descriptor.type = entry->type;
302 __mono_jit_debug_descriptor.size = entry->size;
303 __mono_jit_debug_descriptor.addr = entry->addr;
304 mono_memory_barrier ();
306 GTimer *timer = mono_time_track_start ();
307 __mono_jit_debug_register_code ();
308 mono_time_track_end (®ister_time, timer);
310 //printf ("%lf %d %d\n", register_time, num_entries, entry->type);
316 * register_codegen_region:
318 * Register a codegen region with the debugger if needed.
319 * Return a region id.
322 register_codegen_region (gpointer region_start, int region_size, gboolean dynamic)
324 CodeRegionEntry *region_entry;
327 Buffer *buf = &tmp_buf;
335 if (!codegen_regions)
336 codegen_regions = g_hash_table_new (NULL, NULL);
337 id = GPOINTER_TO_INT (g_hash_table_lookup (codegen_regions, region_start));
343 g_hash_table_insert (codegen_regions, region_start, GINT_TO_POINTER (id));
347 buffer_init (buf, 128);
349 region_entry = (CodeRegionEntry*)buf->p;
350 buf->p += sizeof (CodeRegionEntry);
351 memset (region_entry, 0, sizeof (CodeRegionEntry));
352 strcpy (region_entry->magic, OBJFILE_MAGIC);
353 region_entry->id = id;
354 region_entry->start = (gsize)region_start;
355 region_entry->size = (gsize)region_size;
357 add_entry (ENTRY_CODE_REGION, buf);
363 emit_unwind_info (GSList *unwind_ops, Buffer *buf)
369 ret_reg = mono_unwind_get_dwarf_pc_reg ();
370 g_assert (ret_reg < 256);
372 /* We use the unencoded version of the unwind info to make it easier to decode */
374 for (l = unwind_ops; l; l = l->next) {
375 MonoUnwindOp *op = l->data;
377 /* lldb can't handle these */
378 if (op->op == DW_CFA_mono_advance_loc)
383 buffer_add_byte (buf, ret_reg);
384 buffer_add_int (buf, nunwind_ops);
385 for (l = unwind_ops; l; l = l->next) {
386 MonoUnwindOp *op = l->data;
388 if (op->op == DW_CFA_mono_advance_loc)
390 buffer_add_int (buf, op->op);
391 buffer_add_int (buf, op->when);
394 // LLDB doesn't see to use the switched esp/ebp
395 if (op->reg == X86_ESP)
397 else if (op->reg == X86_EBP)
400 dreg = mono_hw_reg_to_dwarf_reg (op->reg);
402 dreg = mono_hw_reg_to_dwarf_reg (op->reg);
404 buffer_add_int (buf, dreg);
405 buffer_add_int (buf, op->val);
410 mono_lldb_init (const char *options)
413 mono_os_mutex_init_recursive (&mutex);
415 mono_counters_register ("Time spent in LLDB", MONO_COUNTER_JIT | MONO_COUNTER_DOUBLE, ®ister_time);
425 compare_by_addr (const void *arg1, const void *arg2)
427 const FullSeqPoint *sp1 = arg1;
428 const FullSeqPoint *sp2 = arg2;
430 return sp1->native_offset - sp2->native_offset;
434 mono_lldb_save_method_info (MonoCompile *cfg)
440 Buffer *buf = &tmpbuf;
441 MonoDebugMethodInfo *minfo;
442 int i, j, n_il_offsets;
444 GPtrArray *source_file_list;
445 MonoSymSeqPoint *sym_seq_points;
451 /* Find the codegen region which contains the code */
452 memset (&udata, 0, sizeof (udata));
453 udata.code = cfg->native_code;
454 if (cfg->method->dynamic) {
455 mono_code_manager_foreach (cfg->dynamic_info->code_mp, find_code_region, &udata);
456 g_assert (udata.found);
458 region_id = register_codegen_region (udata.region_start, udata.region_size, TRUE);
461 if (!dyn_codegen_regions)
462 dyn_codegen_regions = g_hash_table_new (NULL, NULL);
463 g_hash_table_insert (dyn_codegen_regions, cfg->method, GINT_TO_POINTER (region_id));
466 mono_domain_code_foreach (cfg->domain, find_code_region, &udata);
467 g_assert (udata.found);
469 region_id = register_codegen_region (udata.region_start, udata.region_size, FALSE);
472 buffer_init (buf, 256);
474 entry = (MethodEntry*)buf->p;
475 buf->p += sizeof (MethodEntry);
476 entry->id = ++id_generator;
477 entry->region_id = region_id;
478 entry->code = (gsize)cfg->native_code;
479 entry->code_size = cfg->code_size;
481 emit_unwind_info (cfg->unwind_ops, buf);
483 char *s = mono_method_full_name (cfg->method, TRUE);
484 buffer_add_string (buf, s);
487 minfo = mono_debug_lookup_method (cfg->method);
488 MonoSeqPointInfo *seq_points = cfg->seq_point_info;
489 if (minfo && seq_points) {
490 mono_debug_get_seq_points (minfo, NULL, &source_file_list, &source_files, &sym_seq_points, &n_il_offsets);
491 buffer_add_int (buf, source_file_list->len);
492 for (i = 0; i < source_file_list->len; ++i) {
493 MonoDebugSourceInfo *sinfo = (MonoDebugSourceInfo *)g_ptr_array_index (source_file_list, i);
494 buffer_add_string (buf, sinfo->source_file);
495 for (j = 0; j < 16; ++j)
496 buffer_add_byte (buf, sinfo->hash [j]);
499 // The sym seq points are ordered by il offset, need to order them by address
501 locs = g_new0 (FullSeqPoint, n_il_offsets);
502 for (i = 0; i < n_il_offsets; ++i) {
503 locs [i].sp = sym_seq_points [i];
507 if (mono_seq_point_find_by_il_offset (seq_points, sym_seq_points [i].il_offset, &seq_point)) {
508 locs [i].native_offset = seq_point.native_offset;
510 locs [i].native_offset = 0xffffff;
514 qsort (locs, n_il_offsets, sizeof (FullSeqPoint), compare_by_addr);
516 n_il_offsets -= skipped;
517 buffer_add_int (buf, n_il_offsets);
518 for (i = 0; i < n_il_offsets; ++i) {
519 MonoSymSeqPoint *sp = &locs [i].sp;
520 const char *srcfile = "";
522 if (source_files [i] != -1) {
523 MonoDebugSourceInfo *sinfo = (MonoDebugSourceInfo *)g_ptr_array_index (source_file_list, source_files [i]);
524 srcfile = sinfo->source_file;
527 //printf ("%s %x %d %d\n", cfg->method->name, locs [i].native_offset, sp->il_offset, sp->line);
528 buffer_add_int (buf, locs [i].native_offset);
529 buffer_add_int (buf, sp->il_offset);
530 buffer_add_int (buf, sp->line);
531 buffer_add_int (buf, source_files [i]);
532 buffer_add_int (buf, sp->column);
533 buffer_add_int (buf, sp->end_line);
534 buffer_add_int (buf, sp->end_column);
537 g_free (source_files);
538 g_free (sym_seq_points);
539 g_ptr_array_free (source_file_list, TRUE);
541 buffer_add_int (buf, 0);
542 buffer_add_int (buf, 0);
545 add_entry (ENTRY_METHOD, buf);
550 mono_lldb_remove_method (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *info)
553 UnloadCodeRegionEntry *entry;
555 Buffer *buf = &tmpbuf;
560 g_assert (method->dynamic);
563 region_id = GPOINTER_TO_INT (g_hash_table_lookup (dyn_codegen_regions, method));
564 g_hash_table_remove (dyn_codegen_regions, method);
567 buffer_init (buf, 256);
569 entry = (UnloadCodeRegionEntry*)buf->p;
570 buf->p += sizeof (UnloadCodeRegionEntry);
571 entry->id = region_id;
573 add_entry (ENTRY_UNLOAD_CODE_REGION, buf);
576 /* The method is associated with the code region, so it doesn't have to be unloaded */
580 mono_lldb_save_trampoline_info (MonoTrampInfo *info)
582 TrampolineEntry *entry;
586 Buffer *buf = &tmpbuf;
591 /* Find the codegen region which contains the code */
592 memset (&udata, 0, sizeof (udata));
593 udata.code = info->code;
594 mono_global_codeman_foreach (find_code_region, &udata);
596 mono_domain_code_foreach (mono_get_root_domain (), find_code_region, &udata);
598 /* Can happen with AOT */
601 region_id = register_codegen_region (udata.region_start, udata.region_size, FALSE);
603 buffer_init (buf, 1024);
605 entry = (TrampolineEntry*)buf->p;
606 buf->p += sizeof (TrampolineEntry);
607 entry->id = ++id_generator;
608 entry->region_id = region_id;
609 entry->code = (gsize)info->code;
610 entry->code_size = info->code_size;
612 emit_unwind_info (info->unwind_ops, buf);
614 buffer_add_string (buf, info->name);
616 add_entry (ENTRY_TRAMPOLINE, buf);
621 mono_lldb_save_specific_trampoline_info (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, gpointer code, guint32 code_len)
624 * Avoid emitting these for now,
625 * they slow down execution too much, and they are
626 * only needed during single stepping which doesn't
630 TrampolineEntry *entry;
634 Buffer *buf = &tmpbuf;
639 /* Find the codegen region which contains the code */
640 memset (&udata, 0, sizeof (udata));
642 mono_global_codeman_foreach (find_code_region, &udata);
644 mono_domain_code_foreach (mono_get_root_domain (), find_code_region, &udata);
645 g_assert (udata.found);
647 region_id = register_codegen_region (udata.region_start, udata.region_size, FALSE);
649 buffer_init (buf, 1024);
651 entry = (TrampolineEntry*)buf->p;
652 buf->p += sizeof (TrampolineEntry);
653 entry->id = ++id_generator;
654 entry->region_id = region_id;
655 entry->code = (gsize)code;
656 entry->code_size = code_len;
658 GSList *unwind_ops = mono_unwind_get_cie_program ();
659 emit_unwind_info (unwind_ops, buf);
661 buffer_add_string (buf, "");
663 add_entry (ENTRY_TRAMPOLINE, buf);
672 Similar to the gdb jit interface. The runtime communicates with a plugin running inside lldb.
673 - The runtime allocates a data packet, points a symbol with a well known name at it.
674 - It calls a dummy function with a well known name.
675 - The plugin sets a breakpoint at this function, causing the runtime to be suspended.
676 - The plugin reads the data pointed to by the other symbol and processes it.
678 The data packets are kept in a list, so lldb can read all of them after attaching.
679 Lldb will associate an object file with each mono codegen region.
682 - use a flat byte array so the whole data can be read in one operation.
683 - use 64 bit ints for pointers.
689 mono_lldb_init (const char *options)
691 g_error ("lldb support has been disabled at configure time.");
695 mono_lldb_save_method_info (MonoCompile *cfg)
700 mono_lldb_save_trampoline_info (MonoTrampInfo *info)
705 mono_lldb_remove_method (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *info)
710 mono_lldb_save_specific_trampoline_info (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, gpointer code, guint32 code_len)