2 * xdebug.c: Support for emitting gdb debug info for JITted code.
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2010 Novell, Inc.
11 * This works as follows:
12 * - the runtime writes out an xdb.s file containing DWARF debug info.
13 * - the user calls a gdb macro
14 * - the macro compiles and loads this shared library using add-symbol-file.
16 * This is based on the xdebug functionality in the Kaffe Java VM.
18 * We emit assembly code instead of using the ELF writer, so we can emit debug info
19 * incrementally as each method is JITted, and the debugger doesn't have to call
20 * into the runtime to emit the shared library, which would cause all kinds of
21 * complications, like threading issues, and the fact that the ELF writer's
22 * emit_writeout () function cannot be called more than once.
23 * GDB 7.0 and later has a JIT interface.
26 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
29 #include <sys/types.h>
50 #include "image-writer.h"
51 #include "dwarfwriter.h"
53 #define USE_GDB_JIT_INTERFACE
55 /* The recommended gdb macro is: */
58 shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
59 add-symbol-file xdb.so 0
64 * GDB JIT interface definitions.
66 * http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
77 struct jit_code_entry *next_entry;
78 struct jit_code_entry *prev_entry;
79 const char *symfile_addr;
86 /* This type should be jit_actions_t, but we use guint32
87 to be explicit about the bitwidth. */
89 struct jit_code_entry *relevant_entry;
90 struct jit_code_entry *first_entry;
95 #define MONO_NOINLINE __declspec (noinline)
97 #define MONO_NOINLINE __attribute__((noinline))
100 /* GDB puts a breakpoint in this function. */
101 void MONO_NOINLINE __jit_debug_register_code(void);
103 #if defined(ENABLE_LLVM) && ((LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION >= 7) || LLVM_MAJOR_VERSION > 2)
104 /* LLVM already defines these */
105 extern struct jit_descriptor __jit_debug_descriptor;
108 /* Make sure to specify the version statically, because the
109 debugger may check the version before we can set it. */
110 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
112 void MONO_NOINLINE __jit_debug_register_code(void) { };
115 static MonoImageWriter *xdebug_w;
116 static MonoDwarfWriter *xdebug_writer;
117 static FILE *xdebug_fp, *il_file;
118 static gboolean use_gdb_interface, save_symfiles;
119 static int il_file_line_index;
120 static GHashTable *xdebug_syms;
123 mono_xdebug_init (char *options)
128 args = g_strsplit (options, ",", -1);
129 for (ptr = args; ptr && *ptr; ptr ++) {
132 if (!strcmp (arg, "gdb"))
133 use_gdb_interface = TRUE;
134 if (!strcmp (arg, "save-symfiles"))
135 save_symfiles = TRUE;
138 /* This file will contain the IL code for methods which don't have debug info */
139 il_file = fopen ("xdb.il", "w");
141 if (use_gdb_interface)
145 xdebug_fp = fopen ("xdb.s", "w");
147 w = img_writer_create (xdebug_fp, FALSE);
149 img_writer_emit_start (w);
151 xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE);
153 /* Emit something so the file has a text segment */
154 img_writer_emit_section_change (w, ".text", 0);
155 img_writer_emit_string (w, "");
157 mono_dwarf_writer_emit_base_info (xdebug_writer, mono_arch_get_cie_program ());
161 xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
166 w = img_writer_create (NULL, TRUE);
168 img_writer_emit_start (w);
170 /* This file will contain the IL code for methods which don't have debug info */
172 il_file = fopen ("xdb.il", "w");
174 dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, FALSE);
176 mono_dwarf_writer_emit_base_info (dw, mono_arch_get_cie_program ());
183 xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
187 struct jit_code_entry *entry;
189 il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
190 mono_dwarf_writer_close (dw);
192 img_writer_emit_writeout (w);
194 img = img_writer_get_output (w, &img_size);
196 img_writer_destroy (w);
199 /* Save the symbol files to help debugging */
202 static int file_counter;
205 file_name = g_strdup_printf ("xdb-%d.o", file_counter);
206 //printf ("%s -> %s\n", mono_method_full_name (method, TRUE), file_name);
208 fp = fopen (file_name, "w");
209 fwrite (img, img_size, 1, fp);
214 /* Register the image with GDB */
216 entry = g_malloc (sizeof (struct jit_code_entry));
218 entry->symfile_addr = (const char*)img;
219 entry->symfile_size = img_size;
221 entry->next_entry = __jit_debug_descriptor.first_entry;
222 if (__jit_debug_descriptor.first_entry)
223 __jit_debug_descriptor.first_entry->prev_entry = entry;
224 __jit_debug_descriptor.first_entry = entry;
226 __jit_debug_descriptor.relevant_entry = entry;
227 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
229 __jit_debug_register_code ();
235 * This could be called from inside gdb to flush the debugging information not yet
236 * registered with gdb.
239 mono_xdebug_flush (void)
242 xdebug_end_emit (xdebug_w, xdebug_writer, NULL);
244 xdebug_begin_emit (&xdebug_w, &xdebug_writer);
247 static int xdebug_method_count;
250 * mono_save_xdebug_info:
252 * Emit debugging info for METHOD into an assembly file which can be assembled
253 * and loaded into gdb to provide debugging info for JITted code.
254 * LOCKING: Acquires the loader lock.
257 mono_save_xdebug_info (MonoCompile *cfg)
259 if (use_gdb_interface) {
263 xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
266 * gdb is not designed to handle 1000s of symbol files (one per method). So we
267 * group them into groups of 100.
269 if ((xdebug_method_count % 100) == 0)
270 mono_xdebug_flush ();
272 xdebug_method_count ++;
274 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 ()));
278 * Emit a symbol for the code by emitting it at the beginning of the text
279 * segment, and setting the text segment to have an absolute address.
280 * This symbol can be used to set breakpoints in gdb.
281 * FIXME: This doesn't work when multiple methods are emitted into the same file.
283 sym = get_debug_sym (cfg->jit_info->method, "", xdebug_syms);
284 img_writer_emit_section_change (w, ".text", 0);
285 if (!xdebug_text_addr) {
286 xdebug_text_addr = cfg->jit_info->code_start;
287 img_writer_set_section_addr (w, (gssize)xdebug_text_addr);
289 img_writer_emit_global_with_size (w, sym, cfg->jit_info->code_size, TRUE);
290 img_writer_emit_label (w, sym);
291 img_writer_emit_bytes (w, cfg->jit_info->code_start, cfg->jit_info->code_size);
295 mono_loader_unlock ();
301 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 ()));
303 mono_loader_unlock ();
308 * mono_save_trampoline_xdebug_info:
310 * Same as mono_save_xdebug_info, but for trampolines.
311 * LOCKING: Acquires the loader lock.
314 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
316 if (use_gdb_interface) {
322 xdebug_begin_emit (&w, &dw);
324 mono_dwarf_writer_emit_trampoline (dw, tramp_name, NULL, NULL, code, code_size, unwind_info);
326 xdebug_end_emit (w, dw, NULL);
328 mono_loader_unlock ();
334 mono_dwarf_writer_emit_trampoline (xdebug_writer, tramp_name, NULL, NULL, code, code_size, unwind_info);
336 mono_loader_unlock ();
340 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
343 mono_xdebug_init (char *options)
348 mono_save_xdebug_info (MonoCompile *cfg)
353 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)