3 * Support for emitting gdb debug info for JITted code.
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2010 Novell, Inc.
12 * This works as follows:
13 * - the runtime writes out an xdb.s file containing DWARF debug info.
14 * - the user calls a gdb macro
15 * - the macro compiles and loads this shared library using add-symbol-file.
17 * This is based on the xdebug functionality in the Kaffe Java VM.
19 * We emit assembly code instead of using the ELF writer, so we can emit debug info
20 * incrementally as each method is JITted, and the debugger doesn't have to call
21 * into the runtime to emit the shared library, which would cause all kinds of
22 * complications, like threading issues, and the fact that the ELF writer's
23 * emit_writeout () function cannot be called more than once.
24 * GDB 7.0 and later has a JIT interface.
31 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) && USE_BIN_WRITER
32 #include <sys/types.h>
52 #include "image-writer.h"
53 #include "dwarfwriter.h"
55 #include "mono/utils/mono-compiler.h"
57 #define USE_GDB_JIT_INTERFACE
59 /* The recommended gdb macro is: */
62 shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
63 add-symbol-file xdb.so 0
68 * GDB JIT interface definitions.
70 * http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
81 struct jit_code_entry *next_entry;
82 struct jit_code_entry *prev_entry;
83 const char *symfile_addr;
85 * The gdb code in gdb/jit.c which reads this structure ignores alignment
86 * requirements, so use two 32 bit fields.
88 guint32 symfile_size1, symfile_size2;
94 /* This type should be jit_actions_t, but we use guint32
95 to be explicit about the bitwidth. */
97 struct jit_code_entry *relevant_entry;
98 struct jit_code_entry *first_entry;
101 /* GDB puts a breakpoint in this function. */
102 void MONO_NEVER_INLINE __jit_debug_register_code(void);
104 #if !defined(MONO_LLVM_LOADED) && defined(ENABLE_LLVM) && !defined(MONO_CROSS_COMPILE)
106 /* LLVM already defines these */
108 extern struct jit_descriptor __jit_debug_descriptor;
112 /* gcc seems to inline/eliminate calls to noinline functions, thus the asm () */
113 void MONO_NEVER_INLINE __jit_debug_register_code(void) {
114 #if defined(__GNUC__)
119 /* Make sure to specify the version statically, because the
120 debugger may check the version before we can set it. */
121 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
125 static MonoImageWriter *xdebug_w;
126 static MonoDwarfWriter *xdebug_writer;
127 static FILE *xdebug_fp, *il_file;
128 static gboolean use_gdb_interface, save_symfiles;
129 static int il_file_line_index;
130 static GHashTable *xdebug_syms;
133 mono_xdebug_init (const char *options)
138 args = g_strsplit (options, ",", -1);
139 for (ptr = args; ptr && *ptr; ptr ++) {
142 if (!strcmp (arg, "gdb"))
143 use_gdb_interface = TRUE;
144 if (!strcmp (arg, "save-symfiles"))
145 save_symfiles = TRUE;
148 /* This file will contain the IL code for methods which don't have debug info */
149 il_file = fopen ("xdb.il", "w");
150 if (il_file == NULL) {
151 use_gdb_interface = FALSE;
152 g_warning ("** Unable to create xdb.il. Managed symbol names won't be available.");
156 if (use_gdb_interface)
160 xdebug_fp = fopen ("xdb.s", "w");
162 w = mono_img_writer_create (xdebug_fp, FALSE);
164 mono_img_writer_emit_start (w);
166 xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE);
168 /* Emit something so the file has a text segment */
169 mono_img_writer_emit_section_change (w, ".text", 0);
170 mono_img_writer_emit_string (w, "");
172 mono_dwarf_writer_emit_base_info (xdebug_writer, "JITted code", mono_unwind_get_cie_program ());
176 xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
181 w = mono_img_writer_create (NULL, TRUE);
183 mono_img_writer_emit_start (w);
185 /* This file will contain the IL code for methods which don't have debug info */
187 il_file = fopen ("xdb.il", "w");
189 dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, TRUE);
191 mono_dwarf_writer_emit_base_info (dw, "JITted code", mono_unwind_get_cie_program ());
198 xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
202 struct jit_code_entry *entry;
205 il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
206 mono_dwarf_writer_close (dw);
208 mono_img_writer_emit_writeout (w);
210 img = mono_img_writer_get_output (w, &img_size);
212 mono_img_writer_destroy (w);
215 /* Save the symbol files to help debugging */
218 static int file_counter;
221 file_name = g_strdup_printf ("xdb-%d.o", file_counter);
222 printf ("%s %p %d\n", file_name, img, img_size);
224 fp = fopen (file_name, "w");
225 fwrite (img, img_size, 1, fp);
230 /* Register the image with GDB */
232 entry = g_malloc0 (sizeof (struct jit_code_entry));
234 entry->symfile_addr = (const char*)img;
235 psize = (guint64*)&entry->symfile_size1;
238 entry->next_entry = __jit_debug_descriptor.first_entry;
239 if (__jit_debug_descriptor.first_entry)
240 __jit_debug_descriptor.first_entry->prev_entry = entry;
241 __jit_debug_descriptor.first_entry = entry;
243 __jit_debug_descriptor.relevant_entry = entry;
244 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
246 __jit_debug_register_code ();
252 * This could be called from inside gdb to flush the debugging information not yet
253 * registered with gdb.
256 mono_xdebug_flush (void)
259 xdebug_end_emit (xdebug_w, xdebug_writer, NULL);
261 xdebug_begin_emit (&xdebug_w, &xdebug_writer);
264 static int xdebug_method_count;
267 * mono_save_xdebug_info:
269 * Emit debugging info for METHOD into an assembly file which can be assembled
270 * and loaded into gdb to provide debugging info for JITted code.
271 * LOCKING: Acquires the loader lock.
274 mono_save_xdebug_info (MonoCompile *cfg)
276 MonoDebugMethodJitInfo *dmji;
278 if (use_gdb_interface) {
282 xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
285 * gdb is not designed to handle 1000s of symbol files (one per method). So we
286 * group them into groups of 100.
288 if ((xdebug_method_count % 100) == 0)
289 mono_xdebug_flush ();
291 xdebug_method_count ++;
293 dmji = mono_debug_find_method (jinfo_get_method (cfg->jit_info), mono_domain_get ());;
294 mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, NULL,
295 cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, dmji);
296 mono_debug_free_method_jit_info (dmji);
300 * Emit a symbol for the code by emitting it at the beginning of the text
301 * segment, and setting the text segment to have an absolute address.
302 * This symbol can be used to set breakpoints in gdb.
303 * FIXME: This doesn't work when multiple methods are emitted into the same file.
305 sym = get_debug_sym (cfg->jit_info->method, "", xdebug_syms);
306 mono_img_writer_emit_section_change (w, ".text", 0);
307 if (!xdebug_text_addr) {
308 xdebug_text_addr = cfg->jit_info->code_start;
309 mono_img_writer_set_section_addr (w, (gssize)xdebug_text_addr);
311 mono_img_writer_emit_global_with_size (w, sym, cfg->jit_info->code_size, TRUE);
312 mono_img_writer_emit_label (w, sym);
313 mono_img_writer_emit_bytes (w, cfg->jit_info->code_start, cfg->jit_info->code_size);
317 mono_loader_unlock ();
323 dmji = mono_debug_find_method (jinfo_get_method (cfg->jit_info), mono_domain_get ());
324 mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, NULL,
325 cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, dmji);
326 mono_debug_free_method_jit_info (dmji);
328 mono_loader_unlock ();
334 * mono_save_trampoline_xdebug_info:
336 * Same as mono_save_xdebug_info, but for trampolines.
337 * LOCKING: Acquires the loader lock.
340 mono_save_trampoline_xdebug_info (MonoTrampInfo *info)
342 if (use_gdb_interface) {
346 /* This can be called before the loader lock is initialized */
347 mono_loader_lock_if_inited ();
349 xdebug_begin_emit (&w, &dw);
351 mono_dwarf_writer_emit_trampoline (dw, info->name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
353 xdebug_end_emit (w, dw, NULL);
355 mono_loader_unlock_if_inited ();
360 mono_loader_lock_if_inited ();
361 mono_dwarf_writer_emit_trampoline (xdebug_writer, info->name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
363 mono_loader_unlock_if_inited ();
367 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
370 mono_xdebug_init (const char *options)
375 mono_save_xdebug_info (MonoCompile *cfg)
380 mono_save_trampoline_xdebug_info (MonoTrampInfo *info)