summaryrefslogtreecommitdiff
path: root/mono/mini/xdebug.c
blob: e249e6e8e958f6b1f30ebe79f2366115453fc4cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * xdebug.c: Support for emitting gdb debug info for JITted code.
 *
 * Author:
 *   Zoltan Varga (vargaz@gmail.com)
 *
 * (C) 2010 Novell, Inc.
 */

/*
 * This works as follows:
 * - the runtime writes out an xdb.s file containing DWARF debug info.
 * - the user calls a gdb macro
 * - the macro compiles and loads this shared library using add-symbol-file.
 *
 * This is based on the xdebug functionality in the Kaffe Java VM.
 * 
 * We emit assembly code instead of using the ELF writer, so we can emit debug info
 * incrementally as each method is JITted, and the debugger doesn't have to call
 * into the runtime to emit the shared library, which would cause all kinds of
 * complications, like threading issues, and the fact that the ELF writer's
 * emit_writeout () function cannot be called more than once.
 * GDB 7.0 and later has a JIT interface.
 */

#include "config.h"
#include <glib.h>
#include "mini.h"

#if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#ifndef HOST_WIN32
#include <sys/time.h>
#else
#include <winsock2.h>
#include <windows.h>
#endif

#include <errno.h>
#include <sys/stat.h>

#include "image-writer.h"
#include "dwarfwriter.h"

#include "mono/utils/mono-compiler.h"

#define USE_GDB_JIT_INTERFACE

/* The recommended gdb macro is: */
/*
  define xdb
  shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
  add-symbol-file xdb.so 0
  end
*/

/*
 * GDB JIT interface definitions.
 *
 *	http://sources.redhat.com/gdb/onlinedocs/gdb_30.html
 */
typedef enum
{
  JIT_NOACTION = 0,
  JIT_REGISTER_FN,
  JIT_UNREGISTER_FN
} jit_actions_t;

struct jit_code_entry
{
	struct jit_code_entry *next_entry;
	struct jit_code_entry *prev_entry;
	const char *symfile_addr;
	/*
	 * The gdb code in gdb/jit.c which reads this structure ignores alignment
	 * requirements, so use two 32 bit fields.
	 */
	guint32 symfile_size1, symfile_size2;
};

struct jit_descriptor
{
  guint32 version;
  /* This type should be jit_actions_t, but we use guint32
     to be explicit about the bitwidth.  */
  guint32 action_flag;
  struct jit_code_entry *relevant_entry;
  struct jit_code_entry *first_entry;
};

/* GDB puts a breakpoint in this function.  */
void MONO_NEVER_INLINE __jit_debug_register_code(void);

#if !defined(MONO_LLVM_LOADED) && defined(ENABLE_LLVM) && !defined(MONO_CROSS_COMPILE)

/* LLVM already defines these */

extern struct jit_descriptor __jit_debug_descriptor;

#else

/* gcc seems to inline/eliminate calls to noinline functions, thus the asm () */
void MONO_NEVER_INLINE __jit_debug_register_code(void) {
#if defined(__GNUC__)
	asm ("");
#endif
}

/* Make sure to specify the version statically, because the
   debugger may check the version before we can set it.  */
struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };

#endif

static MonoImageWriter *xdebug_w;
static MonoDwarfWriter *xdebug_writer;
static FILE *xdebug_fp, *il_file;
static gboolean use_gdb_interface, save_symfiles;
static int il_file_line_index;
static GHashTable *xdebug_syms;

void
mono_xdebug_init (const char *options)
{
	MonoImageWriter *w;
	char **args, **ptr;

	args = g_strsplit (options, ",", -1);
	for (ptr = args; ptr && *ptr; ptr ++) {
		char *arg = *ptr;

		if (!strcmp (arg, "gdb"))
			use_gdb_interface = TRUE;
		if (!strcmp (arg, "save-symfiles"))
			save_symfiles = TRUE;
	}

	/* This file will contain the IL code for methods which don't have debug info */
	il_file = fopen ("xdb.il", "w");
	if (il_file == NULL) {
		use_gdb_interface = FALSE;
		g_warning ("** Unable to create xdb.il. Managed symbol names won't be available.");
		return;
	}

	if (use_gdb_interface)
		return;

	unlink ("xdb.s");
	xdebug_fp = fopen ("xdb.s", "w");
	
	w = mono_img_writer_create (xdebug_fp, FALSE);

	mono_img_writer_emit_start (w);

	xdebug_writer = mono_dwarf_writer_create (w, il_file, 0, TRUE, TRUE);

	/* Emit something so the file has a text segment */
	mono_img_writer_emit_section_change (w, ".text", 0);
	mono_img_writer_emit_string (w, "");

	mono_dwarf_writer_emit_base_info (xdebug_writer, "JITted code", mono_unwind_get_cie_program ());
}

static void
xdebug_begin_emit (MonoImageWriter **out_w, MonoDwarfWriter **out_dw)
{
	MonoImageWriter *w;
	MonoDwarfWriter *dw;

	w = mono_img_writer_create (NULL, TRUE);

	mono_img_writer_emit_start (w);

	/* This file will contain the IL code for methods which don't have debug info */
	if (!il_file)
		il_file = fopen ("xdb.il", "w");

	dw = mono_dwarf_writer_create (w, il_file, il_file_line_index, FALSE, TRUE);

	mono_dwarf_writer_emit_base_info (dw, "JITted code", mono_unwind_get_cie_program ());

	*out_w = w;
	*out_dw = dw;
}

