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"
19 /* This is incremented each time the symbol table is modified.
20 * The debugger looks at this variable and if it has a higher value than its current
21 * copy of the symbol table, it must call debugger_update_symbol_file_table().
23 static guint32 debugger_symbol_file_table_generation = 0;
24 static guint32 debugger_symbol_file_table_modified = 0;
26 /* Caution: This variable may be accessed at any time from the debugger;
27 * it is very important not to modify the memory it is pointing to
28 * without previously setting this pointer back to NULL.
30 static MonoDebuggerSymbolFileTable *debugger_symbol_file_table = NULL;
32 /* Caution: This function MUST be called before touching the symbol table! */
33 static void release_symbol_file_table (void);
35 static void initialize_debugger_support (void);
37 static MonoDebugHandle *mono_debug_handle = NULL;
38 static gconstpointer debugger_notification_address = NULL;
40 static mono_once_t debugger_lock_once = MONO_ONCE_INIT;
41 static CRITICAL_SECTION debugger_lock_mutex;
43 static gpointer debugger_thread_cond;
44 static gpointer debugger_start_cond;
46 static gpointer debugger_finished_cond;
47 static CRITICAL_SECTION debugger_finished_mutex;
49 static gboolean debugger_signalled = FALSE;
50 static gboolean must_send_finished = FALSE;
52 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
54 static guint64 debugger_insert_breakpoint (guint64 method_argument, const gchar *string_argument);
55 static guint64 debugger_remove_breakpoint (guint64 breakpoint);
56 static int debugger_update_symbol_file_table (void);
57 static int debugger_update_symbol_file_table_internal (void);
58 static gpointer debugger_compile_method (MonoMethod *method);
60 static void mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data);
61 static void mono_debug_close_assembly (AssemblyDebugInfo* info);
62 static AssemblyDebugInfo *mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image);
65 * This is a global data symbol which is read by the debugger.
67 MonoDebuggerInfo MONO_DEBUGGER__debugger_info = {
68 MONO_SYMBOL_FILE_DYNAMIC_MAGIC,
69 MONO_SYMBOL_FILE_DYNAMIC_VERSION,
70 sizeof (MonoDebuggerInfo),
71 &mono_generic_trampoline_code,
72 &mono_breakpoint_trampoline_code,
73 &debugger_symbol_file_table_generation,
74 &debugger_symbol_file_table_modified,
75 &debugger_notification_address,
76 &debugger_symbol_file_table,
77 &debugger_compile_method,
78 &debugger_insert_breakpoint,
79 &debugger_remove_breakpoint,
84 debugger_init_threads (void)
86 debugger_thread_cond = CreateSemaphore (NULL, 0, 1, NULL);
87 debugger_start_cond = CreateSemaphore (NULL, 0, 1, NULL);
89 InitializeCriticalSection (&debugger_finished_mutex);
90 debugger_finished_cond = CreateSemaphore (NULL, 0, 1, NULL);
94 debugger_lock_init (void)
96 InitializeCriticalSection (&debugger_lock_mutex);
100 mono_debugger_lock (void)
102 mono_once (&debugger_lock_once, debugger_lock_init);
103 EnterCriticalSection (&debugger_lock_mutex);
107 mono_debugger_unlock (void)
109 mono_once (&debugger_lock_once, debugger_lock_init);
110 LeaveCriticalSection (&debugger_lock_mutex);
114 mono_debugger_signal (gboolean modified)
117 debugger_symbol_file_table_modified = TRUE;
118 mono_debugger_lock ();
119 if (!debugger_signalled) {
120 debugger_signalled = TRUE;
121 ReleaseSemaphore (debugger_thread_cond, 1, NULL);
123 mono_debugger_unlock ();
127 mono_debugger_wait (void)
129 LeaveCriticalSection (&debugger_finished_mutex);
130 g_assert (WaitForSingleObject (debugger_finished_cond, INFINITE) == WAIT_OBJECT_0);
131 EnterCriticalSection (&debugger_finished_mutex);
135 free_method_info (MonoDebugMethodInfo *minfo)
137 DebugMethodInfo *priv = minfo->user_data;
145 g_array_free (minfo->jit->line_numbers, TRUE);
146 g_free (minfo->jit->this_var);
147 g_free (minfo->jit->params);
148 g_free (minfo->jit->locals);
152 g_free (minfo->il_offsets);
157 debug_arg_warning (const char *message)
159 g_warning ("Error while processing --debug-args arguments: %s", message);
163 replace_suffix (const char *filename, const char *new_suffix)
165 const char *pos = strrchr (filename, '.');
168 return g_strdup_printf ("%s.%s", filename, new_suffix);
170 int len = pos - filename;
171 gchar *retval = g_malloc0 (len + strlen (new_suffix) + 2);
172 memcpy (retval, filename, len);
174 memcpy (retval + len + 1, new_suffix, strlen (new_suffix) + 1);
180 mono_debug_open (MonoAssembly *assembly, MonoDebugFormat format, const char **args)
182 MonoDebugHandle *debug;
185 g_assert (!mono_debug_handle);
187 debug = g_new0 (MonoDebugHandle, 1);
188 debug->name = g_strdup (assembly->image->name);
189 debug->format = format;
190 debug->producer_name = g_strdup_printf ("Mono JIT compiler version %s", VERSION);
191 debug->next_idx = 100;
194 debug->type_hash = g_hash_table_new (NULL, NULL);
195 debug->source_files = g_ptr_array_new ();
197 debug->images = g_hash_table_new_full (NULL, NULL, NULL,
198 (GDestroyNotify) mono_debug_close_assembly);
200 for (ptr = args; ptr && *ptr; ptr++) {
201 const char *arg = *ptr;
204 switch (debug->format) {
205 case MONO_DEBUG_FORMAT_STABS:
206 case MONO_DEBUG_FORMAT_DWARF2:
207 if (!strncmp (arg, "filename=", 9)) {
209 debug_arg_warning ("The `filename' argument can be given only once.");
210 debug->filename = g_strdup (arg + 9);
212 } else if (!strncmp (arg, "objfile=", 8)) {
214 debug_arg_warning ("The `objfile' argument can be given only once.");
215 debug->objfile = g_strdup (arg + 8);
219 case MONO_DEBUG_FORMAT_MONO:
220 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
221 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES |
222 MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
228 if ((debug->format != MONO_DEBUG_FORMAT_MONO) &&
229 (debug->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER)) {
230 if (!strcmp (arg, "dont_assemble")) {
231 debug->flags |= MONO_DEBUG_FLAGS_DONT_ASSEMBLE;
233 } else if (!strcmp (arg, "update_on_exit")) {
234 debug->flags |= MONO_DEBUG_FLAGS_UPDATE_ON_EXIT;
236 } else if (!strcmp (arg, "install_il_files")) {
237 debug->flags |= MONO_DEBUG_FLAGS_INSTALL_IL_FILES;
239 } else if (!strcmp (arg, "dont_update_il_files")) {
240 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES;
242 } else if (!strcmp (arg, "dont_create_il_files")) {
243 debug->flags |= MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
248 message = g_strdup_printf ("Unknown argument `%s'.", arg);
249 debug_arg_warning (message);
253 switch (debug->format) {
254 case MONO_DEBUG_FORMAT_STABS:
255 if (!debug->filename)
256 debug->filename = g_strdup_printf ("%s-stabs.s", g_basename (debug->name));
258 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
260 case MONO_DEBUG_FORMAT_DWARF2:
261 if (!debug->filename)
262 debug->filename = g_strdup_printf ("%s-dwarf.s", g_basename (debug->name));
264 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
266 case MONO_DEBUG_FORMAT_MONO:
267 mono_debugger_class_init_func = mono_debug_add_type;
269 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
270 mono_debugger_class_init_func = mono_debug_add_type;
271 initialize_debugger_support ();
274 g_assert_not_reached ();
277 mono_debugger_lock ();
278 release_symbol_file_table ();
280 mono_debug_handle = debug;
281 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
283 mono_debug_open_image (mono_debug_handle, assembly->image);
284 mono_debug_open_image (mono_debug_handle, mono_defaults.corlib);
286 mono_debug_add_type (mono_defaults.object_class);
287 mono_debug_add_type (mono_defaults.object_class);
288 mono_debug_add_type (mono_defaults.byte_class);
289 mono_debug_add_type (mono_defaults.void_class);
290 mono_debug_add_type (mono_defaults.boolean_class);
291 mono_debug_add_type (mono_defaults.sbyte_class);
292 mono_debug_add_type (mono_defaults.int16_class);
293 mono_debug_add_type (mono_defaults.uint16_class);
294 mono_debug_add_type (mono_defaults.int32_class);
295 mono_debug_add_type (mono_defaults.uint32_class);
296 mono_debug_add_type (mono_defaults.int_class);
297 mono_debug_add_type (mono_defaults.uint_class);
298 mono_debug_add_type (mono_defaults.int64_class);
299 mono_debug_add_type (mono_defaults.uint64_class);
300 mono_debug_add_type (mono_defaults.single_class);
301 mono_debug_add_type (mono_defaults.double_class);
302 mono_debug_add_type (mono_defaults.char_class);
303 mono_debug_add_type (mono_defaults.string_class);
304 mono_debug_add_type (mono_defaults.enum_class);
305 mono_debug_add_type (mono_defaults.array_class);
306 mono_debug_add_type (mono_defaults.multicastdelegate_class);
307 mono_debug_add_type (mono_defaults.asyncresult_class);
308 mono_debug_add_type (mono_defaults.waithandle_class);
309 mono_debug_add_type (mono_defaults.typehandle_class);
310 mono_debug_add_type (mono_defaults.fieldhandle_class);
311 mono_debug_add_type (mono_defaults.methodhandle_class);
312 mono_debug_add_type (mono_defaults.monotype_class);
313 mono_debug_add_type (mono_defaults.exception_class);
314 mono_debug_add_type (mono_defaults.threadabortexception_class);
315 mono_debug_add_type (mono_defaults.thread_class);
316 mono_debug_add_type (mono_defaults.transparent_proxy_class);
317 mono_debug_add_type (mono_defaults.real_proxy_class);
318 mono_debug_add_type (mono_defaults.mono_method_message_class);
319 mono_debug_add_type (mono_defaults.appdomain_class);
320 mono_debug_add_type (mono_defaults.field_info_class);
321 mono_debug_add_type (mono_defaults.stringbuilder_class);
322 mono_debug_add_type (mono_defaults.math_class);
323 mono_debug_add_type (mono_defaults.stack_frame_class);
324 mono_debug_add_type (mono_defaults.stack_trace_class);
325 mono_debug_add_type (mono_defaults.marshal_class);
326 mono_debug_add_type (mono_defaults.iserializeable_class);
327 mono_debug_add_type (mono_defaults.serializationinfo_class);
328 mono_debug_add_type (mono_defaults.streamingcontext_class);
330 debugger_update_symbol_file_table ();
332 mono_debugger_unlock ();
338 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
340 if (!mono_debug_handle)
343 mono_debugger_lock ();
344 mono_debug_open_image (mono_debug_handle, assembly->image);
345 mono_debugger_unlock ();
349 generate_il_offsets (AssemblyDebugInfo *info, MonoMethod *method)
351 GPtrArray *il_offsets = g_ptr_array_new ();
352 MonoClass *klass = method->klass;
353 MonoDebugMethodInfo *minfo;
354 DebugMethodInfo *priv;
357 g_assert (klass->image == info->image);
359 /* FIXME: doesn't work yet. */
360 if (!strcmp (klass->name_space, "System.Runtime.Remoting.Proxies"))
363 mono_class_init (klass);
365 minfo = g_new0 (MonoDebugMethodInfo, 1);
366 minfo->method = method;
367 minfo->user_data = priv = g_new0 (DebugMethodInfo, 1);
369 priv->name = g_strdup_printf ("%s%s%s.%s", klass->name_space, klass->name_space [0]? ".": "",
370 klass->name, method->name);
371 priv->source_file = info->source_file;
375 * Find the method index in the image.
377 for (i = 0; klass->methods && i < klass->method.count; ++i) {
378 if (klass->methods [i] == minfo->method) {
379 priv->method_number = klass->method.first + i + 1;
380 priv->first_line = info->mlines [priv->method_number];
385 g_assert (priv->method_number);
387 /* info->moffsets contains -1 "outside" of functions. */
388 for (i = priv->first_line; (i > 0) && (info->moffsets [i] == 0); i--)
390 priv->start_line = i + 1;
392 for (i = priv->start_line; info->moffsets [i] != -1; i++) {
393 MonoSymbolFileLineNumberEntry *lne = g_new0 (MonoSymbolFileLineNumberEntry, 1);
395 if (!info->moffsets [i] && (i > priv->start_line))
398 lne->offset = info->moffsets [i];
401 g_ptr_array_add (il_offsets, lne);
406 minfo->start_line = priv->first_line;
407 minfo->end_line = priv->last_line;
409 minfo->num_il_offsets = il_offsets->len;
410 minfo->il_offsets = g_new0 (MonoSymbolFileLineNumberEntry, il_offsets->len);
411 for (i = 0; i < il_offsets->len; i++) {
412 MonoSymbolFileLineNumberEntry *il = g_ptr_array_index (il_offsets, i);
414 minfo->il_offsets [i] = *il;
417 g_ptr_array_free (il_offsets, TRUE);
419 g_hash_table_insert (info->methods, method, minfo);
423 debug_load_method_lines (AssemblyDebugInfo* info)
425 MonoTableInfo *table = &info->image->tables [MONO_TABLE_METHOD];
431 if (info->always_create_il || !(info->handle->flags & MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES)) {
432 char *command = g_strdup_printf ("monodis --output=%s %s",
433 info->ilfile, info->image->name);
434 struct stat stata, statb;
435 int need_update = FALSE;
437 if (stat (info->image->name, &stata)) {
438 g_warning ("cannot access assembly file (%s): %s",
439 info->image->name, g_strerror (errno));
444 /* If the stat() failed or the file is older. */
445 if (stat (info->ilfile, &statb)) {
447 } else if (statb.st_mtime < stata.st_mtime)
451 #ifndef PLATFORM_WIN32
452 struct sigaction act, oldact;
457 #ifndef PLATFORM_WIN32
458 act.sa_handler = SIG_IGN;
459 act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
460 sigemptyset (&act.sa_mask);
461 sigaddset (&act.sa_mask, SIGCHLD);
462 sigprocmask (SIG_BLOCK, &act.sa_mask, &old_set);
463 sigaction (SIGCHLD, &act, &oldact);
466 g_print ("Recreating %s from %s.\n", info->ilfile, info->image->name);
468 ret = system (command);
470 #ifndef PLATFORM_WIN32
471 sigaction (SIGCHLD, &oldact, NULL);
472 sigprocmask (SIG_SETMASK, &old_set, NULL);
476 g_warning ("cannot create IL assembly file (%s): %s",
477 command, g_strerror (errno));
484 /* use an env var with directories for searching. */
485 if (!(f = fopen (info->ilfile, "r"))) {
486 g_warning ("cannot open IL assembly file %s", info->ilfile);
490 info->total_lines = 100;
491 info->moffsets = g_malloc (info->total_lines * sizeof (int));
494 while (fgets (buf, sizeof (buf), f)) {
497 info->moffsets [i++] = offset;
498 if (i + 2 >= info->total_lines) {
499 info->total_lines += 100;
500 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
501 g_assert (info->moffsets);
504 if (!sscanf (buf, " // method line %d", &mnum))
509 if (mnum >= info->nmethods)
512 while (fgets (buf, sizeof (buf), f)) {
516 if (i + 2 >= info->total_lines) {
517 info->total_lines += 100;
518 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
519 g_assert (info->moffsets);
522 if (strstr (buf, "}")) {
527 if (sscanf (buf, " IL_%x:", &newoffset)) {
533 info->moffsets [i] = offset;
535 /* g_print ("method %d found at %d\n", mnum, pos); */
536 info->mlines [mnum] = pos;
540 for (idx = 1; idx <= table->rows; idx++) {
541 guint32 token = mono_metadata_make_token (MONO_TABLE_METHOD, idx);
542 MonoMethod *method = mono_get_method (info->image, token, NULL);
544 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
545 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
546 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
549 if (method->wrapper_type != MONO_WRAPPER_NONE)
552 generate_il_offsets (info, method);
557 record_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, guint32 line)
559 MonoDebugLineNumberEntry *lne = g_new0 (MonoDebugLineNumberEntry, 1);
561 lne->address = address;
562 lne->offset = offset;
565 g_array_append_val (minfo->jit->line_numbers, *lne);
569 debug_generate_method_lines (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
571 guint32 st_address, st_line;
572 DebugMethodInfo *priv = minfo->user_data;
575 if (!priv || !info->moffsets)
578 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
580 st_line = priv->first_line;
581 st_address = minfo->jit->prologue_end;
583 /* This is the first actual code line of the method. */
584 record_line_number (minfo, st_address, 0, st_line);
586 /* start lines of basic blocks */
587 for (i = 0; i < cfg->block_count; ++i) {
590 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
591 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
592 gint32 line_inc = 0, addr_inc;
595 st_line = priv->first_line;
596 st_address = t->addr;
599 addr_inc = t->addr - st_address;
600 st_address += addr_inc;
602 if (t->cli_addr != -1) {
603 int *lines = info->moffsets + st_line;
606 while ((*k != -1) && (*k < t->cli_addr))
609 line_inc = k - lines;
614 if (t->cli_addr != -1)
615 record_line_number (minfo, st_address, t->cli_addr, st_line);
621 generate_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, int debug)
626 g_message (G_STRLOC ": searching IL offset %x", offset);
628 for (i = minfo->num_il_offsets - 1; i >= 0; i--) {
629 MonoDebugLineNumberEntry *lne;
631 if (minfo->il_offsets [i].offset > offset)
635 g_message (G_STRLOC ": found entry %d: offset = %x, row = %d",
636 i, minfo->il_offsets [i].offset, minfo->il_offsets [i].row);
638 if (minfo->jit->line_numbers->len) {
639 MonoDebugLineNumberEntry last = g_array_index (
640 minfo->jit->line_numbers, MonoDebugLineNumberEntry,
641 minfo->jit->line_numbers->len - 1);
643 /* Avoid writing more than one entry for the same line. */
644 if (minfo->il_offsets [i].row == last.line) {
646 g_message (G_STRLOC ": skipping line: line = %d, last line = %d, "
647 "last address = %x, address = %x, "
648 "last offset = %x, offset = %x",
649 last.line, minfo->il_offsets [i].row,
650 last.address, address, last.offset, offset);
657 g_message (G_STRLOC ": writing entry: line = %d, offfset = %x, address = %x",
658 minfo->il_offsets [i].row, offset, address);
660 lne = g_new0 (MonoDebugLineNumberEntry, 1);
661 lne->address = address;
662 lne->offset = offset;
663 lne->line = minfo->il_offsets [i].row;
665 g_array_append_val (minfo->jit->line_numbers, *lne);
671 debug_update_il_offsets (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
673 MonoMethodHeader *header;
674 guint32 address, offset;
678 g_assert (info->symfile);
679 if (info->symfile->is_dynamic)
682 g_assert (!minfo->jit->line_numbers);
683 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
685 address = minfo->jit->prologue_end;
688 g_assert (((MonoMethodNormal*)minfo->method)->header);
689 header = ((MonoMethodNormal*)minfo->method)->header;
692 if (!strcmp (minfo->method->name, "Test") || !strcmp (minfo->method->name, "Main")) {
693 MonoMethodHeader *header = ((MonoMethodNormal*)minfo->method)->header;
696 mono_disassemble_code (minfo->jit->code_start, minfo->jit->code_size,
697 minfo->method->name);
699 printf ("\nDisassembly:\n%s\n", mono_disasm_code (
700 NULL, minfo->method, header->code, header->code + header->code_size));
701 g_message (G_STRLOC ": %x - %x", minfo->jit->prologue_end, minfo->jit->epilogue_begin);
705 generate_line_number (minfo, address, offset, debug);
707 /* start lines of basic blocks */
708 for (i = 0; i < cfg->block_count; ++i) {
711 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
712 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
714 if ((t->cli_addr == -1) || (t->cli_addr == offset) || (t->addr == address))
717 offset = t->cli_addr;
720 generate_line_number (minfo, address, offset, debug);
724 generate_line_number (minfo, minfo->jit->epilogue_begin, header->code_size, debug);
727 for (i = 0; i < minfo->jit->line_numbers->len; i++) {
728 MonoDebugLineNumberEntry lne = g_array_index (
729 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
731 g_message (G_STRLOC ": %x,%x,%d", lne.address, lne.offset, lne.line);
736 static AssemblyDebugInfo *
737 mono_debug_get_image (MonoDebugHandle* debug, MonoImage *image)
739 return g_hash_table_lookup (debug->images, image);
742 static AssemblyDebugInfo *
743 mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image)
745 AssemblyDebugInfo *info;
748 info = mono_debug_get_image (debug, image);
754 info = g_new0 (AssemblyDebugInfo, 1);
756 info->image->ref_count++;
757 info->name = g_strdup (image->assembly_name);
758 info->format = debug->format;
759 info->handle = debug;
760 info->methods = g_hash_table_new_full (g_direct_hash, g_direct_equal,
761 NULL, (GDestroyNotify) free_method_info);
763 info->source_file = debug->source_files->len;
764 g_ptr_array_add (debug->source_files, g_strdup_printf ("%s.il", image->assembly_name));
766 g_hash_table_insert (debug->images, image, info);
768 info->nmethods = image->tables [MONO_TABLE_METHOD].rows + 1;
769 info->mlines = g_new0 (int, info->nmethods);
771 for (ptr = image->references; ptr && *ptr; ptr++)
772 mono_debug_add_assembly (*ptr, NULL);
774 switch (info->format) {
775 case MONO_DEBUG_FORMAT_STABS:
776 case MONO_DEBUG_FORMAT_DWARF2:
777 if (debug->flags & MONO_DEBUG_FLAGS_INSTALL_IL_FILES) {
778 gchar *dirname = g_path_get_dirname (image->name);
779 info->ilfile = g_strdup_printf ("%s/%s.il", dirname, info->name);
782 info->ilfile = g_strdup_printf ("%s.il", info->name);
784 case MONO_DEBUG_FORMAT_MONO:
785 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
786 info->filename = replace_suffix (image->name, "dbg");
787 if (g_file_test (info->filename, G_FILE_TEST_EXISTS))
788 info->symfile = mono_debug_open_mono_symbol_file (info->image, info->filename, TRUE);
789 else if (info->format == MONO_DEBUG_FORMAT_MONO_DEBUGGER)
790 info->symfile = mono_debug_create_mono_symbol_file (info->image);
791 debugger_symbol_file_table_generation++;
798 if ((debug->format != MONO_DEBUG_FORMAT_MONO) && (debug->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
799 debug_load_method_lines (info);
805 mono_debug_write_symbols (MonoDebugHandle *debug)
807 if (!debug || !debug->dirty)
810 release_symbol_file_table ();
812 switch (debug->format) {
813 case MONO_DEBUG_FORMAT_STABS:
814 mono_debug_write_stabs (debug);
816 case MONO_DEBUG_FORMAT_DWARF2:
817 mono_debug_write_dwarf2 (debug);
819 case MONO_DEBUG_FORMAT_MONO:
820 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
823 g_assert_not_reached ();
826 debug->dirty = FALSE;
830 mono_debug_make_symbols (void)
832 if (!mono_debug_handle || !mono_debug_handle->dirty)
835 switch (mono_debug_handle->format) {
836 case MONO_DEBUG_FORMAT_STABS:
837 mono_debug_write_stabs (mono_debug_handle);
839 case MONO_DEBUG_FORMAT_DWARF2:
840 mono_debug_write_dwarf2 (mono_debug_handle);
842 case MONO_DEBUG_FORMAT_MONO:
843 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
844 debugger_update_symbol_file_table ();
847 g_assert_not_reached ();
850 mono_debug_handle->dirty = FALSE;
854 mono_debug_close_assembly (AssemblyDebugInfo* info)
856 switch (info->format) {
857 case MONO_DEBUG_FORMAT_MONO:
858 case MONO_DEBUG_FORMAT_MONO_DEBUGGER:
859 if (info->symfile != NULL)
860 mono_debug_close_mono_symbol_file (info->symfile);
865 g_hash_table_destroy (info->methods);
866 g_free (info->mlines);
867 g_free (info->moffsets);
869 g_free (info->ilfile);
870 g_free (info->filename);
871 g_free (info->objfile);
876 mono_debug_cleanup (void)
878 release_symbol_file_table ();
880 if (!mono_debug_handle)
883 if (mono_debug_handle->flags & MONO_DEBUG_FLAGS_UPDATE_ON_EXIT)
884 mono_debug_write_symbols (mono_debug_handle);
886 g_hash_table_destroy (mono_debug_handle->images);
887 g_ptr_array_free (mono_debug_handle->source_files, TRUE);
888 g_hash_table_destroy (mono_debug_handle->type_hash);
889 g_free (mono_debug_handle->producer_name);
890 g_free (mono_debug_handle->name);
891 g_free (mono_debug_handle);
893 mono_debug_handle = NULL;
897 mono_debug_get_type (MonoDebugHandle *debug, MonoClass *klass)
901 mono_class_init (klass);
903 index = GPOINTER_TO_INT (g_hash_table_lookup (debug->type_hash, klass));
909 index = ++debug->next_klass_idx;
910 g_hash_table_insert (debug->type_hash, klass, GINT_TO_POINTER (index));
915 switch (klass->byval_arg.type) {
916 case MONO_TYPE_CLASS:
918 mono_debug_get_type (debug, klass->parent);
920 for (i = 0; i < klass->method.count; i++) {
921 MonoMethod *method = klass->methods [i];
922 MonoType *ret_type = NULL;
925 if (method->signature->ret->type != MONO_TYPE_VOID)
926 ret_type = method->signature->ret;
929 MonoClass *ret_klass = mono_class_from_mono_type (ret_type);
930 mono_debug_get_type (debug, ret_klass);
933 for (j = 0; j < method->signature->param_count; j++) {
934 MonoType *sub_type = method->signature->params [j];
935 MonoClass *sub_klass = mono_class_from_mono_type (sub_type);
936 mono_debug_get_type (debug, sub_klass);
940 case MONO_TYPE_VALUETYPE:
941 for (i = 0; i < klass->field.count; i++) {
942 MonoClass *subclass = mono_class_from_mono_type (klass->fields [i].type);
943 mono_debug_get_type (debug, subclass);
946 case MONO_TYPE_ARRAY:
947 case MONO_TYPE_SZARRAY:
948 mono_debug_get_type (debug, klass->element_class);
958 il_offset_from_address (MonoDebugMethodInfo *minfo, guint32 address)
962 if (!minfo->jit || !minfo->jit->line_numbers)
965 for (i = minfo->jit->line_numbers->len - 1; i >= 0; i--) {
966 MonoDebugLineNumberEntry lne = g_array_index (
967 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
969 if (lne.address <= address)
977 address_from_il_offset (MonoDebugMethodInfo *minfo, guint32 il_offset)
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.offset <= il_offset)
996 mono_debug_add_type (MonoClass *klass)
998 AssemblyDebugInfo* info;
1000 if (!mono_debug_handle)
1003 info = mono_debug_get_image (mono_debug_handle, klass->image);
1006 if ((mono_debug_handle->format != MONO_DEBUG_FORMAT_MONO) &&
1007 (mono_debug_handle->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1010 if (info->symfile) {
1011 mono_debugger_lock ();
1012 mono_debug_symfile_add_type (info->symfile, klass);
1013 mono_debugger_signal (TRUE);
1014 mono_debugger_unlock ();
1019 il_offset_from_position (MonoFlowGraph *cfg, MonoPosition *pos)
1024 if (pos->abs_pos == 0)
1027 if (pos->pos.bid >= cfg->block_count)
1030 bblock = &cfg->bblocks [pos->pos.bid];
1031 if (pos->pos.tid >= bblock->forest->len)
1034 tree = (MBTree *) g_ptr_array_index (bblock->forest, pos->pos.tid);
1036 return tree->cli_addr;
1039 struct LookupMethodData
1041 MonoDebugMethodInfo *minfo;
1046 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
1048 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1049 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
1055 data->minfo = mono_debug_find_method (info->symfile, data->method);
1057 data->minfo = g_hash_table_lookup (info->methods, data->method);
1060 static MonoDebugMethodInfo *
1061 lookup_method (MonoMethod *method)
1063 struct LookupMethodData data = { NULL, method };
1065 if (!mono_debug_handle)
1068 g_hash_table_foreach (mono_debug_handle->images, lookup_method_func, &data);
1073 mono_debug_add_method (MonoFlowGraph *cfg)
1075 MonoMethod *method = cfg->method;
1076 MonoClass *klass = method->klass;
1077 AssemblyDebugInfo* info;
1078 MonoDebugMethodJitInfo *jit;
1079 MonoDebugMethodInfo *minfo;
1082 if (!mono_debug_handle)
1085 mono_class_init (klass);
1087 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1088 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1089 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1090 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1093 if (method->wrapper_type != MONO_WRAPPER_NONE)
1096 info = mono_debug_get_image (mono_debug_handle, klass->image);
1099 minfo = lookup_method (method);
1100 if (!minfo || minfo->jit)
1103 mono_debugger_lock ();
1105 mono_debug_handle->dirty = TRUE;
1107 minfo->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
1108 jit->code_start = cfg->start;
1109 jit->code_size = cfg->epilogue_end;
1110 jit->prologue_end = cfg->prologue_end;
1111 jit->epilogue_begin = cfg->epilog;
1112 jit->num_params = method->signature->param_count;
1113 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
1115 if (method->signature->hasthis) {
1116 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index;
1118 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
1119 jit->this_var->offset = ptr->offset;
1120 jit->this_var->size = ptr->size;
1123 for (i = 0; i < jit->num_params; i++) {
1124 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index +
1125 method->signature->hasthis;
1127 jit->params [i].offset = ptr [i].offset;
1128 jit->params [i].size = ptr [i].size;
1131 debug_generate_method_lines (info, minfo, cfg);
1132 if ((info->format == MONO_DEBUG_FORMAT_MONO) || (info->format == MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1133 debug_update_il_offsets (info, minfo, cfg);
1135 if (!method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
1136 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
1137 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->locals_start_index;
1138 MonoDebugVarInfo *locals;
1140 locals = g_new0 (MonoDebugVarInfo, header->num_locals);
1141 for (i = 0; i < header->num_locals; i++) {
1142 gint32 begin_offset, end_offset;
1143 gint32 begin_scope, end_scope;
1145 if (ptr [i].reg >= 0) {
1146 locals [i].index = ptr [i].reg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
1147 locals [i].offset = 0;
1149 locals [i].offset = ptr [i].offset;
1151 locals [i].size = ptr [i].size;
1153 begin_offset = il_offset_from_position (cfg, &ptr [i].range.first_use);
1154 end_offset = il_offset_from_position (cfg, &ptr [i].range.last_use);
1155 if (end_offset >= 0)
1158 if (begin_offset >= 0)
1159 begin_scope = address_from_il_offset (minfo, begin_offset);
1162 if (end_offset >= 0)
1163 end_scope = address_from_il_offset (minfo, end_offset);
1167 if (begin_scope > 0)
1168 locals [i].begin_scope = begin_scope;
1170 locals [i].begin_scope = jit->prologue_end;
1172 locals [i].end_scope = end_scope;
1174 locals [i].end_scope = jit->epilogue_begin;
1177 jit->num_locals = header->num_locals;
1178 jit->locals = locals;
1181 if (info->symfile) {
1182 mono_debug_symfile_add_method (info->symfile, method);
1183 mono_debugger_signal (TRUE);
1186 mono_debugger_unlock ();
1190 mono_debug_source_location_from_address (MonoMethod *method, guint32 address, guint32 *line_number)
1192 MonoDebugMethodInfo *minfo = lookup_method (method);
1197 if (minfo->symfile) {
1198 gint32 offset = il_offset_from_address (minfo, address);
1203 return mono_debug_find_source_location (minfo->symfile, method, offset, line_number);
1210 mono_debug_il_offset_from_address (MonoMethod *method, gint32 address)
1212 MonoDebugMethodInfo *minfo;
1217 minfo = lookup_method (method);
1218 if (!minfo || !minfo->il_offsets)
1221 return il_offset_from_address (minfo, address);
1225 mono_debug_address_from_il_offset (MonoMethod *method, gint32 il_offset)
1227 MonoDebugMethodInfo *minfo;
1232 minfo = lookup_method (method);
1233 if (!minfo || !minfo->il_offsets)
1236 return address_from_il_offset (minfo, il_offset);
1240 release_symbol_file_table ()
1242 MonoDebuggerSymbolFileTable *temp;
1244 if (!debugger_symbol_file_table)
1248 * Caution: The debugger may access the memory pointed to by this variable
1249 * at any time. It is very important to set the pointer to NULL
1250 * before freeing the area.
1253 temp = debugger_symbol_file_table;
1254 debugger_symbol_file_table = NULL;
1255 g_free (debugger_symbol_file_table);
1259 update_symbol_file_table_count_func (gpointer key, gpointer value, gpointer user_data)
1261 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1265 if ((info->format != MONO_DEBUG_FORMAT_MONO) && (info->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1268 ++ (* (int *) user_data);
1271 struct SymfileTableData
1273 MonoDebuggerSymbolFileTable *symfile_table;
1278 update_symbol_file_table_func (gpointer key, gpointer value, gpointer user_data)
1280 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1281 struct SymfileTableData *data = (struct SymfileTableData *) user_data;
1285 if ((info->format != MONO_DEBUG_FORMAT_MONO) && (info->format != MONO_DEBUG_FORMAT_MONO_DEBUGGER))
1288 data->symfile_table->symfiles [data->index++] = info->symfile;
1292 debugger_update_symbol_file_table_internal (void)
1295 MonoDebuggerSymbolFileTable *symfile_table;
1296 struct SymfileTableData data;
1299 if (!mono_debug_handle)
1302 mono_debugger_lock ();
1304 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_count_func, &count);
1306 release_symbol_file_table ();
1308 size = sizeof (MonoDebuggerSymbolFileTable) + count * sizeof (MonoSymbolFile *);
1309 symfile_table = g_malloc0 (size);
1310 symfile_table->magic = MONO_SYMBOL_FILE_DYNAMIC_MAGIC;
1311 symfile_table->version = MONO_SYMBOL_FILE_DYNAMIC_VERSION;
1312 symfile_table->total_size = size;
1313 symfile_table->count = count;
1314 symfile_table->generation = debugger_symbol_file_table_generation;
1315 symfile_table->global_symfile = mono_debugger_global_symbol_file;
1317 data.symfile_table = symfile_table;
1320 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_func, &data);
1322 debugger_symbol_file_table = symfile_table;
1324 mono_debugger_unlock ();
1330 debugger_update_symbol_file_table (void)
1334 if (!mono_debug_handle)
1337 mono_debugger_lock ();
1338 retval = debugger_update_symbol_file_table_internal ();
1339 mono_debugger_unlock ();
1344 static gboolean has_mono_debugger_support = FALSE;
1346 static pid_t debugger_background_thread;
1347 extern void mono_debugger_init_thread_debug (pid_t);
1350 * NOTE: We must not call any functions here which we ever may want to debug !
1353 debugger_thread_func (gpointer ptr)
1355 void (*notification_code) (void) = ptr;
1356 int last_generation = 0;
1358 mono_new_thread_init (NULL, &last_generation);
1359 debugger_background_thread = getpid ();
1362 * Ok, we're now running in the debugger - signal the parent thread that
1363 * we're ready and enter the event loop.
1365 g_assert (WaitForSingleObject (debugger_start_cond, INFINITE) == WAIT_OBJECT_0);
1368 * Lock the mutex and raise a SIGSTOP to give the debugger a chance to
1369 * attach to us. This is important since the `notification_code' contains a
1370 * breakpoint instruction which would otherwise be deadly for us.
1372 mono_debugger_lock ();
1377 /* Wait for an event. */
1378 mono_debugger_unlock ();
1379 g_assert (WaitForSingleObject (debugger_thread_cond, INFINITE) == WAIT_OBJECT_0);
1380 mono_debugger_lock ();
1382 /* Reload the symbol file table if necessary. */
1383 if (debugger_symbol_file_table_generation > last_generation) {
1384 debugger_update_symbol_file_table_internal ();
1385 last_generation = debugger_symbol_file_table_generation;
1389 * Send notification - we'll stop on a breakpoint instruction at a special
1390 * address. The debugger will reload the symbol tables while we're stopped -
1391 * and owning the `debugger_thread_mutex' so that no other thread can touch
1392 * them in the meantime.
1394 notification_code ();
1396 /* Clear modified and signalled flag. */
1397 debugger_symbol_file_table_modified = FALSE;
1398 debugger_signalled = FALSE;
1400 if (must_send_finished) {
1401 EnterCriticalSection (&debugger_finished_mutex);
1402 ReleaseSemaphore (debugger_finished_cond, 1, NULL);
1403 must_send_finished = FALSE;
1404 LeaveCriticalSection (&debugger_finished_mutex);
1412 initialize_debugger_support ()
1417 if (has_mono_debugger_support)
1419 debugger_init_threads ();
1420 has_mono_debugger_support = TRUE;
1422 ptr = buf = g_malloc0 (16);
1423 x86_breakpoint (buf);
1424 debugger_notification_address = buf;
1428 * This mutex is only unlocked in mono_debugger_wait().
1430 EnterCriticalSection (&debugger_finished_mutex);
1432 thread = CreateThread (NULL, 0, debugger_thread_func, ptr, FALSE, NULL);
1436 * Wait until the debugger thread has actually been started and we
1437 * have its pid, then actually start the background thread.
1439 mono_debugger_init_thread_debug (debugger_background_thread);
1440 ReleaseSemaphore (debugger_start_cond, 1, NULL);
1443 * We keep this mutex until mono_debugger_jit_exec().
1445 mono_debugger_lock ();
1448 static GPtrArray *breakpoints = NULL;
1451 mono_insert_breakpoint_full (MonoMethodDesc *desc, gboolean use_trampoline)
1453 static int last_breakpoint_id = 0;
1454 MonoDebuggerBreakpointInfo *info;
1456 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
1458 info->use_trampoline = use_trampoline;
1459 info->index = ++last_breakpoint_id;
1462 breakpoints = g_ptr_array_new ();
1464 g_ptr_array_add (breakpoints, info);
1470 debugger_insert_breakpoint (guint64 method_argument, const gchar *string_argument)
1472 MonoMethodDesc *desc;
1474 desc = mono_method_desc_new (string_argument, FALSE);
1478 return mono_insert_breakpoint_full (desc, TRUE);
1482 debugger_remove_breakpoint (guint64 breakpoint)
1489 for (i = 0; i < breakpoints->len; i++) {
1490 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1492 if (info->index != breakpoint)
1495 mono_method_desc_free (info->desc);
1496 g_ptr_array_remove (breakpoints, info);
1505 debugger_compile_method (MonoMethod *method)
1509 mono_debugger_lock ();
1510 retval = mono_compile_method (method);
1511 mono_debugger_signal (FALSE);
1512 mono_debugger_unlock ();
1517 mono_remove_breakpoint (int breakpoint_id)
1519 return debugger_remove_breakpoint (breakpoint_id);
1523 mono_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
1525 MonoMethodDesc *desc;
1527 desc = mono_method_desc_new (method_name, include_namespace);
1531 return mono_insert_breakpoint_full (desc, has_mono_debugger_support);
1535 mono_method_has_breakpoint (MonoMethod* method, gboolean use_trampoline)
1539 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
1542 for (i = 0; i < breakpoints->len; i++) {
1543 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1545 if (info->use_trampoline != use_trampoline)
1548 if (!mono_method_desc_full_match (info->desc, method))
1558 mono_debugger_trampoline_breakpoint_callback (void)
1560 mono_debugger_lock ();
1561 must_send_finished = TRUE;
1562 mono_debugger_signal (FALSE);
1563 mono_debugger_unlock ();
1565 mono_debugger_wait ();
1569 * This is a custom version of mono_jit_exec() which is used when we're being run inside
1570 * the Mono Debugger.
1573 mono_debugger_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
1575 MonoImage *image = assembly->image;
1579 method = mono_get_method (image, mono_image_get_entry_point (image), NULL);
1581 addr = mono_compile_method (method);
1584 * The mutex has been locked in initialize_debugger_support(); we keep it locked
1585 * until we compiled the main method and signalled the debugger.
1587 must_send_finished = TRUE;
1588 mono_debugger_signal (TRUE);
1589 mono_debugger_unlock ();
1592 * Wait until the debugger has loaded the initial symbol tables.
1594 mono_debugger_wait ();
1596 return mono_runtime_run_main (method, argc, argv, NULL);