7 #ifdef HAVE_SYS_PARAM_H
11 #include <mono/metadata/metadata.h>
12 #include <mono/metadata/tabledefs.h>
13 #include <mono/metadata/rawbuffer.h>
14 #include <mono/metadata/tokentype.h>
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/exception.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/mono-debug.h>
19 #include <mono/metadata/debug-mono-symfile.h>
20 #include <mono/metadata/mono-debug-debugger.h>
21 #include <mono/metadata/mono-endian.h>
22 #include <mono/metadata/metadata-internals.h>
23 #include <mono/metadata/class-internals.h>
30 #define RANGE_TABLE_CHUNK_SIZE 256
31 #define CLASS_TABLE_CHUNK_SIZE 256
32 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
33 #define TYPE_TABLE_CHUNK_SIZE 65536
36 free_method_info (MonoDebugMethodInfo *minfo)
42 get_class_name (MonoClass *klass)
44 MonoClass *nested_in = mono_class_get_nesting_type (klass);
45 const char *name_space;
47 gchar *parent_name = get_class_name (nested_in);
48 gchar *name = g_strdup_printf ("%s.%s", parent_name, mono_class_get_name (klass));
53 name_space = mono_class_get_namespace (klass);
54 return g_strdup_printf ("%s%s%s", name_space,
55 name_space [0] ? "." : "", mono_class_get_name (klass));
59 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile, gboolean in_the_debugger)
61 const char *ptr, *start;
66 ptr = start = (const char*)symfile->raw_contents;
71 ptr += sizeof(guint64);
72 if (magic != MONO_SYMBOL_FILE_MAGIC) {
74 g_warning ("Symbol file %s is not a mono symbol file", symfile->filename);
78 version = read32(ptr);
79 ptr += sizeof(guint32);
80 if (version != MONO_SYMBOL_FILE_VERSION) {
82 g_warning ("Symbol file %s has incorrect version "
83 "(expected %d, got %ld)", symfile->filename,
84 MONO_SYMBOL_FILE_VERSION, version);
88 guid = mono_guid_to_string ((const guint8 *) ptr);
91 if (strcmp (handle->image->guid, guid)) {
93 g_warning ("Symbol file %s doesn't match image %s", symfile->filename,
100 symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
102 symfile->method_hash = g_hash_table_new_full (
103 g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) free_method_info);
110 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const guint8 *raw_contents,
111 int size, gboolean in_the_debugger)
113 MonoSymbolFile *symfile;
116 mono_debugger_lock ();
117 symfile = g_new0 (MonoSymbolFile, 1);
119 if (raw_contents != NULL) {
121 symfile->raw_contents_size = size;
122 symfile->raw_contents = p = g_malloc (size);
123 memcpy (p, raw_contents, size);
124 symfile->filename = g_strdup_printf ("LoadedFromMemory");
126 symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
128 if ((f = fopen (symfile->filename, "rb"))) {
129 struct stat stat_buf;
131 if (fstat (fileno (f), &stat_buf) < 0) {
132 if (!in_the_debugger)
133 g_warning ("stat of %s failed: %s",
134 symfile->filename, g_strerror (errno));
136 symfile->raw_contents_size = stat_buf.st_size;
137 symfile->raw_contents = mono_raw_buffer_load (fileno (f), FALSE, 0, stat_buf.st_size);
144 if (load_symfile (handle, symfile, in_the_debugger)) {
145 mono_debugger_unlock ();
147 } else if (!in_the_debugger) {
148 mono_debug_close_mono_symbol_file (symfile);
149 mono_debugger_unlock ();
153 mono_debugger_unlock ();
158 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
163 mono_debugger_lock ();
164 if (symfile->method_hash)
165 g_hash_table_destroy (symfile->method_hash);
167 if (symfile->raw_contents)
168 mono_raw_buffer_free ((gpointer) symfile->raw_contents);
170 if (symfile->filename)
171 g_free (symfile->filename);
173 mono_debugger_unlock ();
177 read_leb128 (const char *ptr, const char **rptr)
186 ret = ret | ((b & 0x7f) << shift);
188 } while ((b & 0x80) == 0x80);
197 read_string (const char *ptr)
199 int len = read_leb128 (ptr, &ptr);
200 return g_filename_from_utf8 (ptr, len, NULL, NULL, NULL);
204 * mono_debug_symfile_lookup_location:
205 * @minfo: A `MonoDebugMethodInfo' which can be retrieved by
206 * mono_debug_lookup_method().
207 * @offset: IL offset within the corresponding method's CIL code.
209 * This function is similar to mono_debug_lookup_location(), but we
210 * already looked up the method and also already did the
211 * `native address -> IL offset' mapping.
213 MonoDebugSourceLocation *
214 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, guint32 offset)
216 MonoSymbolFileLineNumberEntry *lne;
217 MonoSymbolFile *symfile;
218 gchar *source_file = NULL;
219 const unsigned char *ptr;
222 if ((symfile = minfo->handle->symfile) == NULL)
225 mono_debugger_lock ();
227 if (read32(&(minfo->entry->_source_index))) {
228 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
229 (read32(&(minfo->entry->_source_index)) - 1) * sizeof (MonoSymbolFileSourceEntry);
230 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *) (symfile->raw_contents + offset);
232 source_file = read_string ((const char*)symfile->raw_contents + read32(&(se->_name_offset)));
235 ptr = symfile->raw_contents + read32(&(minfo->entry->_line_number_table_offset));
237 count = read32(&(minfo->entry->_num_line_numbers));
238 lne = ((MonoSymbolFileLineNumberEntry *) ptr) + count - 1;
240 for (i = count - 1; i >= 0; i--, lne--) {
241 MonoDebugSourceLocation *location;
243 if (read32(&(lne->_offset)) > offset)
246 location = g_new0 (MonoDebugSourceLocation, 1);
247 location->source_file = source_file;
248 location->row = read32(&(lne->_row));
249 location->il_offset = read32(&(lne->_offset));
251 mono_debugger_unlock ();
255 mono_debugger_unlock ();
260 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, guint32 il_offset)
264 if (!jit || !jit->line_numbers)
267 for (i = jit->num_line_numbers - 1; i >= 0; i--) {
268 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
270 if (lne.il_offset <= il_offset)
271 return lne.native_offset;
278 compare_method (const void *key, const void *object)
280 guint32 token = GPOINTER_TO_UINT (key);
281 MonoSymbolFileMethodIndexEntry *me = (MonoSymbolFileMethodIndexEntry*)object;
283 return token - read32(&(me->_token));
286 MonoDebugMethodInfo *
287 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
289 MonoSymbolFileMethodEntry *me;
290 MonoSymbolFileMethodIndexEntry *first_ie, *ie;
291 MonoDebugMethodInfo *minfo;
292 MonoSymbolFile *symfile = handle->symfile;
294 if (!symfile->method_hash)
297 if (handle->image != mono_class_get_image (mono_method_get_class (method)))
300 mono_debugger_lock ();
301 first_ie = (MonoSymbolFileMethodIndexEntry *)
302 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
304 ie = bsearch (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
305 read32(&(symfile->offset_table->_method_count)),
306 sizeof (MonoSymbolFileMethodIndexEntry), compare_method);
309 mono_debugger_unlock ();
313 me = (MonoSymbolFileMethodEntry *) (symfile->raw_contents + read32(&(ie->_file_offset)));
315 minfo = g_new0 (MonoDebugMethodInfo, 1);
316 minfo->index = (ie - first_ie) + 1;
317 minfo->method = method;
318 minfo->handle = handle;
319 minfo->num_il_offsets = read32(&(me->_num_line_numbers));
320 minfo->il_offsets = (MonoSymbolFileLineNumberEntry *)
321 (symfile->raw_contents + read32(&(me->_line_number_table_offset)));
322 minfo->num_lexical_blocks = read32(&(me->_num_lexical_blocks));
323 minfo->lexical_blocks = (MonoSymbolFileLexicalBlockEntry *)
324 (symfile->raw_contents + read32(&(me->_lexical_block_table_offset)));
327 g_hash_table_insert (symfile->method_hash, method, minfo);
329 mono_debugger_unlock ();