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 mono_debug_update_symbol_file_table().
34 guint32 mono_debugger_symbol_file_table_generation = 0;
35 guint32 mono_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 MonoDebuggerSymbolFileTable *mono_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 MonoDebugHandle *mono_debug_handle = NULL;
47 static gboolean mono_debug_initialized = FALSE;
49 static CRITICAL_SECTION debugger_lock_mutex;
51 extern void (*mono_debugger_class_init_func) (MonoClass *klass);
53 static void mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data);
54 static void mono_debug_close_assembly (AssemblyDebugInfo* info);
55 static AssemblyDebugInfo *mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image);
57 static int running_in_the_mono_debugger = FALSE;
58 void (*mono_debugger_event_handler) (MonoDebuggerEvent event, gpointer data, gpointer data2) = NULL;
60 #ifndef PLATFORM_WIN32
62 MonoDebuggerIOLayer mono_debugger_io_layer = {
63 InitializeCriticalSection, DeleteCriticalSection, TryEnterCriticalSection,
64 EnterCriticalSection, LeaveCriticalSection, WaitForSingleObject, SignalObjectAndWait,
65 WaitForMultipleObjects, CreateSemaphore, ReleaseSemaphore, CreateThread
71 mono_debugger_event (MonoDebuggerEvent event, gpointer data, gpointer data2)
73 if (mono_debugger_event_handler)
74 (* mono_debugger_event_handler) (event, data, data2);
78 mono_debug_init (int in_the_debugger)
80 if (mono_debug_initialized)
83 InitializeCriticalSection (&debugger_lock_mutex);
84 mono_debug_initialized = TRUE;
85 running_in_the_mono_debugger = in_the_debugger;
89 mono_debug_create_notification_function (gpointer *notification_address)
93 ptr = buf = g_malloc0 (16);
95 if (notification_address)
96 *notification_address = buf;
103 mono_debug_lock (void)
105 if (mono_debug_initialized)
106 EnterCriticalSection (&debugger_lock_mutex);
110 mono_debug_unlock (void)
112 if (mono_debug_initialized)
113 LeaveCriticalSection (&debugger_lock_mutex);
117 free_method_info (MonoDebugMethodInfo *minfo)
119 DebugMethodInfo *priv = minfo->user_data;
127 g_array_free (minfo->jit->line_numbers, TRUE);
128 g_free (minfo->jit->this_var);
129 g_free (minfo->jit->params);
130 g_free (minfo->jit->locals);
134 g_free (minfo->il_offsets);
139 debug_arg_warning (const char *message)
141 g_warning ("Error while processing --debug-args arguments: %s", message);
145 replace_suffix (const char *filename, const char *new_suffix)
147 const char *pos = strrchr (filename, '.');
150 return g_strdup_printf ("%s.%s", filename, new_suffix);
152 int len = pos - filename;
153 gchar *retval = g_malloc0 (len + strlen (new_suffix) + 2);
154 memcpy (retval, filename, len);
156 memcpy (retval + len + 1, new_suffix, strlen (new_suffix) + 1);
162 mono_debug_open (MonoAssembly *assembly, MonoDebugFormat format, const char **args)
164 MonoDebugHandle *debug;
167 g_assert (!mono_debug_handle);
169 debug = g_new0 (MonoDebugHandle, 1);
170 debug->name = g_strdup (assembly->image->name);
171 debug->format = format;
172 debug->producer_name = g_strdup_printf ("Mono JIT compiler version %s", VERSION);
173 debug->next_idx = 100;
176 debug->type_hash = g_hash_table_new (NULL, NULL);
177 debug->source_files = g_ptr_array_new ();
179 debug->images = g_hash_table_new_full (NULL, NULL, NULL,
180 (GDestroyNotify) mono_debug_close_assembly);
182 for (ptr = args; ptr && *ptr; ptr++) {
183 const char *arg = *ptr;
186 switch (debug->format) {
187 case MONO_DEBUG_FORMAT_STABS:
188 case MONO_DEBUG_FORMAT_DWARF2:
189 if (!strncmp (arg, "filename=", 9)) {
191 debug_arg_warning ("The `filename' argument can be given only once.");
192 debug->filename = g_strdup (arg + 9);
194 } else if (!strncmp (arg, "objfile=", 8)) {
196 debug_arg_warning ("The `objfile' argument can be given only once.");
197 debug->objfile = g_strdup (arg + 8);
201 case MONO_DEBUG_FORMAT_MONO:
202 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES |
203 MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
209 if (debug->format != MONO_DEBUG_FORMAT_MONO) {
210 if (!strcmp (arg, "dont_assemble")) {
211 debug->flags |= MONO_DEBUG_FLAGS_DONT_ASSEMBLE;
213 } else if (!strcmp (arg, "update_on_exit")) {
214 debug->flags |= MONO_DEBUG_FLAGS_UPDATE_ON_EXIT;
216 } else if (!strcmp (arg, "install_il_files")) {
217 debug->flags |= MONO_DEBUG_FLAGS_INSTALL_IL_FILES;
219 } else if (!strcmp (arg, "dont_update_il_files")) {
220 debug->flags |= MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES;
222 } else if (!strcmp (arg, "dont_create_il_files")) {
223 debug->flags |= MONO_DEBUG_FLAGS_DONT_CREATE_IL_FILES;
228 message = g_strdup_printf ("Unknown argument `%s'.", arg);
229 debug_arg_warning (message);
233 switch (debug->format) {
234 case MONO_DEBUG_FORMAT_STABS:
235 if (!debug->filename)
236 debug->filename = g_strdup_printf ("%s-stabs.s", g_basename (debug->name));
238 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
240 case MONO_DEBUG_FORMAT_DWARF2:
241 if (!debug->filename)
242 debug->filename = g_strdup_printf ("%s-dwarf.s", g_basename (debug->name));
244 debug->objfile = g_strdup_printf ("%s.o", g_basename (debug->name));
246 case MONO_DEBUG_FORMAT_MONO:
247 mono_debugger_class_init_func = mono_debug_add_type;
250 g_assert_not_reached ();
254 release_symbol_file_table ();
256 mono_debug_handle = debug;
257 mono_install_assembly_load_hook (mono_debug_add_assembly, NULL);
259 mono_debug_open_image (mono_debug_handle, assembly->image);
260 mono_debug_open_image (mono_debug_handle, mono_defaults.corlib);
262 mono_debug_add_type (mono_defaults.object_class);
263 mono_debug_add_type (mono_defaults.object_class);
264 mono_debug_add_type (mono_defaults.byte_class);
265 mono_debug_add_type (mono_defaults.void_class);
266 mono_debug_add_type (mono_defaults.boolean_class);
267 mono_debug_add_type (mono_defaults.sbyte_class);
268 mono_debug_add_type (mono_defaults.int16_class);
269 mono_debug_add_type (mono_defaults.uint16_class);
270 mono_debug_add_type (mono_defaults.int32_class);
271 mono_debug_add_type (mono_defaults.uint32_class);
272 mono_debug_add_type (mono_defaults.int_class);
273 mono_debug_add_type (mono_defaults.uint_class);
274 mono_debug_add_type (mono_defaults.int64_class);
275 mono_debug_add_type (mono_defaults.uint64_class);
276 mono_debug_add_type (mono_defaults.single_class);
277 mono_debug_add_type (mono_defaults.double_class);
278 mono_debug_add_type (mono_defaults.char_class);
279 mono_debug_add_type (mono_defaults.string_class);
280 mono_debug_add_type (mono_defaults.enum_class);
281 mono_debug_add_type (mono_defaults.array_class);
282 mono_debug_add_type (mono_defaults.multicastdelegate_class);
283 mono_debug_add_type (mono_defaults.asyncresult_class);
284 mono_debug_add_type (mono_defaults.waithandle_class);
285 mono_debug_add_type (mono_defaults.typehandle_class);
286 mono_debug_add_type (mono_defaults.fieldhandle_class);
287 mono_debug_add_type (mono_defaults.methodhandle_class);
288 mono_debug_add_type (mono_defaults.monotype_class);
289 mono_debug_add_type (mono_defaults.exception_class);
290 mono_debug_add_type (mono_defaults.threadabortexception_class);
291 mono_debug_add_type (mono_defaults.thread_class);
292 mono_debug_add_type (mono_defaults.transparent_proxy_class);
293 mono_debug_add_type (mono_defaults.real_proxy_class);
294 mono_debug_add_type (mono_defaults.mono_method_message_class);
295 mono_debug_add_type (mono_defaults.appdomain_class);
296 mono_debug_add_type (mono_defaults.field_info_class);
297 mono_debug_add_type (mono_defaults.stringbuilder_class);
298 mono_debug_add_type (mono_defaults.math_class);
299 mono_debug_add_type (mono_defaults.stack_frame_class);
300 mono_debug_add_type (mono_defaults.stack_trace_class);
301 mono_debug_add_type (mono_defaults.marshal_class);
302 mono_debug_add_type (mono_defaults.iserializeable_class);
303 mono_debug_add_type (mono_defaults.serializationinfo_class);
304 mono_debug_add_type (mono_defaults.streamingcontext_class);
306 mono_debug_update_symbol_file_table ();
308 mono_debug_unlock ();
314 mono_debug_add_assembly (MonoAssembly *assembly, gpointer user_data)
316 if (!mono_debug_handle)
320 mono_debug_open_image (mono_debug_handle, assembly->image);
321 mono_debug_unlock ();
325 generate_il_offsets (AssemblyDebugInfo *info, MonoMethod *method)
327 GPtrArray *il_offsets = g_ptr_array_new ();
328 MonoClass *klass = method->klass;
329 MonoDebugMethodInfo *minfo;
330 DebugMethodInfo *priv;
333 g_assert (klass->image == info->image);
335 /* FIXME: doesn't work yet. */
336 if (!strcmp (klass->name_space, "System.Runtime.Remoting.Proxies"))
339 mono_class_init (klass);
341 minfo = g_new0 (MonoDebugMethodInfo, 1);
342 minfo->method = method;
343 minfo->user_data = priv = g_new0 (DebugMethodInfo, 1);
345 priv->name = g_strdup_printf ("%s%s%s.%s", klass->name_space, klass->name_space [0]? ".": "",
346 klass->name, method->name);
347 priv->source_file = info->source_file;
351 * Find the method index in the image.
353 for (i = 0; klass->methods && i < klass->method.count; ++i) {
354 if (klass->methods [i] == minfo->method) {
355 priv->method_number = klass->method.first + i + 1;
356 priv->first_line = info->mlines [priv->method_number];
361 g_assert (priv->method_number);
363 /* info->moffsets contains -1 "outside" of functions. */
364 for (i = priv->first_line; (i > 0) && (info->moffsets [i] == 0); i--)
366 priv->start_line = i + 1;
368 for (i = priv->start_line; info->moffsets [i] != -1; i++) {
369 MonoSymbolFileLineNumberEntry *lne = g_new0 (MonoSymbolFileLineNumberEntry, 1);
371 if (!info->moffsets [i] && (i > priv->start_line))
374 lne->offset = info->moffsets [i];
377 g_ptr_array_add (il_offsets, lne);
382 minfo->start_line = priv->first_line;
383 minfo->end_line = priv->last_line;
385 minfo->num_il_offsets = il_offsets->len;
386 minfo->il_offsets = g_new0 (MonoSymbolFileLineNumberEntry, il_offsets->len);
387 for (i = 0; i < il_offsets->len; i++) {
388 MonoSymbolFileLineNumberEntry *il = g_ptr_array_index (il_offsets, i);
390 minfo->il_offsets [i] = *il;
393 g_ptr_array_free (il_offsets, TRUE);
395 g_hash_table_insert (info->methods, method, minfo);
399 debug_load_method_lines (AssemblyDebugInfo* info)
401 MonoTableInfo *table = &info->image->tables [MONO_TABLE_METHOD];
407 if (info->always_create_il || !(info->handle->flags & MONO_DEBUG_FLAGS_DONT_UPDATE_IL_FILES)) {
408 char *command = g_strdup_printf ("monodis --output=%s %s",
409 info->ilfile, info->image->name);
410 struct stat stata, statb;
411 int need_update = FALSE;
413 if (stat (info->image->name, &stata)) {
414 g_warning ("cannot access assembly file (%s): %s",
415 info->image->name, g_strerror (errno));
420 /* If the stat() failed or the file is older. */
421 if (stat (info->ilfile, &statb)) {
423 } else if (statb.st_mtime < stata.st_mtime)
427 #ifndef PLATFORM_WIN32
428 struct sigaction act, oldact;
433 #ifndef PLATFORM_WIN32
434 act.sa_handler = SIG_IGN;
435 act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
436 sigemptyset (&act.sa_mask);
437 sigaddset (&act.sa_mask, SIGCHLD);
438 sigprocmask (SIG_BLOCK, &act.sa_mask, &old_set);
439 sigaction (SIGCHLD, &act, &oldact);
442 g_print ("Recreating %s from %s.\n", info->ilfile, info->image->name);
444 ret = system (command);
446 #ifndef PLATFORM_WIN32
447 sigaction (SIGCHLD, &oldact, NULL);
448 sigprocmask (SIG_SETMASK, &old_set, NULL);
452 g_warning ("cannot create IL assembly file (%s): %s",
453 command, g_strerror (errno));
460 /* use an env var with directories for searching. */
461 if (!(f = fopen (info->ilfile, "r"))) {
462 g_warning ("cannot open IL assembly file %s", info->ilfile);
466 info->total_lines = 100;
467 info->moffsets = g_malloc (info->total_lines * sizeof (int));
470 while (fgets (buf, sizeof (buf), f)) {
473 info->moffsets [i++] = offset;
474 if (i + 2 >= info->total_lines) {
475 info->total_lines += 100;
476 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
477 g_assert (info->moffsets);
480 if (!sscanf (buf, " // method line %d", &mnum))
485 if (mnum >= info->nmethods)
488 while (fgets (buf, sizeof (buf), f)) {
492 if (i + 2 >= info->total_lines) {
493 info->total_lines += 100;
494 info->moffsets = g_realloc (info->moffsets, info->total_lines * sizeof (int));
495 g_assert (info->moffsets);
498 if (strstr (buf, "}")) {
503 if (sscanf (buf, " IL_%x:", &newoffset)) {
509 info->moffsets [i] = offset;
511 /* g_print ("method %d found at %d\n", mnum, pos); */
512 info->mlines [mnum] = pos;
516 for (idx = 1; idx <= table->rows; idx++) {
517 guint32 token = mono_metadata_make_token (MONO_TABLE_METHOD, idx);
518 MonoMethod *method = mono_get_method (info->image, token, NULL);
520 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
521 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
522 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
525 if (method->wrapper_type != MONO_WRAPPER_NONE)
528 generate_il_offsets (info, method);
533 record_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, guint32 line)
535 MonoDebugLineNumberEntry *lne = g_new0 (MonoDebugLineNumberEntry, 1);
537 lne->address = address;
538 lne->offset = offset;
541 g_array_append_val (minfo->jit->line_numbers, *lne);
545 debug_generate_method_lines (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
547 guint32 st_address, st_line;
548 DebugMethodInfo *priv = minfo->user_data;
551 if (!priv || !info->moffsets)
554 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
556 st_line = priv->first_line;
557 st_address = minfo->jit->prologue_end;
559 /* This is the first actual code line of the method. */
560 record_line_number (minfo, st_address, 0, st_line);
562 /* start lines of basic blocks */
563 for (i = 0; i < cfg->block_count; ++i) {
566 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
567 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
568 gint32 line_inc = 0, addr_inc;
571 st_line = priv->first_line;
572 st_address = t->addr;
575 addr_inc = t->addr - st_address;
576 st_address += addr_inc;
578 if (t->cli_addr != -1) {
579 int *lines = info->moffsets + st_line;
582 while ((*k != -1) && (*k < t->cli_addr))
585 line_inc = k - lines;
590 if (t->cli_addr != -1)
591 record_line_number (minfo, st_address, t->cli_addr, st_line);
597 generate_line_number (MonoDebugMethodInfo *minfo, guint32 address, guint32 offset, int debug)
602 g_message (G_STRLOC ": searching IL offset %x", offset);
604 for (i = minfo->num_il_offsets - 1; i >= 0; i--) {
605 MonoDebugLineNumberEntry *lne;
607 if (minfo->il_offsets [i].offset > offset)
611 g_message (G_STRLOC ": found entry %d: offset = %x, row = %d",
612 i, minfo->il_offsets [i].offset, minfo->il_offsets [i].row);
614 if (minfo->jit->line_numbers->len) {
615 MonoDebugLineNumberEntry last = g_array_index (
616 minfo->jit->line_numbers, MonoDebugLineNumberEntry,
617 minfo->jit->line_numbers->len - 1);
619 /* Avoid writing more than one entry for the same line. */
620 if (minfo->il_offsets [i].row == last.line) {
622 g_message (G_STRLOC ": skipping line: line = %d, last line = %d, "
623 "last address = %x, address = %x, "
624 "last offset = %x, offset = %x",
625 last.line, minfo->il_offsets [i].row,
626 last.address, address, last.offset, offset);
633 g_message (G_STRLOC ": writing entry: line = %d, offfset = %x, address = %x",
634 minfo->il_offsets [i].row, offset, address);
636 lne = g_new0 (MonoDebugLineNumberEntry, 1);
637 lne->address = address;
638 lne->offset = offset;
639 lne->line = minfo->il_offsets [i].row;
641 g_array_append_val (minfo->jit->line_numbers, *lne);
647 debug_update_il_offsets (AssemblyDebugInfo *info, MonoDebugMethodInfo *minfo, MonoFlowGraph* cfg)
649 MonoMethodHeader *header;
650 guint32 address, offset;
654 g_assert (info->symfile);
655 if (info->symfile->is_dynamic)
658 g_assert (!minfo->jit->line_numbers);
659 minfo->jit->line_numbers = g_array_new (FALSE, TRUE, sizeof (MonoDebugLineNumberEntry));
661 address = minfo->jit->prologue_end;
664 g_assert (((MonoMethodNormal*)minfo->method)->header);
665 header = ((MonoMethodNormal*)minfo->method)->header;
668 if (!strcmp (minfo->method->name, "Test") || !strcmp (minfo->method->name, "Main")) {
669 MonoMethodHeader *header = ((MonoMethodNormal*)minfo->method)->header;
672 mono_disassemble_code (minfo->jit->code_start, minfo->jit->code_size,
673 minfo->method->name);
675 printf ("\nDisassembly:\n%s\n", mono_disasm_code (
676 NULL, minfo->method, header->code, header->code + header->code_size));
677 g_message (G_STRLOC ": %x - %x", minfo->jit->prologue_end, minfo->jit->epilogue_begin);
681 generate_line_number (minfo, address, offset, debug);
683 /* start lines of basic blocks */
684 for (i = 0; i < cfg->block_count; ++i) {
687 for (j = 0; cfg->bblocks [i].forest && (j < cfg->bblocks [i].forest->len); ++j) {
688 MBTree *t = (MBTree *) g_ptr_array_index (cfg->bblocks [i].forest, j);
690 if ((t->cli_addr == -1) || (t->cli_addr == offset) || (t->addr == address))
693 offset = t->cli_addr;
696 generate_line_number (minfo, address, offset, debug);
700 generate_line_number (minfo, minfo->jit->epilogue_begin, header->code_size, debug);
703 for (i = 0; i < minfo->jit->line_numbers->len; i++) {
704 MonoDebugLineNumberEntry lne = g_array_index (
705 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
707 g_message (G_STRLOC ": %x,%x,%d", lne.address, lne.offset, lne.line);
711 if (minfo->jit->line_numbers->len) {
712 MonoDebugLineNumberEntry lne = g_array_index (
713 minfo->jit->line_numbers, MonoDebugLineNumberEntry, 0);
715 minfo->jit->prologue_end = lne.address;
719 static AssemblyDebugInfo *
720 mono_debug_get_image (MonoDebugHandle* debug, MonoImage *image)
722 return g_hash_table_lookup (debug->images, image);
725 static AssemblyDebugInfo *
726 mono_debug_open_image (MonoDebugHandle* debug, MonoImage *image)
728 AssemblyDebugInfo *info;
731 info = mono_debug_get_image (debug, image);
737 info = g_new0 (AssemblyDebugInfo, 1);
739 info->image->ref_count++;
740 info->name = g_strdup (image->assembly_name);
741 info->format = debug->format;
742 info->handle = debug;
743 info->methods = g_hash_table_new_full (g_direct_hash, g_direct_equal,
744 NULL, (GDestroyNotify) free_method_info);
746 info->source_file = debug->source_files->len;
747 g_ptr_array_add (debug->source_files, g_strdup_printf ("%s.il", image->assembly_name));
749 g_hash_table_insert (debug->images, image, info);
751 info->nmethods = image->tables [MONO_TABLE_METHOD].rows + 1;
752 info->mlines = g_new0 (int, info->nmethods);
754 for (ptr = image->references; ptr && *ptr; ptr++)
755 mono_debug_add_assembly (*ptr, NULL);
757 if (image->assembly->dynamic)
760 switch (info->format) {
761 case MONO_DEBUG_FORMAT_STABS:
762 case MONO_DEBUG_FORMAT_DWARF2:
763 if (debug->flags & MONO_DEBUG_FLAGS_INSTALL_IL_FILES) {
764 gchar *dirname = g_path_get_dirname (image->name);
765 info->ilfile = g_strdup_printf ("%s/%s.il", dirname, info->name);
768 info->ilfile = g_strdup_printf ("%s.il", info->name);
770 case MONO_DEBUG_FORMAT_MONO:
771 info->filename = replace_suffix (image->name, "dbg");
772 if (g_file_test (info->filename, G_FILE_TEST_EXISTS))
773 info->symfile = mono_debug_open_mono_symbol_file (info->image, info->filename, TRUE);
774 else if (running_in_the_mono_debugger)
775 info->symfile = mono_debug_create_mono_symbol_file (info->image);
776 mono_debugger_symbol_file_table_generation++;
783 if (debug->format != MONO_DEBUG_FORMAT_MONO)
784 debug_load_method_lines (info);
790 mono_debug_write_symbols (MonoDebugHandle *debug)
792 if (!debug || !debug->dirty)
795 release_symbol_file_table ();
797 switch (debug->format) {
798 case MONO_DEBUG_FORMAT_STABS:
799 mono_debug_write_stabs (debug);
801 case MONO_DEBUG_FORMAT_DWARF2:
802 mono_debug_write_dwarf2 (debug);
804 case MONO_DEBUG_FORMAT_MONO:
807 g_assert_not_reached ();
810 debug->dirty = FALSE;
814 mono_debug_make_symbols (void)
816 if (!mono_debug_handle || !mono_debug_handle->dirty)
819 switch (mono_debug_handle->format) {
820 case MONO_DEBUG_FORMAT_STABS:
821 mono_debug_write_stabs (mono_debug_handle);
823 case MONO_DEBUG_FORMAT_DWARF2:
824 mono_debug_write_dwarf2 (mono_debug_handle);
826 case MONO_DEBUG_FORMAT_MONO:
827 mono_debug_update_symbol_file_table ();
830 g_assert_not_reached ();
833 mono_debug_handle->dirty = FALSE;
837 mono_debug_close_assembly (AssemblyDebugInfo* info)
839 switch (info->format) {
840 case MONO_DEBUG_FORMAT_MONO:
841 if (info->symfile != NULL)
842 mono_debug_close_mono_symbol_file (info->symfile);
847 g_hash_table_destroy (info->methods);
848 g_free (info->mlines);
849 g_free (info->moffsets);
851 g_free (info->ilfile);
852 g_free (info->filename);
853 g_free (info->objfile);
858 mono_debug_cleanup (void)
860 release_symbol_file_table ();
862 if (!mono_debug_handle)
865 if (mono_debug_handle->flags & MONO_DEBUG_FLAGS_UPDATE_ON_EXIT)
866 mono_debug_write_symbols (mono_debug_handle);
868 g_hash_table_destroy (mono_debug_handle->images);
869 g_ptr_array_free (mono_debug_handle->source_files, TRUE);
870 g_hash_table_destroy (mono_debug_handle->type_hash);
871 g_free (mono_debug_handle->producer_name);
872 g_free (mono_debug_handle->name);
873 g_free (mono_debug_handle);
875 mono_debug_handle = NULL;
879 mono_debug_get_type (MonoDebugHandle *debug, MonoClass *klass)
883 mono_class_init (klass);
885 index = GPOINTER_TO_INT (g_hash_table_lookup (debug->type_hash, klass));
891 index = ++debug->next_klass_idx;
892 g_hash_table_insert (debug->type_hash, klass, GINT_TO_POINTER (index));
897 switch (klass->byval_arg.type) {
898 case MONO_TYPE_CLASS:
900 mono_debug_get_type (debug, klass->parent);
902 for (i = 0; i < klass->method.count; i++) {
903 MonoMethod *method = klass->methods [i];
904 MonoType *ret_type = NULL;
907 if (method->signature->ret->type != MONO_TYPE_VOID)
908 ret_type = method->signature->ret;
911 MonoClass *ret_klass = mono_class_from_mono_type (ret_type);
912 mono_debug_get_type (debug, ret_klass);
915 for (j = 0; j < method->signature->param_count; j++) {
916 MonoType *sub_type = method->signature->params [j];
917 MonoClass *sub_klass = mono_class_from_mono_type (sub_type);
918 mono_debug_get_type (debug, sub_klass);
922 case MONO_TYPE_VALUETYPE:
923 for (i = 0; i < klass->field.count; i++) {
924 MonoClass *subclass = mono_class_from_mono_type (klass->fields [i].type);
925 mono_debug_get_type (debug, subclass);
928 case MONO_TYPE_ARRAY:
929 case MONO_TYPE_SZARRAY:
930 mono_debug_get_type (debug, klass->element_class);
940 il_offset_from_address (MonoDebugMethodInfo *minfo, guint32 address)
944 if (!minfo->jit || !minfo->jit->line_numbers)
947 for (i = minfo->jit->line_numbers->len - 1; i >= 0; i--) {
948 MonoDebugLineNumberEntry lne = g_array_index (
949 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
951 if (lne.address <= address)
959 address_from_il_offset (MonoDebugMethodInfo *minfo, guint32 il_offset)
963 if (!minfo->jit || !minfo->jit->line_numbers)
966 for (i = minfo->jit->line_numbers->len - 1; i >= 0; i--) {
967 MonoDebugLineNumberEntry lne = g_array_index (
968 minfo->jit->line_numbers, MonoDebugLineNumberEntry, i);
970 if (lne.offset <= il_offset)
978 mono_debug_add_type (MonoClass *klass)
980 AssemblyDebugInfo* info;
982 if (!mono_debug_handle)
985 info = mono_debug_get_image (mono_debug_handle, klass->image);
988 if (mono_debug_handle->format != MONO_DEBUG_FORMAT_MONO)
993 mono_debug_symfile_add_type (info->symfile, klass);
994 mono_debugger_event (MONO_DEBUGGER_EVENT_TYPE_ADDED, info->symfile, klass);
995 mono_debug_unlock ();
1000 il_offset_from_position (MonoFlowGraph *cfg, MonoPosition *pos)
1005 if (pos->abs_pos == 0)
1008 if (pos->pos.bid >= cfg->block_count)
1011 bblock = &cfg->bblocks [pos->pos.bid];
1012 if (pos->pos.tid >= bblock->forest->len)
1015 tree = (MBTree *) g_ptr_array_index (bblock->forest, pos->pos.tid);
1017 return tree->cli_addr;
1020 struct LookupMethodData
1022 MonoDebugMethodInfo *minfo;
1027 lookup_method_func (gpointer key, gpointer value, gpointer user_data)
1029 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1030 struct LookupMethodData *data = (struct LookupMethodData *) user_data;
1036 data->minfo = mono_debug_find_method (info->symfile, data->method);
1038 data->minfo = g_hash_table_lookup (info->methods, data->method);
1041 static MonoDebugMethodInfo *
1042 lookup_method (MonoMethod *method)
1044 struct LookupMethodData data = { NULL, method };
1046 if (!mono_debug_handle)
1049 g_hash_table_foreach (mono_debug_handle->images, lookup_method_func, &data);
1054 mono_debug_add_method (MonoFlowGraph *cfg)
1056 MonoMethod *method = cfg->method;
1057 MonoClass *klass = method->klass;
1058 AssemblyDebugInfo* info;
1059 MonoDebugMethodJitInfo *jit;
1060 MonoDebugMethodInfo *minfo;
1063 if (!mono_debug_handle)
1066 mono_class_init (klass);
1068 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1069 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1070 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1071 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1074 if (method->wrapper_type != MONO_WRAPPER_NONE)
1077 info = mono_debug_get_image (mono_debug_handle, klass->image);
1080 minfo = lookup_method (method);
1081 if (!minfo || minfo->jit)
1086 mono_debug_handle->dirty = TRUE;
1088 minfo->jit = jit = g_new0 (MonoDebugMethodJitInfo, 1);
1089 jit->code_start = cfg->start;
1090 jit->code_size = cfg->epilogue_end;
1091 jit->prologue_end = cfg->prologue_end;
1092 jit->epilogue_begin = cfg->epilog;
1093 jit->num_params = method->signature->param_count;
1094 jit->params = g_new0 (MonoDebugVarInfo, jit->num_params);
1096 if (method->signature->hasthis) {
1097 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index;
1099 jit->this_var = g_new0 (MonoDebugVarInfo, 1);
1100 jit->this_var->offset = ptr->offset;
1101 jit->this_var->size = ptr->size;
1104 for (i = 0; i < jit->num_params; i++) {
1105 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->args_start_index +
1106 method->signature->hasthis;
1108 jit->params [i].offset = ptr [i].offset;
1109 jit->params [i].size = ptr [i].size;
1112 debug_generate_method_lines (info, minfo, cfg);
1113 if (info->format == MONO_DEBUG_FORMAT_MONO)
1114 debug_update_il_offsets (info, minfo, cfg);
1116 if (!method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
1117 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
1118 MonoVarInfo *ptr = ((MonoVarInfo *) cfg->varinfo->data) + cfg->locals_start_index;
1119 MonoDebugVarInfo *locals;
1121 locals = g_new0 (MonoDebugVarInfo, header->num_locals);
1122 for (i = 0; i < header->num_locals; i++) {
1123 gint32 begin_offset, end_offset;
1124 gint32 begin_scope, end_scope;
1126 if (ptr [i].reg >= 0) {
1127 locals [i].index = ptr [i].reg | MONO_DEBUG_VAR_ADDRESS_MODE_REGISTER;
1128 locals [i].offset = 0;
1130 locals [i].offset = ptr [i].offset;
1132 locals [i].size = ptr [i].size;
1134 begin_offset = il_offset_from_position (cfg, &ptr [i].range.first_use);
1135 end_offset = il_offset_from_position (cfg, &ptr [i].range.last_use);
1136 if (end_offset >= 0)
1139 if (begin_offset >= 0)
1140 begin_scope = address_from_il_offset (minfo, begin_offset);
1143 if (end_offset >= 0)
1144 end_scope = address_from_il_offset (minfo, end_offset);
1148 if (begin_scope > 0)
1149 locals [i].begin_scope = begin_scope;
1151 locals [i].begin_scope = jit->prologue_end;
1153 locals [i].end_scope = end_scope;
1155 locals [i].end_scope = jit->epilogue_begin;
1158 jit->num_locals = header->num_locals;
1159 jit->locals = locals;
1162 if (info->symfile) {
1163 mono_debug_symfile_add_method (info->symfile, method);
1164 mono_debugger_event (MONO_DEBUGGER_EVENT_METHOD_ADDED, info->symfile, method);
1167 mono_debug_unlock ();
1171 mono_debug_source_location_from_address (MonoMethod *method, guint32 address, guint32 *line_number)
1173 MonoDebugMethodInfo *minfo = lookup_method (method);
1178 if (minfo->symfile) {
1179 gint32 offset = il_offset_from_address (minfo, address);
1184 return mono_debug_find_source_location (minfo->symfile, method, offset, line_number);
1191 mono_debug_il_offset_from_address (MonoMethod *method, gint32 address)
1193 MonoDebugMethodInfo *minfo;
1198 minfo = lookup_method (method);
1199 if (!minfo || !minfo->il_offsets)
1202 return il_offset_from_address (minfo, address);
1206 mono_debug_address_from_il_offset (MonoMethod *method, gint32 il_offset)
1208 MonoDebugMethodInfo *minfo;
1213 minfo = lookup_method (method);
1214 if (!minfo || !minfo->il_offsets)
1217 return address_from_il_offset (minfo, il_offset);
1221 release_symbol_file_table ()
1223 MonoDebuggerSymbolFileTable *temp;
1225 if (!mono_debugger_symbol_file_table)
1229 * Caution: The debugger may access the memory pointed to by this variable
1230 * at any time. It is very important to set the pointer to NULL
1231 * before freeing the area.
1234 temp = mono_debugger_symbol_file_table;
1235 mono_debugger_symbol_file_table = NULL;
1236 g_free (mono_debugger_symbol_file_table);
1240 update_symbol_file_table_count_func (gpointer key, gpointer value, gpointer user_data)
1242 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1246 if (info->format != MONO_DEBUG_FORMAT_MONO)
1249 ++ (* (int *) user_data);
1252 struct SymfileTableData
1254 MonoDebuggerSymbolFileTable *symfile_table;
1259 update_symbol_file_table_func (gpointer key, gpointer value, gpointer user_data)
1261 AssemblyDebugInfo *info = (AssemblyDebugInfo *) value;
1262 struct SymfileTableData *data = (struct SymfileTableData *) user_data;
1266 if (info->format != MONO_DEBUG_FORMAT_MONO)
1269 data->symfile_table->symfiles [data->index++] = info->symfile;
1273 mono_debug_update_symbol_file_table (void)
1276 MonoDebuggerSymbolFileTable *symfile_table;
1277 struct SymfileTableData data;
1280 if (!mono_debug_handle)
1285 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_count_func, &count);
1287 release_symbol_file_table ();
1289 size = sizeof (MonoDebuggerSymbolFileTable) + count * sizeof (MonoSymbolFile *);
1290 symfile_table = g_malloc0 (size);
1291 symfile_table->magic = MONO_SYMBOL_FILE_DYNAMIC_MAGIC;
1292 symfile_table->version = MONO_SYMBOL_FILE_DYNAMIC_VERSION;
1293 symfile_table->total_size = size;
1294 symfile_table->count = count;
1295 symfile_table->generation = mono_debugger_symbol_file_table_generation;
1296 symfile_table->global_symfile = mono_debugger_global_symbol_file;
1298 data.symfile_table = symfile_table;
1301 g_hash_table_foreach (mono_debug_handle->images, update_symbol_file_table_func, &data);
1303 mono_debugger_symbol_file_table = symfile_table;
1305 mono_debug_unlock ();
1310 static GPtrArray *breakpoints = NULL;
1313 mono_insert_breakpoint_full (MonoMethodDesc *desc, gboolean use_trampoline)
1315 static int last_breakpoint_id = 0;
1316 MonoDebuggerBreakpointInfo *info;
1318 info = g_new0 (MonoDebuggerBreakpointInfo, 1);
1320 info->use_trampoline = use_trampoline;
1321 info->index = ++last_breakpoint_id;
1324 breakpoints = g_ptr_array_new ();
1326 g_ptr_array_add (breakpoints, info);
1332 mono_remove_breakpoint (int breakpoint_id)
1339 for (i = 0; i < breakpoints->len; i++) {
1340 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1342 if (info->index != breakpoint_id)
1345 mono_method_desc_free (info->desc);
1346 g_ptr_array_remove (breakpoints, info);
1355 mono_insert_breakpoint (const gchar *method_name, gboolean include_namespace)
1357 MonoMethodDesc *desc;
1359 desc = mono_method_desc_new (method_name, include_namespace);
1363 return mono_insert_breakpoint_full (desc, running_in_the_mono_debugger);
1367 mono_method_has_breakpoint (MonoMethod* method, gboolean use_trampoline)
1371 if (!breakpoints || (method->wrapper_type != MONO_WRAPPER_NONE))
1374 for (i = 0; i < breakpoints->len; i++) {
1375 MonoDebuggerBreakpointInfo *info = g_ptr_array_index (breakpoints, i);
1377 if (info->use_trampoline != use_trampoline)
1380 if (!mono_method_desc_full_match (info->desc, method))
1390 mono_debugger_trampoline_breakpoint_callback (void)
1392 mono_debugger_event (MONO_DEBUGGER_EVENT_BREAKPOINT_TRAMPOLINE, NULL, NULL);