Merge pull request #5714 from alexischr/update_bockbuild
[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 #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
53 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) && USE_BIN_WRITER
54
55 #include "dwarfwriter.h"
56
57 #include "mono/utils/mono-compiler.h"
58
59 #define USE_GDB_JIT_INTERFACE
60
61 /* The recommended gdb macro is: */
62 /*
63   define xdb
64   shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
65   add-symbol-file xdb.so 0
66   end
67 */
68
69 /*
70  * GDB JIT interface definitions.
71  *
72  *      http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
73  */
74 typedef enum
75 {
76   JIT_NOACTION = 0,
77   JIT_REGISTER_FN,
78   JIT_UNREGISTER_FN
79 } jit_actions_t;
80
81 struct jit_code_entry
82 {
83         struct jit_code_entry *next_entry;
84         struct jit_code_entry *prev_entry;
85         const char *symfile_addr;
86         /*
87          * The gdb code in gdb/jit.c which reads this structure ignores alignment
88          * requirements, so use two 32 bit fields.
89          */
90         guint32 symfile_size1, symfile_size2;
91 };
92
93 struct jit_descriptor
94 {
95   guint32 version;
96   /* This type should be jit_actions_t, but we use guint32
97      to be explicit about the bitwidth.  */
98   guint32 action_flag;
99   struct jit_code_entry *relevant_entry;
100   struct jit_code_entry *first_entry;
101 };
102
103 /* GDB puts a breakpoint in this function.  */
104 void MONO_NEVER_INLINE __jit_debug_register_code(void);
105
106 #if !defined(MONO_LLVM_LOADED) && defined(ENABLE_LLVM) && !defined(MONO_CROSS_COMPILE)
107
108 /* LLVM already defines these */
109
110 extern struct jit_descriptor __jit_debug_descriptor;
111
112 #else
113
114 /* gcc seems to inline/eliminate calls to noinline functions, thus the asm () */
115 void MONO_NEVER_INLINE __jit_debug_register_code(void) {
116 #if defined(__GNUC__)
117         asm ("");
118 #endif
119 }
120
121 /* Make sure to specify the version statically, because the
122    debugger may check the version before we can set it.  */
123 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
124
125 #endif
126
127 static MonoImageWriter *xdebug_w;
128 static MonoDwarfWriter *xdebug_writer;
129 static FILE *xdebug_fp, *il_file;
130 static gboolean use_gdb_interface, save_symfiles;
131 static int il_file_line_index;
132 static GHashTable *xdebug_syms;
133
134 void
135 mono_xdebug_init (const char *options)
136 {
137         MonoImageWriter *w;
138         char **args, **ptr;
139
140         args = g_strsplit (options, ",", -1);
141         for (ptr = args; ptr && *ptr; ptr ++) {
142                 char *arg = *ptr;
143
144                 if (!strcmp (arg, "gdb"))
145                         use_gdb_interface = TRUE;
146                 if (!strcmp (arg, "save-symfiles"))
147                         save_symfiles = TRUE;
148         }
149
150         /* This file will contain the IL code for methods which don't have debug info */
151         il_file = fopen ("xdb.il", "w");
152         if (il_file == NULL) {
153                 use_gdb_interface = FALSE;
154                 g_warning ("** Unable to create xdb.il. Managed symbol names won't be available.");
155                 return;
156         }
157
158         if (use_gdb_interface)
159                 return;
160
161         unlink ("xdb.s");
162         xdebug_fp = fopen ("xdb.s", "w");
163         
164         w = mono_img_writer_create (xdebug_fp, FALSE);
165
166         mono_img_writer_emit_start (w);
167
168         xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE);
169
170         /* Emit something so the file has a text segment */
171         mono_img_writer_emit_section_change (w, ".text", 0);
172         mono_img_writer_emit_string (w, "");
173
174         mono_dwarf_writer_emit_base_info (xdebug_writer, "JITted code", mono_unwind_get_cie_program ());
175 }
176
177 static void
178 xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
179 {
180         MonoImageWriter *w;
181         MonoDwarfWriter *dw;
182
183         w = mono_img_writer_create (NULL, TRUE);
184
185         mono_img_writer_emit_start (w);
186
187         /* This file will contain the IL code for methods which don't have debug info */
188         if (!il_file)
189                 il_file = fopen ("xdb.il", "w");
190
191         dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, TRUE);
192
193         mono_dwarf_writer_emit_base_info (dw, "JITted code", mono_unwind_get_cie_program ());
194
195         *out_w = w;
196         *out_dw = dw;
197 }
198
199 static void
200 xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
201 {
202         guint8 *img;
203         guint32 img_size;
204         struct jit_code_entry *entry;
205         guint64 *psize;
206
207         il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
208         mono_dwarf_writer_close (dw);
209
210         mono_img_writer_emit_writeout (w);
211
212         img = mono_img_writer_get_output (w, &img_size);
213
214         mono_img_writer_destroy (w);
215
216         if (FALSE) {
217                 /* Save the symbol files to help debugging */
218                 FILE *fp;
219                 char *file_name;
220                 static int file_counter;
221
222                 file_counter ++;
223                 file_name = g_strdup_printf ("xdb-%d.o", file_counter);
224                 printf ("%s %p %d\n", file_name, img, img_size);
225
226                 fp = fopen (file_name, "w");
227                 fwrite (img, img_size, 1, fp);
228                 fclose (fp);
229                 g_free (file_name);
230         }
231
232         /* Register the image with GDB */
233
234         entry = g_malloc0 (sizeof (struct jit_code_entry));
235
236         entry->symfile_addr = (const char*)img;
237         psize = (guint64*)&entry->symfile_size1;
238         *psize = img_size;
239
240         entry->next_entry = __jit_debug_descriptor.first_entry;
241         if (__jit_debug_descriptor.first_entry)
242                 __jit_debug_descriptor.first_entry->prev_entry = entry;
243         __jit_debug_descriptor.first_entry = entry;
244         
245         __jit_debug_descriptor.relevant_entry = entry;
246         __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
247
248         __jit_debug_register_code ();
249 }
250
251 /*
252  * mono_xdebug_flush:
253  *
254  *   This could be called from inside gdb to flush the debugging information not yet
255  * registered with gdb.
256  */
257 void
258 mono_xdebug_flush (void)
259 {
260         if (xdebug_w)
261                 xdebug_end_emit (xdebug_w, xdebug_writer, NULL);
262
263         xdebug_begin_emit (&xdebug_w, &xdebug_writer);
264 }
265
266 static int xdebug_method_count;
267
268 /*
269  * mono_save_xdebug_info:
270  *
271  *   Emit debugging info for METHOD into an assembly file which can be assembled
272  * and loaded into gdb to provide debugging info for JITted code.
273  * LOCKING: Acquires the loader lock.
274  */
275 void
276 mono_save_xdebug_info (MonoCompile *cfg)
277 {
278         MonoDebugMethodJitInfo *dmji;
279
280         if (use_gdb_interface) {
281                 mono_loader_lock ();
282
283                 if (!xdebug_syms)
284                         xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
285
286                 /*
287                  * gdb is not designed to handle 1000s of symbol files (one per method). So we
288                  * group them into groups of 100.
289                  */
290                 if ((xdebug_method_count % 100) == 0)
291                         mono_xdebug_flush ();
292
293                 xdebug_method_count ++;
294
295                 dmji = mono_debug_find_method (jinfo_get_method (cfg->jit_info), mono_domain_get ());;
296                 mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, NULL,
297                                                                            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                 mono_img_writer_emit_section_change (w, ".text", 0);
309                 if (!xdebug_text_addr) {
310                         xdebug_text_addr = cfg->jit_info->code_start;
311                         mono_img_writer_set_section_addr (w, (gssize)xdebug_text_addr);
312                 }
313                 mono_img_writer_emit_global_with_size (w, sym, cfg->jit_info->code_size, TRUE);
314                 mono_img_writer_emit_label (w, sym);
315                 mono_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 (jinfo_get_method (cfg->jit_info), mono_domain_get ());
326                 mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, NULL,
327                                                                            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         const char *info_name = info->name;
345         if (info_name == NULL)
346                 info_name = "";
347
348         if (use_gdb_interface) {
349                 MonoImageWriter *w;
350                 MonoDwarfWriter *dw;
351
352                 /* This can be called before the loader lock is initialized */
353                 mono_loader_lock_if_inited ();
354
355                 xdebug_begin_emit (&w, &dw);
356
357                 mono_dwarf_writer_emit_trampoline (dw, info_name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
358
359                 xdebug_end_emit (w, dw, NULL);
360                 
361                 mono_loader_unlock_if_inited ();
362         } else {
363                 if (!xdebug_writer)
364                         return;
365
366                 mono_loader_lock_if_inited ();
367                 mono_dwarf_writer_emit_trampoline (xdebug_writer, info_name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
368                 fflush (xdebug_fp);
369                 mono_loader_unlock_if_inited ();
370         }
371 }
372
373 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
374
375 void
376 mono_xdebug_init (const char *options)
377 {
378 }
379
380 void
381 mono_save_xdebug_info (MonoCompile *cfg)
382 {
383 }
384
385 void
386 mono_save_trampoline_xdebug_info (MonoTrampInfo *info)
387 {
388 }
389
390 #endif