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/mono-debug.h>
16 #include <mono/metadata/mono-debug-debugger.h>
17 #include <mono/metadata/debug-mono-symfile.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 */
52 /* Keep these as pointers so accessing them is atomic */
54 /* List of all entries */
55 DebugEntry *all_entries;
59 * Represents a memory region used for code.
63 * OBJFILE_MAGIC. This is needed to make it easier for lldb to
64 * create object files from this packet.
74 } UnloadCodeRegionEntry;
77 * Represents a managed method
82 /* The id of the codegen region which contains CODE */
87 /* Followed by variable size data */
91 * Represents a trampoline
96 /* The id of the codegen region which contains CODE */
101 /* Followed by variable size data */
104 #define MAJOR_VERSION 1
105 #define MINOR_VERSION 0
107 static const char* OBJFILE_MAGIC = { "MONO_JIT_OBJECT_FILE" };
109 JitDescriptor __mono_jit_debug_descriptor = { (MAJOR_VERSION << 16) | MINOR_VERSION };
111 static gboolean enabled;
112 static int id_generator;
113 static GHashTable *codegen_regions;
114 static DebugEntry *last_entry;
115 static mono_mutex_t mutex;
116 static GHashTable *dyn_codegen_regions;
118 #define lldb_lock() mono_os_mutex_lock (&mutex)
119 #define lldb_unlock() mono_os_mutex_unlock (&mutex)
121 void MONO_NEVER_INLINE __mono_jit_debug_register_code (void);
123 /* The native debugger puts a breakpoint in this function. */
124 void MONO_NEVER_INLINE
125 __mono_jit_debug_register_code (void)
127 /* Make sure that even compilers that ignore __noinline__ don't inline this */
128 #if defined(__GNUC__)
134 * Functions to encode protocol data
138 guint8 *buf, *p, *end;
142 buffer_init (Buffer *buf, int size)
144 buf->buf = (guint8 *)g_malloc (size);
146 buf->end = buf->buf + size;
150 buffer_len (Buffer *buf)
152 return buf->p - buf->buf;
156 buffer_make_room (Buffer *buf, int size)
158 if (buf->end - buf->p < size) {
159 int new_size = buf->end - buf->buf + size + 32;
160 guint8 *p = (guint8 *)g_realloc (buf->buf, new_size);
161 size = buf->p - buf->buf;
164 buf->end = buf->buf + new_size;
169 buffer_add_byte (Buffer *buf, guint8 val)
171 buffer_make_room (buf, 1);
177 buffer_add_short (Buffer *buf, guint32 val)
179 buffer_make_room (buf, 2);
180 buf->p [0] = (val >> 8) & 0xff;
181 buf->p [1] = (val >> 0) & 0xff;
186 buffer_add_int (Buffer *buf, guint32 val)
188 buffer_make_room (buf, 4);
189 buf->p [0] = (val >> 24) & 0xff;
190 buf->p [1] = (val >> 16) & 0xff;
191 buf->p [2] = (val >> 8) & 0xff;
192 buf->p [3] = (val >> 0) & 0xff;
197 buffer_add_long (Buffer *buf, guint64 l)
199 buffer_add_int (buf, (l >> 32) & 0xffffffff);
200 buffer_add_int (buf, (l >> 0) & 0xffffffff);
204 buffer_add_id (Buffer *buf, int id)
206 buffer_add_int (buf, (guint64)id);
210 buffer_add_data (Buffer *buf, guint8 *data, int len)
212 buffer_make_room (buf, len);
213 memcpy (buf->p, data, len);
218 buffer_add_string (Buffer *buf, const char *str)
223 buffer_add_int (buf, 0);
226 buffer_add_int (buf, len);
227 buffer_add_data (buf, (guint8*)str, len);
232 buffer_add_buffer (Buffer *buf, Buffer *data)
234 buffer_add_data (buf, data->buf, buffer_len (data));
238 buffer_free (Buffer *buf)
245 gpointer region_start;
251 find_code_region (void *data, int csize, int size, void *user_data)
253 UserData *ud = user_data;
255 if ((char*)ud->code >= (char*)data && (char*)ud->code < (char*)data + csize) {
256 ud->region_start = data;
257 ud->region_size = csize;
265 add_entry (EntryType type, Buffer *buf)
269 int size = buffer_len (buf);
271 data = g_malloc (size);
272 memcpy (data, buf->buf, size);
274 entry = g_malloc0 (sizeof (DebugEntry));
276 entry->addr = (guint64)(gsize)data;
279 mono_memory_barrier ();
283 /* The debugger can read the list of entries asynchronously, so this has to be async safe */
284 // FIXME: Make sure this is async safe
286 last_entry->next_addr = (guint64)(gsize) (entry);
290 __mono_jit_debug_descriptor.all_entries = entry;
293 __mono_jit_debug_descriptor.entry = entry;
294 __mono_jit_debug_register_code ();
300 * register_codegen_region:
302 * Register a codegen region with the debugger if needed.
303 * Return a region id.
306 register_codegen_region (gpointer region_start, int region_size, gboolean dynamic)
308 CodeRegionEntry *region_entry;
311 Buffer *buf = &tmp_buf;
319 if (!codegen_regions)
320 codegen_regions = g_hash_table_new (NULL, NULL);
321 id = GPOINTER_TO_INT (g_hash_table_lookup (codegen_regions, region_start));
327 g_hash_table_insert (codegen_regions, region_start, GINT_TO_POINTER (id));
331 buffer_init (buf, 128);
333 region_entry = (CodeRegionEntry*)buf->p;
334 buf->p += sizeof (CodeRegionEntry);
335 memset (region_entry, 0, sizeof (CodeRegionEntry));
336 strcpy (region_entry->magic, OBJFILE_MAGIC);
337 region_entry->id = id;
338 region_entry->start = (gsize)region_start;
339 region_entry->size = (gsize)region_size;
341 add_entry (ENTRY_CODE_REGION, buf);
347 emit_unwind_info (GSList *unwind_ops, Buffer *buf)
353 ret_reg = mono_unwind_get_dwarf_pc_reg ();
354 g_assert (ret_reg < 256);
356 /* We use the unencoded version of the unwind info to make it easier to decode */
358 for (l = unwind_ops; l; l = l->next) {
359 MonoUnwindOp *op = l->data;
361 /* lldb can't handle these */
362 if (op->op == DW_CFA_mono_advance_loc)
367 buffer_add_byte (buf, ret_reg);
368 buffer_add_int (buf, nunwind_ops);
369 for (l = unwind_ops; l; l = l->next) {
370 MonoUnwindOp *op = l->data;
372 if (op->op == DW_CFA_mono_advance_loc)
374 buffer_add_int (buf, op->op);
375 buffer_add_int (buf, op->when);
378 // LLDB doesn't see to use the switched esp/ebp
379 if (op->reg == X86_ESP)
381 else if (op->reg == X86_EBP)
384 dreg = mono_hw_reg_to_dwarf_reg (op->reg);
386 dreg = mono_hw_reg_to_dwarf_reg (op->reg);
388 buffer_add_int (buf, dreg);
389 buffer_add_int (buf, op->val);
394 mono_lldb_init (const char *options)
397 mono_os_mutex_init_recursive (&mutex);
407 compare_by_addr (const void *arg1, const void *arg2)
409 const FullSeqPoint *sp1 = arg1;
410 const FullSeqPoint *sp2 = arg2;
412 return sp1->native_offset - sp2->native_offset;
416 mono_lldb_save_method_info (MonoCompile *cfg)
422 Buffer *buf = &tmpbuf;
423 MonoDebugMethodInfo *minfo;
424 int i, j, n_il_offsets;
426 GPtrArray *source_file_list;
427 MonoSymSeqPoint *sym_seq_points;
433 /* Find the codegen region which contains the code */
434 memset (&udata, 0, sizeof (udata));
435 udata.code = cfg->native_code;
436 if (cfg->method->dynamic) {
437 mono_code_manager_foreach (cfg->dynamic_info->code_mp, find_code_region, &udata);
438 g_assert (udata.found);
440 region_id = register_codegen_region (udata.region_start, udata.region_size, TRUE);
443 if (!dyn_codegen_regions)
444 dyn_codegen_regions = g_hash_table_new (NULL, NULL);
445 g_hash_table_insert (dyn_codegen_regions, cfg->method, GINT_TO_POINTER (region_id));
448 mono_domain_code_foreach (cfg->domain, find_code_region, &udata);
449 g_assert (udata.found);
451 region_id = register_codegen_region (udata.region_start, udata.region_size, FALSE);
454 buffer_init (buf, 256);
456 entry = (MethodEntry*)buf->p;
457 buf->p += sizeof (MethodEntry);
458 entry->id = ++id_generator;
459 entry->region_id = region_id;
460 entry->code = (gsize)cfg->native_code;
461 entry->code_size = cfg->code_size;
463 emit_unwind_info (cfg->unwind_ops, buf);
465 char *s = mono_method_full_name (cfg->method, TRUE);
466 buffer_add_string (buf, s);
469 minfo = mono_debug_lookup_method (cfg->method);
470 MonoSeqPointInfo *seq_points = cfg->seq_point_info;
471 if (minfo && seq_points) {
472 mono_debug_get_seq_points (minfo, NULL, &source_file_list, &source_files, &sym_seq_points, &n_il_offsets);
473 buffer_add_int (buf, source_file_list->len);
474 for (i = 0; i < source_file_list->len; ++i) {
475 MonoDebugSourceInfo *sinfo = (MonoDebugSourceInfo *)g_ptr_array_index (source_file_list, i);
476 buffer_add_string (buf, sinfo->source_file);
477 for (j = 0; j < 16; ++j)
478 buffer_add_byte (buf, sinfo->hash [j]);
481 // The sym seq points are ordered by il offset, need to order them by address
483 locs = g_new0 (FullSeqPoint, n_il_offsets);
484 for (i = 0; i < n_il_offsets; ++i) {
485 locs [i].sp = sym_seq_points [i];
489 if (mono_seq_point_find_by_il_offset (seq_points, sym_seq_points [i].il_offset, &seq_point)) {
490 locs [i].native_offset = seq_point.native_offset;
492 locs [i].native_offset = 0xffffff;
496 qsort (locs, n_il_offsets, sizeof (FullSeqPoint), compare_by_addr);
498 n_il_offsets -= skipped;
499 buffer_add_int (buf, n_il_offsets);
500 for (i = 0; i < n_il_offsets; ++i) {
501 MonoSymSeqPoint *sp = &locs [i].sp;
502 const char *srcfile = "";
504 if (source_files [i] != -1) {
505 MonoDebugSourceInfo *sinfo = (MonoDebugSourceInfo *)g_ptr_array_index (source_file_list, source_files [i]);
506 srcfile = sinfo->source_file;
509 //printf ("%s %x %d %d\n", cfg->method->name, locs [i].native_offset, sp->il_offset, sp->line);
510 buffer_add_int (buf, locs [i].native_offset);
511 buffer_add_int (buf, sp->il_offset);
512 buffer_add_int (buf, sp->line);
513 buffer_add_int (buf, source_files [i]);
514 buffer_add_int (buf, sp->column);
515 buffer_add_int (buf, sp->end_line);
516 buffer_add_int (buf, sp->end_column);
519 g_free (source_files);
520 g_free (sym_seq_points);
521 g_ptr_array_free (source_file_list, TRUE);
523 buffer_add_int (buf, 0);
524 buffer_add_int (buf, 0);
527 add_entry (ENTRY_METHOD, buf);
532 mono_lldb_remove_method (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *info)
535 UnloadCodeRegionEntry *entry;
537 Buffer *buf = &tmpbuf;
539 g_assert (method->dynamic);
542 region_id = GPOINTER_TO_INT (g_hash_table_lookup (dyn_codegen_regions, method));
543 g_hash_table_remove (dyn_codegen_regions, method);
546 buffer_init (buf, 256);
548 entry = (UnloadCodeRegionEntry*)buf->p;
549 buf->p += sizeof (UnloadCodeRegionEntry);
550 entry->id = region_id;
552 add_entry (ENTRY_UNLOAD_CODE_REGION, buf);
555 /* The method is associated with the code region, so it doesn't have to be unloaded */
559 mono_lldb_save_trampoline_info (MonoTrampInfo *info)
561 TrampolineEntry *entry;
565 Buffer *buf = &tmpbuf;
570 /* Find the codegen region which contains the code */
571 memset (&udata, 0, sizeof (udata));
572 udata.code = info->code;
573 mono_global_codeman_foreach (find_code_region, &udata);
575 mono_domain_code_foreach (mono_get_root_domain (), find_code_region, &udata);
576 g_assert (udata.found);
578 region_id = register_codegen_region (udata.region_start, udata.region_size, FALSE);
580 buffer_init (buf, 1024);
582 entry = (TrampolineEntry*)buf->p;
583 buf->p += sizeof (TrampolineEntry);
584 entry->id = ++id_generator;
585 entry->region_id = region_id;
586 entry->code = (gsize)info->code;
587 entry->code_size = info->code_size;
589 emit_unwind_info (info->unwind_ops, buf);
591 buffer_add_string (buf, info->name);
593 add_entry (ENTRY_TRAMPOLINE, buf);
598 mono_lldb_save_specific_trampoline_info (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, gpointer code, guint32 code_len)
600 TrampolineEntry *entry;
604 Buffer *buf = &tmpbuf;
609 /* Find the codegen region which contains the code */
610 memset (&udata, 0, sizeof (udata));
612 mono_global_codeman_foreach (find_code_region, &udata);
614 mono_domain_code_foreach (mono_get_root_domain (), find_code_region, &udata);
615 g_assert (udata.found);
617 region_id = register_codegen_region (udata.region_start, udata.region_size, FALSE);
619 buffer_init (buf, 1024);
621 entry = (TrampolineEntry*)buf->p;
622 buf->p += sizeof (TrampolineEntry);
623 entry->id = ++id_generator;
624 entry->region_id = region_id;
625 entry->code = (gsize)code;
626 entry->code_size = code_len;
628 GSList *unwind_ops = mono_arch_get_cie_program ();
629 emit_unwind_info (unwind_ops, buf);
631 buffer_add_string (buf, "");
633 add_entry (ENTRY_TRAMPOLINE, buf);
641 Similar to the gdb jit interface. The runtime communicates with a plugin running inside lldb.
642 - The runtime allocates a data packet, points a symbol with a well known name at it.
643 - It calls a dummy function with a well known name.
644 - The plugin sets a breakpoint at this function, causing the runtime to be suspended.
645 - The plugin reads the data pointed to by the other symbol and processes it.
647 The data packets are kept in a list, so lldb can read all of them after attaching.
648 Lldb will associate an object file with each mono codegen region.
651 - use a flat byte array so the whole data can be read in one operation.
652 - use 64 bit ints for pointers.
658 mono_lldb_init (const char *options)
660 g_error ("lldb support has been disabled at configure time.");
664 mono_lldb_save_method_info (MonoCompile *cfg)
669 mono_lldb_save_trampoline_info (MonoTrampInfo *info)
674 mono_lldb_remove_method (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *info)
679 mono_lldb_save_specific_trampoline_info (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, gpointer code, guint32 code_len)