6 #include <mono/metadata/class.h>
7 #include <mono/metadata/assembly.h>
8 #include <mono/metadata/tabledefs.h>
9 #include <mono/metadata/tokentype.h>
10 #include <mono/metadata/debug-helpers.h>
11 #include <mono/metadata/debug-mono-symfile.h>
12 #include <mono/jit/codegen.h>
13 #include <mono/jit/debug.h>
15 #include "debug-private.h"
18 /* This is incremented each time the symbol table is modified.
19 * The debugger looks at this variable and if it has a higher value than its current
20 * copy of the symbol table, it must call debugger_update_symbol_file_table().
22 static guint32 debugger_symbol_file_table_generation = 0;
23 static guint32 debugger_symbol_file_table_modified = 0;
25 /* Caution: This variable may be accessed at any time from the debugger;
26 * it is very important not to modify the memory it is pointing to
27 * without previously setting this pointer back to NULL.
29 static MonoDebuggerSymbolFileTable *debugger_symbol_file_table = NULL;
31 /* Caution: This function MUST be called before touching the symbol table! */
32 static void release_symbol_file_table (void);
34 static void initialize_debugger_support (void);
36 static MonoDebugHandle *mono_debug_handle = NULL;
37 static gconstpointer debugger_notification_address = NULL;
38 static GCond *debugger_thread_cond = NULL;
39 static GStaticRecMutex debugger_lock_mutex = G_STATIC_REC_MUTEX_INIT;
40 static GMutex *debugger_thread_mutex = NULL;
41 static GCond *debugger_finished_cond = NULL;
42 static GMutex *debugger_finished_mutex = NULL;
43 static GCond *debugger_start_cond = NULL;
44 static GMutex *debugger_start_mutex = NULL;
45 static gboolean debugger_signalled = FALSE;
46 static gboolean must_send_finished = FALSE;
48 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
50 static guint64 debugger_insert_breakpoint (guint64 method_argument, const gchar *string_argument);
51 static guint64 debugger_remove_breakpoint (guint64 breakpoint);
52 static int debugger_update_symbol_file_table (void);
53 static gpointer debugger_compile_method (MonoMethod *method);
55 static void mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data);
56 static void mono_debug_close_assembly (AssemblyDebugInfo* info);
57 static AssemblyDebugInfo *mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image);
60 * This is a global data symbol which is read by the debugger.
62 MonoDebuggerInfo MONO_DEBUGGER__debugger_info = {
63 MONO_SYMBOL_FILE_DYNAMIC_MAGIC,
64 MONO_SYMBOL_FILE_DYNAMIC_VERSION,
65 sizeof (MonoDebuggerInfo),
66 &mono_generic_trampoline_code,
67 &mono_breakpoint_trampoline_code,
68 &debugger_symbol_file_table_generation,
69 &debugger_symbol_file_table_modified,
70 &debugger_notification_address,
71 &debugger_symbol_file_table,
72 &debugger_compile_method,
73 &debugger_insert_breakpoint,
74 &debugger_remove_breakpoint,
79 debugger_init_threads (void)
83 debugger_thread_cond = g_cond_new ();
84 debugger_finished_cond = g_cond_new ();
85 debugger_start_cond = g_cond_new ();
87 debugger_thread_mutex = g_mutex_new ();
88 debugger_finished_mutex = g_mutex_new ();
89 debugger_start_mutex = g_mutex_new ();
93 mono_debugger_lock (void)
95 g_static_rec_mutex_lock (&debugger_lock_mutex);
99 mono_debugger_unlock (void)
101 g_static_rec_mutex_unlock (&debugger_lock_mutex);
105 mono_debugger_signal (gboolean modified)
108 debugger_symbol_file_table_modified = TRUE;
109 if (!debugger_signalled) {
110 debugger_signalled = TRUE;
111 g_cond_signal (debugger_thread_cond);
116 mono_debugger_wait (void)
118 g_cond_wait (debugger_finished_cond, debugger_finished_mutex);
122 free_method_info (MonoDebugMethodInfo *minfo)
124 DebugMethodInfo *priv = minfo->user_data;
132 g_array_free (minfo->jit->line_numbers, TRUE);
133 g_free (minfo->jit->this_var);
134 g_free (minfo->jit->params);
135 g_free (minfo->jit->locals);
139 g_free (minfo->il_offsets);
144 debug_arg_warning (const char *message)
146 g_warning ("Error while processing --debug-args arguments: %s", message);
150 replace_suffix (const char *filename, const char *new_suffix)
152 const char *pos = strrchr (filename, '.');
155 return g_strdup_printf ("%s.%s", filename, new_suffix);
157 int len = pos - filename;
158 gchar *retval = g_malloc0 (len + strlen (new_suffix) + 2);
159 memcpy (retval, filename, len);
161 memcpy (retval + len + 1, new_suffix, strlen (new_suffix) + 1);
167 mono_debug_open (MonoAssembly *assembly, MonoDebugFormat format, const char **args)
169 MonoDebugHandle *debug;
172 g_assert (!mono_debug_handle);
174 debug = g_new0 (MonoDebugHandle, 1);
175 debug->name = g_strdup (assembly->image->name);
176 debug->format = format;
177 debug->producer_name = g_strdup_printf ("Mono JIT compiler version %s", VERSION);
178 debug->next_idx = 100;
181 debug->type_hash = g_hash_table_new (NULL, NULL);
182 debug->source_files = g_ptr_array_new ();
184 debug->images = g_hash_table_new_full (NULL, NULL, NULL,
185 (GDestroyNotify) mono_debug_close_assembly);
187 for (ptr = args; ptr && *ptr; ptr++) {
188 const char *arg = *ptr;
191 switch (debug->format) {
192 case MONO_DEBUG_FORMAT_STABS:
193 case MONO_DEBUG_FORMAT_DWARF2:
194 if (!strncmp (arg, "filename=", 9)) {
196 debug_arg_warning ("The `filename' argument can be given only once.");
197 debug->filename = g_strdup (arg + 9);
199 } else if (!strncmp (arg, "objfile=", 8)) {
201 debug_arg_warning ("The `objfile' argument can be given only once.");
202 debug->objfile = g_strdup (arg + 8);
206 case MONO_DEBUG_FORMAT_MONO:
207 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
208 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES |
209 MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
215 if ((debug->format != MONO_DEBUG_FORMAT_MONO) &&
216 (debug->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER)) {
217 if (!strcmp (arg, "dont_assemble")) {
218 debug->flags |= MONO_DEBUG_FLAGS_DONT_ASSEMBLE;
220 } else if (!strcmp (arg, "update_on_exit")) {
221 debug->flags |= MONO_DEBUG_FLAGS_UPDATE_ON_EXIT;
223 } else if (!strcmp (arg, "install_il_files")) {
224 debug->flags |= MONO_DEBUG_FLAGS_INSTALL_IL_FILES;
226 } else if (!strcmp (arg, "dont_update_il_files")) {
227 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES;
229 } else if (!strcmp (arg, "dont_create_il_files")) {
230 debug->flags |= MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
235 message = g_strdup_printf ("Unknown argument `%s'.", arg);
236 debug_arg_warning (message);
240 switch (debug->format) {
241 case MONO_DEBUG_FORMAT_STABS:
242 if (!debug->filename)
243 debug->filename = g_strdup_printf ("%s-stabs.s", g_basename (debug->name));
245 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
247 case MONO_DEBUG_FORMAT_DWARF2:
248 if (!debug->filename)
249 debug->filename = g_strdup_printf ("%s-dwarf.s", g_basename (debug->name));
251 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
253 case MONO_DEBUG_FORMAT_MONO:
254 mono_debugger_class_init_func = mono_debug_add_type;
256 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
257 mono_debugger_class_init_func = mono_debug_add_type;
258 initialize_debugger_support ();
261 g_assert_not_reached ();
264 mono_debugger_lock ();
265 release_symbol_file_table ();
267 mono_debug_handle = debug;
268 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
270 mono_debug_open_image (mono_debug_handle, assembly->image);
271 mono_debug_open_image (mono_debug_handle, mono_defaults.corlib);
273 mono_debug_add_type (mono_defaults.object_class);
274 mono_debug_add_type (mono_defaults.object_class);
275 mono_debug_add_type (mono_defaults.byte_class);
276 mono_debug_add_type (mono_defaults.void_class);
277 mono_debug_add_type (mono_defaults.boolean_class);
278 mono_debug_add_type (mono_defaults.sbyte_class);
279 mono_debug_add_type (mono_defaults.int16_class);
280 mono_debug_add_type (mono_defaults.uint16_class);
281 mono_debug_add_type (mono_defaults.int32_class);
282 mono_debug_add_type (mono_defaults.uint32_class);
283 mono_debug_add_type (mono_defaults.int_class);
284 mono_debug_add_type (mono_defaults.uint_class);
285 mono_debug_add_type (mono_defaults.int64_class);
286 mono_debug_add_type (mono_defaults.uint64_class);
287 mono_debug_add_type (mono_defaults.single_class);
288 mono_debug_add_type (mono_defaults.double_class);
289 mono_debug_add_type (mono_defaults.char_class);
290 mono_debug_add_type (mono_defaults.string_class);
291 mono_debug_add_type (mono_defaults.enum_class);
292 mono_debug_add_type (mono_defaults.array_class);
293 mono_debug_add_type (mono_defaults.multicastdelegate_class);
294 mono_debug_add_type (mono_defaults.asyncresult_class);
295 mono_debug_add_type (mono_defaults.waithandle_class);
296 mono_debug_add_type (mono_defaults.typehandle_class);
297 mono_debug_add_type (mono_defaults.fieldhandle_class);
298 mono_debug_add_type (mono_defaults.methodhandle_class);
299 mono_debug_add_type (mono_defaults.monotype_class);
300 mono_debug_add_type (mono_defaults.exception_class);
301 mono_debug_add_type (mono_defaults.threadabortexception_class);
302 mono_debug_add_type (mono_defaults.thread_class);
303 mono_debug_add_type (mono_defaults.transparent_proxy_class);
304 mono_debug_add_type (mono_defaults.real_proxy_class);
305 mono_debug_add_type (mono_defaults.mono_method_message_class);
306 mono_debug_add_type (mono_defaults.appdomain_class);
307 mono_debug_add_type (mono_defaults.field_info_class);
308 mono_debug_add_type (mono_defaults.stringbuilder_class);
309 mono_debug_add_type (mono_defaults.math_class);
310 mono_debug_add_type (mono_defaults.stack_frame_class);
311 mono_debug_add_type (mono_defaults.stack_trace_class);
312 mono_debug_add_type (mono_defaults.marshal_class);
313 mono_debug_add_type (mono_defaults.iserializeable_class);
314 mono_debug_add_type (mono_defaults.serializationinfo_class);
315 mono_debug_add_type (mono_defaults.streamingcontext_class);
317 debugger_update_symbol_file_table ();
319 mono_debugger_unlock ();
325 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
327 if (!mono_debug_handle)
330 mono_debugger_lock ();
331 mono_debug_open_image (mono_debug_handle, assembly->image);
332 mono_debugger_unlock ();
336 generate_il_offsets (AssemblyDebugInfo *info, MonoMethod *method)
338 GPtrArray *il_offsets = g_ptr_array_new ();
339 MonoClass *klass = method->klass;
340 MonoDebugMethodInfo *minfo;
341 DebugMethodInfo *priv;
344 g_assert (klass->image == info->image);
346 /* FIXME: doesn't work yet. */
347 if (!strcmp (klass->name_space, "System.Runtime.Remoting.Proxies"))
350 mono_class_init (klass);
352 minfo = g_new0 (MonoDebugMethodInfo, 1);
353 minfo->method = method;
354 minfo->user_data = priv = g_new0 (DebugMethodInfo, 1);
356 priv->name = g_strdup_printf ("%s%s%s.%s", klass->name_space, klass->name_space [0]? ".": "",
357 klass->name, method->name);
358 priv->source_file = info->source_file;
362 * Find the method index in the image.
364 for (i = 0; klass->methods && i < klass->method.count; ++i) {
365 if (klass->methods [i] == minfo->method) {
366 priv->method_number = klass->method.first + i + 1;
367 priv->first_line = info->mlines [priv->method_number];
372 g_assert (priv->method_number);
374 /* info->moffsets contains -1 "outside" of functions. */
375 for (i = priv->first_line; (i > 0) && (info->moffsets [i] == 0); i--)
377 priv->start_line = i + 1;
379 for (i = priv->start_line; info->moffsets [i] != -1; i++) {
380 MonoSymbolFileLineNumberEntry *lne = g_new0 (MonoSymbolFileLineNumberEntry, 1);
382 if (!info->moffsets [i] && (i > priv->start_line))
385 lne->offset = info->moffsets [i];
388 g_ptr_array_add (il_offsets, lne);
393 minfo->start_line = priv->first_line;
394 minfo->end_line = priv->last_line;
396 minfo->num_il_offsets = il_offsets->len;
397 minfo->il_offsets = g_new0 (MonoSymbolFileLineNumberEntry, il_offsets->len);
398 for (i = 0; i < il_offsets->len; i++) {
399 MonoSymbolFileLineNumberEntry *il = g_ptr_array_index (il_offsets, i);
401 minfo->il_offsets [i] = *il;
404 g_ptr_array_free (il_offsets, TRUE);
406 g_hash_table_insert (info->methods, method, minfo);
410 debug_load_method_lines (AssemblyDebugInfo* info)
412 MonoTableInfo *table = &info->image->tables [MONO_TABLE_METHOD];
418 if (info->always_create_il || !(info->handle->flags & MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES)) {
419 char *command = g_strdup_printf ("monodis --output=%s %s",
420 info->ilfile, info->image->name);
421 struct stat stata, statb;
422 int need_update = FALSE;
424 if (stat (info->image->name, &stata)) {
425 g_warning ("cannot access assembly file (%s): %s",
426 info->image->name, g_strerror (errno));
431 /* If the stat() failed or the file is older. */
432 if (stat (info->ilfile, &statb)) {
434 } else if (statb.st_mtime < stata.st_mtime)
438 #ifndef PLATFORM_WIN32
439 struct sigaction act, oldact;
444 #ifndef PLATFORM_WIN32
445 act.sa_handler = SIG_IGN;
446 act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
447 sigemptyset (&act.sa_mask);
448 sigaddset (&act.sa_mask, SIGCHLD);
449 sigprocmask (SIG_BLOCK, &act.sa_mask, &old_set);
450 sigaction (SIGCHLD, &act, &oldact);
453 g_print ("Recreating %s from %s.\n", info->ilfile, info->image->name);
455 ret = system (command);
457 #ifndef PLATFORM_WIN32
458 sigaction (SIGCHLD, &oldact, NULL);
459 sigprocmask (SIG_SETMASK, &old_set, NULL);
463 g_warning ("cannot create IL assembly file (%s): %s",
464 command, g_strerror (errno));
471 /* use an env var with directories for searching. */
472 if (!(f = fopen (info->ilfile, "r"))) {
473 g_warning ("cannot open IL assembly file %s", info->ilfile);
477 info->total_lines = 100;
478 info->moffsets = g_malloc (info->total_lines * sizeof (int));
481 while (fgets (buf, sizeof (buf), f)) {
484 info->moffsets [i++] = offset;
485 if (i + 2 >= info->total_lines) {
486 info->total_lines += 100;
487 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
488 g_assert (info->moffsets);
491 if (!sscanf (buf, " // method line %d", &mnum))
496 if (mnum >= info->nmethods)
499 while (fgets (buf, sizeof (buf), f)) {
503 if (i + 2 >= info->total_lines) {
504 info->total_lines += 100;
505 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
506 g_assert (info->moffsets);
509 if (strstr (buf, "}")) {
514 if (sscanf (buf, " IL_%x:", &newoffset)) {
520 info->moffsets [i] = offset;
522 /* g_print ("method %d found at %d\n", mnum, pos); */
523 info->mlines [mnum] = pos;
527 for (idx = 1; idx <= table->rows; idx++) {
528 guint32 token = mono_metadata_make_token (MONO_TABLE_METHOD, idx);
529 MonoMethod *method = mono_get_method (info->image, token, NULL);
531 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
532 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
533 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
536 if (method->wrapper_type != MONO_WRAPPER_NONE)
539 generate_il_offsets (info, method);
544 record_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, guint32 line)
546 MonoDebugLineNumberEntry *lne = g_new0 (MonoDebugLineNumberEntry, 1);
548 lne->address = address;
549 lne->offset = offset;
552 g_array_append_val (minfo->jit->line_numbers, *lne);
556 debug_generate_method_lines (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
558 guint32 st_address, st_line;
559 DebugMethodInfo *priv = minfo->user_data;
562 if (!priv || !info->moffsets)
565 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
567 st_line = priv->first_line;
568 st_address = minfo->jit->prologue_end;
570 /* This is the first actual code line of the method. */
571 record_line_number (minfo, st_address, 0, st_line);
573 /* start lines of basic blocks */
574 for (i = 0; i < cfg->block_count; ++i) {
577 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
578 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
579 gint32 line_inc = 0, addr_inc;
582 st_line = priv->first_line;
583 st_address = t->addr;
586 addr_inc = t->addr - st_address;
587 st_address += addr_inc;
589 if (t->cli_addr != -1) {
590 int *lines = info->moffsets + st_line;
593 while ((*k != -1) && (*k < t->cli_addr))
596 line_inc = k - lines;
601 if (t->cli_addr != -1)
602 record_line_number (minfo, st_address, t->cli_addr, st_line);
608 generate_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, int debug)
613 g_message (G_STRLOC ": searching IL offset %x", offset);
615 for (i = minfo->num_il_offsets - 1; i >= 0; i--) {
616 MonoDebugLineNumberEntry *lne;
618 if (minfo->il_offsets [i].offset > offset)
622 g_message (G_STRLOC ": found entry %d: offset = %x, row = %d",
623 i, minfo->il_offsets [i].offset, minfo->il_offsets [i].row);
625 if (minfo->jit->line_numbers->len) {
626 MonoDebugLineNumberEntry last = g_array_index (
627 minfo->jit->line_numbers, MonoDebugLineNumberEntry,
628 minfo->jit->line_numbers->len - 1);
630 /* Avoid writing more than one entry for the same line. */
631 if (minfo->il_offsets [i].row == last.line) {
633 g_message (G_STRLOC ": skipping line: line = %d, last line = %d, "
634 "last address = %x, address = %x, "
635 "last offset = %x, offset = %x",
636 last.line, minfo->il_offsets [i].row,
637 last.address, address, last.offset, offset);
644 g_message (G_STRLOC ": writing entry: line = %d, offfset = %x, address = %x",
645 minfo->il_offsets [i].row, offset, address);
647 lne = g_new0 (MonoDebugLineNumberEntry, 1);
648 lne->address = address;
649 lne->offset = offset;
650 lne->line = minfo->il_offsets [i].row;
652 g_array_append_val (minfo->jit->line_numbers, *lne);
658 debug_update_il_offsets (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
660 MonoMethodHeader *header;
661 guint32 address, offset;
665 g_assert (info->symfile);
666 if (info->symfile->is_dynamic)
669 g_assert (!minfo->jit->line_numbers);
670 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
672 address = minfo->jit->prologue_end;
675 g_assert (((MonoMethodNormal*)minfo->method)->header);
676 header = ((MonoMethodNormal*)minfo->method)->header;
679 if (!strcmp (minfo->method->name, "Test") || !strcmp (minfo->method->name, "Main")) {
680 MonoMethodHeader *header = ((MonoMethodNormal*)minfo->method)->header;
683 mono_disassemble_code (minfo->jit->code_start, minfo->jit->code_size,
684 minfo->method->name);
686 printf ("\nDisassembly:\n%s\n", mono_disasm_code (
687 NULL, minfo->method, header->code, header->code + header->code_size));
688 g_message (G_STRLOC ": %x - %x", minfo->jit->prologue_end, minfo->jit->epilogue_begin);
692 generate_line_number (minfo, address, offset, debug);
694 /* start lines of basic blocks */
695 for (i = 0; i < cfg->block_count; ++i) {
698 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
699 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
701 if ((t->cli_addr == -1) || (t->cli_addr == offset) || (t->addr == address))
704 offset = t->cli_addr;
707 generate_line_number (minfo, address, offset, debug);
711 generate_line_number (minfo, minfo->jit->epilogue_begin, header->code_size, debug);
714 for (i = 0; i < minfo->jit->line_numbers->len; i++) {
715 MonoDebugLineNumberEntry lne = g_array_index (
716 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
718 g_message (G_STRLOC ": %x,%x,%d", lne.address, lne.offset, lne.line);
723 static AssemblyDebugInfo *
724 mono_debug_get_image (MonoDebugHandle* debug, MonoImage *image)
726 return g_hash_table_lookup (debug->images, image);
729 static AssemblyDebugInfo *
730 mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image)
732 AssemblyDebugInfo *info;
735 info = mono_debug_get_image (debug, image);
741 info = g_new0 (AssemblyDebugInfo, 1);
743 info->image->ref_count++;
744 info->name = g_strdup (image->assembly_name);
745 info->format = debug->format;
746 info->handle = debug;
747 info->methods = g_hash_table_new_full (g_direct_hash, g_direct_equal,
748 NULL, (GDestroyNotify) free_method_info);
750 info->source_file = debug->source_files->len;
751 g_ptr_array_add (debug->source_files, g_strdup_printf ("%s.il", image->assembly_name));
753 g_hash_table_insert (debug->images, image, info);
755 info->nmethods = image->tables [MONO_TABLE_METHOD].rows + 1;
756 info->mlines = g_new0 (int, info->nmethods);
758 for (ptr = image->references; ptr && *ptr; ptr++)
759 mono_debug_add_assembly (*ptr, NULL);
761 switch (info->format) {
762 case MONO_DEBUG_FORMAT_STABS:
763 case MONO_DEBUG_FORMAT_DWARF2:
764 if (debug->flags & MONO_DEBUG_FLAGS_INSTALL_IL_FILES) {
765 gchar *dirname = g_path_get_dirname (image->name);
766 info->ilfile = g_strdup_printf ("%s/%s.il", dirname, info->name);
769 info->ilfile = g_strdup_printf ("%s.il", info->name);
771 case MONO_DEBUG_FORMAT_MONO:
772 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
773 info->filename = replace_suffix (image->name, "dbg");
774 if (g_file_test (info->filename, G_FILE_TEST_EXISTS))
775 info->symfile = mono_debug_open_mono_symbol_file (info->image, info->filename, TRUE);
776 else if (info->format == MONO_DEBUG_FORMAT_MONO_DEBUGGER)
777 info->symfile = mono_debug_create_mono_symbol_file (info->image);
778 debugger_symbol_file_table_generation++;
785 if ((debug->format != MONO_DEBUG_FORMAT_MONO) && (debug->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
786 debug_load_method_lines (info);
792 mono_debug_write_symbols (MonoDebugHandle *debug)
794 if (!debug || !debug->dirty)
797 release_symbol_file_table ();
799 switch (debug->format) {
800 case MONO_DEBUG_FORMAT_STABS:
801 mono_debug_write_stabs (debug);
803 case MONO_DEBUG_FORMAT_DWARF2:
804 mono_debug_write_dwarf2 (debug);
806 case MONO_DEBUG_FORMAT_MONO:
807 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
810 g_assert_not_reached ();
813 debug->dirty = FALSE;
817 mono_debug_make_symbols (void)
819 if (!mono_debug_handle || !mono_debug_handle->dirty)
822 switch (mono_debug_handle->format) {
823 case MONO_DEBUG_FORMAT_STABS:
824 mono_debug_write_stabs (mono_debug_handle);
826 case MONO_DEBUG_FORMAT_DWARF2:
827 mono_debug_write_dwarf2 (mono_debug_handle);
829 case MONO_DEBUG_FORMAT_MONO:
830 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
831 debugger_update_symbol_file_table ();
834 g_assert_not_reached ();
837 mono_debug_handle->dirty = FALSE;
841 mono_debug_close_assembly (AssemblyDebugInfo* info)
843 switch (info->format) {
844 case MONO_DEBUG_FORMAT_MONO:
845 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
846 if (info->symfile != NULL)
847 mono_debug_close_mono_symbol_file (info->symfile);
852 g_hash_table_destroy (info->methods);
853 g_free (info->mlines);
854 g_free (info->moffsets);
856 g_free (info->ilfile);
857 g_free (info->filename);
858 g_free (info->objfile);
863 mono_debug_cleanup (void)
865 release_symbol_file_table ();
867 if (!mono_debug_handle)
870 if (mono_debug_handle->flags & MONO_DEBUG_FLAGS_UPDATE_ON_EXIT)
871 mono_debug_write_symbols (mono_debug_handle);
873 g_hash_table_destroy (mono_debug_handle->images);
874 g_ptr_array_free (mono_debug_handle->source_files, TRUE);
875 g_hash_table_destroy (mono_debug_handle->type_hash);
876 g_free (mono_debug_handle->producer_name);
877 g_free (mono_debug_handle->name);
878 g_free (mono_debug_handle);
880 mono_debug_handle = NULL;
884 mono_debug_get_type (MonoDebugHandle *debug, MonoClass *klass)
888 mono_class_init (klass);
890 index = GPOINTER_TO_INT (g_hash_table_lookup (debug->type_hash, klass));
896 index = ++debug->next_klass_idx;
897 g_hash_table_insert (debug->type_hash, klass, GINT_TO_POINTER (index));
902 switch (klass->byval_arg.type) {
903 case MONO_TYPE_CLASS:
905 mono_debug_get_type (debug, klass->parent);
907 for (i = 0; i < klass->method.count; i++) {
908 MonoMethod *method = klass->methods [i];
909 MonoType *ret_type = NULL;
912 if (method->signature->ret->type != MONO_TYPE_VOID)
913 ret_type = method->signature->ret;
916 MonoClass *ret_klass = mono_class_from_mono_type (ret_type);
917 mono_debug_get_type (debug, ret_klass);
920 for (j = 0; j < method->signature->param_count; j++) {
921 MonoType *sub_type = method->signature->params [j];
922 MonoClass *sub_klass = mono_class_from_mono_type (sub_type);
923 mono_debug_get_type (debug, sub_klass);
927 case MONO_TYPE_VALUETYPE:
928 for (i = 0; i < klass->field.count; i++) {
929 MonoClass *subclass = mono_class_from_mono_type (klass->fields [i].type);
930 mono_debug_get_type (debug, subclass);
933 case MONO_TYPE_ARRAY:
934 case MONO_TYPE_SZARRAY:
935 mono_debug_get_type (debug, klass->element_class);
945 il_offset_from_address (MonoDebugMethodInfo *minfo, guint32 address)
949 if (!minfo->jit || !minfo->jit->line_numbers)
952 for (i = minfo->jit->line_numbers->len - 1; i >= 0; i--) {
953 MonoDebugLineNumberEntry lne = g_array_index (
954 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
956 if (lne.address <= address)
964 address_from_il_offset (MonoDebugMethodInfo *minfo, guint32 il_offset)
968 if (!minfo->jit || !minfo->jit->line_numbers)
971 for (i = minfo->jit->line_numbers->len - 1; i >= 0; i--) {
972 MonoDebugLineNumberEntry lne = g_array_index (
973 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
975 if (lne.offset <= il_offset)
983 mono_debug_add_type (MonoClass *klass)
985 AssemblyDebugInfo* info;
987 if (!mono_debug_handle)
990 info = mono_debug_get_image (mono_debug_handle, klass->image);
993 if ((mono_debug_handle->format != MONO_DEBUG_FORMAT_MONO) &&
994 (mono_debug_handle->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
998 mono_debugger_lock ();
999 mono_debug_symfile_add_type (info->symfile, klass);
1000 mono_debugger_signal (TRUE);
1001 mono_debugger_unlock ();
1006 il_offset_from_position (MonoFlowGraph *cfg, MonoPosition *pos)
1011 if (pos->abs_pos == 0)
1014 if (pos->pos.bid >= cfg->block_count)
1017 bblock = &cfg->bblocks [pos->pos.bid];
1018 if (pos->pos.tid >= bblock->forest->len)
1021 tree = (MBTree *) g_ptr_array_index (bblock->forest, pos->pos.tid);
1023 return tree->cli_addr;
1026 struct LookupMethodData
1028 MonoDebugMethodInfo *minfo;
1033 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
1035 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1036 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
1042 data->minfo = mono_debug_find_method (info->symfile, data->method);
1044 data->minfo = g_hash_table_lookup (info->methods, data->method);
1047 static MonoDebugMethodInfo *
1048 lookup_method (MonoMethod *method)
1050 struct LookupMethodData data = { NULL, method };
1052 if (!mono_debug_handle)
1055 g_hash_table_foreach (mono_debug_handle->images, lookup_method_func, &data);
1060 mono_debug_add_method (MonoFlowGraph *cfg)
1062 MonoMethod *method = cfg->method;
1063 MonoClass *klass = method->klass;
1064 AssemblyDebugInfo* info;
1065 MonoDebugMethodJitInfo *jit;
1066 MonoDebugMethodInfo *minfo;
1069 if (!mono_debug_handle)
1072 mono_class_init (klass);
1074 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1075 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1076 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1077 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1080 if (method->wrapper_type != MONO_WRAPPER_NONE)
1083 info = mono_debug_get_image (mono_debug_handle, klass->image);
1086 minfo = lookup_method (method);
1087 if (!minfo || minfo->jit)
1090 mono_debugger_lock ();
1092 mono_debug_handle->dirty = TRUE;
1094 minfo->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
1095 jit->code_start = cfg->start;
1096 jit->code_size = cfg->epilogue_end;
1097 jit->prologue_end = cfg->prologue_end;
1098 jit->epilogue_begin = cfg->epilog;
1099 jit->num_params = method->signature->param_count;
1100 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
1102 if (method->signature->hasthis) {
1103 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index;
1105 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
1106 jit->this_var->offset = ptr->offset;
1107 jit->this_var->size = ptr->size;
1110 for (i = 0; i < jit->num_params; i++) {
1111 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index +
1112 method->signature->hasthis;
1114 jit->params [i].offset = ptr [i].offset;
1115 jit->params [i].size = ptr [i].size;
1118 debug_generate_method_lines (info, minfo, cfg);
1119 if ((info->format == MONO_DEBUG_FORMAT_MONO) || (info->format == MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1120 debug_update_il_offsets (info, minfo, cfg);
1122 if (!method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
1123 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
1124 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->locals_start_index;
1125 MonoDebugVarInfo *locals;
1127 locals = g_new0 (MonoDebugVarInfo, header->num_locals);
1128 for (i = 0; i < header->num_locals; i++) {
1129 gint32 begin_offset, end_offset;
1130 gint32 begin_scope, end_scope;
1132 if (ptr [i].reg >= 0) {
1133 locals [i].index = ptr [i].reg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
1134 locals [i].offset = 0;
1136 locals [i].offset = ptr [i].offset;
1138 locals [i].size = ptr [i].size;
1140 begin_offset = il_offset_from_position (cfg, &ptr [i].range.first_use);
1141 end_offset = il_offset_from_position (cfg, &ptr [i].range.last_use);
1142 if (end_offset >= 0)
1145 if (begin_offset >= 0)
1146 begin_scope = address_from_il_offset (minfo, begin_offset);
1149 if (end_offset >= 0)
1150 end_scope = address_from_il_offset (minfo, end_offset);
1154 if (begin_scope > 0)
1155 locals [i].begin_scope = begin_scope;
1157 locals [i].begin_scope = jit->prologue_end;
1159 locals [i].end_scope = end_scope;
1161 locals [i].end_scope = jit->epilogue_begin;
1164 jit->num_locals = header->num_locals;
1165 jit->locals = locals;
1168 if (info->symfile) {
1169 mono_debug_symfile_add_method (info->symfile, method);
1170 mono_debugger_signal (TRUE);
1173 mono_debugger_unlock ();
1177 mono_debug_source_location_from_address (MonoMethod *method, guint32 address, guint32 *line_number)
1179 MonoDebugMethodInfo *minfo = lookup_method (method);
1184 if (minfo->symfile) {
1185 gint32 offset = il_offset_from_address (minfo, address);
1190 return mono_debug_find_source_location (minfo->symfile, method, offset, line_number);
1197 mono_debug_il_offset_from_address (MonoMethod *method, gint32 address)
1199 MonoDebugMethodInfo *minfo;
1204 minfo = lookup_method (method);
1205 if (!minfo || !minfo->il_offsets)
1208 return il_offset_from_address (minfo, address);
1212 mono_debug_address_from_il_offset (MonoMethod *method, gint32 il_offset)
1214 MonoDebugMethodInfo *minfo;
1219 minfo = lookup_method (method);
1220 if (!minfo || !minfo->il_offsets)
1223 return address_from_il_offset (minfo, il_offset);
1227 release_symbol_file_table ()
1229 MonoDebuggerSymbolFileTable *temp;
1231 if (!debugger_symbol_file_table)
1235 * Caution: The debugger may access the memory pointed to by this variable
1236 * at any time. It is very important to set the pointer to NULL
1237 * before freeing the area.
1240 temp = debugger_symbol_file_table;
1241 debugger_symbol_file_table = NULL;
1242 g_free (debugger_symbol_file_table);
1246 update_symbol_file_table_count_func (gpointer key, gpointer value, gpointer user_data)
1248 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1252 if ((info->format != MONO_DEBUG_FORMAT_MONO) && (info->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1255 ++ (* (int *) user_data);
1258 struct SymfileTableData
1260 MonoDebuggerSymbolFileTable *symfile_table;
1265 update_symbol_file_table_func (gpointer key, gpointer value, gpointer user_data)
1267 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1268 struct SymfileTableData *data = (struct SymfileTableData *) user_data;
1272 if ((info->format != MONO_DEBUG_FORMAT_MONO) && (info->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1275 data->symfile_table->symfiles [data->index++] = info->symfile;
1279 debugger_update_symbol_file_table (void)
1282 MonoDebuggerSymbolFileTable *symfile_table;
1283 struct SymfileTableData data;
1286 if (!mono_debug_handle)
1289 mono_debugger_lock ();
1291 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_count_func, &count);
1293 release_symbol_file_table ();
1295 size = sizeof (MonoDebuggerSymbolFileTable) + count * sizeof (MonoSymbolFile *);
1296 symfile_table = g_malloc0 (size);
1297 symfile_table->magic = MONO_SYMBOL_FILE_DYNAMIC_MAGIC;
1298 symfile_table->version = MONO_SYMBOL_FILE_DYNAMIC_VERSION;
1299 symfile_table->total_size = size;
1300 symfile_table->count = count;
1301 symfile_table->generation = debugger_symbol_file_table_generation;
1302 symfile_table->global_symfile = mono_debugger_global_symbol_file;
1304 data.symfile_table = symfile_table;
1307 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_func, &data);
1309 debugger_symbol_file_table = symfile_table;
1311 mono_debugger_unlock ();
1316 static gboolean has_mono_debugger_support = FALSE;
1319 * NOTE: We must not call any functions here which we ever may want to debug !
1322 debugger_thread_func (gpointer ptr)
1324 void (*notification_code) (void) = ptr;
1325 int last_generation = 0;
1328 * Lock the mutex and raise a SIGSTOP to give the debugger a chance to
1329 * attach to us. This is important since the `notification_code' contains a
1330 * breakpoint instruction which would otherwise be deadly for us.
1332 g_mutex_lock (debugger_thread_mutex);
1336 * Ok, we're now running in the debugger - signal the parent thread that
1337 * we're ready and enter the event loop.
1339 g_mutex_lock (debugger_start_mutex);
1340 g_cond_signal (debugger_start_cond);
1341 g_mutex_unlock (debugger_start_mutex);
1344 /* Wait for an event. */
1345 g_cond_wait (debugger_thread_cond, debugger_thread_mutex);
1347 /* Reload the symbol file table if necessary. */
1348 if (debugger_symbol_file_table_generation > last_generation) {
1349 debugger_update_symbol_file_table ();
1350 last_generation = debugger_symbol_file_table_generation;
1354 * Send notification - we'll stop on a breakpoint instruction at a special
1355 * address. The debugger will reload the symbol tables while we're stopped -
1356 * and owning the `debugger_thread_mutex' so that no other thread can touch
1357 * them in the meantime.
1359 notification_code ();
1361 /* Clear modified and signalled flag. */
1362 debugger_symbol_file_table_modified = FALSE;
1363 debugger_signalled = FALSE;
1365 if (must_send_finished) {
1366 g_mutex_lock (debugger_finished_mutex);
1367 g_cond_signal (debugger_finished_cond);
1368 must_send_finished = FALSE;
1369 g_mutex_unlock (debugger_finished_mutex);
1377 initialize_debugger_support ()
1382 if (has_mono_debugger_support)
1384 debugger_init_threads ();
1385 has_mono_debugger_support = TRUE;
1387 ptr = buf = g_malloc0 (16);
1388 x86_breakpoint (buf);
1389 debugger_notification_address = buf;
1392 g_mutex_lock (debugger_start_mutex);
1395 * This mutex is only unlocked by the g_cond_wait() in
1396 * mono_debugger_wait().
1398 g_mutex_lock (debugger_finished_mutex);
1400 thread = g_thread_create (debugger_thread_func, ptr, FALSE, NULL);
1404 * Wait until the debugger thread has actually been started and the
1405 * debugger attached to it.
1407 g_cond_wait (debugger_start_cond, debugger_start_mutex);
1410 * We keep this mutex until mono_debugger_jit_exec().
1412 g_static_rec_mutex_lock (&debugger_lock_mutex);
1415 static GPtrArray *breakpoints = NULL;
1418 mono_insert_breakpoint_full (MonoMethodDesc *desc, gboolean use_trampoline)
1420 static int last_breakpoint_id = 0;
1421 MonoDebuggerBreakpointInfo *info;
1423 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
1425 info->use_trampoline = use_trampoline;
1426 info->index = ++last_breakpoint_id;
1429 breakpoints = g_ptr_array_new ();
1431 g_ptr_array_add (breakpoints, info);
1437 debugger_insert_breakpoint (guint64 method_argument, const gchar *string_argument)
1439 MonoMethodDesc *desc;
1441 desc = mono_method_desc_new (string_argument, FALSE);
1445 return mono_insert_breakpoint_full (desc, TRUE);
1449 debugger_remove_breakpoint (guint64 breakpoint)
1456 for (i = 0; i < breakpoints->len; i++) {
1457 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1459 if (info->index != breakpoint)
1462 mono_method_desc_free (info->desc);
1463 g_ptr_array_remove (breakpoints, info);
1472 debugger_compile_method (MonoMethod *method)
1476 mono_debugger_lock ();
1477 retval = mono_compile_method (method);
1478 mono_debugger_signal (FALSE);
1479 mono_debugger_unlock ();
1484 mono_remove_breakpoint (int breakpoint_id)
1486 return debugger_remove_breakpoint (breakpoint_id);
1490 mono_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
1492 MonoMethodDesc *desc;
1494 desc = mono_method_desc_new (method_name, include_namespace);
1498 return mono_insert_breakpoint_full (desc, has_mono_debugger_support);
1502 mono_method_has_breakpoint (MonoMethod* method, gboolean use_trampoline)
1506 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
1509 for (i = 0; i < breakpoints->len; i++) {
1510 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1512 if (info->use_trampoline != use_trampoline)
1515 if (!mono_method_desc_full_match (info->desc, method))
1525 mono_debugger_trampoline_breakpoint_callback (void)
1527 mono_debugger_lock ();
1528 must_send_finished = TRUE;
1529 mono_debugger_signal (FALSE);
1530 mono_debugger_unlock ();
1532 mono_debugger_wait ();
1536 * This is a custom version of mono_jit_exec() which is used when we're being run inside
1537 * the Mono Debugger.
1540 mono_debugger_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
1542 MonoImage *image = assembly->image;
1546 method = mono_get_method (image, mono_image_get_entry_point (image), NULL);
1548 addr = mono_compile_method (method);
1551 * The mutex has been locked in initialize_debugger_support(); we keep it locked
1552 * until we compiled the main method and signalled the debugger.
1554 must_send_finished = TRUE;
1555 mono_debugger_signal (TRUE);
1556 mono_debugger_unlock ();
1559 * Wait until the debugger has loaded the initial symbol tables.
1561 mono_debugger_wait ();
1563 return mono_runtime_run_main (method, argc, argv, NULL);