7 #include <mono/metadata/class.h>
8 #include <mono/metadata/assembly.h>
9 #include <mono/metadata/tabledefs.h>
10 #include <mono/metadata/tokentype.h>
11 #include <mono/metadata/debug-helpers.h>
12 #include <mono/metadata/debug-mono-symfile.h>
13 #include <mono/jit/codegen.h>
14 #include <mono/jit/debug.h>
16 #include "debug-private.h"
20 * NOTE: Functions and variables starting with `mono_debug_' and `debug_' are
21 * part of the general debugging code.
23 * Functions and variables starting with `mono_debugger_' and `debugger_'
24 * are only used when the JIT is running inside the Mono Debugger.
26 * FIXME: This file needs some API loving.
29 /* This is incremented each time the symbol table is modified.
30 * The debugger looks at this variable and if it has a higher value than its current
31 * copy of the symbol table, it must call debugger_update_symbol_file_table().
33 static guint32 debugger_symbol_file_table_generation = 0;
34 static guint32 debugger_symbol_file_table_modified = 0;
36 /* Caution: This variable may be accessed at any time from the debugger;
37 * it is very important not to modify the memory it is pointing to
38 * without previously setting this pointer back to NULL.
40 static MonoDebuggerSymbolFileTable *debugger_symbol_file_table = NULL;
42 /* Caution: This function MUST be called before touching the symbol table! */
43 static void release_symbol_file_table (void);
45 static void initialize_debugger_support (void);
47 static gboolean running_in_the_mono_debugger = FALSE;
49 static MonoDebugHandle *mono_debug_handle = NULL;
50 static gboolean mono_debug_initialized = FALSE;
51 static gconstpointer debugger_notification_address = NULL;
53 static CRITICAL_SECTION debugger_lock_mutex;
55 static gpointer debugger_thread_cond;
56 static gpointer debugger_start_cond;
58 static gpointer debugger_finished_cond;
59 static CRITICAL_SECTION debugger_finished_mutex;
61 static gboolean debugger_signalled = FALSE;
62 static gboolean must_send_finished = FALSE;
64 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
66 static guint64 debugger_insert_breakpoint (guint64 method_argument, const gchar *string_argument);
67 static guint64 debugger_remove_breakpoint (guint64 breakpoint);
68 static int debugger_update_symbol_file_table (void);
69 static int debugger_update_symbol_file_table_internal (void);
70 static gpointer debugger_compile_method (MonoMethod *method);
72 static void mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data);
73 static void mono_debug_close_assembly (AssemblyDebugInfo* info);
74 static AssemblyDebugInfo *mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image);
77 * This is a global data symbol which is read by the debugger.
79 MonoDebuggerInfo MONO_DEBUGGER__debugger_info = {
80 MONO_SYMBOL_FILE_DYNAMIC_MAGIC,
81 MONO_SYMBOL_FILE_DYNAMIC_VERSION,
82 sizeof (MonoDebuggerInfo),
83 &mono_generic_trampoline_code,
84 &mono_breakpoint_trampoline_code,
85 &debugger_symbol_file_table_generation,
86 &debugger_symbol_file_table_modified,
87 &debugger_notification_address,
88 &debugger_symbol_file_table,
89 &debugger_compile_method,
90 &debugger_insert_breakpoint,
91 &debugger_remove_breakpoint,
96 debugger_init_threads (void)
98 debugger_thread_cond = CreateSemaphore (NULL, 0, 1, NULL);
99 debugger_start_cond = CreateSemaphore (NULL, 0, 1, NULL);
101 InitializeCriticalSection (&debugger_finished_mutex);
102 debugger_finished_cond = CreateSemaphore (NULL, 0, 1, NULL);
106 mono_debug_init (void)
108 if (!mono_debug_initialized) {
109 InitializeCriticalSection (&debugger_lock_mutex);
110 mono_debug_initialized = TRUE;
115 mono_debug_lock (void)
117 if (mono_debug_initialized)
118 EnterCriticalSection (&debugger_lock_mutex);
122 mono_debug_unlock (void)
124 if (mono_debug_initialized)
125 LeaveCriticalSection (&debugger_lock_mutex);
129 mono_debugger_signal (gboolean modified)
131 g_assert (running_in_the_mono_debugger);
133 debugger_symbol_file_table_modified = TRUE;
135 if (!debugger_signalled) {
136 debugger_signalled = TRUE;
137 ReleaseSemaphore (debugger_thread_cond, 1, NULL);
139 mono_debug_unlock ();
143 mono_debugger_wait (void)
145 g_assert (running_in_the_mono_debugger);
146 LeaveCriticalSection (&debugger_finished_mutex);
147 g_assert (WaitForSingleObject (debugger_finished_cond, INFINITE) == WAIT_OBJECT_0);
148 EnterCriticalSection (&debugger_finished_mutex);
152 free_method_info (MonoDebugMethodInfo *minfo)
154 DebugMethodInfo *priv = minfo->user_data;
162 g_array_free (minfo->jit->line_numbers, TRUE);
163 g_free (minfo->jit->this_var);
164 g_free (minfo->jit->params);
165 g_free (minfo->jit->locals);
169 g_free (minfo->il_offsets);
174 debug_arg_warning (const char *message)
176 g_warning ("Error while processing --debug-args arguments: %s", message);
180 replace_suffix (const char *filename, const char *new_suffix)
182 const char *pos = strrchr (filename, '.');
185 return g_strdup_printf ("%s.%s", filename, new_suffix);
187 int len = pos - filename;
188 gchar *retval = g_malloc0 (len + strlen (new_suffix) + 2);
189 memcpy (retval, filename, len);
191 memcpy (retval + len + 1, new_suffix, strlen (new_suffix) + 1);
197 mono_debug_open (MonoAssembly *assembly, MonoDebugFormat format, const char **args)
199 MonoDebugHandle *debug;
202 g_assert (!mono_debug_handle);
206 debug = g_new0 (MonoDebugHandle, 1);
207 debug->name = g_strdup (assembly->image->name);
208 debug->format = format;
209 debug->producer_name = g_strdup_printf ("Mono JIT compiler version %s", VERSION);
210 debug->next_idx = 100;
213 debug->type_hash = g_hash_table_new (NULL, NULL);
214 debug->source_files = g_ptr_array_new ();
216 debug->images = g_hash_table_new_full (NULL, NULL, NULL,
217 (GDestroyNotify) mono_debug_close_assembly);
219 for (ptr = args; ptr && *ptr; ptr++) {
220 const char *arg = *ptr;
223 switch (debug->format) {
224 case MONO_DEBUG_FORMAT_STABS:
225 case MONO_DEBUG_FORMAT_DWARF2:
226 if (!strncmp (arg, "filename=", 9)) {
228 debug_arg_warning ("The `filename' argument can be given only once.");
229 debug->filename = g_strdup (arg + 9);
231 } else if (!strncmp (arg, "objfile=", 8)) {
233 debug_arg_warning ("The `objfile' argument can be given only once.");
234 debug->objfile = g_strdup (arg + 8);
238 case MONO_DEBUG_FORMAT_MONO:
239 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
240 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES |
241 MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
247 if ((debug->format != MONO_DEBUG_FORMAT_MONO) &&
248 (debug->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER)) {
249 if (!strcmp (arg, "dont_assemble")) {
250 debug->flags |= MONO_DEBUG_FLAGS_DONT_ASSEMBLE;
252 } else if (!strcmp (arg, "update_on_exit")) {
253 debug->flags |= MONO_DEBUG_FLAGS_UPDATE_ON_EXIT;
255 } else if (!strcmp (arg, "install_il_files")) {
256 debug->flags |= MONO_DEBUG_FLAGS_INSTALL_IL_FILES;
258 } else if (!strcmp (arg, "dont_update_il_files")) {
259 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES;
261 } else if (!strcmp (arg, "dont_create_il_files")) {
262 debug->flags |= MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
267 message = g_strdup_printf ("Unknown argument `%s'.", arg);
268 debug_arg_warning (message);
272 switch (debug->format) {
273 case MONO_DEBUG_FORMAT_STABS:
274 if (!debug->filename)
275 debug->filename = g_strdup_printf ("%s-stabs.s", g_basename (debug->name));
277 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
279 case MONO_DEBUG_FORMAT_DWARF2:
280 if (!debug->filename)
281 debug->filename = g_strdup_printf ("%s-dwarf.s", g_basename (debug->name));
283 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
285 case MONO_DEBUG_FORMAT_MONO:
286 mono_debugger_class_init_func = mono_debug_add_type;
288 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
289 mono_debugger_class_init_func = mono_debug_add_type;
290 initialize_debugger_support ();
293 g_assert_not_reached ();
297 release_symbol_file_table ();
299 mono_debug_handle = debug;
300 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
302 mono_debug_open_image (mono_debug_handle, assembly->image);
303 mono_debug_open_image (mono_debug_handle, mono_defaults.corlib);
305 mono_debug_add_type (mono_defaults.object_class);
306 mono_debug_add_type (mono_defaults.object_class);
307 mono_debug_add_type (mono_defaults.byte_class);
308 mono_debug_add_type (mono_defaults.void_class);
309 mono_debug_add_type (mono_defaults.boolean_class);
310 mono_debug_add_type (mono_defaults.sbyte_class);
311 mono_debug_add_type (mono_defaults.int16_class);
312 mono_debug_add_type (mono_defaults.uint16_class);
313 mono_debug_add_type (mono_defaults.int32_class);
314 mono_debug_add_type (mono_defaults.uint32_class);
315 mono_debug_add_type (mono_defaults.int_class);
316 mono_debug_add_type (mono_defaults.uint_class);
317 mono_debug_add_type (mono_defaults.int64_class);
318 mono_debug_add_type (mono_defaults.uint64_class);
319 mono_debug_add_type (mono_defaults.single_class);
320 mono_debug_add_type (mono_defaults.double_class);
321 mono_debug_add_type (mono_defaults.char_class);
322 mono_debug_add_type (mono_defaults.string_class);
323 mono_debug_add_type (mono_defaults.enum_class);
324 mono_debug_add_type (mono_defaults.array_class);
325 mono_debug_add_type (mono_defaults.multicastdelegate_class);
326 mono_debug_add_type (mono_defaults.asyncresult_class);
327 mono_debug_add_type (mono_defaults.waithandle_class);
328 mono_debug_add_type (mono_defaults.typehandle_class);
329 mono_debug_add_type (mono_defaults.fieldhandle_class);
330 mono_debug_add_type (mono_defaults.methodhandle_class);
331 mono_debug_add_type (mono_defaults.monotype_class);
332 mono_debug_add_type (mono_defaults.exception_class);
333 mono_debug_add_type (mono_defaults.threadabortexception_class);
334 mono_debug_add_type (mono_defaults.thread_class);
335 mono_debug_add_type (mono_defaults.transparent_proxy_class);
336 mono_debug_add_type (mono_defaults.real_proxy_class);
337 mono_debug_add_type (mono_defaults.mono_method_message_class);
338 mono_debug_add_type (mono_defaults.appdomain_class);
339 mono_debug_add_type (mono_defaults.field_info_class);
340 mono_debug_add_type (mono_defaults.stringbuilder_class);
341 mono_debug_add_type (mono_defaults.math_class);
342 mono_debug_add_type (mono_defaults.stack_frame_class);
343 mono_debug_add_type (mono_defaults.stack_trace_class);
344 mono_debug_add_type (mono_defaults.marshal_class);
345 mono_debug_add_type (mono_defaults.iserializeable_class);
346 mono_debug_add_type (mono_defaults.serializationinfo_class);
347 mono_debug_add_type (mono_defaults.streamingcontext_class);
349 debugger_update_symbol_file_table ();
351 mono_debug_unlock ();
357 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
359 if (!mono_debug_handle)
363 mono_debug_open_image (mono_debug_handle, assembly->image);
364 mono_debug_unlock ();
368 generate_il_offsets (AssemblyDebugInfo *info, MonoMethod *method)
370 GPtrArray *il_offsets = g_ptr_array_new ();
371 MonoClass *klass = method->klass;
372 MonoDebugMethodInfo *minfo;
373 DebugMethodInfo *priv;
376 g_assert (klass->image == info->image);
378 /* FIXME: doesn't work yet. */
379 if (!strcmp (klass->name_space, "System.Runtime.Remoting.Proxies"))
382 mono_class_init (klass);
384 minfo = g_new0 (MonoDebugMethodInfo, 1);
385 minfo->method = method;
386 minfo->user_data = priv = g_new0 (DebugMethodInfo, 1);
388 priv->name = g_strdup_printf ("%s%s%s.%s", klass->name_space, klass->name_space [0]? ".": "",
389 klass->name, method->name);
390 priv->source_file = info->source_file;
394 * Find the method index in the image.
396 for (i = 0; klass->methods && i < klass->method.count; ++i) {
397 if (klass->methods [i] == minfo->method) {
398 priv->method_number = klass->method.first + i + 1;
399 priv->first_line = info->mlines [priv->method_number];
404 g_assert (priv->method_number);
406 /* info->moffsets contains -1 "outside" of functions. */
407 for (i = priv->first_line; (i > 0) && (info->moffsets [i] == 0); i--)
409 priv->start_line = i + 1;
411 for (i = priv->start_line; info->moffsets [i] != -1; i++) {
412 MonoSymbolFileLineNumberEntry *lne = g_new0 (MonoSymbolFileLineNumberEntry, 1);
414 if (!info->moffsets [i] && (i > priv->start_line))
417 lne->offset = info->moffsets [i];
420 g_ptr_array_add (il_offsets, lne);
425 minfo->start_line = priv->first_line;
426 minfo->end_line = priv->last_line;
428 minfo->num_il_offsets = il_offsets->len;
429 minfo->il_offsets = g_new0 (MonoSymbolFileLineNumberEntry, il_offsets->len);
430 for (i = 0; i < il_offsets->len; i++) {
431 MonoSymbolFileLineNumberEntry *il = g_ptr_array_index (il_offsets, i);
433 minfo->il_offsets [i] = *il;
436 g_ptr_array_free (il_offsets, TRUE);
438 g_hash_table_insert (info->methods, method, minfo);
442 debug_load_method_lines (AssemblyDebugInfo* info)
444 MonoTableInfo *table = &info->image->tables [MONO_TABLE_METHOD];
450 if (info->always_create_il || !(info->handle->flags & MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES)) {
451 char *command = g_strdup_printf ("monodis --output=%s %s",
452 info->ilfile, info->image->name);
453 struct stat stata, statb;
454 int need_update = FALSE;
456 if (stat (info->image->name, &stata)) {
457 g_warning ("cannot access assembly file (%s): %s",
458 info->image->name, g_strerror (errno));
463 /* If the stat() failed or the file is older. */
464 if (stat (info->ilfile, &statb)) {
466 } else if (statb.st_mtime < stata.st_mtime)
470 #ifndef PLATFORM_WIN32
471 struct sigaction act, oldact;
476 #ifndef PLATFORM_WIN32
477 act.sa_handler = SIG_IGN;
478 act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
479 sigemptyset (&act.sa_mask);
480 sigaddset (&act.sa_mask, SIGCHLD);
481 sigprocmask (SIG_BLOCK, &act.sa_mask, &old_set);
482 sigaction (SIGCHLD, &act, &oldact);
485 g_print ("Recreating %s from %s.\n", info->ilfile, info->image->name);
487 ret = system (command);
489 #ifndef PLATFORM_WIN32
490 sigaction (SIGCHLD, &oldact, NULL);
491 sigprocmask (SIG_SETMASK, &old_set, NULL);
495 g_warning ("cannot create IL assembly file (%s): %s",
496 command, g_strerror (errno));
503 /* use an env var with directories for searching. */
504 if (!(f = fopen (info->ilfile, "r"))) {
505 g_warning ("cannot open IL assembly file %s", info->ilfile);
509 info->total_lines = 100;
510 info->moffsets = g_malloc (info->total_lines * sizeof (int));
513 while (fgets (buf, sizeof (buf), f)) {
516 info->moffsets [i++] = offset;
517 if (i + 2 >= info->total_lines) {
518 info->total_lines += 100;
519 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
520 g_assert (info->moffsets);
523 if (!sscanf (buf, " // method line %d", &mnum))
528 if (mnum >= info->nmethods)
531 while (fgets (buf, sizeof (buf), f)) {
535 if (i + 2 >= info->total_lines) {
536 info->total_lines += 100;
537 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
538 g_assert (info->moffsets);
541 if (strstr (buf, "}")) {
546 if (sscanf (buf, " IL_%x:", &newoffset)) {
552 info->moffsets [i] = offset;
554 /* g_print ("method %d found at %d\n", mnum, pos); */
555 info->mlines [mnum] = pos;
559 for (idx = 1; idx <= table->rows; idx++) {
560 guint32 token = mono_metadata_make_token (MONO_TABLE_METHOD, idx);
561 MonoMethod *method = mono_get_method (info->image, token, NULL);
563 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
564 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
565 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
568 if (method->wrapper_type != MONO_WRAPPER_NONE)
571 generate_il_offsets (info, method);
576 record_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, guint32 line)
578 MonoDebugLineNumberEntry *lne = g_new0 (MonoDebugLineNumberEntry, 1);
580 lne->address = address;
581 lne->offset = offset;
584 g_array_append_val (minfo->jit->line_numbers, *lne);
588 debug_generate_method_lines (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
590 guint32 st_address, st_line;
591 DebugMethodInfo *priv = minfo->user_data;
594 if (!priv || !info->moffsets)
597 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
599 st_line = priv->first_line;
600 st_address = minfo->jit->prologue_end;
602 /* This is the first actual code line of the method. */
603 record_line_number (minfo, st_address, 0, st_line);
605 /* start lines of basic blocks */
606 for (i = 0; i < cfg->block_count; ++i) {
609 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
610 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
611 gint32 line_inc = 0, addr_inc;
614 st_line = priv->first_line;
615 st_address = t->addr;
618 addr_inc = t->addr - st_address;
619 st_address += addr_inc;
621 if (t->cli_addr != -1) {
622 int *lines = info->moffsets + st_line;
625 while ((*k != -1) && (*k < t->cli_addr))
628 line_inc = k - lines;
633 if (t->cli_addr != -1)
634 record_line_number (minfo, st_address, t->cli_addr, st_line);
640 generate_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, int debug)
645 g_message (G_STRLOC ": searching IL offset %x", offset);
647 for (i = minfo->num_il_offsets - 1; i >= 0; i--) {
648 MonoDebugLineNumberEntry *lne;
650 if (minfo->il_offsets [i].offset > offset)
654 g_message (G_STRLOC ": found entry %d: offset = %x, row = %d",
655 i, minfo->il_offsets [i].offset, minfo->il_offsets [i].row);
657 if (minfo->jit->line_numbers->len) {
658 MonoDebugLineNumberEntry last = g_array_index (
659 minfo->jit->line_numbers, MonoDebugLineNumberEntry,
660 minfo->jit->line_numbers->len - 1);
662 /* Avoid writing more than one entry for the same line. */
663 if (minfo->il_offsets [i].row == last.line) {
665 g_message (G_STRLOC ": skipping line: line = %d, last line = %d, "
666 "last address = %x, address = %x, "
667 "last offset = %x, offset = %x",
668 last.line, minfo->il_offsets [i].row,
669 last.address, address, last.offset, offset);
676 g_message (G_STRLOC ": writing entry: line = %d, offfset = %x, address = %x",
677 minfo->il_offsets [i].row, offset, address);
679 lne = g_new0 (MonoDebugLineNumberEntry, 1);
680 lne->address = address;
681 lne->offset = offset;
682 lne->line = minfo->il_offsets [i].row;
684 g_array_append_val (minfo->jit->line_numbers, *lne);
690 debug_update_il_offsets (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
692 MonoMethodHeader *header;
693 guint32 address, offset;
697 g_assert (info->symfile);
698 if (info->symfile->is_dynamic)
701 g_assert (!minfo->jit->line_numbers);
702 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
704 address = minfo->jit->prologue_end;
707 g_assert (((MonoMethodNormal*)minfo->method)->header);
708 header = ((MonoMethodNormal*)minfo->method)->header;
711 if (!strcmp (minfo->method->name, "Test") || !strcmp (minfo->method->name, "Main")) {
712 MonoMethodHeader *header = ((MonoMethodNormal*)minfo->method)->header;
715 mono_disassemble_code (minfo->jit->code_start, minfo->jit->code_size,
716 minfo->method->name);
718 printf ("\nDisassembly:\n%s\n", mono_disasm_code (
719 NULL, minfo->method, header->code, header->code + header->code_size));
720 g_message (G_STRLOC ": %x - %x", minfo->jit->prologue_end, minfo->jit->epilogue_begin);
724 generate_line_number (minfo, address, offset, debug);
726 /* start lines of basic blocks */
727 for (i = 0; i < cfg->block_count; ++i) {
730 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
731 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
733 if ((t->cli_addr == -1) || (t->cli_addr == offset) || (t->addr == address))
736 offset = t->cli_addr;
739 generate_line_number (minfo, address, offset, debug);
743 generate_line_number (minfo, minfo->jit->epilogue_begin, header->code_size, debug);
746 for (i = 0; i < minfo->jit->line_numbers->len; i++) {
747 MonoDebugLineNumberEntry lne = g_array_index (
748 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
750 g_message (G_STRLOC ": %x,%x,%d", lne.address, lne.offset, lne.line);
755 static AssemblyDebugInfo *
756 mono_debug_get_image (MonoDebugHandle* debug, MonoImage *image)
758 return g_hash_table_lookup (debug->images, image);
761 static AssemblyDebugInfo *
762 mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image)
764 AssemblyDebugInfo *info;
767 info = mono_debug_get_image (debug, image);
773 info = g_new0 (AssemblyDebugInfo, 1);
775 info->image->ref_count++;
776 info->name = g_strdup (image->assembly_name);
777 info->format = debug->format;
778 info->handle = debug;
779 info->methods = g_hash_table_new_full (g_direct_hash, g_direct_equal,
780 NULL, (GDestroyNotify) free_method_info);
782 info->source_file = debug->source_files->len;
783 g_ptr_array_add (debug->source_files, g_strdup_printf ("%s.il", image->assembly_name));
785 g_hash_table_insert (debug->images, image, info);
787 info->nmethods = image->tables [MONO_TABLE_METHOD].rows + 1;
788 info->mlines = g_new0 (int, info->nmethods);
790 for (ptr = image->references; ptr && *ptr; ptr++)
791 mono_debug_add_assembly (*ptr, NULL);
793 switch (info->format) {
794 case MONO_DEBUG_FORMAT_STABS:
795 case MONO_DEBUG_FORMAT_DWARF2:
796 if (debug->flags & MONO_DEBUG_FLAGS_INSTALL_IL_FILES) {
797 gchar *dirname = g_path_get_dirname (image->name);
798 info->ilfile = g_strdup_printf ("%s/%s.il", dirname, info->name);
801 info->ilfile = g_strdup_printf ("%s.il", info->name);
803 case MONO_DEBUG_FORMAT_MONO:
804 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
805 info->filename = replace_suffix (image->name, "dbg");
806 if (g_file_test (info->filename, G_FILE_TEST_EXISTS))
807 info->symfile = mono_debug_open_mono_symbol_file (info->image, info->filename, TRUE);
808 else if (info->format == MONO_DEBUG_FORMAT_MONO_DEBUGGER)
809 info->symfile = mono_debug_create_mono_symbol_file (info->image);
810 debugger_symbol_file_table_generation++;
817 if ((debug->format != MONO_DEBUG_FORMAT_MONO) && (debug->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
818 debug_load_method_lines (info);
824 mono_debug_write_symbols (MonoDebugHandle *debug)
826 if (!debug || !debug->dirty)
829 release_symbol_file_table ();
831 switch (debug->format) {
832 case MONO_DEBUG_FORMAT_STABS:
833 mono_debug_write_stabs (debug);
835 case MONO_DEBUG_FORMAT_DWARF2:
836 mono_debug_write_dwarf2 (debug);
838 case MONO_DEBUG_FORMAT_MONO:
839 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
842 g_assert_not_reached ();
845 debug->dirty = FALSE;
849 mono_debug_make_symbols (void)
851 if (!mono_debug_handle || !mono_debug_handle->dirty)
854 switch (mono_debug_handle->format) {
855 case MONO_DEBUG_FORMAT_STABS:
856 mono_debug_write_stabs (mono_debug_handle);
858 case MONO_DEBUG_FORMAT_DWARF2:
859 mono_debug_write_dwarf2 (mono_debug_handle);
861 case MONO_DEBUG_FORMAT_MONO:
862 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
863 debugger_update_symbol_file_table ();
866 g_assert_not_reached ();
869 mono_debug_handle->dirty = FALSE;
873 mono_debug_close_assembly (AssemblyDebugInfo* info)
875 switch (info->format) {
876 case MONO_DEBUG_FORMAT_MONO:
877 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
878 if (info->symfile != NULL)
879 mono_debug_close_mono_symbol_file (info->symfile);
884 g_hash_table_destroy (info->methods);
885 g_free (info->mlines);
886 g_free (info->moffsets);
888 g_free (info->ilfile);
889 g_free (info->filename);
890 g_free (info->objfile);
895 mono_debug_cleanup (void)
897 release_symbol_file_table ();
899 if (!mono_debug_handle)
902 if (mono_debug_handle->flags & MONO_DEBUG_FLAGS_UPDATE_ON_EXIT)
903 mono_debug_write_symbols (mono_debug_handle);
905 g_hash_table_destroy (mono_debug_handle->images);
906 g_ptr_array_free (mono_debug_handle->source_files, TRUE);
907 g_hash_table_destroy (mono_debug_handle->type_hash);
908 g_free (mono_debug_handle->producer_name);
909 g_free (mono_debug_handle->name);
910 g_free (mono_debug_handle);
912 mono_debug_handle = NULL;
916 mono_debug_get_type (MonoDebugHandle *debug, MonoClass *klass)
920 mono_class_init (klass);
922 index = GPOINTER_TO_INT (g_hash_table_lookup (debug->type_hash, klass));
928 index = ++debug->next_klass_idx;
929 g_hash_table_insert (debug->type_hash, klass, GINT_TO_POINTER (index));
934 switch (klass->byval_arg.type) {
935 case MONO_TYPE_CLASS:
937 mono_debug_get_type (debug, klass->parent);
939 for (i = 0; i < klass->method.count; i++) {
940 MonoMethod *method = klass->methods [i];
941 MonoType *ret_type = NULL;
944 if (method->signature->ret->type != MONO_TYPE_VOID)
945 ret_type = method->signature->ret;
948 MonoClass *ret_klass = mono_class_from_mono_type (ret_type);
949 mono_debug_get_type (debug, ret_klass);
952 for (j = 0; j < method->signature->param_count; j++) {
953 MonoType *sub_type = method->signature->params [j];
954 MonoClass *sub_klass = mono_class_from_mono_type (sub_type);
955 mono_debug_get_type (debug, sub_klass);
959 case MONO_TYPE_VALUETYPE:
960 for (i = 0; i < klass->field.count; i++) {
961 MonoClass *subclass = mono_class_from_mono_type (klass->fields [i].type);
962 mono_debug_get_type (debug, subclass);
965 case MONO_TYPE_ARRAY:
966 case MONO_TYPE_SZARRAY:
967 mono_debug_get_type (debug, klass->element_class);
977 il_offset_from_address (MonoDebugMethodInfo *minfo, guint32 address)
981 if (!minfo->jit || !minfo->jit->line_numbers)
984 for (i = minfo->jit->line_numbers->len - 1; i >= 0; i--) {
985 MonoDebugLineNumberEntry lne = g_array_index (
986 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
988 if (lne.address <= address)
996 address_from_il_offset (MonoDebugMethodInfo *minfo, guint32 il_offset)
1000 if (!minfo->jit || !minfo->jit->line_numbers)
1003 for (i = minfo->jit->line_numbers->len - 1; i >= 0; i--) {
1004 MonoDebugLineNumberEntry lne = g_array_index (
1005 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
1007 if (lne.offset <= il_offset)
1015 mono_debug_add_type (MonoClass *klass)
1017 AssemblyDebugInfo* info;
1019 if (!mono_debug_handle)
1022 info = mono_debug_get_image (mono_debug_handle, klass->image);
1025 if ((mono_debug_handle->format != MONO_DEBUG_FORMAT_MONO) &&
1026 (mono_debug_handle->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1029 if (info->symfile) {
1031 mono_debug_symfile_add_type (info->symfile, klass);
1032 if (running_in_the_mono_debugger)
1033 mono_debugger_signal (TRUE);
1034 mono_debug_unlock ();
1039 il_offset_from_position (MonoFlowGraph *cfg, MonoPosition *pos)
1044 if (pos->abs_pos == 0)
1047 if (pos->pos.bid >= cfg->block_count)
1050 bblock = &cfg->bblocks [pos->pos.bid];
1051 if (pos->pos.tid >= bblock->forest->len)
1054 tree = (MBTree *) g_ptr_array_index (bblock->forest, pos->pos.tid);
1056 return tree->cli_addr;
1059 struct LookupMethodData
1061 MonoDebugMethodInfo *minfo;
1066 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
1068 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1069 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
1075 data->minfo = mono_debug_find_method (info->symfile, data->method);
1077 data->minfo = g_hash_table_lookup (info->methods, data->method);
1080 static MonoDebugMethodInfo *
1081 lookup_method (MonoMethod *method)
1083 struct LookupMethodData data = { NULL, method };
1085 if (!mono_debug_handle)
1088 g_hash_table_foreach (mono_debug_handle->images, lookup_method_func, &data);
1093 mono_debug_add_method (MonoFlowGraph *cfg)
1095 MonoMethod *method = cfg->method;
1096 MonoClass *klass = method->klass;
1097 AssemblyDebugInfo* info;
1098 MonoDebugMethodJitInfo *jit;
1099 MonoDebugMethodInfo *minfo;
1102 if (!mono_debug_handle)
1105 mono_class_init (klass);
1107 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1108 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1109 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1110 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1113 if (method->wrapper_type != MONO_WRAPPER_NONE)
1116 info = mono_debug_get_image (mono_debug_handle, klass->image);
1119 minfo = lookup_method (method);
1120 if (!minfo || minfo->jit)
1125 mono_debug_handle->dirty = TRUE;
1127 minfo->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
1128 jit->code_start = cfg->start;
1129 jit->code_size = cfg->epilogue_end;
1130 jit->prologue_end = cfg->prologue_end;
1131 jit->epilogue_begin = cfg->epilog;
1132 jit->num_params = method->signature->param_count;
1133 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
1135 if (method->signature->hasthis) {
1136 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index;
1138 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
1139 jit->this_var->offset = ptr->offset;
1140 jit->this_var->size = ptr->size;
1143 for (i = 0; i < jit->num_params; i++) {
1144 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index +
1145 method->signature->hasthis;
1147 jit->params [i].offset = ptr [i].offset;
1148 jit->params [i].size = ptr [i].size;
1151 debug_generate_method_lines (info, minfo, cfg);
1152 if ((info->format == MONO_DEBUG_FORMAT_MONO) || (info->format == MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1153 debug_update_il_offsets (info, minfo, cfg);
1155 if (!method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
1156 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
1157 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->locals_start_index;
1158 MonoDebugVarInfo *locals;
1160 locals = g_new0 (MonoDebugVarInfo, header->num_locals);
1161 for (i = 0; i < header->num_locals; i++) {
1162 gint32 begin_offset, end_offset;
1163 gint32 begin_scope, end_scope;
1165 if (ptr [i].reg >= 0) {
1166 locals [i].index = ptr [i].reg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
1167 locals [i].offset = 0;
1169 locals [i].offset = ptr [i].offset;
1171 locals [i].size = ptr [i].size;
1173 begin_offset = il_offset_from_position (cfg, &ptr [i].range.first_use);
1174 end_offset = il_offset_from_position (cfg, &ptr [i].range.last_use);
1175 if (end_offset >= 0)
1178 if (begin_offset >= 0)
1179 begin_scope = address_from_il_offset (minfo, begin_offset);
1182 if (end_offset >= 0)
1183 end_scope = address_from_il_offset (minfo, end_offset);
1187 if (begin_scope > 0)
1188 locals [i].begin_scope = begin_scope;
1190 locals [i].begin_scope = jit->prologue_end;
1192 locals [i].end_scope = end_scope;
1194 locals [i].end_scope = jit->epilogue_begin;
1197 jit->num_locals = header->num_locals;
1198 jit->locals = locals;
1201 if (info->symfile) {
1202 mono_debug_symfile_add_method (info->symfile, method);
1203 if (running_in_the_mono_debugger)
1204 mono_debugger_signal (TRUE);
1207 mono_debug_unlock ();
1211 mono_debug_source_location_from_address (MonoMethod *method, guint32 address, guint32 *line_number)
1213 MonoDebugMethodInfo *minfo = lookup_method (method);
1218 if (minfo->symfile) {
1219 gint32 offset = il_offset_from_address (minfo, address);
1224 return mono_debug_find_source_location (minfo->symfile, method, offset, line_number);
1231 mono_debug_il_offset_from_address (MonoMethod *method, gint32 address)
1233 MonoDebugMethodInfo *minfo;
1238 minfo = lookup_method (method);
1239 if (!minfo || !minfo->il_offsets)
1242 return il_offset_from_address (minfo, address);
1246 mono_debug_address_from_il_offset (MonoMethod *method, gint32 il_offset)
1248 MonoDebugMethodInfo *minfo;
1253 minfo = lookup_method (method);
1254 if (!minfo || !minfo->il_offsets)
1257 return address_from_il_offset (minfo, il_offset);
1261 release_symbol_file_table ()
1263 MonoDebuggerSymbolFileTable *temp;
1265 if (!debugger_symbol_file_table)
1269 * Caution: The debugger may access the memory pointed to by this variable
1270 * at any time. It is very important to set the pointer to NULL
1271 * before freeing the area.
1274 temp = debugger_symbol_file_table;
1275 debugger_symbol_file_table = NULL;
1276 g_free (debugger_symbol_file_table);
1280 update_symbol_file_table_count_func (gpointer key, gpointer value, gpointer user_data)
1282 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1286 if ((info->format != MONO_DEBUG_FORMAT_MONO) && (info->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1289 ++ (* (int *) user_data);
1292 struct SymfileTableData
1294 MonoDebuggerSymbolFileTable *symfile_table;
1299 update_symbol_file_table_func (gpointer key, gpointer value, gpointer user_data)
1301 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1302 struct SymfileTableData *data = (struct SymfileTableData *) user_data;
1306 if ((info->format != MONO_DEBUG_FORMAT_MONO) && (info->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1309 data->symfile_table->symfiles [data->index++] = info->symfile;
1313 debugger_update_symbol_file_table_internal (void)
1316 MonoDebuggerSymbolFileTable *symfile_table;
1317 struct SymfileTableData data;
1320 if (!mono_debug_handle)
1325 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_count_func, &count);
1327 release_symbol_file_table ();
1329 size = sizeof (MonoDebuggerSymbolFileTable) + count * sizeof (MonoSymbolFile *);
1330 symfile_table = g_malloc0 (size);
1331 symfile_table->magic = MONO_SYMBOL_FILE_DYNAMIC_MAGIC;
1332 symfile_table->version = MONO_SYMBOL_FILE_DYNAMIC_VERSION;
1333 symfile_table->total_size = size;
1334 symfile_table->count = count;
1335 symfile_table->generation = debugger_symbol_file_table_generation;
1336 symfile_table->global_symfile = mono_debugger_global_symbol_file;
1338 data.symfile_table = symfile_table;
1341 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_func, &data);
1343 debugger_symbol_file_table = symfile_table;
1345 mono_debug_unlock ();
1351 debugger_update_symbol_file_table (void)
1355 if (!mono_debug_handle)
1359 retval = debugger_update_symbol_file_table_internal ();
1360 mono_debug_unlock ();
1365 static pid_t debugger_background_thread;
1366 extern void mono_debugger_init_thread_debug (pid_t);
1369 * NOTE: We must not call any functions here which we ever may want to debug !
1372 debugger_thread_func (gpointer ptr)
1374 void (*notification_code) (void) = ptr;
1375 int last_generation = 0;
1377 mono_new_thread_init (NULL, &last_generation);
1378 debugger_background_thread = getpid ();
1381 * The parent thread waits on this condition because it needs our pid.
1383 ReleaseSemaphore (debugger_start_cond, 1, NULL);
1386 * This mutex is locked by the parent thread until the debugger actually
1387 * attached to us, so we don't need a SIGSTOP here anymore.
1392 /* Wait for an event. */
1393 mono_debug_unlock ();
1394 g_assert (WaitForSingleObject (debugger_thread_cond, INFINITE) == WAIT_OBJECT_0);
1397 /* Reload the symbol file table if necessary. */
1398 if (debugger_symbol_file_table_generation > last_generation) {
1399 debugger_update_symbol_file_table_internal ();
1400 last_generation = debugger_symbol_file_table_generation;
1404 * Send notification - we'll stop on a breakpoint instruction at a special
1405 * address. The debugger will reload the symbol tables while we're stopped -
1406 * and owning the `debugger_thread_mutex' so that no other thread can touch
1407 * them in the meantime.
1409 notification_code ();
1411 /* Clear modified and signalled flag. */
1412 debugger_symbol_file_table_modified = FALSE;
1413 debugger_signalled = FALSE;
1415 if (must_send_finished) {
1416 EnterCriticalSection (&debugger_finished_mutex);
1417 ReleaseSemaphore (debugger_finished_cond, 1, NULL);
1418 must_send_finished = FALSE;
1419 LeaveCriticalSection (&debugger_finished_mutex);
1427 initialize_debugger_support ()
1432 if (running_in_the_mono_debugger)
1434 debugger_init_threads ();
1435 running_in_the_mono_debugger = TRUE;
1437 ptr = buf = g_malloc0 (16);
1438 x86_breakpoint (buf);
1439 debugger_notification_address = buf;
1443 * We keep this mutex until mono_debugger_jit_exec().
1448 * This mutex is only unlocked in mono_debugger_wait().
1450 EnterCriticalSection (&debugger_finished_mutex);
1452 thread = CreateThread (NULL, 0, debugger_thread_func, ptr, FALSE, NULL);
1456 * Wait until the background thread set its pid.
1458 g_assert (WaitForSingleObject (debugger_start_cond, INFINITE) == WAIT_OBJECT_0);
1461 * Wait until the debugger thread has actually been started and we
1462 * have its pid, then actually start the background thread.
1464 #ifndef PLATFORM_WIN32
1465 mono_debugger_init_thread_debug (debugger_background_thread);
1469 static GPtrArray *breakpoints = NULL;
1472 mono_insert_breakpoint_full (MonoMethodDesc *desc, gboolean use_trampoline)
1474 static int last_breakpoint_id = 0;
1475 MonoDebuggerBreakpointInfo *info;
1477 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
1479 info->use_trampoline = use_trampoline;
1480 info->index = ++last_breakpoint_id;
1483 breakpoints = g_ptr_array_new ();
1485 g_ptr_array_add (breakpoints, info);
1491 debugger_insert_breakpoint (guint64 method_argument, const gchar *string_argument)
1493 MonoMethodDesc *desc;
1495 desc = mono_method_desc_new (string_argument, FALSE);
1499 return mono_insert_breakpoint_full (desc, TRUE);
1503 debugger_remove_breakpoint (guint64 breakpoint)
1510 for (i = 0; i < breakpoints->len; i++) {
1511 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1513 if (info->index != breakpoint)
1516 mono_method_desc_free (info->desc);
1517 g_ptr_array_remove (breakpoints, info);
1526 debugger_compile_method (MonoMethod *method)
1531 retval = mono_compile_method (method);
1532 mono_debugger_signal (FALSE);
1533 mono_debug_unlock ();
1538 mono_remove_breakpoint (int breakpoint_id)
1540 return debugger_remove_breakpoint (breakpoint_id);
1544 mono_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
1546 MonoMethodDesc *desc;
1548 desc = mono_method_desc_new (method_name, include_namespace);
1552 return mono_insert_breakpoint_full (desc, running_in_the_mono_debugger);
1556 mono_method_has_breakpoint (MonoMethod* method, gboolean use_trampoline)
1560 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
1563 for (i = 0; i < breakpoints->len; i++) {
1564 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1566 if (info->use_trampoline != use_trampoline)
1569 if (!mono_method_desc_full_match (info->desc, method))
1579 mono_debugger_trampoline_breakpoint_callback (void)
1582 must_send_finished = TRUE;
1583 mono_debugger_signal (FALSE);
1584 mono_debug_unlock ();
1586 mono_debugger_wait ();
1590 * This is a custom version of mono_jit_exec() which is used when we're being run inside
1591 * the Mono Debugger.
1594 mono_debugger_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
1596 MonoImage *image = assembly->image;
1601 method = mono_get_method (image, mono_image_get_entry_point (image), NULL);
1603 addr = mono_compile_method (method);
1606 * The mutex has been locked in initialize_debugger_support(); we keep it locked
1607 * until we compiled the main method and signalled the debugger.
1609 must_send_finished = TRUE;
1610 mono_debugger_signal (TRUE);
1611 mono_debug_unlock ();
1614 * Wait until the debugger has loaded the initial symbol tables.
1616 mono_debugger_wait ();
1618 retval = mono_runtime_run_main (method, argc, argv, NULL);
1621 * Kill the background thread.
1623 kill (debugger_background_thread, SIGKILL);