6 #include <mono/metadata/metadata.h>
7 #include <mono/metadata/tabledefs.h>
8 #include <mono/metadata/rawbuffer.h>
9 #include <mono/metadata/tokentype.h>
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/exception.h>
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/mono-debug.h>
14 #include <mono/metadata/debug-mono-symfile.h>
15 #include <mono/metadata/mono-endian.h>
20 #define RANGE_TABLE_CHUNK_SIZE 256
21 #define CLASS_TABLE_CHUNK_SIZE 256
22 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
23 #define TYPE_TABLE_CHUNK_SIZE 65536
26 free_method_info (MonoDebugMethodInfo *minfo)
32 get_class_name (MonoClass *klass)
34 if (klass->nested_in) {
35 gchar *parent_name = get_class_name (klass->nested_in);
36 gchar *name = g_strdup_printf ("%s.%s", parent_name, klass->name);
41 return g_strdup_printf ("%s%s%s", klass->name_space,
42 klass->name_space [0] ? "." : "", klass->name);
46 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile)
48 const char *ptr, *start;
52 ptr = start = symfile->raw_contents;
57 ptr += sizeof(guint64);
58 if (magic != MONO_SYMBOL_FILE_MAGIC) {
59 g_warning ("Symbol file %s is not a mono symbol file", handle->image_file);
63 version = read32(ptr);
64 ptr += sizeof(guint32);
65 if (version != MONO_SYMBOL_FILE_VERSION) {
66 g_warning ("Symbol file %s has incorrect version "
67 "(expected %d, got %ld)", handle->image_file,
68 MONO_SYMBOL_FILE_VERSION, version);
72 symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
74 symfile->method_hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
75 (GDestroyNotify) free_method_info);
81 open_symfile (MonoImage *image, guint32 *size)
83 MonoTableInfo *table = mono_image_get_table_info (image, MONO_TABLE_MANIFESTRESOURCE);
85 guint32 cols [MONO_MANIFEST_SIZE];
88 num_rows = mono_table_info_get_rows (table);
89 for (i = 0; i < num_rows; ++i) {
90 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
91 val = mono_metadata_string_heap (image, cols [MONO_MANIFEST_NAME]);
92 if (!strcmp (val, "MonoSymbolFile"))
97 g_assert (!cols [MONO_MANIFEST_IMPLEMENTATION]);
99 return mono_image_get_resource (image, cols [MONO_MANIFEST_OFFSET], size);
103 mono_debug_open_mono_symbol_file (MonoDebugHandle *handle, gboolean create_symfile)
105 MonoSymbolFile *symfile;
107 symfile = g_new0 (MonoSymbolFile, 1);
109 symfile->raw_contents = open_symfile (handle->image, &symfile->raw_contents_size);
111 if (load_symfile (handle, symfile))
113 else if (!create_symfile) {
114 mono_debug_close_mono_symbol_file (symfile);
122 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
127 if (symfile->method_hash)
128 g_hash_table_destroy (symfile->method_hash);
134 read_string (const char *ptr)
136 int len = *((guint32 *) ptr);
137 ptr += sizeof(guint32);
138 return g_filename_from_utf8 (ptr, len, NULL, NULL, NULL);
142 mono_debug_find_source_location (MonoSymbolFile *symfile, MonoMethod *method, guint32 offset,
143 guint32 *line_number)
145 MonoSymbolFileLineNumberEntry *lne;
146 MonoDebugMethodInfo *minfo;
147 gchar *source_file = NULL;
151 if (!symfile->method_hash)
154 minfo = g_hash_table_lookup (symfile->method_hash, method);
158 if (read32(&(minfo->entry->_source_index))) {
159 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
160 (read32(&(minfo->entry->_source_index)) - 1) * sizeof (MonoSymbolFileSourceEntry);
161 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *) (symfile->raw_contents + offset);
163 source_file = read_string (symfile->raw_contents + read32(&(se->_name_offset)));
166 ptr = symfile->raw_contents + read32(&(minfo->entry->_line_number_table_offset));
168 lne = (MonoSymbolFileLineNumberEntry *) ptr;
170 for (i = 0; i < read32(&(minfo->entry->_num_line_numbers)); i++, lne++) {
171 if (read32(&(lne->_offset)) < offset)
175 *line_number = read32(&(lne->_row));
180 } else if (source_file) {
181 gchar *retval = g_strdup_printf ("%s:%d", source_file, read32(&(lne->_row)));
182 g_free (source_file);
185 return g_strdup_printf ("%d", read32(&(lne->_row)));
192 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, guint32 il_offset)
196 if (!jit || !jit->line_numbers)
199 for (i = jit->line_numbers->len - 1; i >= 0; i--) {
200 MonoDebugLineNumberEntry lne = g_array_index (
201 jit->line_numbers, MonoDebugLineNumberEntry, i);
203 if (lne.offset <= il_offset)
211 compare_method (const void *key, const void *object)
213 guint32 token = GPOINTER_TO_UINT (key);
214 MonoSymbolFileMethodIndexEntry *me = (MonoSymbolFileMethodIndexEntry*)object;
216 return token - read32(&(me->_token));
219 MonoDebugMethodInfo *
220 mono_debug_find_method (MonoDebugHandle *handle, MonoMethod *method)
222 MonoSymbolFileMethodEntry *me;
223 MonoSymbolFileMethodIndexEntry *first_ie, *ie;
224 MonoDebugMethodInfo *minfo;
225 MonoSymbolFile *symfile = handle->symfile;
227 if (!symfile->method_hash)
230 if (handle->image != method->klass->image)
233 first_ie = (MonoSymbolFileMethodIndexEntry *)
234 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
236 ie = bsearch (GUINT_TO_POINTER (method->token), first_ie,
237 read32(&(symfile->offset_table->_method_count)),
238 sizeof (MonoSymbolFileMethodIndexEntry), compare_method);
243 me = (MonoSymbolFileMethodEntry *) (symfile->raw_contents + read32(&(ie->_file_offset)));
245 minfo = g_new0 (MonoDebugMethodInfo, 1);
246 minfo->index = (ie - first_ie) + 1;
247 minfo->method = method;
248 minfo->handle = handle;
249 minfo->num_il_offsets = read32(&(me->_num_line_numbers));
250 minfo->il_offsets = (MonoSymbolFileLineNumberEntry *)
251 (symfile->raw_contents + read32(&(me->_line_number_table_offset)));
254 g_hash_table_insert (symfile->method_hash, method, minfo);