ee8f4f79061c26081ef606cd010e4afd5d79bc0e
[mono.git] / mono / mini / xdebug.c
1 /**
2  * \file
3  * Support for emitting gdb debug info for JITted code.
4  *
5  * Author:
6  *   Zoltan Varga (vargaz@gmail.com)
7  *
8  * (C) 2010 Novell, Inc.
9  */
10
11 /*
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.
16  *
17  * This is based on the xdebug functionality in the Kaffe Java VM.
18  * 
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.
25  */
26
27 #include "config.h"
28 #include <glib.h>
29 #include "mini.h"
30
31 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) && USE_BIN_WRITER
32 #include <sys/types.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #ifdef HAVE_STDINT_H
37 #include <stdint.h>
38 #endif
39 #include <fcntl.h>
40 #include <ctype.h>
41 #include <string.h>
42 #ifndef HOST_WIN32
43 #include <sys/time.h>
44 #else
45 #include <winsock2.h>
46 #include <windows.h>
47 #endif
48
49 #include <errno.h>
50 #include <sys/stat.h>
51
52 #include "image-writer.h"
53 #include "dwarfwriter.h"
54
55 #include "mono/utils/mono-compiler.h"
56
57 #define USE_GDB_JIT_INTERFACE
58
59 /* The recommended gdb macro is: */
60 /*
61   define xdb
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
64   end
65 */
66
67 /*
68  * GDB JIT interface definitions.
69  *
70  *      http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
71  */
72 typedef enum
73 {
74   JIT_NOACTION = 0,
75   JIT_REGISTER_FN,
76   JIT_UNREGISTER_FN
77 } jit_actions_t;
78
79 struct jit_code_entry
80 {
81         struct jit_code_entry *next_entry;
82         struct jit_code_entry *prev_entry;
83         const char *symfile_addr;
84         /*
85          * The gdb code in gdb/jit.c which reads this structure ignores alignment
86          * requirements, so use two 32 bit fields.
87          */
88         guint32 symfile_size1, symfile_size2;
89 };
90
91 struct jit_descriptor
92 {
93   guint32 version;
94   /* This type should be jit_actions_t, but we use guint32
95      to be explicit about the bitwidth.  */
96   guint32 action_flag;
97   struct jit_code_entry *relevant_entry;
98   struct jit_code_entry *first_entry;
99 };
100
101 /* GDB puts a breakpoint in this function.  */
102 void MONO_NEVER_INLINE __jit_debug_register_code(void);
103
104 #if !defined(MONO_LLVM_LOADED) && defined(ENABLE_LLVM) && !defined(MONO_CROSS_COMPILE)
105
106 /* LLVM already defines these */
107
108 extern struct jit_descriptor __jit_debug_descriptor;
109
110 #else
111
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__)
115         asm ("");
116 #endif
117 }
118
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 };
122
123 #endif
124
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;
131
132 void
133 mono_xdebug_init (const char *options)
134 {
135         MonoImageWriter *w;
136         char **args, **ptr;
137
138         args = g_strsplit (options, ",", -1);
139         for (ptr = args; ptr && *ptr; ptr ++) {
140                 char *arg = *ptr;
141
142                 if (!strcmp (arg, "gdb"))
143                         use_gdb_interface = TRUE;
144                 if (!strcmp (arg, "save-symfiles"))
145                         save_symfiles = TRUE;
146         }
147
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.");
153                 return;
154         }
155
156         if (use_gdb_interface)
157                 return;
158
159         unlink ("xdb.s");
160         xdebug_fp = fopen ("xdb.s", "w");
161         
162         w = mono_img_writer_create (xdebug_fp, FALSE);
163
164         mono_img_writer_emit_start (w);
165
166         xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE);
167
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, "");
171
172         mono_dwarf_writer_emit_base_info (xdebug_writer, "JITted code", mono_unwind_get_cie_program ());
173 }
174
175 static void
176 xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
177 {
178         MonoImageWriter *w;
179         MonoDwarfWriter *dw;
180
181         w = mono_img_writer_create (NULL, TRUE);
182
183         mono_img_writer_emit_start (w);
184
185         /* This file will contain the IL code for methods which don't have debug info */
186         if (!il_file)
187                 il_file = fopen ("xdb.il", "w");
188
189         dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, TRUE);
190
191         mono_dwarf_writer_emit_base_info (dw, "JITted code", mono_unwind_get_cie_program ());
192
193         *out_w = w;
194         *out_dw = dw;
195 }
196
197 static void
198 xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
199 {
200         guint8 *img;
201         guint32 img_size;
202         struct jit_code_entry *entry;
203         guint64 *psize;
204
205         il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
206         mono_dwarf_writer_close (dw);
207
208         mono_img_writer_emit_writeout (w);
209
210         img = mono_img_writer_get_output (w, &img_size);
211
212         mono_img_writer_destroy (w);
213
214         if (FALSE) {
215                 /* Save the symbol files to help debugging */
216                 FILE *fp;
217                 char *file_name;
218                 static int file_counter;
219
220                 file_counter ++;
221                 file_name = g_strdup_printf ("xdb-%d.o", file_counter);
222                 printf ("%s %p %d\n", file_name, img, img_size);
223
224                 fp = fopen (file_name, "w");
225                 fwrite (img, img_size, 1, fp);
226                 fclose (fp);
227                 g_free (file_name);
228         }
229
230         /* Register the image with GDB */
231
232         entry = g_malloc0 (sizeof (struct jit_code_entry));
233
234         entry->symfile_addr = (const char*)img;
235         psize = (guint64*)&entry->symfile_size1;
236         *psize = img_size;
237
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;
242         
243         __jit_debug_descriptor.relevant_entry = entry;
244         __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
245
246         __jit_debug_register_code ();
247 }
248
249 /*
250  * mono_xdebug_flush:
251  *
252  *   This could be called from inside gdb to flush the debugging information not yet
253  * registered with gdb.
254  */
255 void
256 mono_xdebug_flush (void)
257 {
258         if (xdebug_w)
259                 xdebug_end_emit (xdebug_w, xdebug_writer, NULL);
260
261         xdebug_begin_emit (&xdebug_w, &xdebug_writer);
262 }
263
264 static int xdebug_method_count;
265
266 /*
267  * mono_save_xdebug_info:
268  *
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.
272  */
273 void
274 mono_save_xdebug_info (MonoCompile *cfg)
275 {
276         MonoDebugMethodJitInfo *dmji;
277
278         if (use_gdb_interface) {
279                 mono_loader_lock ();
280
281                 if (!xdebug_syms)
282                         xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
283
284                 /*
285                  * gdb is not designed to handle 1000s of symbol files (one per method). So we
286                  * group them into groups of 100.
287                  */
288                 if ((xdebug_method_count % 100) == 0)
289                         mono_xdebug_flush ();
290
291                 xdebug_method_count ++;
292
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);
297
298 #if 0
299                 /* 
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.
304                  */
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);
310                 }
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);
314                 g_free (sym);
315 #endif
316                 
317                 mono_loader_unlock ();
318         } else {
319                 if (!xdebug_writer)
320                         return;
321
322                 mono_loader_lock ();
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);
327                 fflush (xdebug_fp);
328                 mono_loader_unlock ();
329         }
330
331 }
332
333 /*
334  * mono_save_trampoline_xdebug_info:
335  *
336  *   Same as mono_save_xdebug_info, but for trampolines.
337  * LOCKING: Acquires the loader lock.
338  */
339 void
340 mono_save_trampoline_xdebug_info (MonoTrampInfo *info)
341 {
342         if (use_gdb_interface) {
343                 MonoImageWriter *w;
344                 MonoDwarfWriter *dw;
345
346                 /* This can be called before the loader lock is initialized */
347                 mono_loader_lock_if_inited ();
348
349                 xdebug_begin_emit (&w, &dw);
350
351                 mono_dwarf_writer_emit_trampoline (dw, info->name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
352
353                 xdebug_end_emit (w, dw, NULL);
354                 
355                 mono_loader_unlock_if_inited ();
356         } else {
357                 if (!xdebug_writer)
358                         return;
359
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);
362                 fflush (xdebug_fp);
363                 mono_loader_unlock_if_inited ();
364         }
365 }
366
367 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
368
369 void
370 mono_xdebug_init (const char *options)
371 {
372 }
373
374 void
375 mono_save_xdebug_info (MonoCompile *cfg)
376 {
377 }
378
379 void
380 mono_save_trampoline_xdebug_info (MonoTrampInfo *info)
381 {
382 }
383
384 #endif