2010-03-01 Zoltan Varga <vargaz@gmail.com>
[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   guint64 symfile_size;
82 };
83
84 struct jit_descriptor
85 {
86   guint32 version;
87   /* This type should be jit_actions_t, but we use guint32
88      to be explicit about the bitwidth.  */
89   guint32 action_flag;
90   struct jit_code_entry *relevant_entry;
91   struct jit_code_entry *first_entry;
92 };
93
94
95 #ifdef _MSC_VER
96 #define MONO_NOINLINE __declspec (noinline)
97 #else
98 #define MONO_NOINLINE __attribute__((noinline))
99 #endif
100
101 /* GDB puts a breakpoint in this function.  */
102 void MONO_NOINLINE __jit_debug_register_code(void);
103
104 #if defined(ENABLE_LLVM) && ((LLVM_MAJOR_VERSION == 2 && LLVM_MINOR_VERSION >= 7) || LLVM_MAJOR_VERSION > 2)
105 /* LLVM already defines these */
106 extern struct jit_descriptor __jit_debug_descriptor;
107 #else
108
109 /* Make sure to specify the version statically, because the
110    debugger may check the version before we can set it.  */
111 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
112
113 void MONO_NOINLINE __jit_debug_register_code(void) { };
114 #endif
115
116 static MonoImageWriter *xdebug_w;
117 static MonoDwarfWriter *xdebug_writer;
118 static FILE *xdebug_fp, *il_file;
119 static gboolean use_gdb_interface, save_symfiles;
120 static int il_file_line_index;
121 static GHashTable *xdebug_syms;
122
123 void
124 mono_xdebug_init (char *options)
125 {
126         MonoImageWriter *w;
127         char **args, **ptr;
128
129         args = g_strsplit (options, ",", -1);
130         for (ptr = args; ptr && *ptr; ptr ++) {
131                 char *arg = *ptr;
132
133                 if (!strcmp (arg, "gdb"))
134                         use_gdb_interface = TRUE;
135                 if (!strcmp (arg, "save-symfiles"))
136                         save_symfiles = TRUE;
137         }
138
139         /* This file will contain the IL code for methods which don't have debug info */
140         il_file = fopen ("xdb.il", "w");
141
142         if (use_gdb_interface)
143                 return;
144
145         unlink ("xdb.s");
146         xdebug_fp = fopen ("xdb.s", "w");
147         
148         w = img_writer_create (xdebug_fp, FALSE);
149
150         img_writer_emit_start (w);
151
152         xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE);
153
154         /* Emit something so the file has a text segment */
155         img_writer_emit_section_change (w, ".text", 0);
156         img_writer_emit_string (w, "");
157
158         mono_dwarf_writer_emit_base_info (xdebug_writer, mono_unwind_get_cie_program ());
159 }
160
161 static void
162 xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
163 {
164         MonoImageWriter *w;
165         MonoDwarfWriter *dw;
166
167         w = img_writer_create (NULL, TRUE);
168
169         img_writer_emit_start (w);
170
171         /* This file will contain the IL code for methods which don't have debug info */
172         if (!il_file)
173                 il_file = fopen ("xdb.il", "w");
174
175         dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, FALSE);
176
177         mono_dwarf_writer_emit_base_info (dw, mono_unwind_get_cie_program ());
178
179         *out_w = w;
180         *out_dw = dw;
181 }
182
183 static void
184 xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
185 {
186         guint8 *img;
187         guint32 img_size;
188         struct jit_code_entry *entry;
189
190         il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
191         mono_dwarf_writer_close (dw);
192
193         img_writer_emit_writeout (w);
194
195         img = img_writer_get_output (w, &img_size);
196
197         img_writer_destroy (w);
198
199         if (FALSE) {
200                 /* Save the symbol files to help debugging */
201                 FILE *fp;
202                 char *file_name;
203                 static int file_counter;
204
205                 file_counter ++;
206                 file_name = g_strdup_printf ("xdb-%d.o", file_counter);
207                 //printf ("%s -> %s\n", mono_method_full_name (method, TRUE), file_name);
208
209                 fp = fopen (file_name, "w");
210                 fwrite (img, img_size, 1, fp);
211                 fclose (fp);
212                 g_free (file_name);
213         }
214
215         /* Register the image with GDB */
216
217         entry = g_malloc (sizeof (struct jit_code_entry));
218
219         entry->symfile_addr = (const char*)img;
220         entry->symfile_size = img_size;
221
222         entry->next_entry = __jit_debug_descriptor.first_entry;
223         if (__jit_debug_descriptor.first_entry)
224                 __jit_debug_descriptor.first_entry->prev_entry = entry;
225         __jit_debug_descriptor.first_entry = entry;
226         
227         __jit_debug_descriptor.relevant_entry = entry;
228         __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
229
230         __jit_debug_register_code ();
231 }
232
233 /*
234  * mono_xdebug_flush:
235  *
236  *   This could be called from inside gdb to flush the debugging information not yet
237  * registered with gdb.
238  */
239 void
240 mono_xdebug_flush (void)
241 {
242         if (xdebug_w)
243                 xdebug_end_emit (xdebug_w, xdebug_writer, NULL);
244
245         xdebug_begin_emit (&xdebug_w, &xdebug_writer);
246 }
247
248 static int xdebug_method_count;
249
250 /*
251  * mono_save_xdebug_info:
252  *
253  *   Emit debugging info for METHOD into an assembly file which can be assembled
254  * and loaded into gdb to provide debugging info for JITted code.
255  * LOCKING: Acquires the loader lock.
256  */
257 void
258 mono_save_xdebug_info (MonoCompile *cfg)
259 {
260         if (use_gdb_interface) {
261                 mono_loader_lock ();
262
263                 if (!xdebug_syms)
264                         xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
265
266                 /*
267                  * gdb is not designed to handle 1000s of symbol files (one per method). So we
268                  * group them into groups of 100.
269                  */
270                 if ((xdebug_method_count % 100) == 0)
271                         mono_xdebug_flush ();
272
273                 xdebug_method_count ++;
274
275                 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 ()));
276
277 #if 0
278                 /* 
279                  * Emit a symbol for the code by emitting it at the beginning of the text 
280                  * segment, and setting the text segment to have an absolute address.
281                  * This symbol can be used to set breakpoints in gdb.
282                  * FIXME: This doesn't work when multiple methods are emitted into the same file.
283                  */
284                 sym = get_debug_sym (cfg->jit_info->method, "", xdebug_syms);
285                 img_writer_emit_section_change (w, ".text", 0);
286                 if (!xdebug_text_addr) {
287                         xdebug_text_addr = cfg->jit_info->code_start;
288                         img_writer_set_section_addr (w, (gssize)xdebug_text_addr);
289                 }
290                 img_writer_emit_global_with_size (w, sym, cfg->jit_info->code_size, TRUE);
291                 img_writer_emit_label (w, sym);
292                 img_writer_emit_bytes (w, cfg->jit_info->code_start, cfg->jit_info->code_size);
293                 g_free (sym);
294 #endif
295                 
296                 mono_loader_unlock ();
297         } else {
298                 if (!xdebug_writer)
299                         return;
300
301                 mono_loader_lock ();
302                 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 ()));
303                 fflush (xdebug_fp);
304                 mono_loader_unlock ();
305         }
306 }
307
308 /*
309  * mono_save_trampoline_xdebug_info:
310  *
311  *   Same as mono_save_xdebug_info, but for trampolines.
312  * LOCKING: Acquires the loader lock.
313  */
314 void
315 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
316 {
317         if (use_gdb_interface) {
318                 MonoImageWriter *w;
319                 MonoDwarfWriter *dw;
320
321                 mono_loader_lock ();
322
323                 xdebug_begin_emit (&w, &dw);
324
325                 mono_dwarf_writer_emit_trampoline (dw, tramp_name, NULL, NULL, code, code_size, unwind_info);
326
327                 xdebug_end_emit (w, dw, NULL);
328                 
329                 mono_loader_unlock ();
330         } else {
331                 if (!xdebug_writer)
332                         return;
333
334                 mono_loader_lock ();
335                 mono_dwarf_writer_emit_trampoline (xdebug_writer, tramp_name, NULL, NULL, code, code_size, unwind_info);
336                 fflush (xdebug_fp);
337                 mono_loader_unlock ();
338         }
339 }
340
341 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
342
343 void
344 mono_xdebug_init (char *options)
345 {
346 }
347
348 void
349 mono_save_xdebug_info (MonoCompile *cfg)
350 {
351 }
352
353 void
354 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
355 {
356 }
357
358 #endif