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.
30 #include "mini-runtime.h"
32 #include <sys/types.h>
52 #include "image-writer.h"
54 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) && USE_BIN_WRITER
56 #include "dwarfwriter.h"
58 #include "mono/utils/mono-compiler.h"
60 #define USE_GDB_JIT_INTERFACE
62 /* The recommended gdb macro is: */
65 shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
66 add-symbol-file xdb.so 0
71 * GDB JIT interface definitions.
73 * http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
82 struct jit_code_entry
;
83 typedef struct jit_code_entry jit_code_entry
;
87 jit_code_entry
*next_entry
;
88 jit_code_entry
*prev_entry
;
89 const char *symfile_addr
;
91 * The gdb code in gdb/jit.c which reads this structure ignores alignment
92 * requirements, so use two 32 bit fields.
94 guint32 symfile_size1
, symfile_size2
;
97 typedef struct jit_descriptor
100 /* This type should be jit_actions_t, but we use guint32
101 to be explicit about the bitwidth. */
103 jit_code_entry
*relevant_entry
;
104 jit_code_entry
*first_entry
;
109 /* GDB puts a breakpoint in this function. */
110 void MONO_NEVER_INLINE
__jit_debug_register_code(void);
112 #if defined(ENABLE_LLVM) && !defined(MONO_CROSS_COMPILE)
114 /* LLVM already defines these */
116 extern jit_descriptor __jit_debug_descriptor
;
120 /* gcc seems to inline/eliminate calls to noinline functions, thus the asm () */
121 void MONO_NEVER_INLINE
__jit_debug_register_code(void) {
122 #if defined(__GNUC__)
127 /* Make sure to specify the version statically, because the
128 debugger may check the version before we can set it. */
129 jit_descriptor __jit_debug_descriptor
= { 1, 0, 0, 0 };
135 static MonoImageWriter
*xdebug_w
;
136 static MonoDwarfWriter
*xdebug_writer
;
137 static FILE *xdebug_fp
, *il_file
;
138 static gboolean use_gdb_interface
, save_symfiles
;
139 static int il_file_line_index
;
140 static GHashTable
*xdebug_syms
;
143 mono_xdebug_init (const char *options
)
148 args
= g_strsplit (options
, ",", -1);
149 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
152 if (!strcmp (arg
, "gdb"))
153 use_gdb_interface
= TRUE
;
154 if (!strcmp (arg
, "save-symfiles"))
155 save_symfiles
= TRUE
;
158 /* This file will contain the IL code for methods which don't have debug info */
159 il_file
= fopen ("xdb.il", "w");
160 if (il_file
== NULL
) {
161 use_gdb_interface
= FALSE
;
162 g_warning ("** Unable to create xdb.il. Managed symbol names won't be available.");
166 if (use_gdb_interface
)
170 xdebug_fp
= fopen ("xdb.s", "w");
172 w
= mono_img_writer_create (xdebug_fp
, FALSE
);
174 mono_img_writer_emit_start (w
);
176 xdebug_writer
= mono_dwarf_writer_create (w
, il_file
, 0, TRUE
);
178 /* Emit something so the file has a text segment */
179 mono_img_writer_emit_section_change (w
, ".text", 0);
180 mono_img_writer_emit_string (w
, "");
182 mono_dwarf_writer_emit_base_info (xdebug_writer
, "JITted code", mono_unwind_get_cie_program ());
186 xdebug_begin_emit (MonoImageWriter
**out_w
, MonoDwarfWriter
**out_dw
)
191 w
= mono_img_writer_create (NULL
, TRUE
);
193 mono_img_writer_emit_start (w
);
195 /* This file will contain the IL code for methods which don't have debug info */
197 il_file
= fopen ("xdb.il", "w");
199 dw
= mono_dwarf_writer_create (w
, il_file
, il_file_line_index
, TRUE
);
201 mono_dwarf_writer_emit_base_info (dw
, "JITted code", mono_unwind_get_cie_program ());
208 xdebug_end_emit (MonoImageWriter
*w
, MonoDwarfWriter
*dw
, MonoMethod
*method
)
212 jit_code_entry
*entry
;
215 il_file_line_index
= mono_dwarf_writer_get_il_file_line_index (dw
);
216 mono_dwarf_writer_close (dw
);
218 mono_img_writer_emit_writeout (w
);
220 img
= mono_img_writer_get_output (w
, &img_size
);
222 mono_img_writer_destroy (w
);
225 /* Save the symbol files to help debugging */
228 static int file_counter
;
231 file_name
= g_strdup_printf ("xdb-%d.o", file_counter
);
232 printf ("%s %p %d\n", file_name
, img
, img_size
);
234 fp
= fopen (file_name
, "w");
235 fwrite (img
, img_size
, 1, fp
);
240 /* Register the image with GDB */
242 entry
= g_malloc0 (sizeof (jit_code_entry
));
244 entry
->symfile_addr
= (const char*)img
;
245 psize
= (guint64
*)&entry
->symfile_size1
;
248 entry
->next_entry
= __jit_debug_descriptor
.first_entry
;
249 if (__jit_debug_descriptor
.first_entry
)
250 __jit_debug_descriptor
.first_entry
->prev_entry
= entry
;
251 __jit_debug_descriptor
.first_entry
= entry
;
253 __jit_debug_descriptor
.relevant_entry
= entry
;
254 __jit_debug_descriptor
.action_flag
= JIT_REGISTER_FN
;
256 __jit_debug_register_code ();
262 * This could be called from inside gdb to flush the debugging information not yet
263 * registered with gdb.
266 mono_xdebug_flush (void)
269 xdebug_end_emit (xdebug_w
, xdebug_writer
, NULL
);
271 xdebug_begin_emit (&xdebug_w
, &xdebug_writer
);
274 static int xdebug_method_count
;
277 * mono_save_xdebug_info:
279 * Emit debugging info for METHOD into an assembly file which can be assembled
280 * and loaded into gdb to provide debugging info for JITted code.
281 * LOCKING: Acquires the loader lock.
284 mono_save_xdebug_info (MonoCompile
*cfg
)
286 MonoDebugMethodJitInfo
*dmji
;
288 if (use_gdb_interface
) {
292 xdebug_syms
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
295 * gdb is not designed to handle 1000s of symbol files (one per method). So we
296 * group them into groups of 100.
298 if ((xdebug_method_count
% 100) == 0)
299 mono_xdebug_flush ();
301 xdebug_method_count
++;
303 dmji
= mono_debug_find_method (jinfo_get_method (cfg
->jit_info
), mono_domain_get ());
304 mono_dwarf_writer_emit_method (xdebug_writer
, cfg
, jinfo_get_method (cfg
->jit_info
), NULL
, NULL
, NULL
,
305 (guint8
*)cfg
->jit_info
->code_start
, cfg
->jit_info
->code_size
, cfg
->args
, cfg
->locals
, cfg
->unwind_ops
, dmji
);
306 mono_debug_free_method_jit_info (dmji
);
310 * Emit a symbol for the code by emitting it at the beginning of the text
311 * segment, and setting the text segment to have an absolute address.
312 * This symbol can be used to set breakpoints in gdb.
313 * FIXME: This doesn't work when multiple methods are emitted into the same file.
315 sym
= get_debug_sym (cfg
->jit_info
->method
, "", xdebug_syms
);
316 mono_img_writer_emit_section_change (w
, ".text", 0);
317 if (!xdebug_text_addr
) {
318 xdebug_text_addr
= cfg
->jit_info
->code_start
;
319 mono_img_writer_set_section_addr (w
, (gssize
)xdebug_text_addr
);
321 mono_img_writer_emit_global_with_size (w
, sym
, cfg
->jit_info
->code_size
, TRUE
);
322 mono_img_writer_emit_label (w
, sym
);
323 mono_img_writer_emit_bytes (w
, cfg
->jit_info
->code_start
, cfg
->jit_info
->code_size
);
327 mono_loader_unlock ();
333 dmji
= mono_debug_find_method (jinfo_get_method (cfg
->jit_info
), mono_domain_get ());
334 mono_dwarf_writer_emit_method (xdebug_writer
, cfg
, jinfo_get_method (cfg
->jit_info
), NULL
, NULL
, NULL
,
335 (guint8
*)cfg
->jit_info
->code_start
, cfg
->jit_info
->code_size
, cfg
->args
, cfg
->locals
, cfg
->unwind_ops
, dmji
);
336 mono_debug_free_method_jit_info (dmji
);
338 mono_loader_unlock ();
344 * mono_save_trampoline_xdebug_info:
346 * Same as mono_save_xdebug_info, but for trampolines.
347 * LOCKING: Acquires the loader lock.
350 mono_save_trampoline_xdebug_info (MonoTrampInfo
*info
)
352 const char *info_name
= info
->name
;
353 if (info_name
== NULL
)
356 if (use_gdb_interface
) {
360 /* This can be called before the loader lock is initialized */
361 mono_loader_lock_if_inited ();
363 xdebug_begin_emit (&w
, &dw
);
365 mono_dwarf_writer_emit_trampoline (dw
, info_name
, NULL
, NULL
, info
->code
, info
->code_size
, info
->unwind_ops
);
367 xdebug_end_emit (w
, dw
, NULL
);
369 mono_loader_unlock_if_inited ();
374 mono_loader_lock_if_inited ();
375 mono_dwarf_writer_emit_trampoline (xdebug_writer
, info_name
, NULL
, NULL
, info
->code
, info
->code_size
, info
->unwind_ops
);
377 mono_loader_unlock_if_inited ();
381 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
384 mono_xdebug_init (const char *options
)
389 mono_save_xdebug_info (MonoCompile
*cfg
)
394 mono_save_trampoline_xdebug_info (MonoTrampInfo
*info
)