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