-
-/*
- * Support for emitting debug info for JITted code.
- *
- * This works as follows:
- * - the runtime writes out an xdb.s file containing DWARF debug info.
- * - the user calls a gdb macro
- * - the macro compiles and loads this shared library using add-symbol-file.
- *
- * This is based on the xdebug functionality in the Kaffe Java VM.
- *
- * We emit assembly code instead of using the ELF writer, so we can emit debug info
- * incrementally as each method is JITted, and the debugger doesn't have to call
- * into the runtime to emit the shared library, which would cause all kinds of
- * complications, like threading issues, and the fact that the ELF writer's
- * emit_writeout () function cannot be called more than once.
- * GDB 7.0 and later has a JIT interface.
- */
-
-#define USE_GDB_JIT_INTERFACE
-
-/* The recommended gdb macro is: */
-/*
- define xdb
- shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
- add-symbol-file xdb.so 0
- end
-*/
-
-/*
- * GDB JIT interface definitions.
- *
- * http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
- */
-typedef enum
-{
- JIT_NOACTION = 0,
- JIT_REGISTER_FN,
- JIT_UNREGISTER_FN
-} jit_actions_t;
-
-struct jit_code_entry
-{
- struct jit_code_entry *next_entry;
- struct jit_code_entry *prev_entry;
- const char *symfile_addr;
- uint64_t symfile_size;
-};
-
-struct jit_descriptor
-{
- uint32_t version;
- /* This type should be jit_actions_t, but we use uint32_t
- to be explicit about the bitwidth. */
- uint32_t action_flag;
- struct jit_code_entry *relevant_entry;
- struct jit_code_entry *first_entry;
-};
-
-/* GDB puts a breakpoint in this function. */
-void __attribute__((noinline)) __jit_debug_register_code(void);
-
-void __attribute__((noinline)) __jit_debug_register_code(void) { };
-
-/* Make sure to specify the version statically, because the
- debugger may check the version before we can set it. */
-struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
-
-static MonoDwarfWriter *xdebug_writer;
-static FILE *xdebug_fp, *il_file;
-static gboolean use_gdb_interface, save_symfiles;
-static int il_file_line_index;
-
-void
-mono_xdebug_init (char *options)
-{
- MonoImageWriter *w;
- char **args, **ptr;
-
- args = g_strsplit (options, ",", -1);
- for (ptr = args; ptr && *ptr; ptr ++) {
- char *arg = *ptr;
-
- if (!strcmp (arg, "gdb"))
- use_gdb_interface = TRUE;
- if (!strcmp (arg, "save-symfiles"))
- save_symfiles = TRUE;
- }
-
- /* This file will contain the IL code for methods which don't have debug info */
- il_file = fopen ("xdb.il", "w");
-
- if (use_gdb_interface)
- return;
-
- unlink ("xdb.s");
- xdebug_fp = fopen ("xdb.s", "w");
-
- w = img_writer_create (xdebug_fp, FALSE);
-
- img_writer_emit_start (w);
-
- xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE);
-
- /* Emit something so the file has a text segment */
- img_writer_emit_section_change (w, ".text", 0);
- img_writer_emit_string (w, "");
-
- mono_dwarf_writer_emit_base_info (xdebug_writer, arch_get_cie_program ());
-}
-
-static void
-xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
-{
- MonoImageWriter *w;
- MonoDwarfWriter *dw;
-
- w = img_writer_create (NULL, TRUE);
-
- img_writer_emit_start (w);
-
- /* This file will contain the IL code for methods which don't have debug info */
- if (!il_file)
- il_file = fopen ("xdb.il", "w");
-
- dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, FALSE);
-
- /* Emit something so the file has a text segment */
- img_writer_emit_section_change (w, ".text", 0);
- img_writer_emit_string (w, "");
-
- mono_dwarf_writer_emit_base_info (dw, arch_get_cie_program ());
-
- *out_w = w;
- *out_dw = dw;
-}
-
-static void
-xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
-{
- guint8 *img;
- guint32 img_size;
- struct jit_code_entry *entry;
-
- il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
- mono_dwarf_writer_close (dw);
-
- img_writer_emit_writeout (w);
-
- img = img_writer_get_output (w, &img_size);
-
- img_writer_destroy (w);
-
- if (save_symfiles && method) {
- /* Save the symbol files to help debugging */
- FILE *fp;
- char *file_name;
- static int file_counter;
-
- file_counter ++;
- file_name = g_strdup_printf ("xdb-%d.o", file_counter);
- printf ("%s -> %s\n", mono_method_full_name (method, TRUE), file_name);
-
- fp = fopen (file_name, "w");
- fwrite (img, img_size, 1, fp);
- fclose (fp);
- g_free (file_name);
- }
-
- /* Register the image with GDB */
-
- entry = g_malloc (sizeof (struct jit_code_entry));
-
- entry->symfile_addr = (const char*)img;
- entry->symfile_size = img_size;
-
- entry->next_entry = __jit_debug_descriptor.first_entry;
- if (__jit_debug_descriptor.first_entry)
- __jit_debug_descriptor.first_entry->prev_entry = entry;
- __jit_debug_descriptor.first_entry = entry;
-
- __jit_debug_descriptor.relevant_entry = entry;
- __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
-
- __jit_debug_register_code ();
-}
-
-/*
- * mono_save_xdebug_info:
- *
- * Emit debugging info for METHOD into an assembly file which can be assembled
- * and loaded into gdb to provide debugging info for JITted code.
- * LOCKING: Acquires the loader lock.
- */
-void
-mono_save_xdebug_info (MonoCompile *cfg)
-{
- if (use_gdb_interface) {
- MonoImageWriter *w;
- MonoDwarfWriter *dw;
-
- mono_loader_lock ();
-
- xdebug_begin_emit (&w, &dw);
-
- mono_dwarf_writer_emit_method (dw, cfg, cfg->jit_info->method, NULL, NULL, cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, mono_debug_find_method (cfg->jit_info->method, mono_domain_get ()));
-
- xdebug_end_emit (w, dw, cfg->jit_info->method);
-
- mono_loader_unlock ();
- } else {
- if (!xdebug_writer)
- return;
-
- mono_loader_lock ();
- mono_dwarf_writer_emit_method (xdebug_writer, cfg, cfg->jit_info->method, NULL, NULL, cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, mono_debug_find_method (cfg->jit_info->method, mono_domain_get ()));
- fflush (xdebug_fp);
- mono_loader_unlock ();
- }
-}
-
-/*
- * mono_save_trampoline_xdebug_info:
- *
- * Same as mono_save_xdebug_info, but for trampolines.
- * LOCKING: Acquires the loader lock.
- */
-void
-mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
-{
- if (use_gdb_interface) {
- MonoImageWriter *w;
- MonoDwarfWriter *dw;
-
- mono_loader_lock ();
-
- xdebug_begin_emit (&w, &dw);
-
- mono_dwarf_writer_emit_trampoline (dw, tramp_name, NULL, NULL, code, code_size, unwind_info);
-
- xdebug_end_emit (w, dw, NULL);
-
- mono_loader_unlock ();
- } else {
- if (!xdebug_writer)
- return;
-
- mono_loader_lock ();
- mono_dwarf_writer_emit_trampoline (xdebug_writer, tramp_name, NULL, NULL, code, code_size, unwind_info);
- fflush (xdebug_fp);
- mono_loader_unlock ();
- }
-}