8 #include <mono/metadata/class.h>
9 #include <mono/metadata/assembly.h>
10 #include <mono/metadata/tabledefs.h>
11 #include <mono/metadata/tokentype.h>
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/debug-mono-symfile.h>
14 #include <mono/jit/codegen.h>
15 #include <mono/jit/debug.h>
17 #include "debug-private.h"
21 * NOTE: Functions and variables starting with `mono_debug_' and `debug_' are
22 * part of the general debugging code.
24 * Functions and variables starting with `mono_debugger_' and `debugger_'
25 * are only used when the JIT is running inside the Mono Debugger.
27 * FIXME: This file needs some API loving.
30 /* This is incremented each time the symbol table is modified.
31 * The debugger looks at this variable and if it has a higher value than its current
32 * copy of the symbol table, it must call debugger_update_symbol_file_table().
34 static guint32 debugger_symbol_file_table_generation = 0;
35 static guint32 debugger_symbol_file_table_modified = 0;
37 /* Caution: This variable may be accessed at any time from the debugger;
38 * it is very important not to modify the memory it is pointing to
39 * without previously setting this pointer back to NULL.
41 static MonoDebuggerSymbolFileTable *debugger_symbol_file_table = NULL;
43 /* Caution: This function MUST be called before touching the symbol table! */
44 static void release_symbol_file_table (void);
46 static void initialize_debugger_support (void);
48 static gboolean running_in_the_mono_debugger = FALSE;
50 static MonoDebugHandle *mono_debug_handle = NULL;
51 static gboolean mono_debug_initialized = FALSE;
52 static gconstpointer debugger_notification_address = NULL;
54 static CRITICAL_SECTION debugger_lock_mutex;
56 static gpointer debugger_thread_cond;
57 static gpointer debugger_start_cond;
59 static gpointer debugger_finished_cond;
60 static CRITICAL_SECTION debugger_finished_mutex;
62 static gboolean debugger_signalled = FALSE;
63 static gboolean must_send_finished = FALSE;
65 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
67 static guint64 debugger_insert_breakpoint (guint64 method_argument, const gchar *string_argument);
68 static guint64 debugger_remove_breakpoint (guint64 breakpoint);
69 static int debugger_update_symbol_file_table (void);
70 static int debugger_update_symbol_file_table_internal (void);
71 static gpointer debugger_compile_method (MonoMethod *method);
73 static void mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data);
74 static void mono_debug_close_assembly (AssemblyDebugInfo* info);
75 static AssemblyDebugInfo *mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image);
78 * This is a global data symbol which is read by the debugger.
80 MonoDebuggerInfo MONO_DEBUGGER__debugger_info = {
81 MONO_SYMBOL_FILE_DYNAMIC_MAGIC,
82 MONO_SYMBOL_FILE_DYNAMIC_VERSION,
83 sizeof (MonoDebuggerInfo),
84 &mono_generic_trampoline_code,
85 &mono_breakpoint_trampoline_code,
86 &debugger_symbol_file_table_generation,
87 &debugger_symbol_file_table_modified,
88 &debugger_notification_address,
89 &debugger_symbol_file_table,
90 &debugger_compile_method,
91 &debugger_insert_breakpoint,
92 &debugger_remove_breakpoint,
97 debugger_init_threads (void)
99 debugger_thread_cond = CreateSemaphore (NULL, 0, 1, NULL);
100 debugger_start_cond = CreateSemaphore (NULL, 0, 1, NULL);
102 InitializeCriticalSection (&debugger_finished_mutex);
103 debugger_finished_cond = CreateSemaphore (NULL, 0, 1, NULL);
107 mono_debug_init (void)
109 if (!mono_debug_initialized) {
110 InitializeCriticalSection (&debugger_lock_mutex);
111 mono_debug_initialized = TRUE;
116 mono_debug_lock (void)
118 if (mono_debug_initialized)
119 EnterCriticalSection (&debugger_lock_mutex);
123 mono_debug_unlock (void)
125 if (mono_debug_initialized)
126 LeaveCriticalSection (&debugger_lock_mutex);
130 mono_debugger_signal (gboolean modified)
132 g_assert (running_in_the_mono_debugger);
134 debugger_symbol_file_table_modified = TRUE;
136 if (!debugger_signalled) {
137 debugger_signalled = TRUE;
138 ReleaseSemaphore (debugger_thread_cond, 1, NULL);
140 mono_debug_unlock ();
144 mono_debugger_wait (void)
146 g_assert (running_in_the_mono_debugger);
147 LeaveCriticalSection (&debugger_finished_mutex);
148 g_assert (WaitForSingleObject (debugger_finished_cond, INFINITE) == WAIT_OBJECT_0);
149 EnterCriticalSection (&debugger_finished_mutex);
153 free_method_info (MonoDebugMethodInfo *minfo)
155 DebugMethodInfo *priv = minfo->user_data;
163 g_array_free (minfo->jit->line_numbers, TRUE);
164 g_free (minfo->jit->this_var);
165 g_free (minfo->jit->params);
166 g_free (minfo->jit->locals);
170 g_free (minfo->il_offsets);
175 debug_arg_warning (const char *message)
177 g_warning ("Error while processing --debug-args arguments: %s", message);
181 replace_suffix (const char *filename, const char *new_suffix)
183 const char *pos = strrchr (filename, '.');
186 return g_strdup_printf ("%s.%s", filename, new_suffix);
188 int len = pos - filename;
189 gchar *retval = g_malloc0 (len + strlen (new_suffix) + 2);
190 memcpy (retval, filename, len);
192 memcpy (retval + len + 1, new_suffix, strlen (new_suffix) + 1);
198 mono_debug_open (MonoAssembly *assembly, MonoDebugFormat format, const char **args)
200 MonoDebugHandle *debug;
203 g_assert (!mono_debug_handle);
207 debug = g_new0 (MonoDebugHandle, 1);
208 debug->name = g_strdup (assembly->image->name);
209 debug->format = format;
210 debug->producer_name = g_strdup_printf ("Mono JIT compiler version %s", VERSION);
211 debug->next_idx = 100;
214 debug->type_hash = g_hash_table_new (NULL, NULL);
215 debug->source_files = g_ptr_array_new ();
217 debug->images = g_hash_table_new_full (NULL, NULL, NULL,
218 (GDestroyNotify) mono_debug_close_assembly);
220 for (ptr = args; ptr && *ptr; ptr++) {
221 const char *arg = *ptr;
224 switch (debug->format) {
225 case MONO_DEBUG_FORMAT_STABS:
226 case MONO_DEBUG_FORMAT_DWARF2:
227 if (!strncmp (arg, "filename=", 9)) {
229 debug_arg_warning ("The `filename' argument can be given only once.");
230 debug->filename = g_strdup (arg + 9);
232 } else if (!strncmp (arg, "objfile=", 8)) {
234 debug_arg_warning ("The `objfile' argument can be given only once.");
235 debug->objfile = g_strdup (arg + 8);
239 case MONO_DEBUG_FORMAT_MONO:
240 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
241 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES |
242 MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
248 if ((debug->format != MONO_DEBUG_FORMAT_MONO) &&
249 (debug->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER)) {
250 if (!strcmp (arg, "dont_assemble")) {
251 debug->flags |= MONO_DEBUG_FLAGS_DONT_ASSEMBLE;
253 } else if (!strcmp (arg, "update_on_exit")) {
254 debug->flags |= MONO_DEBUG_FLAGS_UPDATE_ON_EXIT;
256 } else if (!strcmp (arg, "install_il_files")) {
257 debug->flags |= MONO_DEBUG_FLAGS_INSTALL_IL_FILES;
259 } else if (!strcmp (arg, "dont_update_il_files")) {
260 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES;
262 } else if (!strcmp (arg, "dont_create_il_files")) {
263 debug->flags |= MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
268 message = g_strdup_printf ("Unknown argument `%s'.", arg);
269 debug_arg_warning (message);
273 switch (debug->format) {
274 case MONO_DEBUG_FORMAT_STABS:
275 if (!debug->filename)
276 debug->filename = g_strdup_printf ("%s-stabs.s", g_basename (debug->name));
278 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
280 case MONO_DEBUG_FORMAT_DWARF2:
281 if (!debug->filename)
282 debug->filename = g_strdup_printf ("%s-dwarf.s", g_basename (debug->name));
284 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
286 case MONO_DEBUG_FORMAT_MONO:
287 mono_debugger_class_init_func = mono_debug_add_type;
289 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
290 mono_debugger_class_init_func = mono_debug_add_type;
291 initialize_debugger_support ();
294 g_assert_not_reached ();
298 release_symbol_file_table ();
300 mono_debug_handle = debug;
301 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
303 mono_debug_open_image (mono_debug_handle, assembly->image);
304 mono_debug_open_image (mono_debug_handle, mono_defaults.corlib);
306 mono_debug_add_type (mono_defaults.object_class);
307 mono_debug_add_type (mono_defaults.object_class);
308 mono_debug_add_type (mono_defaults.byte_class);
309 mono_debug_add_type (mono_defaults.void_class);
310 mono_debug_add_type (mono_defaults.boolean_class);
311 mono_debug_add_type (mono_defaults.sbyte_class);
312 mono_debug_add_type (mono_defaults.int16_class);
313 mono_debug_add_type (mono_defaults.uint16_class);
314 mono_debug_add_type (mono_defaults.int32_class);
315 mono_debug_add_type (mono_defaults.uint32_class);
316 mono_debug_add_type (mono_defaults.int_class);
317 mono_debug_add_type (mono_defaults.uint_class);
318 mono_debug_add_type (mono_defaults.int64_class);
319 mono_debug_add_type (mono_defaults.uint64_class);
320 mono_debug_add_type (mono_defaults.single_class);
321 mono_debug_add_type (mono_defaults.double_class);
322 mono_debug_add_type (mono_defaults.char_class);
323 mono_debug_add_type (mono_defaults.string_class);
324 mono_debug_add_type (mono_defaults.enum_class);
325 mono_debug_add_type (mono_defaults.array_class);
326 mono_debug_add_type (mono_defaults.multicastdelegate_class);
327 mono_debug_add_type (mono_defaults.asyncresult_class);
328 mono_debug_add_type (mono_defaults.waithandle_class);
329 mono_debug_add_type (mono_defaults.typehandle_class);
330 mono_debug_add_type (mono_defaults.fieldhandle_class);
331 mono_debug_add_type (mono_defaults.methodhandle_class);
332 mono_debug_add_type (mono_defaults.monotype_class);
333 mono_debug_add_type (mono_defaults.exception_class);
334 mono_debug_add_type (mono_defaults.threadabortexception_class);
335 mono_debug_add_type (mono_defaults.thread_class);
336 mono_debug_add_type (mono_defaults.transparent_proxy_class);
337 mono_debug_add_type (mono_defaults.real_proxy_class);
338 mono_debug_add_type (mono_defaults.mono_method_message_class);
339 mono_debug_add_type (mono_defaults.appdomain_class);
340 mono_debug_add_type (mono_defaults.field_info_class);
341 mono_debug_add_type (mono_defaults.stringbuilder_class);
342 mono_debug_add_type (mono_defaults.math_class);
343 mono_debug_add_type (mono_defaults.stack_frame_class);
344 mono_debug_add_type (mono_defaults.stack_trace_class);
345 mono_debug_add_type (mono_defaults.marshal_class);
346 mono_debug_add_type (mono_defaults.iserializeable_class);
347 mono_debug_add_type (mono_defaults.serializationinfo_class);
348 mono_debug_add_type (mono_defaults.streamingcontext_class);
350 debugger_update_symbol_file_table ();
352 mono_debug_unlock ();
358 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
360 if (!mono_debug_handle)
364 mono_debug_open_image (mono_debug_handle, assembly->image);
365 mono_debug_unlock ();
369 generate_il_offsets (AssemblyDebugInfo *info, MonoMethod *method)
371 GPtrArray *il_offsets = g_ptr_array_new ();
372 MonoClass *klass = method->klass;
373 MonoDebugMethodInfo *minfo;
374 DebugMethodInfo *priv;
377 g_assert (klass->image == info->image);
379 /* FIXME: doesn't work yet. */
380 if (!strcmp (klass->name_space, "System.Runtime.Remoting.Proxies"))
383 mono_class_init (klass);
385 minfo = g_new0 (MonoDebugMethodInfo, 1);
386 minfo->method = method;
387 minfo->user_data = priv = g_new0 (DebugMethodInfo, 1);
389 priv->name = g_strdup_printf ("%s%s%s.%s", klass->name_space, klass->name_space [0]? ".": "",
390 klass->name, method->name);
391 priv->source_file = info->source_file;
395 * Find the method index in the image.
397 for (i = 0; klass->methods && i < klass->method.count; ++i) {
398 if (klass->methods [i] == minfo->method) {
399 priv->method_number = klass->method.first + i + 1;
400 priv->first_line = info->mlines [priv->method_number];
405 g_assert (priv->method_number);
407 /* info->moffsets contains -1 "outside" of functions. */
408 for (i = priv->first_line; (i > 0) && (info->moffsets [i] == 0); i--)
410 priv->start_line = i + 1;
412 for (i = priv->start_line; info->moffsets [i] != -1; i++) {
413 MonoSymbolFileLineNumberEntry *lne = g_new0 (MonoSymbolFileLineNumberEntry, 1);
415 if (!info->moffsets [i] && (i > priv->start_line))
418 lne->offset = info->moffsets [i];
421 g_ptr_array_add (il_offsets, lne);
426 minfo->start_line = priv->first_line;
427 minfo->end_line = priv->last_line;
429 minfo->num_il_offsets = il_offsets->len;
430 minfo->il_offsets = g_new0 (MonoSymbolFileLineNumberEntry, il_offsets->len);
431 for (i = 0; i < il_offsets->len; i++) {
432 MonoSymbolFileLineNumberEntry *il = g_ptr_array_index (il_offsets, i);
434 minfo->il_offsets [i] = *il;
437 g_ptr_array_free (il_offsets, TRUE);
439 g_hash_table_insert (info->methods, method, minfo);
443 debug_load_method_lines (AssemblyDebugInfo* info)
445 MonoTableInfo *table = &info->image->tables [MONO_TABLE_METHOD];
451 if (info->always_create_il || !(info->handle->flags & MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES)) {
452 char *command = g_strdup_printf ("monodis --output=%s %s",
453 info->ilfile, info->image->name);
454 struct stat stata, statb;
455 int need_update = FALSE;
457 if (stat (info->image->name, &stata)) {
458 g_warning ("cannot access assembly file (%s): %s",
459 info->image->name, g_strerror (errno));
464 /* If the stat() failed or the file is older. */
465 if (stat (info->ilfile, &statb)) {
467 } else if (statb.st_mtime < stata.st_mtime)
471 #ifndef PLATFORM_WIN32
472 struct sigaction act, oldact;
477 #ifndef PLATFORM_WIN32
478 act.sa_handler = SIG_IGN;
479 act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
480 sigemptyset (&act.sa_mask);
481 sigaddset (&act.sa_mask, SIGCHLD);
482 sigprocmask (SIG_BLOCK, &act.sa_mask, &old_set);
483 sigaction (SIGCHLD, &act, &oldact);
486 g_print ("Recreating %s from %s.\n", info->ilfile, info->image->name);
488 ret = system (command);
490 #ifndef PLATFORM_WIN32
491 sigaction (SIGCHLD, &oldact, NULL);
492 sigprocmask (SIG_SETMASK, &old_set, NULL);
496 g_warning ("cannot create IL assembly file (%s): %s",
497 command, g_strerror (errno));
504 /* use an env var with directories for searching. */
505 if (!(f = fopen (info->ilfile, "r"))) {
506 g_warning ("cannot open IL assembly file %s", info->ilfile);
510 info->total_lines = 100;
511 info->moffsets = g_malloc (info->total_lines * sizeof (int));
514 while (fgets (buf, sizeof (buf), f)) {
517 info->moffsets [i++] = offset;
518 if (i + 2 >= info->total_lines) {
519 info->total_lines += 100;
520 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
521 g_assert (info->moffsets);
524 if (!sscanf (buf, " // method line %d", &mnum))
529 if (mnum >= info->nmethods)
532 while (fgets (buf, sizeof (buf), f)) {
536 if (i + 2 >= info->total_lines) {
537 info->total_lines += 100;
538 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
539 g_assert (info->moffsets);
542 if (strstr (buf, "}")) {
547 if (sscanf (buf, " IL_%x:", &newoffset)) {
553 info->moffsets [i] = offset;
555 /* g_print ("method %d found at %d\n", mnum, pos); */
556 info->mlines [mnum] = pos;
560 for (idx = 1; idx <= table->rows; idx++) {
561 guint32 token = mono_metadata_make_token (MONO_TABLE_METHOD, idx);
562 MonoMethod *method = mono_get_method (info->image, token, NULL);
564 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
565 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
566 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
569 if (method->wrapper_type != MONO_WRAPPER_NONE)
572 generate_il_offsets (info, method);
577 record_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, guint32 line)
579 MonoDebugLineNumberEntry *lne = g_new0 (MonoDebugLineNumberEntry, 1);
581 lne->address = address;
582 lne->offset = offset;
585 g_array_append_val (minfo->jit->line_numbers, *lne);
589 debug_generate_method_lines (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
591 guint32 st_address, st_line;
592 DebugMethodInfo *priv = minfo->user_data;
595 if (!priv || !info->moffsets)
598 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
600 st_line = priv->first_line;
601 st_address = minfo->jit->prologue_end;
603 /* This is the first actual code line of the method. */
604 record_line_number (minfo, st_address, 0, st_line);
606 /* start lines of basic blocks */
607 for (i = 0; i < cfg->block_count; ++i) {
610 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
611 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
612 gint32 line_inc = 0, addr_inc;
615 st_line = priv->first_line;
616 st_address = t->addr;
619 addr_inc = t->addr - st_address;
620 st_address += addr_inc;
622 if (t->cli_addr != -1) {
623 int *lines = info->moffsets + st_line;
626 while ((*k != -1) && (*k < t->cli_addr))
629 line_inc = k - lines;
634 if (t->cli_addr != -1)
635 record_line_number (minfo, st_address, t->cli_addr, st_line);
641 generate_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, int debug)
646 g_message (G_STRLOC ": searching IL offset %x", offset);
648 for (i = minfo->num_il_offsets - 1; i >= 0; i--) {
649 MonoDebugLineNumberEntry *lne;
651 if (minfo->il_offsets [i].offset > offset)
655 g_message (G_STRLOC ": found entry %d: offset = %x, row = %d",
656 i, minfo->il_offsets [i].offset, minfo->il_offsets [i].row);
658 if (minfo->jit->line_numbers->len) {
659 MonoDebugLineNumberEntry last = g_array_index (
660 minfo->jit->line_numbers, MonoDebugLineNumberEntry,
661 minfo->jit->line_numbers->len - 1);
663 /* Avoid writing more than one entry for the same line. */
664 if (minfo->il_offsets [i].row == last.line) {
666 g_message (G_STRLOC ": skipping line: line = %d, last line = %d, "
667 "last address = %x, address = %x, "
668 "last offset = %x, offset = %x",
669 last.line, minfo->il_offsets [i].row,
670 last.address, address, last.offset, offset);
677 g_message (G_STRLOC ": writing entry: line = %d, offfset = %x, address = %x",
678 minfo->il_offsets [i].row, offset, address);
680 lne = g_new0 (MonoDebugLineNumberEntry, 1);
681 lne->address = address;
682 lne->offset = offset;
683 lne->line = minfo->il_offsets [i].row;
685 g_array_append_val (minfo->jit->line_numbers, *lne);
691 debug_update_il_offsets (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
693 MonoMethodHeader *header;
694 guint32 address, offset;
698 g_assert (info->symfile);
699 if (info->symfile->is_dynamic)
702 g_assert (!minfo->jit->line_numbers);
703 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
705 address = minfo->jit->prologue_end;
708 g_assert (((MonoMethodNormal*)minfo->method)->header);
709 header = ((MonoMethodNormal*)minfo->method)->header;
712 if (!strcmp (minfo->method->name, "Test") || !strcmp (minfo->method->name, "Main")) {
713 MonoMethodHeader *header = ((MonoMethodNormal*)minfo->method)->header;
716 mono_disassemble_code (minfo->jit->code_start, minfo->jit->code_size,
717 minfo->method->name);
719 printf ("\nDisassembly:\n%s\n", mono_disasm_code (
720 NULL, minfo->method, header->code, header->code + header->code_size));
721 g_message (G_STRLOC ": %x - %x", minfo->jit->prologue_end, minfo->jit->epilogue_begin);
725 generate_line_number (minfo, address, offset, debug);
727 /* start lines of basic blocks */
728 for (i = 0; i < cfg->block_count; ++i) {
731 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
732 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
734 if ((t->cli_addr == -1) || (t->cli_addr == offset) || (t->addr == address))
737 offset = t->cli_addr;
740 generate_line_number (minfo, address, offset, debug);
744 generate_line_number (minfo, minfo->jit->epilogue_begin, header->code_size, debug);
747 for (i = 0; i < minfo->jit->line_numbers->len; i++) {
748 MonoDebugLineNumberEntry lne = g_array_index (
749 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
751 g_message (G_STRLOC ": %x,%x,%d", lne.address, lne.offset, lne.line);
756 static AssemblyDebugInfo *
757 mono_debug_get_image (MonoDebugHandle* debug, MonoImage *image)
759 return g_hash_table_lookup (debug->images, image);
762 static AssemblyDebugInfo *
763 mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image)
765 AssemblyDebugInfo *info;
768 info = mono_debug_get_image (debug, image);
774 info = g_new0 (AssemblyDebugInfo, 1);
776 info->image->ref_count++;
777 info->name = g_strdup (image->assembly_name);
778 info->format = debug->format;
779 info->handle = debug;
780 info->methods = g_hash_table_new_full (g_direct_hash, g_direct_equal,
781 NULL, (GDestroyNotify) free_method_info);
783 info->source_file = debug->source_files->len;
784 g_ptr_array_add (debug->source_files, g_strdup_printf ("%s.il", image->assembly_name));
786 g_hash_table_insert (debug->images, image, info);
788 info->nmethods = image->tables [MONO_TABLE_METHOD].rows + 1;
789 info->mlines = g_new0 (int, info->nmethods);
791 for (ptr = image->references; ptr && *ptr; ptr++)
792 mono_debug_add_assembly (*ptr, NULL);
794 switch (info->format) {
795 case MONO_DEBUG_FORMAT_STABS:
796 case MONO_DEBUG_FORMAT_DWARF2:
797 if (debug->flags & MONO_DEBUG_FLAGS_INSTALL_IL_FILES) {
798 gchar *dirname = g_path_get_dirname (image->name);
799 info->ilfile = g_strdup_printf ("%s/%s.il", dirname, info->name);
802 info->ilfile = g_strdup_printf ("%s.il", info->name);
804 case MONO_DEBUG_FORMAT_MONO:
805 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
806 info->filename = replace_suffix (image->name, "dbg");
807 if (g_file_test (info->filename, G_FILE_TEST_EXISTS))
808 info->symfile = mono_debug_open_mono_symbol_file (info->image, info->filename, TRUE);
809 else if (info->format == MONO_DEBUG_FORMAT_MONO_DEBUGGER)
810 info->symfile = mono_debug_create_mono_symbol_file (info->image);
811 debugger_symbol_file_table_generation++;
818 if ((debug->format != MONO_DEBUG_FORMAT_MONO) && (debug->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
819 debug_load_method_lines (info);
825 mono_debug_write_symbols (MonoDebugHandle *debug)
827 if (!debug || !debug->dirty)
830 release_symbol_file_table ();
832 switch (debug->format) {
833 case MONO_DEBUG_FORMAT_STABS:
834 mono_debug_write_stabs (debug);
836 case MONO_DEBUG_FORMAT_DWARF2:
837 mono_debug_write_dwarf2 (debug);
839 case MONO_DEBUG_FORMAT_MONO:
840 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
843 g_assert_not_reached ();
846 debug->dirty = FALSE;
850 mono_debug_make_symbols (void)
852 if (!mono_debug_handle || !mono_debug_handle->dirty)
855 switch (mono_debug_handle->format) {
856 case MONO_DEBUG_FORMAT_STABS:
857 mono_debug_write_stabs (mono_debug_handle);
859 case MONO_DEBUG_FORMAT_DWARF2:
860 mono_debug_write_dwarf2 (mono_debug_handle);
862 case MONO_DEBUG_FORMAT_MONO:
863 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
864 debugger_update_symbol_file_table ();
867 g_assert_not_reached ();
870 mono_debug_handle->dirty = FALSE;
874 mono_debug_close_assembly (AssemblyDebugInfo* info)
876 switch (info->format) {
877 case MONO_DEBUG_FORMAT_MONO:
878 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
879 if (info->symfile != NULL)
880 mono_debug_close_mono_symbol_file (info->symfile);
885 g_hash_table_destroy (info->methods);
886 g_free (info->mlines);
887 g_free (info->moffsets);
889 g_free (info->ilfile);
890 g_free (info->filename);
891 g_free (info->objfile);
896 mono_debug_cleanup (void)
898 release_symbol_file_table ();
900 if (!mono_debug_handle)
903 if (mono_debug_handle->flags & MONO_DEBUG_FLAGS_UPDATE_ON_EXIT)
904 mono_debug_write_symbols (mono_debug_handle);
906 g_hash_table_destroy (mono_debug_handle->images);
907 g_ptr_array_free (mono_debug_handle->source_files, TRUE);
908 g_hash_table_destroy (mono_debug_handle->type_hash);
909 g_free (mono_debug_handle->producer_name);
910 g_free (mono_debug_handle->name);
911 g_free (mono_debug_handle);
913 mono_debug_handle = NULL;
917 mono_debug_get_type (MonoDebugHandle *debug, MonoClass *klass)
921 mono_class_init (klass);
923 index = GPOINTER_TO_INT (g_hash_table_lookup (debug->type_hash, klass));
929 index = ++debug->next_klass_idx;
930 g_hash_table_insert (debug->type_hash, klass, GINT_TO_POINTER (index));
935 switch (klass->byval_arg.type) {
936 case MONO_TYPE_CLASS:
938 mono_debug_get_type (debug, klass->parent);
940 for (i = 0; i < klass->method.count; i++) {
941 MonoMethod *method = klass->methods [i];
942 MonoType *ret_type = NULL;
945 if (method->signature->ret->type != MONO_TYPE_VOID)
946 ret_type = method->signature->ret;
949 MonoClass *ret_klass = mono_class_from_mono_type (ret_type);
950 mono_debug_get_type (debug, ret_klass);
953 for (j = 0; j < method->signature->param_count; j++) {
954 MonoType *sub_type = method->signature->params [j];
955 MonoClass *sub_klass = mono_class_from_mono_type (sub_type);
956 mono_debug_get_type (debug, sub_klass);
960 case MONO_TYPE_VALUETYPE:
961 for (i = 0; i < klass->field.count; i++) {
962 MonoClass *subclass = mono_class_from_mono_type (klass->fields [i].type);
963 mono_debug_get_type (debug, subclass);
966 case MONO_TYPE_ARRAY:
967 case MONO_TYPE_SZARRAY:
968 mono_debug_get_type (debug, klass->element_class);
978 il_offset_from_address (MonoDebugMethodInfo *minfo, guint32 address)
982 if (!minfo->jit || !minfo->jit->line_numbers)
985 for (i = minfo->jit->line_numbers->len - 1; i >= 0; i--) {
986 MonoDebugLineNumberEntry lne = g_array_index (
987 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
989 if (lne.address <= address)
997 address_from_il_offset (MonoDebugMethodInfo *minfo, guint32 il_offset)
1001 if (!minfo->jit || !minfo->jit->line_numbers)
1004 for (i = minfo->jit->line_numbers->len - 1; i >= 0; i--) {
1005 MonoDebugLineNumberEntry lne = g_array_index (
1006 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
1008 if (lne.offset <= il_offset)
1016 mono_debug_add_type (MonoClass *klass)
1018 AssemblyDebugInfo* info;
1020 if (!mono_debug_handle)
1023 info = mono_debug_get_image (mono_debug_handle, klass->image);
1026 if ((mono_debug_handle->format != MONO_DEBUG_FORMAT_MONO) &&
1027 (mono_debug_handle->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1030 if (info->symfile) {
1032 mono_debug_symfile_add_type (info->symfile, klass);
1033 if (running_in_the_mono_debugger)
1034 mono_debugger_signal (TRUE);
1035 mono_debug_unlock ();
1040 il_offset_from_position (MonoFlowGraph *cfg, MonoPosition *pos)
1045 if (pos->abs_pos == 0)
1048 if (pos->pos.bid >= cfg->block_count)
1051 bblock = &cfg->bblocks [pos->pos.bid];
1052 if (pos->pos.tid >= bblock->forest->len)
1055 tree = (MBTree *) g_ptr_array_index (bblock->forest, pos->pos.tid);
1057 return tree->cli_addr;
1060 struct LookupMethodData
1062 MonoDebugMethodInfo *minfo;
1067 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
1069 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1070 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
1076 data->minfo = mono_debug_find_method (info->symfile, data->method);
1078 data->minfo = g_hash_table_lookup (info->methods, data->method);
1081 static MonoDebugMethodInfo *
1082 lookup_method (MonoMethod *method)
1084 struct LookupMethodData data = { NULL, method };
1086 if (!mono_debug_handle)
1089 g_hash_table_foreach (mono_debug_handle->images, lookup_method_func, &data);
1094 mono_debug_add_method (MonoFlowGraph *cfg)
1096 MonoMethod *method = cfg->method;
1097 MonoClass *klass = method->klass;
1098 AssemblyDebugInfo* info;
1099 MonoDebugMethodJitInfo *jit;
1100 MonoDebugMethodInfo *minfo;
1103 if (!mono_debug_handle)
1106 mono_class_init (klass);
1108 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1109 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1110 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1111 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1114 if (method->wrapper_type != MONO_WRAPPER_NONE)
1117 info = mono_debug_get_image (mono_debug_handle, klass->image);
1120 minfo = lookup_method (method);
1121 if (!minfo || minfo->jit)
1126 mono_debug_handle->dirty = TRUE;
1128 minfo->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
1129 jit->code_start = cfg->start;
1130 jit->code_size = cfg->epilogue_end;
1131 jit->prologue_end = cfg->prologue_end;
1132 jit->epilogue_begin = cfg->epilog;
1133 jit->num_params = method->signature->param_count;
1134 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
1136 if (method->signature->hasthis) {
1137 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index;
1139 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
1140 jit->this_var->offset = ptr->offset;
1141 jit->this_var->size = ptr->size;
1144 for (i = 0; i < jit->num_params; i++) {
1145 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index +
1146 method->signature->hasthis;
1148 jit->params [i].offset = ptr [i].offset;
1149 jit->params [i].size = ptr [i].size;
1152 debug_generate_method_lines (info, minfo, cfg);
1153 if ((info->format == MONO_DEBUG_FORMAT_MONO) || (info->format == MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1154 debug_update_il_offsets (info, minfo, cfg);
1156 if (!method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
1157 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
1158 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->locals_start_index;
1159 MonoDebugVarInfo *locals;
1161 locals = g_new0 (MonoDebugVarInfo, header->num_locals);
1162 for (i = 0; i < header->num_locals; i++) {
1163 gint32 begin_offset, end_offset;
1164 gint32 begin_scope, end_scope;
1166 if (ptr [i].reg >= 0) {
1167 locals [i].index = ptr [i].reg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
1168 locals [i].offset = 0;
1170 locals [i].offset = ptr [i].offset;
1172 locals [i].size = ptr [i].size;
1174 begin_offset = il_offset_from_position (cfg, &ptr [i].range.first_use);
1175 end_offset = il_offset_from_position (cfg, &ptr [i].range.last_use);
1176 if (end_offset >= 0)
1179 if (begin_offset >= 0)
1180 begin_scope = address_from_il_offset (minfo, begin_offset);
1183 if (end_offset >= 0)
1184 end_scope = address_from_il_offset (minfo, end_offset);
1188 if (begin_scope > 0)
1189 locals [i].begin_scope = begin_scope;
1191 locals [i].begin_scope = jit->prologue_end;
1193 locals [i].end_scope = end_scope;
1195 locals [i].end_scope = jit->epilogue_begin;
1198 jit->num_locals = header->num_locals;
1199 jit->locals = locals;
1202 if (info->symfile) {
1203 mono_debug_symfile_add_method (info->symfile, method);
1204 if (running_in_the_mono_debugger)
1205 mono_debugger_signal (TRUE);
1208 mono_debug_unlock ();
1212 mono_debug_source_location_from_address (MonoMethod *method, guint32 address, guint32 *line_number)
1214 MonoDebugMethodInfo *minfo = lookup_method (method);
1219 if (minfo->symfile) {
1220 gint32 offset = il_offset_from_address (minfo, address);
1225 return mono_debug_find_source_location (minfo->symfile, method, offset, line_number);
1232 mono_debug_il_offset_from_address (MonoMethod *method, gint32 address)
1234 MonoDebugMethodInfo *minfo;
1239 minfo = lookup_method (method);
1240 if (!minfo || !minfo->il_offsets)
1243 return il_offset_from_address (minfo, address);
1247 mono_debug_address_from_il_offset (MonoMethod *method, gint32 il_offset)
1249 MonoDebugMethodInfo *minfo;
1254 minfo = lookup_method (method);
1255 if (!minfo || !minfo->il_offsets)
1258 return address_from_il_offset (minfo, il_offset);
1262 release_symbol_file_table ()
1264 MonoDebuggerSymbolFileTable *temp;
1266 if (!debugger_symbol_file_table)
1270 * Caution: The debugger may access the memory pointed to by this variable
1271 * at any time. It is very important to set the pointer to NULL
1272 * before freeing the area.
1275 temp = debugger_symbol_file_table;
1276 debugger_symbol_file_table = NULL;
1277 g_free (debugger_symbol_file_table);
1281 update_symbol_file_table_count_func (gpointer key, gpointer value, gpointer user_data)
1283 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1287 if ((info->format != MONO_DEBUG_FORMAT_MONO) && (info->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1290 ++ (* (int *) user_data);
1293 struct SymfileTableData
1295 MonoDebuggerSymbolFileTable *symfile_table;
1300 update_symbol_file_table_func (gpointer key, gpointer value, gpointer user_data)
1302 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1303 struct SymfileTableData *data = (struct SymfileTableData *) user_data;
1307 if ((info->format != MONO_DEBUG_FORMAT_MONO) && (info->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1310 data->symfile_table->symfiles [data->index++] = info->symfile;
1314 debugger_update_symbol_file_table_internal (void)
1317 MonoDebuggerSymbolFileTable *symfile_table;
1318 struct SymfileTableData data;
1321 if (!mono_debug_handle)
1326 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_count_func, &count);
1328 release_symbol_file_table ();
1330 size = sizeof (MonoDebuggerSymbolFileTable) + count * sizeof (MonoSymbolFile *);
1331 symfile_table = g_malloc0 (size);
1332 symfile_table->magic = MONO_SYMBOL_FILE_DYNAMIC_MAGIC;
1333 symfile_table->version = MONO_SYMBOL_FILE_DYNAMIC_VERSION;
1334 symfile_table->total_size = size;
1335 symfile_table->count = count;
1336 symfile_table->generation = debugger_symbol_file_table_generation;
1337 symfile_table->global_symfile = mono_debugger_global_symbol_file;
1339 data.symfile_table = symfile_table;
1342 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_func, &data);
1344 debugger_symbol_file_table = symfile_table;
1346 mono_debug_unlock ();
1352 debugger_update_symbol_file_table (void)
1356 if (!mono_debug_handle)
1360 retval = debugger_update_symbol_file_table_internal ();
1361 mono_debug_unlock ();
1366 static pid_t debugger_background_thread;
1367 extern void mono_debugger_init_thread_debug (pid_t);
1370 * NOTE: We must not call any functions here which we ever may want to debug !
1373 debugger_thread_func (gpointer ptr)
1375 void (*notification_code) (void) = ptr;
1376 int last_generation = 0;
1378 mono_new_thread_init (NULL, &last_generation);
1379 debugger_background_thread = getpid ();
1382 * The parent thread waits on this condition because it needs our pid.
1384 ReleaseSemaphore (debugger_start_cond, 1, NULL);
1387 * This mutex is locked by the parent thread until the debugger actually
1388 * attached to us, so we don't need a SIGSTOP here anymore.
1393 /* Wait for an event. */
1394 mono_debug_unlock ();
1395 g_assert (WaitForSingleObject (debugger_thread_cond, INFINITE) == WAIT_OBJECT_0);
1398 /* Reload the symbol file table if necessary. */
1399 if (debugger_symbol_file_table_generation > last_generation) {
1400 debugger_update_symbol_file_table_internal ();
1401 last_generation = debugger_symbol_file_table_generation;
1405 * Send notification - we'll stop on a breakpoint instruction at a special
1406 * address. The debugger will reload the symbol tables while we're stopped -
1407 * and owning the `debugger_thread_mutex' so that no other thread can touch
1408 * them in the meantime.
1410 notification_code ();
1412 /* Clear modified and signalled flag. */
1413 debugger_symbol_file_table_modified = FALSE;
1414 debugger_signalled = FALSE;
1416 if (must_send_finished) {
1417 EnterCriticalSection (&debugger_finished_mutex);
1418 ReleaseSemaphore (debugger_finished_cond, 1, NULL);
1419 must_send_finished = FALSE;
1420 LeaveCriticalSection (&debugger_finished_mutex);
1428 initialize_debugger_support ()
1433 if (running_in_the_mono_debugger)
1435 debugger_init_threads ();
1436 running_in_the_mono_debugger = TRUE;
1438 ptr = buf = g_malloc0 (16);
1439 x86_breakpoint (buf);
1440 debugger_notification_address = buf;
1444 * We keep this mutex until mono_debugger_jit_exec().
1449 * This mutex is only unlocked in mono_debugger_wait().
1451 EnterCriticalSection (&debugger_finished_mutex);
1453 thread = CreateThread (NULL, 0, debugger_thread_func, ptr, FALSE, NULL);
1457 * Wait until the background thread set its pid.
1459 g_assert (WaitForSingleObject (debugger_start_cond, INFINITE) == WAIT_OBJECT_0);
1462 * Wait until the debugger thread has actually been started and we
1463 * have its pid, then actually start the background thread.
1465 #ifndef PLATFORM_WIN32
1466 mono_debugger_init_thread_debug (debugger_background_thread);
1470 static GPtrArray *breakpoints = NULL;
1473 mono_insert_breakpoint_full (MonoMethodDesc *desc, gboolean use_trampoline)
1475 static int last_breakpoint_id = 0;
1476 MonoDebuggerBreakpointInfo *info;
1478 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
1480 info->use_trampoline = use_trampoline;
1481 info->index = ++last_breakpoint_id;
1484 breakpoints = g_ptr_array_new ();
1486 g_ptr_array_add (breakpoints, info);
1492 debugger_insert_breakpoint (guint64 method_argument, const gchar *string_argument)
1494 MonoMethodDesc *desc;
1496 desc = mono_method_desc_new (string_argument, FALSE);
1500 return mono_insert_breakpoint_full (desc, TRUE);
1504 debugger_remove_breakpoint (guint64 breakpoint)
1511 for (i = 0; i < breakpoints->len; i++) {
1512 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1514 if (info->index != breakpoint)
1517 mono_method_desc_free (info->desc);
1518 g_ptr_array_remove (breakpoints, info);
1527 debugger_compile_method (MonoMethod *method)
1532 retval = mono_compile_method (method);
1533 mono_debugger_signal (FALSE);
1534 mono_debug_unlock ();
1539 mono_remove_breakpoint (int breakpoint_id)
1541 return debugger_remove_breakpoint (breakpoint_id);
1545 mono_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
1547 MonoMethodDesc *desc;
1549 desc = mono_method_desc_new (method_name, include_namespace);
1553 return mono_insert_breakpoint_full (desc, running_in_the_mono_debugger);
1557 mono_method_has_breakpoint (MonoMethod* method, gboolean use_trampoline)
1561 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
1564 for (i = 0; i < breakpoints->len; i++) {
1565 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1567 if (info->use_trampoline != use_trampoline)
1570 if (!mono_method_desc_full_match (info->desc, method))
1580 mono_debugger_trampoline_breakpoint_callback (void)
1583 must_send_finished = TRUE;
1584 mono_debugger_signal (FALSE);
1585 mono_debug_unlock ();
1587 mono_debugger_wait ();
1591 * This is a custom version of mono_jit_exec() which is used when we're being run inside
1592 * the Mono Debugger.
1595 mono_debugger_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
1597 MonoImage *image = assembly->image;
1602 method = mono_get_method (image, mono_image_get_entry_point (image), NULL);
1604 addr = mono_compile_method (method);
1607 * The mutex has been locked in initialize_debugger_support(); we keep it locked
1608 * until we compiled the main method and signalled the debugger.
1610 must_send_finished = TRUE;
1611 mono_debugger_signal (TRUE);
1612 mono_debug_unlock ();
1615 * Wait until the debugger has loaded the initial symbol tables.
1617 mono_debugger_wait ();
1619 retval = mono_runtime_run_main (method, argc, argv, NULL);
1622 * Kill the background thread.
1624 #ifndef PLATFORM_WIN32
1625 /* Someone should look at this too */
1626 kill (debugger_background_thread, SIGKILL);