Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[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) && !defined(MONO_CROSS_COMPILE)
109
110 /* LLVM already defines these */
111
112 extern struct jit_descriptor __jit_debug_descriptor;
113
114 #else
115
116 /* gcc seems to inline/eliminate calls to noinline functions, thus the asm () */
117 void MONO_NOINLINE __jit_debug_register_code(void) {
118 #if defined(__GNUC__)
119         asm ("");
120 #endif
121 }
122
123 /* Make sure to specify the version statically, because the
124    debugger may check the version before we can set it.  */
125 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
126
127 #endif
128
129 static MonoImageWriter *xdebug_w;
130 static MonoDwarfWriter *xdebug_writer;
131 static FILE *xdebug_fp, *il_file;
132 static gboolean use_gdb_interface, save_symfiles;
133 static int il_file_line_index;
134 static GHashTable *xdebug_syms;
135
136 void
137 mono_xdebug_init (const char *options)
138 {
139         MonoImageWriter *w;
140         char **args, **ptr;
141
142         args = g_strsplit (options, ",", -1);
143         for (ptr = args; ptr && *ptr; ptr ++) {
144                 char *arg = *ptr;
145
146                 if (!strcmp (arg, "gdb"))
147                         use_gdb_interface = TRUE;
148                 if (!strcmp (arg, "save-symfiles"))
149                         save_symfiles = TRUE;
150         }
151
152         /* This file will contain the IL code for methods which don't have debug info */
153         il_file = fopen ("xdb.il", "w");
154         if (il_file == NULL) {
155                 use_gdb_interface = FALSE;
156                 g_warning ("** Unable to create xdb.il. Managed symbol names won't be available.");
157                 return;
158         }
159
160         if (use_gdb_interface)
161                 return;
162
163         unlink ("xdb.s");
164         xdebug_fp = fopen ("xdb.s", "w");
165         
166         w = img_writer_create (xdebug_fp, FALSE);
167
168         img_writer_emit_start (w);
169
170         xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE, TRUE);
171
172         /* Emit something so the file has a text segment */
173         img_writer_emit_section_change (w, ".text", 0);
174         img_writer_emit_string (w, "");
175
176         mono_dwarf_writer_emit_base_info (xdebug_writer, "JITted code", mono_unwind_get_cie_program ());
177 }
178
179 static void
180 xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
181 {
182         MonoImageWriter *w;
183         MonoDwarfWriter *dw;
184
185         w = img_writer_create (NULL, TRUE);
186
187         img_writer_emit_start (w);
188
189         /* This file will contain the IL code for methods which don't have debug info */
190         if (!il_file)
191                 il_file = fopen ("xdb.il", "w");
192
193         dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, FALSE, TRUE);
194
195         mono_dwarf_writer_emit_base_info (dw, "JITted code", mono_unwind_get_cie_program ());
196
197         *out_w = w;
198         *out_dw = dw;
199 }
200
201 static void
202 xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
203 {
204         guint8 *img;
205         guint32 img_size;
206         struct jit_code_entry *entry;
207         guint64 *psize;
208
209         il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
210         mono_dwarf_writer_close (dw);
211
212         img_writer_emit_writeout (w);
213
214         img = img_writer_get_output (w, &img_size);
215
216         img_writer_destroy (w);
217
218         if (FALSE) {
219                 /* Save the symbol files to help debugging */
220                 FILE *fp;
221                 char *file_name;
222                 static int file_counter;
223
224                 file_counter ++;
225                 file_name = g_strdup_printf ("xdb-%d.o", file_counter);
226                 printf ("%s %p %d\n", file_name, img, img_size);
227
228                 fp = fopen (file_name, "w");
229                 fwrite (img, img_size, 1, fp);
230                 fclose (fp);
231                 g_free (file_name);
232         }
233
234         /* Register the image with GDB */
235
236         entry = g_malloc0 (sizeof (struct jit_code_entry));
237
238         entry->symfile_addr = (const char*)img;
239         psize = (guint64*)&entry->symfile_size1;
240         *psize = img_size;
241
242         entry->next_entry = __jit_debug_descriptor.first_entry;
243         if (__jit_debug_descriptor.first_entry)
244                 __jit_debug_descriptor.first_entry->prev_entry = entry;
245         __jit_debug_descriptor.first_entry = entry;
246         
247         __jit_debug_descriptor.relevant_entry = entry;
248         __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
249
250         __jit_debug_register_code ();
251 }
252
253 /*
254  * mono_xdebug_flush:
255  *
256  *   This could be called from inside gdb to flush the debugging information not yet
257  * registered with gdb.
258  */
259 void
260 mono_xdebug_flush (void)
261 {
262         if (xdebug_w)
263                 xdebug_end_emit (xdebug_w, xdebug_writer, NULL);
264
265         xdebug_begin_emit (&xdebug_w, &xdebug_writer);
266 }
267
268 static int xdebug_method_count;
269
270 /*
271  * mono_save_xdebug_info:
272  *
273  *   Emit debugging info for METHOD into an assembly file which can be assembled
274  * and loaded into gdb to provide debugging info for JITted code.
275  * LOCKING: Acquires the loader lock.
276  */
277 void
278 mono_save_xdebug_info (MonoCompile *cfg)
279 {
280         MonoDebugMethodJitInfo *dmji;
281
282         if (use_gdb_interface) {
283                 mono_loader_lock ();
284
285                 if (!xdebug_syms)
286                         xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
287
288                 /*
289                  * gdb is not designed to handle 1000s of symbol files (one per method). So we
290                  * group them into groups of 100.
291                  */
292                 if ((xdebug_method_count % 100) == 0)
293                         mono_xdebug_flush ();
294
295                 xdebug_method_count ++;
296
297                 dmji = mono_debug_find_method (jinfo_get_method (cfg->jit_info), mono_domain_get ());;
298                 mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, dmji);
299                 mono_debug_free_method_jit_info (dmji);
300
301 #if 0
302                 /* 
303                  * Emit a symbol for the code by emitting it at the beginning of the text 
304                  * segment, and setting the text segment to have an absolute address.
305                  * This symbol can be used to set breakpoints in gdb.
306                  * FIXME: This doesn't work when multiple methods are emitted into the same file.
307                  */
308                 sym = get_debug_sym (cfg->jit_info->method, "", xdebug_syms);
309                 img_writer_emit_section_change (w, ".text", 0);
310                 if (!xdebug_text_addr) {
311                         xdebug_text_addr = cfg->jit_info->code_start;
312                         img_writer_set_section_addr (w, (gssize)xdebug_text_addr);
313                 }
314                 img_writer_emit_global_with_size (w, sym, cfg->jit_info->code_size, TRUE);
315                 img_writer_emit_label (w, sym);
316                 img_writer_emit_bytes (w, cfg->jit_info->code_start, cfg->jit_info->code_size);
317                 g_free (sym);
318 #endif
319                 
320                 mono_loader_unlock ();
321         } else {
322                 if (!xdebug_writer)
323                         return;
324
325                 mono_loader_lock ();
326                 dmji = mono_debug_find_method (jinfo_get_method (cfg->jit_info), mono_domain_get ());
327                 mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, dmji);
328                 mono_debug_free_method_jit_info (dmji);
329                 fflush (xdebug_fp);
330                 mono_loader_unlock ();
331         }
332
333 }
334
335 /*
336  * mono_save_trampoline_xdebug_info:
337  *
338  *   Same as mono_save_xdebug_info, but for trampolines.
339  * LOCKING: Acquires the loader lock.
340  */
341 void
342 mono_save_trampoline_xdebug_info (MonoTrampInfo *info)
343 {
344         if (use_gdb_interface) {
345                 MonoImageWriter *w;
346                 MonoDwarfWriter *dw;
347
348                 /* This can be called before the loader lock is initialized */
349                 mono_loader_lock_if_inited ();
350
351                 xdebug_begin_emit (&w, &dw);
352
353                 mono_dwarf_writer_emit_trampoline (dw, info->name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
354
355                 xdebug_end_emit (w, dw, NULL);
356                 
357                 mono_loader_unlock_if_inited ();
358         } else {
359                 if (!xdebug_writer)
360                         return;
361
362                 mono_loader_lock_if_inited ();
363                 mono_dwarf_writer_emit_trampoline (xdebug_writer, info->name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
364                 fflush (xdebug_fp);
365                 mono_loader_unlock_if_inited ();
366         }
367 }
368
369 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
370
371 void
372 mono_xdebug_init (const char *options)
373 {
374 }
375
376 void
377 mono_save_xdebug_info (MonoCompile *cfg)
378 {
379 }
380
381 void
382 mono_save_trampoline_xdebug_info (MonoTrampInfo *info)
383 {
384 }
385
386 #endif