2 * debug-mono-symfile.c:
5 * Mono Project (http://www.mono-project.com)
7 * Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
15 #ifdef HAVE_SYS_PARAM_H
16 #include <sys/param.h>
19 #include <mono/metadata/metadata.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/tokentype.h>
22 #include <mono/metadata/appdomain.h>
23 #include <mono/metadata/exception.h>
24 #include <mono/metadata/debug-helpers.h>
25 #include <mono/metadata/mono-debug.h>
26 #include <mono/metadata/debug-mono-symfile.h>
27 #include <mono/metadata/mono-debug-debugger.h>
28 #include <mono/metadata/mono-endian.h>
29 #include <mono/metadata/metadata-internals.h>
30 #include <mono/metadata/class-internals.h>
31 #include <mono/utils/mono-mmap.h>
38 #define RANGE_TABLE_CHUNK_SIZE 256
39 #define CLASS_TABLE_CHUNK_SIZE 256
40 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
41 #define TYPE_TABLE_CHUNK_SIZE 65536
44 free_method_info (MonoDebugMethodInfo *minfo)
50 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile, gboolean in_the_debugger)
52 const char *ptr, *start;
57 ptr = start = (const char*)symfile->raw_contents;
62 ptr += sizeof(guint64);
63 if (magic != MONO_SYMBOL_FILE_MAGIC) {
65 g_warning ("Symbol file %s is not a mono symbol file", symfile->filename);
70 ptr += sizeof(guint32);
72 ptr += sizeof(guint32);
75 * 50.0 is the frozen version for Mono 2.0.
77 * Nobody except me (Martin) is allowed to check the minor version.
79 if (major != MONO_SYMBOL_FILE_MAJOR_VERSION) {
81 g_warning ("Symbol file %s has incorrect version (expected %d.%d, got %d)",
82 symfile->filename, MONO_SYMBOL_FILE_MAJOR_VERSION,
83 MONO_SYMBOL_FILE_MINOR_VERSION, major);
87 guid = mono_guid_to_string ((const guint8 *) ptr);
90 if (strcmp (handle->image->guid, guid)) {
92 g_warning ("Symbol file %s doesn't match image %s", symfile->filename,
99 symfile->major_version = major;
100 symfile->minor_version = minor;
102 symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
104 symfile->method_hash = g_hash_table_new_full (
105 g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) free_method_info);
112 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const guint8 *raw_contents,
113 int size, gboolean in_the_debugger)
115 MonoSymbolFile *symfile;
117 mono_debugger_lock ();
118 symfile = g_new0 (MonoSymbolFile, 1);
120 if (raw_contents != NULL) {
122 symfile->raw_contents_size = size;
123 symfile->raw_contents = p = g_malloc (size);
124 memcpy (p, raw_contents, size);
125 symfile->filename = g_strdup_printf ("LoadedFromMemory");
128 symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
130 if ((f = mono_file_map_open (symfile->filename))) {
131 symfile->raw_contents_size = mono_file_map_size (f);
132 if (symfile->raw_contents_size == 0) {
133 if (!in_the_debugger)
134 g_warning ("stat of %s failed: %s",
135 symfile->filename, g_strerror (errno));
137 symfile->raw_contents = mono_file_map (symfile->raw_contents_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (f), 0, &symfile->raw_contents_handle);
140 mono_file_map_close (f);
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_file_unmap ((gpointer) symfile->raw_contents, symfile->raw_contents_handle);
170 if (symfile->filename)
171 g_free (symfile->filename);
173 mono_debugger_unlock ();
177 read_leb128 (const guint8 *ptr, const guint8 **rptr)
186 ret = ret | ((b & 0x7f) << shift);
188 } while ((b & 0x80) == 0x80);
197 read_string (const guint8 *ptr)
199 int len = read_leb128 (ptr, &ptr);
200 return g_filename_from_utf8 ((const char *) ptr, len, NULL, NULL, NULL);
204 MonoSymbolFile *symfile;
205 int line_base, line_range, max_address_incr;
207 guint32 last_line, last_file, last_offset;
208 guint32 line, file, offset;
212 check_line (StatementMachine *stm, int offset, MonoDebugSourceLocation **location)
214 gchar *source_file = NULL;
216 if ((offset > 0) && (stm->offset <= offset)) {
217 stm->last_offset = stm->offset;
218 stm->last_file = stm->file;
219 if (stm->line != 0xfeefee)
220 stm->last_line = stm->line;
224 if (stm->last_file) {
225 int offset = read32(&(stm->symfile->offset_table->_source_table_offset)) +
226 (stm->last_file - 1) * sizeof (MonoSymbolFileSourceEntry);
227 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
228 (stm->symfile->raw_contents + offset);
230 source_file = read_string (stm->symfile->raw_contents + read32(&(se->_data_offset)));
233 if (stm->last_line == 0) {
235 * The IL offset is less than the first IL offset which has a corresponding
242 *location = g_new0 (MonoDebugSourceLocation, 1);
243 (*location)->source_file = source_file;
244 (*location)->row = stm->last_line;
245 (*location)->il_offset = stm->last_offset;
250 * mono_debug_symfile_lookup_location:
251 * @minfo: A `MonoDebugMethodInfo' which can be retrieved by
252 * mono_debug_lookup_method().
253 * @offset: IL offset within the corresponding method's CIL code.
255 * This function is similar to mono_debug_lookup_location(), but we
256 * already looked up the method and also already did the
257 * `native address -> IL offset' mapping.
259 MonoDebugSourceLocation *
260 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, guint32 offset)
262 MonoDebugSourceLocation *location = NULL;
263 MonoSymbolFile *symfile;
264 const unsigned char *ptr;
265 StatementMachine stm;
267 #define DW_LNS_copy 1
268 #define DW_LNS_advance_pc 2
269 #define DW_LNS_advance_line 3
270 #define DW_LNS_set_file 4
271 #define DW_LNS_const_add_pc 8
273 #define DW_LNE_end_sequence 1
274 #define DW_LNE_MONO_negate_is_hidden 0x40
276 #define DW_LNE_MONO__extensions_start 0x40
277 #define DW_LNE_MONO__extensions_end 0x7f
279 if ((symfile = minfo->handle->symfile) == NULL)
282 stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
283 stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
284 stm.opcode_base = (guint8) read32 (&symfile->offset_table->_line_number_table_opcode_base);
285 stm.max_address_incr = (255 - stm.opcode_base) / stm.line_range;
287 mono_debugger_lock ();
289 ptr = symfile->raw_contents + minfo->lnt_offset;
291 stm.symfile = symfile;
292 stm.offset = stm.last_offset = 0;
299 guint8 opcode = *ptr++;
302 guint8 size = *ptr++;
303 const unsigned char *end_ptr = ptr + size;
307 if (opcode == DW_LNE_end_sequence) {
308 if (check_line (&stm, -1, &location))
311 } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
313 } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
314 (opcode <= DW_LNE_MONO__extensions_end)) {
315 ; // reserved for future extensions
317 g_warning ("Unknown extended opcode %x in LNT", opcode);
322 } else if (opcode < stm.opcode_base) {
325 if (check_line (&stm, offset, &location))
328 case DW_LNS_advance_pc:
329 stm.offset += read_leb128 (ptr, &ptr);
331 case DW_LNS_advance_line:
332 stm.line += read_leb128 (ptr, &ptr);
334 case DW_LNS_set_file:
335 stm.file = read_leb128 (ptr, &ptr);
337 case DW_LNS_const_add_pc:
338 stm.offset += stm.max_address_incr;
341 g_warning ("Unknown standard opcode %x in LNT", opcode);
345 opcode -= stm.opcode_base;
347 stm.offset += opcode / stm.line_range;
348 stm.line += stm.line_base + (opcode % stm.line_range);
350 if (check_line (&stm, offset, &location))
356 mono_debugger_unlock ();
360 mono_debugger_unlock ();
365 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, guint32 il_offset)
369 if (!jit || !jit->line_numbers)
372 for (i = jit->num_line_numbers - 1; i >= 0; i--) {
373 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
375 if (lne.il_offset < 0)
377 if (lne.il_offset <= il_offset)
378 return lne.native_offset;
385 compare_method (const void *key, const void *object)
387 guint32 token = GPOINTER_TO_UINT (key);
388 MonoSymbolFileMethodEntry *me = (MonoSymbolFileMethodEntry*)object;
390 return token - read32(&(me->_token));
393 MonoDebugMethodInfo *
394 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
396 MonoSymbolFileMethodEntry *first_ie, *ie;
397 MonoDebugMethodInfo *minfo;
398 MonoSymbolFile *symfile = handle->symfile;
400 if (!symfile->method_hash)
403 if (handle->image != mono_class_get_image (mono_method_get_class (method)))
406 mono_debugger_lock ();
408 minfo = g_hash_table_lookup (symfile->method_hash, method);
410 mono_debugger_unlock ();
414 first_ie = (MonoSymbolFileMethodEntry *)
415 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
417 ie = bsearch (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
418 read32(&(symfile->offset_table->_method_count)),
419 sizeof (MonoSymbolFileMethodEntry), compare_method);
422 mono_debugger_unlock ();
426 minfo = g_new0 (MonoDebugMethodInfo, 1);
427 minfo->index = (ie - first_ie) + 1;
428 minfo->method = method;
429 minfo->handle = handle;
431 minfo->data_offset = read32 (&(ie->_data_offset));
432 minfo->lnt_offset = read32 (&(ie->_line_number_table));
434 g_hash_table_insert (symfile->method_hash, method, minfo);
436 mono_debugger_unlock ();
441 * mono_debug_symfile_lookup_locals:
443 * Return information about the local variables of MINFO from the symbol file.
444 * NAMES and INDEXES are set to g_malloc-ed arrays containing the local names and
446 * Returns: the number of elements placed into the arrays, or -1 if there is no
447 * local variable info.
450 mono_debug_symfile_lookup_locals (MonoDebugMethodInfo *minfo, char ***names, int **indexes)
452 MonoSymbolFile *symfile = minfo->handle->symfile;
454 int i, len, compile_unit_index, locals_offset, num_locals, index, block_index;
462 p = symfile->raw_contents + minfo->data_offset;
464 compile_unit_index = read_leb128 (p, &p);
465 locals_offset = read_leb128 (p, &p);
467 p = symfile->raw_contents + locals_offset;
468 num_locals = read_leb128 (p, &p);
470 *names = g_new0 (char*, num_locals);
471 *indexes = g_new0 (int, num_locals);
473 for (i = 0; i < num_locals; ++i) {
474 index = read_leb128 (p, &p);
475 (*indexes) [i] = index;
476 len = read_leb128 (p, &p);
477 (*names) [i] = g_malloc (len + 1);
478 memcpy ((*names) [i], p, len);
479 (*names) [i][len] = '\0';
481 block_index = read_leb128 (p, &p);