static void
xdebug_end_emit (MonoImageWriter *w, MonoDwarfWriter *dw, MonoMethod *method)
{
	guint8 *img;
	guint32 img_size;
	struct jit_code_entry *entry;
	guint64 *psize;

	il_file_line_index = mono_dwarf_writer_get_il_file_line_index (dw);
	mono_dwarf_writer_close (dw);

	mono_img_writer_emit_writeout (w);

	img = mono_img_writer_get_output (w, &img_size);

	mono_img_writer_destroy (w);

	if (FALSE) {
		/* Save the symbol files to help debugging */
		FILE *fp;
		char *file_name;
		static int file_counter;

		file_counter ++;
		file_name = g_strdup_printf ("xdb-%d.o", file_counter);
		printf ("%s %p %d\n", file_name, img, img_size);

		fp = fopen (file_name, "w");
		fwrite (img, img_size, 1, fp);
		fclose (fp);
		g_free (file_name);
	}

	/* Register the image with GDB */

	entry = g_malloc0 (sizeof (struct jit_code_entry));

	entry->symfile_addr = (const char*)img;
	psize = (guint64*)&entry->symfile_size1;
	*psize = img_size;

	entry->next_entry = __jit_debug_descriptor.first_entry;
	if (__jit_debug_descriptor.first_entry)
		__jit_debug_descriptor.first_entry->prev_entry = entry;
	__jit_debug_descriptor.first_entry = entry;
	
	__jit_debug_descriptor.relevant_entry = entry;
	__jit_debug_descriptor.action_flag = JIT_REGISTER_FN;

	__jit_debug_register_code ();
}

/*
 * mono_xdebug_flush:
 *
 *   This could be called from inside gdb to flush the debugging information not yet
 * registered with gdb.
 */
void
mono_xdebug_flush (void)
{
	if (xdebug_w)
		xdebug_end_emit (xdebug_w, xdebug_writer, NULL);

	xdebug_begin_emit (&xdebug_w, &xdebug_writer);
}

static int xdebug_method_count;

/*
 * mono_save_xdebug_info:
 *
 *   Emit debugging info for METHOD into an assembly file which can be assembled
 * and loaded into gdb to provide debugging info for JITted code.
 * LOCKING: Acquires the loader lock.
 */
void
mono_save_xdebug_info (MonoCompile *cfg)
{
	MonoDebugMethodJitInfo *dmji;

	if (use_gdb_interface) {
		mono_loader_lock ();

		if (!xdebug_syms)
			xdebug_syms = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

		/*
		 * gdb is not designed to handle 1000s of symbol files (one per method). So we
		 * group them into groups of 100.
		 */
		if ((xdebug_method_count % 100) == 0)
			mono_xdebug_flush ();

		xdebug_method_count ++;

		dmji = mono_debug_find_method (jinfo_get_method (cfg->jit_info), mono_domain_get ());;
		mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, NULL,
									   cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, dmji);
		mono_debug_free_method_jit_info (dmji);

#if 0
		/* 
		 * Emit a symbol for the code by emitting it at the beginning of the text 
		 * segment, and setting the text segment to have an absolute address.
		 * This symbol can be used to set breakpoints in gdb.
		 * FIXME: This doesn't work when multiple methods are emitted into the same file.
		 */
		sym = get_debug_sym (cfg->jit_info->method, "", xdebug_syms);
		mono_img_writer_emit_section_change (w, ".text", 0);
		if (!xdebug_text_addr) {
			xdebug_text_addr = cfg->jit_info->code_start;
			mono_img_writer_set_section_addr (w, (gssize)xdebug_text_addr);
		}
		mono_img_writer_emit_global_with_size (w, sym, cfg->jit_info->code_size, TRUE);
		mono_img_writer_emit_label (w, sym);
		mono_img_writer_emit_bytes (w, cfg->jit_info->code_start, cfg->jit_info->code_size);
		g_free (sym);
#endif
		
		mono_loader_unlock ();
	} else {
		if (!xdebug_writer)
			return;

		mono_loader_lock ();
		dmji = mono_debug_find_method (jinfo_get_method (cfg->jit_info), mono_domain_get ());
		mono_dwarf_writer_emit_method (xdebug_writer, cfg, jinfo_get_method (cfg->jit_info), NULL, NULL, NULL,
									   cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, dmji);
		mono_debug_free_method_jit_info (dmji);
		fflush (xdebug_fp);
		mono_loader_unlock ();
	}

}

/*
 * mono_save_trampoline_xdebug_info:
 *
 *   Same as mono_save_xdebug_info, but for trampolines.
 * LOCKING: Acquires the loader lock.
 */
void
mono_save_trampoline_xdebug_info (MonoTrampInfo *info)
{
	if (use_gdb_interface) {
		MonoImageWriter *w;
		MonoDwarfWriter *dw;

		/* This can be called before the loader lock is initialized */
		mono_loader_lock_if_inited ();

		xdebug_begin_emit (&w, &dw);

		mono_dwarf_writer_emit_trampoline (dw, info->name, NULL, NULL, info->code, info->code_size, info->unwind_ops);

		xdebug_end_emit (w, dw, NULL);
		
		mono_loader_unlock_if_inited ();
	} else {
		if (!xdebug_writer)
			return;

		mono_loader_lock_if_inited ();
		mono_dwarf_writer_emit_trampoline (xdebug_writer, info->name, NULL, NULL, info->code, info->code_size, info->unwind_ops);
		fflush (xdebug_fp);
		mono_loader_unlock_if_inited ();
	}
}

#else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */

void
mono_xdebug_init (const char *options)
{
}

void
mono_save_xdebug_info (MonoCompile *cfg)
{
}

void
mono_save_trampoline_xdebug_info (MonoTrampInfo *info)
{
}

#endif