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.
30 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
31 #include <sys/types.h>
51 #include "image-writer.h"
52 #include "dwarfwriter.h"
54 #define USE_GDB_JIT_INTERFACE
56 /* The recommended gdb macro is: */
59 shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
60 add-symbol-file xdb.so 0
65 * GDB JIT interface definitions.
67 * http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
78 struct jit_code_entry
*next_entry
;
79 struct jit_code_entry
*prev_entry
;
80 const char *symfile_addr
;
82 * The gdb code in gdb/jit.c which reads this structure ignores alignment
83 * requirements, so use two 32 bit fields.
85 guint32 symfile_size1
, symfile_size2
;
91 /* This type should be jit_actions_t, but we use guint32
92 to be explicit about the bitwidth. */
94 struct jit_code_entry
*relevant_entry
;
95 struct jit_code_entry
*first_entry
;
100 #define MONO_NOINLINE __declspec (noinline)
102 #define MONO_NOINLINE __attribute__((noinline))
105 /* GDB puts a breakpoint in this function. */
106 void MONO_NOINLINE
__jit_debug_register_code(void);
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 /* gcc seems to inline/eliminate calls to noinline functions, thus the asm () */
113 void MONO_NOINLINE
__jit_debug_register_code(void) {
114 #if defined(__GNUC__)
119 static MonoImageWriter
*xdebug_w
;
120 static MonoDwarfWriter
*xdebug_writer
;
121 static FILE *xdebug_fp
, *il_file
;
122 static gboolean use_gdb_interface
, save_symfiles
;
123 static int il_file_line_index
;
124 static GHashTable
*xdebug_syms
;
127 mono_xdebug_init (char *options
)
132 args
= g_strsplit (options
, ",", -1);
133 for (ptr
= args
; ptr
&& *ptr
; ptr
++) {
136 if (!strcmp (arg
, "gdb"))
137 use_gdb_interface
= TRUE
;
138 if (!strcmp (arg
, "save-symfiles"))
139 save_symfiles
= TRUE
;
142 /* This file will contain the IL code for methods which don't have debug info */
143 il_file
= fopen ("xdb.il", "w");
144 if (il_file
== NULL
) {
145 use_gdb_interface
= FALSE
;
146 g_warning ("** Unable to create xdb.il. Managed symbol names won't be available.");
150 if (use_gdb_interface
)
154 xdebug_fp
= fopen ("xdb.s", "w");
156 w
= img_writer_create (xdebug_fp
, FALSE
);
158 img_writer_emit_start (w
);
160 xdebug_writer
= mono_dwarf_writer_create (w
, il_file
, 0, TRUE
);
162 /* Emit something so the file has a text segment */
163 img_writer_emit_section_change (w
, ".text", 0);
164 img_writer_emit_string (w
, "");
166 mono_dwarf_writer_emit_base_info (xdebug_writer
, mono_unwind_get_cie_program ());
170 xdebug_begin_emit (MonoImageWriter
**out_w
, MonoDwarfWriter
**out_dw
)
175 w
= img_writer_create (NULL
, TRUE
);
177 img_writer_emit_start (w
);
179 /* This file will contain the IL code for methods which don't have debug info */
181 il_file
= fopen ("xdb.il", "w");
183 dw
= mono_dwarf_writer_create (w
, il_file
, il_file_line_index
, FALSE
);
185 mono_dwarf_writer_emit_base_info (dw
, mono_unwind_get_cie_program ());
192 xdebug_end_emit (MonoImageWriter
*w
, MonoDwarfWriter
*dw
, MonoMethod
*method
)
196 struct jit_code_entry
*entry
;
199 il_file_line_index
= mono_dwarf_writer_get_il_file_line_index (dw
);
200 mono_dwarf_writer_close (dw
);
202 img_writer_emit_writeout (w
);
204 img
= img_writer_get_output (w
, &img_size
);
206 img_writer_destroy (w
);
209 /* Save the symbol files to help debugging */
212 static int file_counter
;
215 file_name
= g_strdup_printf ("xdb-%d.o", file_counter
);
216 printf ("%s %p %d\n", file_name
, img
, img_size
);
218 fp
= fopen (file_name
, "w");
219 fwrite (img
, img_size
, 1, fp
);
224 /* Register the image with GDB */
226 entry
= g_malloc0 (sizeof (struct jit_code_entry
));
228 entry
->symfile_addr
= (const char*)img
;
229 psize
= (guint64
*)&entry
->symfile_size1
;
232 entry
->next_entry
= __jit_debug_descriptor
.first_entry
;
233 if (__jit_debug_descriptor
.first_entry
)
234 __jit_debug_descriptor
.first_entry
->prev_entry
= entry
;
235 __jit_debug_descriptor
.first_entry
= entry
;
237 __jit_debug_descriptor
.relevant_entry
= entry
;
238 __jit_debug_descriptor
.action_flag
= JIT_REGISTER_FN
;
240 __jit_debug_register_code ();
246 * This could be called from inside gdb to flush the debugging information not yet
247 * registered with gdb.
250 mono_xdebug_flush (void)
253 xdebug_end_emit (xdebug_w
, xdebug_writer
, NULL
);
255 xdebug_begin_emit (&xdebug_w
, &xdebug_writer
);
258 static int xdebug_method_count
;
261 * mono_save_xdebug_info:
263 * Emit debugging info for METHOD into an assembly file which can be assembled
264 * and loaded into gdb to provide debugging info for JITted code.
265 * LOCKING: Acquires the loader lock.
268 mono_save_xdebug_info (MonoCompile
*cfg
)
270 MonoDebugMethodJitInfo
*dmji
;
272 if (use_gdb_interface
) {
276 xdebug_syms
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
279 * gdb is not designed to handle 1000s of symbol files (one per method). So we
280 * group them into groups of 100.
282 if ((xdebug_method_count
% 100) == 0)
283 mono_xdebug_flush ();
285 xdebug_method_count
++;
287 dmji
= mono_debug_find_method (cfg
->jit_info
->method
, mono_domain_get ());;
288 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
, dmji
);
289 mono_debug_free_method_jit_info (dmji
);
293 * Emit a symbol for the code by emitting it at the beginning of the text
294 * segment, and setting the text segment to have an absolute address.
295 * This symbol can be used to set breakpoints in gdb.
296 * FIXME: This doesn't work when multiple methods are emitted into the same file.
298 sym
= get_debug_sym (cfg
->jit_info
->method
, "", xdebug_syms
);
299 img_writer_emit_section_change (w
, ".text", 0);
300 if (!xdebug_text_addr
) {
301 xdebug_text_addr
= cfg
->jit_info
->code_start
;
302 img_writer_set_section_addr (w
, (gssize
)xdebug_text_addr
);
304 img_writer_emit_global_with_size (w
, sym
, cfg
->jit_info
->code_size
, TRUE
);
305 img_writer_emit_label (w
, sym
);
306 img_writer_emit_bytes (w
, cfg
->jit_info
->code_start
, cfg
->jit_info
->code_size
);
310 mono_loader_unlock ();
316 dmji
= mono_debug_find_method (cfg
->jit_info
->method
, mono_domain_get ());;
317 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
, dmji
);
318 mono_debug_free_method_jit_info (dmji
);
320 mono_loader_unlock ();
326 * mono_save_trampoline_xdebug_info:
328 * Same as mono_save_xdebug_info, but for trampolines.
329 * LOCKING: Acquires the loader lock.
332 mono_save_trampoline_xdebug_info (MonoTrampInfo
*info
)
334 if (use_gdb_interface
) {
338 /* This can be called before the loader lock is initialized */
339 mono_loader_lock_if_inited ();
341 xdebug_begin_emit (&w
, &dw
);
343 mono_dwarf_writer_emit_trampoline (dw
, info
->name
, NULL
, NULL
, info
->code
, info
->code_size
, info
->unwind_ops
);
345 xdebug_end_emit (w
, dw
, NULL
);
347 mono_loader_unlock_if_inited ();
352 mono_loader_lock_if_inited ();
353 mono_dwarf_writer_emit_trampoline (xdebug_writer
, info
->name
, NULL
, NULL
, info
->code
, info
->code_size
, info
->unwind_ops
);
355 mono_loader_unlock_if_inited ();
359 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
362 mono_xdebug_init (char *options
)
367 mono_save_xdebug_info (MonoCompile
*cfg
)
372 mono_save_trampoline_xdebug_info (MonoTrampInfo
*info
)