9 #include <mono/metadata/metadata.h>
10 #include <mono/metadata/tabledefs.h>
11 #include <mono/metadata/rawbuffer.h>
12 #include <mono/metadata/tokentype.h>
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/exception.h>
15 #include <mono/metadata/debug-helpers.h>
16 #include <mono/metadata/mono-debug.h>
17 #include <mono/metadata/debug-mono-symfile.h>
18 #include <mono/metadata/mono-debug-debugger.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/metadata/metadata-internals.h>
21 #include <mono/metadata/class-internals.h>
26 #define RANGE_TABLE_CHUNK_SIZE 256
27 #define CLASS_TABLE_CHUNK_SIZE 256
28 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
29 #define TYPE_TABLE_CHUNK_SIZE 65536
32 free_method_info (MonoDebugMethodInfo *minfo)
38 get_class_name (MonoClass *klass)
40 MonoClass *nested_in = mono_class_get_nesting_type (klass);
41 const char *name_space;
43 gchar *parent_name = get_class_name (nested_in);
44 gchar *name = g_strdup_printf ("%s.%s", parent_name, mono_class_get_name (klass));
49 name_space = mono_class_get_namespace (klass);
50 return g_strdup_printf ("%s%s%s", name_space,
51 name_space [0] ? "." : "", mono_class_get_name (klass));
55 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile, gboolean in_the_debugger)
57 const char *ptr, *start;
62 ptr = start = symfile->raw_contents;
67 ptr += sizeof(guint64);
68 if (magic != MONO_SYMBOL_FILE_MAGIC) {
70 g_warning ("Symbol file %s is not a mono symbol file", symfile->filename);
74 version = read32(ptr);
75 ptr += sizeof(guint32);
76 if (version != MONO_SYMBOL_FILE_VERSION) {
78 g_warning ("Symbol file %s has incorrect version "
79 "(expected %d, got %ld)", symfile->filename,
80 MONO_SYMBOL_FILE_VERSION, version);
84 guid = mono_guid_to_string ((const guint8 *) ptr);
87 if (strcmp (handle->image->guid, guid)) {
89 g_warning ("Symbol file %s doesn't match image %s", symfile->filename,
96 symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
98 symfile->method_hash = g_hash_table_new_full (
99 g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) free_method_info);
106 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const guint8 *raw_contents,
107 int size, gboolean in_the_debugger)
109 MonoSymbolFile *symfile;
112 mono_debugger_lock ();
113 symfile = g_new0 (MonoSymbolFile, 1);
115 if (raw_contents != NULL) {
116 symfile->raw_contents_size = size;
117 symfile->raw_contents = g_malloc (size);
118 memcpy(symfile->raw_contents, raw_contents, size);
119 symfile->filename = g_strdup_printf ("LoadedFromMemory");
121 symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
123 if ((f = fopen (symfile->filename, "rb"))) {
124 struct stat stat_buf;
126 if (fstat (fileno (f), &stat_buf) < 0) {
127 if (!in_the_debugger)
128 g_warning ("stat of %s failed: %s",
129 symfile->filename, g_strerror (errno));
131 symfile->raw_contents_size = stat_buf.st_size;
132 symfile->raw_contents = mono_raw_buffer_load (fileno (f), FALSE, 0, stat_buf.st_size);
139 if (load_symfile (handle, symfile, in_the_debugger)) {
140 mono_debugger_unlock ();
142 } else if (!in_the_debugger) {
143 mono_debug_close_mono_symbol_file (symfile);
144 mono_debugger_unlock ();
148 mono_debugger_unlock ();
153 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
158 mono_debugger_lock ();
159 if (symfile->method_hash)
160 g_hash_table_destroy (symfile->method_hash);
162 if (symfile->raw_contents)
163 mono_raw_buffer_free ((gpointer) symfile->raw_contents);
165 if (symfile->filename)
166 g_free (symfile->filename);
168 mono_debugger_unlock ();
172 read_leb128 (const char *ptr, const char **rptr)
181 ret = ret | ((b & 0x7f) << shift);
183 } while ((b & 0x80) == 0x80);
192 read_string (const char *ptr)
194 int len = read_leb128 (ptr, &ptr);
195 return g_filename_from_utf8 (ptr, len, NULL, NULL, NULL);
199 * mono_debug_symfile_lookup_location:
200 * @minfo: A `MonoDebugMethodInfo' which can be retrieved by
201 * mono_debug_lookup_method().
202 * @offset: IL offset within the corresponding method's CIL code.
204 * This function is similar to mono_debug_lookup_location(), but we
205 * already looked up the method and also already did the
206 * `native address -> IL offset' mapping.
208 MonoDebugSourceLocation *
209 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, guint32 offset)
211 MonoSymbolFileLineNumberEntry *lne;
212 MonoSymbolFile *symfile;
213 gchar *source_file = NULL;
217 if ((symfile = minfo->handle->symfile) == NULL)
220 mono_debugger_lock ();
222 if (read32(&(minfo->entry->_source_index))) {
223 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
224 (read32(&(minfo->entry->_source_index)) - 1) * sizeof (MonoSymbolFileSourceEntry);
225 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *) (symfile->raw_contents + offset);
227 source_file = read_string (symfile->raw_contents + read32(&(se->_name_offset)));
230 ptr = symfile->raw_contents + read32(&(minfo->entry->_line_number_table_offset));
232 count = read32(&(minfo->entry->_num_line_numbers));
233 lne = ((MonoSymbolFileLineNumberEntry *) ptr) + count - 1;
235 for (i = count - 1; i >= 0; i--, lne--) {
236 MonoDebugSourceLocation *location;
238 if (read32(&(lne->_offset)) > offset)
241 location = g_new0 (MonoDebugSourceLocation, 1);
242 location->source_file = source_file;
243 location->row = read32(&(lne->_row));
244 location->il_offset = read32(&(lne->_offset));
246 mono_debugger_unlock ();
250 mono_debugger_unlock ();
255 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, guint32 il_offset)
259 if (!jit || !jit->line_numbers)
262 for (i = jit->num_line_numbers - 1; i >= 0; i--) {
263 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
265 if (lne.il_offset <= il_offset)
266 return lne.native_offset;
273 compare_method (const void *key, const void *object)
275 guint32 token = GPOINTER_TO_UINT (key);
276 MonoSymbolFileMethodIndexEntry *me = (MonoSymbolFileMethodIndexEntry*)object;
278 return token - read32(&(me->_token));
281 MonoDebugMethodInfo *
282 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
284 MonoSymbolFileMethodEntry *me;
285 MonoSymbolFileMethodIndexEntry *first_ie, *ie;
286 MonoDebugMethodInfo *minfo;
287 MonoSymbolFile *symfile = handle->symfile;
289 if (!symfile->method_hash)
292 if (handle->image != mono_class_get_image (mono_method_get_class (method)))
295 mono_debugger_lock ();
296 first_ie = (MonoSymbolFileMethodIndexEntry *)
297 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
299 ie = bsearch (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
300 read32(&(symfile->offset_table->_method_count)),
301 sizeof (MonoSymbolFileMethodIndexEntry), compare_method);
304 mono_debugger_unlock ();
308 me = (MonoSymbolFileMethodEntry *) (symfile->raw_contents + read32(&(ie->_file_offset)));
310 minfo = g_new0 (MonoDebugMethodInfo, 1);
311 minfo->index = (ie - first_ie) + 1;
312 minfo->method = method;
313 minfo->handle = handle;
314 minfo->num_il_offsets = read32(&(me->_num_line_numbers));
315 minfo->il_offsets = (MonoSymbolFileLineNumberEntry *)
316 (symfile->raw_contents + read32(&(me->_line_number_table_offset)));
317 minfo->num_lexical_blocks = read32(&(me->_num_lexical_blocks));
318 minfo->lexical_blocks = (MonoSymbolFileLexicalBlockEntry *)
319 (symfile->raw_contents + read32(&(me->_lexical_block_table_offset)));
322 g_hash_table_insert (symfile->method_hash, method, minfo);
324 mono_debugger_unlock ();