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