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>
16 #include <mono/metadata/metadata-internals.h>
17 #include <mono/metadata/class-internals.h>
18 #include <mono/metadata/mono-debug-debugger.h>
23 #define RANGE_TABLE_CHUNK_SIZE 256
24 #define CLASS_TABLE_CHUNK_SIZE 256
25 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
26 #define TYPE_TABLE_CHUNK_SIZE 65536
29 free_method_info (MonoDebugMethodInfo *minfo)
35 get_class_name (MonoClass *klass)
37 MonoClass *nested_in = mono_class_get_nesting_type (klass);
38 const char *name_space;
40 gchar *parent_name = get_class_name (nested_in);
41 gchar *name = g_strdup_printf ("%s.%s", parent_name, mono_class_get_name (klass));
46 name_space = mono_class_get_namespace (klass);
47 return g_strdup_printf ("%s%s%s", name_space,
48 name_space [0] ? "." : "", mono_class_get_name (klass));
52 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile)
54 const char *ptr, *start;
59 ptr = start = symfile->raw_contents;
64 ptr += sizeof(guint64);
65 if (magic != MONO_SYMBOL_FILE_MAGIC) {
66 g_warning ("Symbol file %s is not a mono symbol file", symfile->filename);
70 version = read32(ptr);
71 ptr += sizeof(guint32);
72 if (version != MONO_SYMBOL_FILE_VERSION) {
73 g_warning ("Symbol file %s has incorrect version "
74 "(expected %d, got %ld)", symfile->filename,
75 MONO_SYMBOL_FILE_VERSION, version);
79 guid = mono_guid_to_string ((const guint8 *) ptr);
82 if (strcmp (handle->image->guid, guid)) {
83 g_warning ("Symbol file %s doesn't match image %s", symfile->filename,
88 symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
90 symfile->method_hash = g_hash_table_new_full (
91 g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) free_method_info);
97 mono_debug_open_mono_symbol_file (MonoDebugHandle *handle, gboolean create_symfile)
99 MonoSymbolFile *symfile;
102 symfile = g_new0 (MonoSymbolFile, 1);
104 symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
106 if (!g_file_get_contents (symfile->filename, (gchar **) &symfile->raw_contents,
107 &symfile->raw_contents_size, NULL))
108 symfile->raw_contents = NULL;
110 if (load_symfile (handle, symfile)) {
111 mono_loader_unlock ();
113 } else if (!create_symfile) {
114 mono_debug_close_mono_symbol_file (symfile);
115 mono_loader_unlock ();
119 mono_loader_unlock ();
124 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
130 if (symfile->method_hash)
131 g_hash_table_destroy (symfile->method_hash);
134 mono_loader_unlock ();
138 read_leb128 (const char *ptr, const char **rptr)
147 ret = ret | ((b & 0x7f) << shift);
149 } while ((b & 0x80) == 0x80);
158 read_string (const char *ptr)
160 int len = read_leb128 (ptr, &ptr);
161 return g_filename_from_utf8 (ptr, len, NULL, NULL, NULL);
165 mono_debug_find_source_location (MonoSymbolFile *symfile, MonoMethod *method, guint32 offset,
166 guint32 *line_number)
168 MonoSymbolFileLineNumberEntry *lne;
169 MonoDebugMethodInfo *minfo;
170 gchar *source_file = NULL;
175 if (!symfile->method_hash) {
176 mono_loader_unlock ();
180 minfo = g_hash_table_lookup (symfile->method_hash, method);
182 mono_loader_unlock ();
186 if (read32(&(minfo->entry->_source_index))) {
187 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
188 (read32(&(minfo->entry->_source_index)) - 1) * sizeof (MonoSymbolFileSourceEntry);
189 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *) (symfile->raw_contents + offset);
191 source_file = read_string (symfile->raw_contents + read32(&(se->_name_offset)));
194 ptr = symfile->raw_contents + read32(&(minfo->entry->_line_number_table_offset));
196 lne = (MonoSymbolFileLineNumberEntry *) ptr;
198 for (i = 0; i < read32(&(minfo->entry->_num_line_numbers)); i++, lne++) {
199 if (read32(&(lne->_offset)) < offset)
203 *line_number = read32(&(lne->_row));
204 mono_loader_unlock ();
209 } else if (source_file) {
210 gchar *retval = g_strdup_printf ("%s:%d", source_file, read32(&(lne->_row)));
211 g_free (source_file);
212 mono_loader_unlock ();
215 gchar* retval = g_strdup_printf ("%d", read32(&(lne->_row)));
216 mono_loader_unlock ();
221 mono_loader_unlock ();
226 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, guint32 il_offset)
230 if (!jit || !jit->line_numbers)
233 for (i = jit->line_numbers->len - 1; i >= 0; i--) {
234 MonoDebugLineNumberEntry lne = g_array_index (
235 jit->line_numbers, MonoDebugLineNumberEntry, i);
237 if (lne.offset <= il_offset)
245 compare_method (const void *key, const void *object)
247 guint32 token = GPOINTER_TO_UINT (key);
248 MonoSymbolFileMethodIndexEntry *me = (MonoSymbolFileMethodIndexEntry*)object;
250 return token - read32(&(me->_token));
253 MonoDebugMethodInfo *
254 mono_debug_find_method (MonoDebugHandle *handle, MonoMethod *method)
256 MonoSymbolFileMethodEntry *me;
257 MonoSymbolFileMethodIndexEntry *first_ie, *ie;
258 MonoDebugMethodInfo *minfo;
259 MonoSymbolFile *symfile = handle->symfile;
261 if (!symfile->method_hash)
264 if (handle->image != mono_class_get_image (mono_method_get_class (method)))
268 first_ie = (MonoSymbolFileMethodIndexEntry *)
269 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
271 ie = bsearch (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
272 read32(&(symfile->offset_table->_method_count)),
273 sizeof (MonoSymbolFileMethodIndexEntry), compare_method);
276 mono_loader_unlock ();
280 me = (MonoSymbolFileMethodEntry *) (symfile->raw_contents + read32(&(ie->_file_offset)));
282 minfo = g_new0 (MonoDebugMethodInfo, 1);
283 minfo->index = (ie - first_ie) + 1;
284 minfo->method = method;
285 minfo->handle = handle;
286 minfo->num_il_offsets = read32(&(me->_num_line_numbers));
287 minfo->il_offsets = (MonoSymbolFileLineNumberEntry *)
288 (symfile->raw_contents + read32(&(me->_line_number_table_offset)));
291 g_hash_table_insert (symfile->method_hash, method, minfo);
293 mono_loader_unlock ();