1e83f9cb01d5993ba830323b8e12c6c26b95f9cf
[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 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
27
28 #include "config.h"
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #ifdef HAVE_STDINT_H
34 #include <stdint.h>
35 #endif
36 #include <fcntl.h>
37 #include <ctype.h>
38 #include <string.h>
39 #ifndef HOST_WIN32
40 #include <sys/time.h>
41 #else
42 #include <winsock2.h>
43 #include <windows.h>
44 #endif
45
46 #include <errno.h>
47 #include <sys/stat.h>
48
49 #include "mini.h"
50 #include "image-writer.h"
51 #include "dwarfwriter.h"
52
53 #define USE_GDB_JIT_INTERFACE
54
55 /* The recommended gdb macro is: */
56 /*
57   define xdb
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
60   end
61 */
62
63 /*
64  * GDB JIT interface definitions.
65  *
66  *      http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
67  */
68 typedef enum
69 {
70   JIT_NOACTION = 0,
71   JIT_REGISTER_FN,
72   JIT_UNREGISTER_FN
73 } jit_actions_t;
74
75 struct jit_code_entry
76 {
77   struct jit_code_entry *next_entry;
78   struct jit_code_entry *prev_entry;
79   const char *symfile_addr;
80   guint64 symfile_size;
81 };
82
83 struct jit_descriptor
84 {
85   guint32 version;
86   /* This type should be jit_actions_t, but we use guint32
87      to be explicit about the bitwidth.  */
88   guint32 action_flag;
89   struct jit_code_entry *relevant_entry;
90   struct jit_code_entry *first_entry;
91 };
92
93
94 #ifdef _MSC_VER
95 #define MONO_NOINLINE __declspec (noinline)
96 #else
97 #define MONO_NOINLINE __attribute__((noinline))
98 #endif
99
100 /* GDB puts a breakpoint in this function.  */
101 void MONO_NOINLINE __jit_debug_register_code(void);
102
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;
106 #else
107
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 };
111
112 void MONO_NOINLINE __jit_debug_register_code(void) { };
113 #endif
114
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;
121
122 void
123 mono_xdebug_init (char *options)
124 {
125         MonoImageWriter *w;
126         char **args, **ptr;
127
128         args = g_strsplit (options, ",", -1);
129         for (ptr = args; ptr && *ptr; ptr ++) {
130                 char *arg = *ptr;
131
132                 if (!strcmp (arg, "gdb"))
133                         use_gdb_interface = TRUE;
134                 if (!strcmp (arg, "save-symfiles"))
135                         save_symfiles = TRUE;
136         }
137
138         /* This file will contain the IL code for methods which don't have debug info */
139         il_file = fopen ("xdb.il", "w");
140
141         if (use_gdb_interface)
142                 return;
143
144         unlink ("xdb.s");
145         xdebug_fp = fopen ("xdb.s", "w");
146         
147         w = img_writer_create (xdebug_fp, FALSE);
148
149         img_writer_emit_start (w);
150
151         xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE);
152
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, "");
156
157         mono_dwarf_writer_emit_base_info (xdebug_writer, mono_arch_get_cie_program ());
158 }
159
160 static void
161 xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
162 {
163         MonoImageWriter *w;
164         MonoDwarfWriter *dw;
165
166         w = img_writer_create (NULL, TRUE);
167
168         img_writer_emit_start (w);
169
170         /* This file will contain the IL code for methods which don't have debug info */
171         if (!il_file)
172                 il_file = fopen ("xdb.il", "w");
173
174         dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, FALSE);
175
176         mono_dwarf_writer_emit_base_info (dw, mono_arch_get_cie_program ());
177
178         *out_w = w;
179         *out_dw = dw;
180 }
181
182 static void
183 xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
184 {
185         guint8 *img;
186         guint32 img_size;
187         struct jit_code_entry *entry;
188
189         il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
190         mono_dwarf_writer_close (dw);
191
192         img_writer_emit_writeout (w);
193
194         img = img_writer_get_output (w, &img_size);
195
196         img_writer_destroy (w);
197
198         if (FALSE) {
199                 /* Save the symbol files to help debugging */
200                 FILE *fp;
201                 char *file_name;
202                 static int file_counter;
203
204                 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);
207
208                 fp = fopen (file_name, "w");
209                 fwrite (img, img_size, 1, fp);
210                 fclose (fp);
211                 g_free (file_name);
212         }
213
214         /* Register the image with GDB */
215
216         entry = g_malloc (sizeof (struct jit_code_entry));
217
218         entry->symfile_addr = (const char*)img;
219         entry->symfile_size = img_size;
220
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;
225         
226         __jit_debug_descriptor.relevant_entry = entry;
227         __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
228
229         __jit_debug_register_code ();
230 }
231
232 /*
233  * mono_xdebug_flush:
234  *
235  *   This could be called from inside gdb to flush the debugging information not yet
236  * registered with gdb.
237  */
238 void
239 mono_xdebug_flush (void)
240 {
241         if (xdebug_w)
242                 xdebug_end_emit (xdebug_w, xdebug_writer, NULL);
243
244         xdebug_begin_emit (&xdebug_w, &xdebug_writer);
245 }
246
247 static int xdebug_method_count;
248
249 /*
250  * mono_save_xdebug_info:
251  *
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.
255  */
256 void
257 mono_save_xdebug_info (MonoCompile *cfg)
258 {
259         if (use_gdb_interface) {
260                 mono_loader_lock ();
261
262                 if (!xdebug_syms)
263                         xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
264
265                 /*
266                  * gdb is not designed to handle 1000s of symbol files (one per method). So we
267                  * group them into groups of 100.
268                  */
269                 if ((xdebug_method_count % 100) == 0)
270                         mono_xdebug_flush ();
271
272                 xdebug_method_count ++;
273
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 ()));
275
276 #if 0
277                 /* 
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.
282                  */
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);
288                 }
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);
292                 g_free (sym);
293 #endif
294                 
295                 mono_loader_unlock ();
296         } else {
297                 if (!xdebug_writer)
298                         return;
299
300                 mono_loader_lock ();
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 ()));
302                 fflush (xdebug_fp);
303                 mono_loader_unlock ();
304         }
305 }
306
307 /*
308  * mono_save_trampoline_xdebug_info:
309  *
310  *   Same as mono_save_xdebug_info, but for trampolines.
311  * LOCKING: Acquires the loader lock.
312  */
313 void
314 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
315 {
316         if (use_gdb_interface) {
317                 MonoImageWriter *w;
318                 MonoDwarfWriter *dw;
319
320                 mono_loader_lock ();
321
322                 xdebug_begin_emit (&w, &dw);
323
324                 mono_dwarf_writer_emit_trampoline (dw, tramp_name, NULL, NULL, code, code_size, unwind_info);
325
326                 xdebug_end_emit (w, dw, NULL);
327                 
328                 mono_loader_unlock ();
329         } else {
330                 if (!xdebug_writer)
331                         return;
332
333                 mono_loader_lock ();
334                 mono_dwarf_writer_emit_trampoline (xdebug_writer, tramp_name, NULL, NULL, code, code_size, unwind_info);
335                 fflush (xdebug_fp);
336                 mono_loader_unlock ();
337         }
338 }
339
340 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
341
342 void
343 mono_xdebug_init (char *options)
344 {
345 }
346
347 void
348 mono_save_xdebug_info (MonoCompile *cfg)
349 {
350 }
351
352 void
353 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
354 {
355 }
356
357 #endif