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 #include <sys/types.h>
51 #include "image-writer.h"
53 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) && USE_BIN_WRITER
55 #include "dwarfwriter.h"
57 #include "mono/utils/mono-compiler.h"
59 #define USE_GDB_JIT_INTERFACE
61 /* The recommended gdb macro is: */
64 shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
65 add-symbol-file xdb.so 0
70 * GDB JIT interface definitions.
72 * http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
83 struct jit_code_entry *next_entry;
84 struct jit_code_entry *prev_entry;
85 const char *symfile_addr;
87 * The gdb code in gdb/jit.c which reads this structure ignores alignment
88 * requirements, so use two 32 bit fields.
90 guint32 symfile_size1, symfile_size2;
96 /* This type should be jit_actions_t, but we use guint32
97 to be explicit about the bitwidth. */
99 struct jit_code_entry *relevant_entry;
100 struct jit_code_entry *first_entry;
103 /* GDB puts a breakpoint in this function. */
104 void MONO_NEVER_INLINE __jit_debug_register_code(void);
106 #if !defined(MONO_LLVM_LOADED) && defined(ENABLE_LLVM) && !defined(MONO_CROSS_COMPILE)
108 /* LLVM already defines these */
110 extern struct jit_descriptor __jit_debug_descriptor;
114 /* gcc seems to inline/eliminate calls to noinline functions, thus the asm () */
115 void MONO_NEVER_INLINE __jit_debug_register_code(void) {
116 #if defined(__GNUC__)
121 /* Make sure to specify the version statically, because the
122 debugger may check the version before we can set it. */
123 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
127 static MonoImageWriter *xdebug_w;
128 static MonoDwarfWriter *xdebug_writer;
129 static FILE *xdebug_fp, *il_file;
130 static gboolean use_gdb_interface, save_symfiles;
131 static int il_file_line_index;
132 static GHashTable *xdebug_syms;
135 mono_xdebug_init (const char *options)
140 args = g_strsplit (options, ",", -1);
141 for (ptr = args; ptr && *ptr; ptr ++) {
144 if (!strcmp (arg, "gdb"))
145 use_gdb_interface = TRUE;
146 if (!strcmp (arg, "save-symfiles"))
147 save_symfiles = TRUE;
150 /* This file will contain the IL code for methods which don't have debug info */
151 il_file = fopen ("xdb.il", "w");
152 if (il_file == NULL) {
153 use_gdb_interface = FALSE;
154 g_warning ("** Unable to create xdb.il. Managed symbol names won't be available.");
158 if (use_gdb_interface)
162 xdebug_fp = fopen ("xdb.s", "w");
164 w = mono_img_writer_create (xdebug_fp, FALSE);
166 mono_img_writer_emit_start (w);
168 xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE);
170 /* Emit something so the file has a text segment */
171 mono_img_writer_emit_section_change (w, ".text", 0);
172 mono_img_writer_emit_string (w, "");
174 mono_dwarf_writer_emit_base_info (xdebug_writer, "JITted code", mono_unwind_get_cie_program ());
178 xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
183 w = mono_img_writer_create (NULL, TRUE);
185 mono_img_writer_emit_start (w);
187 /* This file will contain the IL code for methods which don't have debug info */
189 il_file = fopen ("xdb.il", "w");
191 dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, TRUE);
193 mono_dwarf_writer_emit_base_info (dw, "JITted code", mono_unwind_get_cie_program ());
200 xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
204 struct jit_code_entry *entry;
207 il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
208 mono_dwarf_writer_close (dw);
210 mono_img_writer_emit_writeout (w);
212 img = mono_img_writer_get_output (w, &img_size);
214 mono_img_writer_destroy (w);
217 /* Save the symbol files to help debugging */
220 static int file_counter;
223 file_name = g_strdup_printf ("xdb-%d.o", file_counter);
224 printf ("%s %p %d\n", file_name, img, img_size);
226 fp = fopen (file_name, "w");
227 fwrite (img, img_size, 1, fp);
232 /* Register the image with GDB */
234 entry = g_malloc0 (sizeof (struct jit_code_entry));
236 entry->symfile_addr = (const char*)img;
237 psize = (guint64*)&entry->symfile_size1;
240 entry->next_entry = __jit_debug_descriptor.first_entry;
241 if (__jit_debug_descriptor.first_entry)
242 __jit_debug_descriptor.first_entry->prev_entry = entry;
243 __jit_debug_descriptor.first_entry = entry;
245 __jit_debug_descriptor.relevant_entry = entry;
246 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
248 __jit_debug_register_code ();
254 * This could be called from inside gdb to flush the debugging information not yet
255 * registered with gdb.
258 mono_xdebug_flush (void)
261 xdebug_end_emit (xdebug_w, xdebug_writer, NULL);
263 xdebug_begin_emit (&xdebug_w, &xdebug_writer);
266 static int xdebug_method_count;
269 * mono_save_xdebug_info:
271 * Emit debugging info for METHOD into an assembly file which can be assembled
272 * and loaded into gdb to provide debugging info for JITted code.
273 * LOCKING: Acquires the loader lock.
276 mono_save_xdebug_info (MonoCompile *cfg)
278 MonoDebugMethodJitInfo *dmji;
280 if (use_gdb_interface) {
284 xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
287 * gdb is not designed to handle 1000s of symbol files (one per method). So we
288 * group them into groups of 100.
290 if ((xdebug_method_count % 100) == 0)
291 mono_xdebug_flush ();
293 xdebug_method_count ++;
295 dmji = mono_debug_find_method (jinfo_get_method (cfg->jit_info), mono_domain_get ());;
296 mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, NULL,
297 cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, dmji);
298 mono_debug_free_method_jit_info (dmji);
302 * Emit a symbol for the code by emitting it at the beginning of the text
303 * segment, and setting the text segment to have an absolute address.
304 * This symbol can be used to set breakpoints in gdb.
305 * FIXME: This doesn't work when multiple methods are emitted into the same file.
307 sym = get_debug_sym (cfg->jit_info->method, "", xdebug_syms);
308 mono_img_writer_emit_section_change (w, ".text", 0);
309 if (!xdebug_text_addr) {
310 xdebug_text_addr = cfg->jit_info->code_start;
311 mono_img_writer_set_section_addr (w, (gssize)xdebug_text_addr);
313 mono_img_writer_emit_global_with_size (w, sym, cfg->jit_info->code_size, TRUE);
314 mono_img_writer_emit_label (w, sym);
315 mono_img_writer_emit_bytes (w, cfg->jit_info->code_start, cfg->jit_info->code_size);
319 mono_loader_unlock ();
325 dmji = mono_debug_find_method (jinfo_get_method (cfg->jit_info), mono_domain_get ());
326 mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, NULL,
327 cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, dmji);
328 mono_debug_free_method_jit_info (dmji);
330 mono_loader_unlock ();
336 * mono_save_trampoline_xdebug_info:
338 * Same as mono_save_xdebug_info, but for trampolines.
339 * LOCKING: Acquires the loader lock.
342 mono_save_trampoline_xdebug_info (MonoTrampInfo *info)
344 const char *info_name = info->name;
345 if (info_name == NULL)
348 if (use_gdb_interface) {
352 /* This can be called before the loader lock is initialized */
353 mono_loader_lock_if_inited ();
355 xdebug_begin_emit (&w, &dw);
357 mono_dwarf_writer_emit_trampoline (dw, info_name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
359 xdebug_end_emit (w, dw, NULL);
361 mono_loader_unlock_if_inited ();
366 mono_loader_lock_if_inited ();
367 mono_dwarf_writer_emit_trampoline (xdebug_writer, info_name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
369 mono_loader_unlock_if_inited ();
373 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
376 mono_xdebug_init (const char *options)
381 mono_save_xdebug_info (MonoCompile *cfg)
386 mono_save_trampoline_xdebug_info (MonoTrampInfo *info)