[runtime] fix warnings on linux (gcc 8.3.0) (#18479)
[mono-project.git] / mono / mini / lldb.c
blob3201c65b3144e4257da43b20d57c4fc5a43f85d5
1 /**
2 * \file
3 * Mono support for LLDB.
5 * Author:
6 * Zoltan Varga (vargaz@gmail.com)
8 * Copyright 2016 Xamarin, Inc (http://www.xamarin.com)
9 */
11 #include "config.h"
12 #include "mini.h"
13 #include "lldb.h"
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)
21 typedef enum {
22 ENTRY_CODE_REGION = 1,
23 ENTRY_METHOD = 2,
24 ENTRY_TRAMPOLINE = 3,
25 ENTRY_UNLOAD_CODE_REGION = 4
26 } EntryType;
29 * Need to make sure these structures have the same size and alignment on
30 * all platforms.
33 /* One data packet sent from the runtime to the debugger */
34 typedef struct {
35 /* Pointer to the next entry */
36 guint64 next_addr;
37 /* The type of data pointed to by ADDR */
38 /* One of the ENTRY_ constants */
39 guint32 type;
40 /* Align */
41 guint32 dummy;
42 guint64 size;
43 guint64 addr;
44 } DebugEntry;
46 typedef struct
48 /* (MAJOR << 16) | MINOR */
49 guint32 version;
50 /* Align */
51 guint32 dummy;
52 DebugEntry *entry;
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 */
57 guint32 type;
58 guint32 dummy2;
59 guint64 size;
60 guint64 addr;
61 } JitDescriptor;
64 * Represents a memory region used for code.
66 typedef struct {
68 * OBJFILE_MAGIC. This is needed to make it easier for lldb to
69 * create object files from this packet.
71 char magic [32];
72 guint64 start;
73 guint32 size;
74 int id;
75 } CodeRegionEntry;
77 typedef struct {
78 int id;
79 } UnloadCodeRegionEntry;
82 * Represents a managed method
84 typedef struct {
85 guint64 code;
86 int id;
87 /* The id of the codegen region which contains CODE */
88 int region_id;
89 int code_size;
90 /* Align */
91 guint32 dummy;
92 /* Followed by variable size data */
93 } MethodEntry;
96 * Represents a trampoline
98 typedef struct {
99 guint64 code;
100 int id;
101 /* The id of the codegen region which contains CODE */
102 int region_id;
103 int code_size;
104 /* Align */
105 guint32 dummy;
106 /* Followed by variable size data */
107 } TrampolineEntry;
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)
128 G_BEGIN_DECLS
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__)
138 asm ("");
139 #endif
142 G_END_DECLS
145 * Functions to encode protocol data
148 typedef struct {
149 guint8 *buf, *p, *end;
150 } Buffer;
152 static void
153 buffer_init (Buffer *buf, int size)
155 buf->buf = (guint8 *)g_malloc (size);
156 buf->p = buf->buf;
157 buf->end = buf->buf + size;
160 static int
161 buffer_len (Buffer *buf)
163 return buf->p - buf->buf;
166 static void
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;
173 buf->buf = p;
174 buf->p = p + size;
175 buf->end = buf->buf + new_size;
179 static void
180 buffer_add_byte (Buffer *buf, guint8 val)
182 buffer_make_room (buf, 1);
183 buf->p [0] = val;
184 buf->p++;
187 static void
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;
195 buf->p += 4;
198 static void
199 buffer_add_data (Buffer *buf, guint8 *data, int len)
201 buffer_make_room (buf, len);
202 memcpy (buf->p, data, len);
203 buf->p += len;
206 static void
207 buffer_add_string (Buffer *buf, const char *str)
209 int len;
211 if (str == NULL) {
212 buffer_add_int (buf, 0);
213 } else {
214 len = strlen (str);
215 buffer_add_int (buf, len);
216 buffer_add_data (buf, (guint8*)str, len);
220 static void
221 buffer_free (Buffer *buf)
223 g_free (buf->buf);
226 typedef struct {
227 gpointer code;
228 gpointer region_start;
229 guint32 region_size;
230 gboolean found;
231 } UserData;
233 static int
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;
241 ud->found = TRUE;
242 return 1;
244 return 0;
247 static void
248 add_entry (EntryType type, Buffer *buf)
250 DebugEntry *entry;
251 guint8 *data;
252 int size = buffer_len (buf);
254 data = g_malloc (size);
255 memcpy (data, buf->buf, size);
257 entry = g_malloc0 (sizeof (DebugEntry));
258 entry->type = type;
259 entry->addr = (guint64)(gsize)data;
260 entry->size = size;
262 mono_memory_barrier ();
264 lldb_lock ();
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
268 if (last_entry) {
269 last_entry->next_addr = (guint64)(gsize) (entry);
270 last_entry = entry;
271 } else {
272 last_entry = 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 (&register_time, start);
286 num_entries ++;
287 //printf ("%lf %d %d\n", register_time, num_entries, entry->type);
289 lldb_unlock ();
293 * register_codegen_region:
295 * Register a codegen region with the debugger if needed.
296 * Return a region id.
298 static int
299 register_codegen_region (gpointer region_start, int region_size, gboolean dynamic)
301 CodeRegionEntry *region_entry;
302 int id;
303 Buffer tmp_buf;
304 Buffer *buf = &tmp_buf;
306 if (dynamic) {
307 lldb_lock ();
308 id = ++id_generator;
309 lldb_unlock ();
310 } else {
311 lldb_lock ();
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));
315 if (id) {
316 lldb_unlock ();
317 return id;
319 id = ++id_generator;
320 g_hash_table_insert (codegen_regions, region_start, GINT_TO_POINTER (id));
321 lldb_unlock ();
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);
335 buffer_free (buf);
336 return id;
339 static void
340 emit_unwind_info (GSList *unwind_ops, Buffer *buf)
342 int ret_reg;
343 int nunwind_ops;
344 GSList *l;
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 */
350 nunwind_ops = 0;
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)
356 break;
357 nunwind_ops ++;
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)
366 break;
367 buffer_add_int (buf, op->op);
368 buffer_add_int (buf, op->when);
369 int dreg;
370 #if TARGET_X86
371 // LLDB doesn't see to use the switched esp/ebp
372 if (op->reg == X86_ESP)
373 dreg = X86_ESP;
374 else if (op->reg == X86_EBP)
375 dreg = X86_EBP;
376 else
377 dreg = mono_hw_reg_to_dwarf_reg (op->reg);
378 #else
379 dreg = mono_hw_reg_to_dwarf_reg (op->reg);
380 #endif
381 buffer_add_int (buf, dreg);
382 buffer_add_int (buf, op->val);
386 void
387 mono_lldb_init (const char *options)
389 enabled = TRUE;
390 mono_os_mutex_init_recursive (&mutex);
392 mono_counters_register ("Time spent in LLDB", MONO_COUNTER_JIT | MONO_COUNTER_LONG | MONO_COUNTER_TIME, &register_time);
395 typedef struct
397 MonoSymSeqPoint sp;
398 int native_offset;
399 } FullSeqPoint;
401 static int
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;
410 void
411 mono_lldb_save_method_info (MonoCompile *cfg)
413 MethodEntry *entry;
414 UserData udata;
415 int region_id;
416 Buffer tmpbuf;
417 Buffer *buf = &tmpbuf;
418 MonoDebugMethodInfo *minfo;
419 int i, j, n_il_offsets;
420 int *source_files;
421 GPtrArray *source_file_list;
422 MonoSymSeqPoint *sym_seq_points;
423 FullSeqPoint *locs;
425 if (!enabled)
426 return;
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);
437 lldb_lock ();
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));
441 lldb_unlock ();
442 } else {
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);
462 g_free (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
477 int skipped = 0;
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];
482 // FIXME: O(n^2)
483 SeqPoint seq_point;
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;
486 } else {
487 locs [i].native_offset = 0xffffff;
488 skipped ++;
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);
507 g_free (locs);
508 g_free (source_files);
509 g_free (sym_seq_points);
510 g_ptr_array_free (source_file_list, TRUE);
511 } else {
512 buffer_add_int (buf, 0);
513 buffer_add_int (buf, 0);
516 add_entry (ENTRY_METHOD, buf);
517 buffer_free (buf);
520 void
521 mono_lldb_remove_method (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *info)
523 int region_id;
524 UnloadCodeRegionEntry *entry;
525 Buffer tmpbuf;
526 Buffer *buf = &tmpbuf;
528 if (!enabled)
529 return;
531 g_assert (method->dynamic);
533 lldb_lock ();
534 region_id = GPOINTER_TO_INT (g_hash_table_lookup (dyn_codegen_regions, method));
535 g_hash_table_remove (dyn_codegen_regions, method);
536 lldb_unlock ();
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);
545 buffer_free (buf);
547 /* The method is associated with the code region, so it doesn't have to be unloaded */
550 void
551 mono_lldb_save_trampoline_info (MonoTrampInfo *info)
553 TrampolineEntry *entry;
554 UserData udata;
555 int region_id;
556 Buffer tmpbuf;
557 Buffer *buf = &tmpbuf;
559 if (!enabled)
560 return;
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);
566 if (!udata.found)
567 mono_domain_code_foreach (mono_get_root_domain (), find_code_region, &udata);
568 if (!udata.found)
569 /* Can happen with AOT */
570 return;
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);
588 buffer_free (buf);
591 void
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
598 * work anyway.
600 #if 0
601 TrampolineEntry *entry;
602 UserData udata;
603 int region_id;
604 Buffer tmpbuf;
605 Buffer *buf = &tmpbuf;
607 if (!enabled)
608 return;
610 /* Find the codegen region which contains the code */
611 memset (&udata, 0, sizeof (udata));
612 udata.code = code;
613 mono_global_codeman_foreach (find_code_region, &udata);
614 if (!udata.found)
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);
635 buffer_free (buf);
636 #endif
640 DESIGN:
642 Communication:
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.
652 Packet design:
653 - use a flat byte array so the whole data can be read in one operation.
654 - use 64 bit ints for pointers.
657 #else
659 void
660 mono_lldb_init (const char *options)
662 g_error ("lldb support has been disabled at configure time.");
665 void
666 mono_lldb_save_method_info (MonoCompile *cfg)
670 void
671 mono_lldb_save_trampoline_info (MonoTrampInfo *info)
675 void
676 mono_lldb_remove_method (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *info)
680 void
681 mono_lldb_save_specific_trampoline_info (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, gpointer code, guint32 code_len)
685 #endif