[mini] Always emit safepoints, except WASM
[mono-project.git] / mono / metadata / debug-mono-symfile.c
blobd8eed685da2b57b3237185fa2da52b3f28333a5c
1 /**
2 * \file
4 * Support for reading debug info from .mdb files.
6 * Author:
7 * Mono Project (http://www.mono-project.com)
9 * Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
10 * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include <config.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <errno.h>
18 #include <string.h>
19 #ifdef HAVE_SYS_PARAM_H
20 #include <sys/param.h>
21 #endif
22 #include <sys/stat.h>
23 #include <mono/metadata/metadata.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/metadata/tokentype.h>
26 #include <mono/metadata/appdomain.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/debug-helpers.h>
29 #include <mono/metadata/mono-debug.h>
30 #include <mono/metadata/debug-mono-symfile.h>
31 #include <mono/metadata/mono-endian.h>
32 #include <mono/metadata/metadata-internals.h>
33 #include <mono/metadata/class-internals.h>
34 #include <mono/utils/mono-mmap.h>
35 #include <mono/utils/bsearch.h>
37 #ifndef DISABLE_MDB
39 #include <fcntl.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
44 #define RANGE_TABLE_CHUNK_SIZE 256
45 #define CLASS_TABLE_CHUNK_SIZE 256
46 #define TYPE_TABLE_PTR_CHUNK_SIZE 256
47 #define TYPE_TABLE_CHUNK_SIZE 65536
49 struct _MonoSymbolFile {
50 const uint8_t *raw_contents;
51 int raw_contents_size;
52 void *raw_contents_handle;
53 int major_version;
54 int minor_version;
55 char *filename;
56 GHashTable *method_hash;
57 GHashTable *source_hash;
58 MonoSymbolFileOffsetTable *offset_table;
59 gboolean was_loaded_from_memory;
62 static void
63 free_method_info (MonoDebugMethodInfo *minfo)
65 g_free (minfo);
68 static void
69 free_source_info (MonoDebugSourceInfo *sinfo)
71 g_free (sinfo->source_file);
72 g_free (sinfo->guid);
73 g_free (sinfo->hash);
74 g_free (sinfo);
77 static int
78 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile, mono_bool in_the_debugger)
80 const char *ptr, *start;
81 gchar *guid;
82 uint64_t magic;
83 int minor, major;
85 ptr = start = (const char*)symfile->raw_contents;
86 if (!ptr)
87 return FALSE;
89 magic = read64(ptr);
90 ptr += sizeof(uint64_t);
91 if (magic != MONO_SYMBOL_FILE_MAGIC) {
92 if (!in_the_debugger)
93 g_warning ("Symbol file %s is not a mono symbol file", symfile->filename);
94 return FALSE;
97 major = read32(ptr);
98 ptr += sizeof(uint32_t);
99 minor = read32(ptr);
100 ptr += sizeof(uint32_t);
103 * 50.0 is the frozen version for Mono 2.0.
105 * Nobody except me (Martin) is allowed to check the minor version.
107 if (major != MONO_SYMBOL_FILE_MAJOR_VERSION) {
108 if (!in_the_debugger)
109 g_warning ("Symbol file %s has incorrect version (expected %d.%d, got %d)",
110 symfile->filename, MONO_SYMBOL_FILE_MAJOR_VERSION,
111 MONO_SYMBOL_FILE_MINOR_VERSION, major);
112 return FALSE;
115 guid = mono_guid_to_string ((const uint8_t *) ptr);
116 ptr += 16;
118 if (strcmp (handle->image->guid, guid)) {
119 if (!in_the_debugger)
120 g_warning ("Symbol file %s doesn't match image %s", symfile->filename,
121 handle->image->name);
122 if (guid)
123 g_free (guid);
124 return FALSE;
127 symfile->major_version = major;
128 symfile->minor_version = minor;
130 symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
132 symfile->method_hash = g_hash_table_new_full (
133 NULL, NULL, NULL, (GDestroyNotify) free_method_info);
135 symfile->source_hash = g_hash_table_new_full (
136 NULL, NULL, NULL, (GDestroyNotify) free_source_info);
138 g_free (guid);
139 return TRUE;
143 * mono_debug_open_mono_symbols:
145 MonoSymbolFile *
146 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const uint8_t *raw_contents,
147 int size, gboolean in_the_debugger)
149 MonoSymbolFile *symfile;
151 mono_debugger_lock ();
152 symfile = g_new0 (MonoSymbolFile, 1);
154 if (raw_contents != NULL) {
155 unsigned char *p;
156 symfile->raw_contents_size = size;
157 symfile->raw_contents = p = (unsigned char *)g_malloc (size);
158 memcpy (p, raw_contents, size);
159 symfile->filename = g_strdup_printf ("LoadedFromMemory");
160 symfile->was_loaded_from_memory = TRUE;
161 } else {
162 MonoFileMap *f;
164 symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
165 symfile->was_loaded_from_memory = FALSE;
166 if ((f = mono_file_map_open (symfile->filename))) {
167 symfile->raw_contents_size = mono_file_map_size (f);
168 if (symfile->raw_contents_size == 0) {
169 if (!in_the_debugger)
170 g_warning ("stat of %s failed: %s",
171 symfile->filename, g_strerror (errno));
172 } else {
173 symfile->raw_contents = (const unsigned char *)mono_file_map (symfile->raw_contents_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (f), 0, &symfile->raw_contents_handle);
176 mono_file_map_close (f);
180 if (load_symfile (handle, symfile, in_the_debugger)) {
181 mono_debugger_unlock ();
182 return symfile;
183 } else if (!in_the_debugger) {
184 mono_debug_close_mono_symbol_file (symfile);
185 mono_debugger_unlock ();
186 return NULL;
189 mono_debugger_unlock ();
190 return symfile;
194 * mono_debug_close_mono_symbol_file:
196 void
197 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
199 if (!symfile)
200 return;
202 mono_debugger_lock ();
203 if (symfile->method_hash)
204 g_hash_table_destroy (symfile->method_hash);
206 if (symfile->raw_contents) {
207 if (symfile->was_loaded_from_memory)
208 g_free ((gpointer)symfile->raw_contents);
209 else
210 mono_file_unmap ((gpointer) symfile->raw_contents, symfile->raw_contents_handle);
213 g_free (symfile->filename);
214 g_free (symfile);
215 mono_debugger_unlock ();
219 * mono_debug_symfile_is_loaded:
221 mono_bool
222 mono_debug_symfile_is_loaded (MonoSymbolFile *symfile)
224 return symfile && symfile->offset_table;
227 static int
228 read_leb128 (const uint8_t *ptr, const uint8_t **rptr)
230 int ret = 0;
231 int shift = 0;
232 char b;
234 do {
235 b = *ptr++;
237 ret = ret | ((b & 0x7f) << shift);
238 shift += 7;
239 } while ((b & 0x80) == 0x80);
241 if (rptr)
242 *rptr = ptr;
244 return ret;
247 static gchar *
248 read_string (const uint8_t *ptr, const uint8_t **endp)
250 gchar *s;
251 int len = read_leb128 (ptr, &ptr);
253 s = g_filename_from_utf8 ((const char *) ptr, len, NULL, NULL, NULL);
254 ptr += len;
255 if (endp)
256 *endp = ptr;
257 return s;
260 typedef struct {
261 MonoSymbolFile *symfile;
262 int line_base, line_range, max_address_incr;
263 uint8_t opcode_base;
264 uint32_t last_line, last_file, last_offset;
265 uint32_t first_file;
266 int line, file, offset;
267 gboolean is_hidden;
268 } StatementMachine;
270 static gboolean
271 check_line (StatementMachine *stm, int offset, MonoDebugSourceLocation **location)
273 gchar *source_file = NULL;
275 if (stm->offset <= offset) {
276 stm->last_offset = stm->offset;
277 stm->last_file = stm->file;
278 if (stm->line != 0xfeefee)
279 stm->last_line = stm->line;
280 return FALSE;
283 if (stm->last_file) {
284 int offset = read32(&(stm->symfile->offset_table->_source_table_offset)) +
285 (stm->last_file - 1) * sizeof (MonoSymbolFileSourceEntry);
286 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
287 (stm->symfile->raw_contents + offset);
289 source_file = read_string (stm->symfile->raw_contents + read32(&(se->_data_offset)), NULL);
292 if (stm->last_line == 0) {
294 * The IL offset is less than the first IL offset which has a corresponding
295 * source line.
297 *location = NULL;
298 return TRUE;
301 *location = g_new0 (MonoDebugSourceLocation, 1);
302 (*location)->source_file = source_file;
303 (*location)->row = stm->last_line;
304 (*location)->il_offset = stm->last_offset;
305 return TRUE;
309 * mono_debug_symfile_lookup_location:
310 * \param minfo A \c MonoDebugMethodInfo which can be retrieved by \c mono_debug_lookup_method.
311 * \param offset IL offset within the corresponding method's CIL code.
313 * This function is similar to \c mono_debug_lookup_location, but we
314 * already looked up the method and also already did the
315 * native address -> IL offset mapping.
317 MonoDebugSourceLocation *
318 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, uint32_t offset)
320 MonoDebugSourceLocation *location = NULL;
321 MonoSymbolFile *symfile;
322 const unsigned char *ptr;
323 StatementMachine stm;
325 #define DW_LNS_copy 1
326 #define DW_LNS_advance_pc 2
327 #define DW_LNS_advance_line 3
328 #define DW_LNS_set_file 4
329 #define DW_LNS_const_add_pc 8
331 #define DW_LNE_end_sequence 1
332 #define DW_LNE_MONO_negate_is_hidden 0x40
334 #define DW_LNE_MONO__extensions_start 0x40
335 #define DW_LNE_MONO__extensions_end 0x7f
337 if ((symfile = minfo->handle->symfile) == NULL)
338 return NULL;
340 stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
341 stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
342 stm.opcode_base = (uint8_t) read32 (&symfile->offset_table->_line_number_table_opcode_base);
343 stm.max_address_incr = (255 - stm.opcode_base) / stm.line_range;
345 mono_debugger_lock ();
347 ptr = symfile->raw_contents + minfo->lnt_offset;
349 stm.symfile = symfile;
350 stm.offset = stm.last_offset = 0;
351 stm.last_file = 0;
352 stm.last_line = 0;
353 stm.first_file = 0;
354 stm.file = 1;
355 stm.line = 1;
356 stm.is_hidden = FALSE;
358 while (TRUE) {
359 uint8_t opcode = *ptr++;
361 if (opcode == 0) {
362 uint8_t size = *ptr++;
363 const unsigned char *end_ptr = ptr + size;
365 opcode = *ptr++;
367 if (opcode == DW_LNE_end_sequence) {
368 if (check_line (&stm, -1, &location))
369 goto out_success;
370 break;
371 } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
372 stm.is_hidden = !stm.is_hidden;
373 } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
374 (opcode <= DW_LNE_MONO__extensions_end)) {
375 ; // reserved for future extensions
376 } else {
377 g_warning ("Unknown extended opcode %x in LNT", opcode);
380 ptr = end_ptr;
381 continue;
382 } else if (opcode < stm.opcode_base) {
383 switch (opcode) {
384 case DW_LNS_copy:
385 if (check_line (&stm, offset, &location))
386 goto out_success;
387 break;
388 case DW_LNS_advance_pc:
389 stm.offset += read_leb128 (ptr, &ptr);
390 break;
391 case DW_LNS_advance_line:
392 stm.line += read_leb128 (ptr, &ptr);
393 break;
394 case DW_LNS_set_file:
395 stm.file = read_leb128 (ptr, &ptr);
396 break;
397 case DW_LNS_const_add_pc:
398 stm.offset += stm.max_address_incr;
399 break;
400 default:
401 g_warning ("Unknown standard opcode %x in LNT", opcode);
402 goto error_out;
404 } else {
405 opcode -= stm.opcode_base;
407 stm.offset += opcode / stm.line_range;
408 stm.line += stm.line_base + (opcode % stm.line_range);
410 if (check_line (&stm, offset, &location))
411 goto out_success;
415 error_out:
416 mono_debugger_unlock ();
417 return NULL;
419 out_success:
420 mono_debugger_unlock ();
421 return location;
424 static void
425 add_line (StatementMachine *stm, GPtrArray *il_offset_array, GPtrArray *line_number_array, GPtrArray *source_file_array, GPtrArray *hidden_array)
427 g_ptr_array_add (il_offset_array, GUINT_TO_POINTER (stm->offset));
428 g_ptr_array_add (line_number_array, GUINT_TO_POINTER (stm->line));
429 g_ptr_array_add (source_file_array, GUINT_TO_POINTER (stm->file));
430 g_ptr_array_add (hidden_array, GUINT_TO_POINTER (stm->is_hidden || stm->line <= 0));
432 if (!stm->is_hidden && !stm->first_file)
433 stm->first_file = stm->file;
437 * mono_debug_symfile_free_location:
439 * Free a \c MonoDebugSourceLocation returned by
440 * \c mono_debug_symfile_lookup_location
442 void
443 mono_debug_symfile_free_location (MonoDebugSourceLocation *location)
445 g_free (location->source_file);
446 g_free (location);
450 * LOCKING: Assumes the debugger lock is held.
452 static MonoDebugSourceInfo*
453 get_source_info (MonoSymbolFile *symfile, int index)
455 MonoDebugSourceInfo *info;
457 info = (MonoDebugSourceInfo *)g_hash_table_lookup (symfile->source_hash, GUINT_TO_POINTER (index));
458 if (!info) {
459 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
460 (index - 1) * sizeof (MonoSymbolFileSourceEntry);
461 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
462 (symfile->raw_contents + offset);
463 const uint8_t *ptr = symfile->raw_contents + read32(&(se->_data_offset));
465 info = g_new0 (MonoDebugSourceInfo, 1);
466 info->source_file = read_string (ptr, &ptr);
467 info->guid = (guint8 *)g_malloc0 (16);
468 memcpy (info->guid, ptr, 16);
469 ptr += 16;
470 info->hash = (guint8 *)g_malloc0 (16);
471 memcpy (info->hash, ptr, 16);
472 ptr += 16;
473 g_hash_table_insert (symfile->source_hash, GUINT_TO_POINTER (index), info);
475 return info;
478 typedef enum {
479 LNT_FLAG_HAS_COLUMN_INFO = 1 << 1,
480 LNT_FLAG_HAS_END_INFO = 1 << 2,
481 } LineNumberTableFlags;
483 static LineNumberTableFlags
484 method_get_lnt_flags (MonoDebugMethodInfo *minfo)
486 MonoSymbolFile *symfile;
487 const unsigned char *ptr;
488 guint32 flags;
490 if ((symfile = minfo->handle->symfile) == NULL)
491 return (LineNumberTableFlags)0;
493 ptr = symfile->raw_contents + minfo->data_offset;
495 /* Has to read 'flags' which is preceeded by a bunch of other data */
496 /* compile_unit_index */
497 read_leb128 (ptr, &ptr);
498 /* local variable table offset */
499 read_leb128 (ptr, &ptr);
500 /* namespace id */
501 read_leb128 (ptr, &ptr);
502 /* code block table offset */
503 read_leb128 (ptr, &ptr);
504 /* scope variable table offset */
505 read_leb128 (ptr, &ptr);
506 /* real name offset */
507 read_leb128 (ptr, &ptr);
509 flags = read_leb128 (ptr, &ptr);
510 return (LineNumberTableFlags)flags;
514 * mono_debug_symfile_get_seq_points:
516 * On return, SOURCE_FILE_LIST will point to a GPtrArray of MonoDebugSourceFile
517 * structures, and SOURCE_FILES will contain indexes into this array.
518 * The MonoDebugSourceFile structures are owned by this module.
520 void
521 mono_debug_symfile_get_seq_points (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int **source_files, MonoSymSeqPoint **seq_points, int *n_seq_points)
523 // FIXME: Unify this with mono_debug_symfile_lookup_location
524 MonoSymbolFile *symfile;
525 const unsigned char *ptr;
526 StatementMachine stm;
527 uint32_t i, j, n;
528 LineNumberTableFlags flags;
529 GPtrArray *il_offset_array, *line_number_array, *source_file_array, *hidden_array;
530 gboolean has_column_info, has_end_info;
531 MonoSymSeqPoint *sps;
533 if (source_file_list)
534 *source_file_list = NULL;
535 if (seq_points)
536 *seq_points = NULL;
537 if (n_seq_points)
538 *n_seq_points = 0;
539 if (source_files)
540 *source_files = NULL;
541 if (source_file)
542 *source_file = NULL;
544 if ((symfile = minfo->handle->symfile) == NULL)
545 return;
547 flags = method_get_lnt_flags (minfo);
548 has_column_info = (flags & LNT_FLAG_HAS_COLUMN_INFO) > 0;
549 has_end_info = (flags & LNT_FLAG_HAS_END_INFO) > 0;
551 il_offset_array = g_ptr_array_new ();
552 line_number_array = g_ptr_array_new ();
553 source_file_array = g_ptr_array_new ();
554 hidden_array = g_ptr_array_new();
556 stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
557 stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
558 stm.opcode_base = (uint8_t) read32 (&symfile->offset_table->_line_number_table_opcode_base);
559 stm.max_address_incr = (255 - stm.opcode_base) / stm.line_range;
561 mono_debugger_lock ();
563 ptr = symfile->raw_contents + minfo->lnt_offset;
565 stm.symfile = symfile;
566 stm.offset = stm.last_offset = 0;
567 stm.last_file = 0;
568 stm.last_line = 0;
569 stm.first_file = 0;
570 stm.file = 1;
571 stm.line = 1;
572 stm.is_hidden = FALSE;
574 while (TRUE) {
575 uint8_t opcode = *ptr++;
577 if (opcode == 0) {
578 uint8_t size = *ptr++;
579 const unsigned char *end_ptr = ptr + size;
581 opcode = *ptr++;
583 if (opcode == DW_LNE_end_sequence) {
584 if (il_offset_array->len == 0)
585 /* Empty table */
586 break;
587 break;
588 } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
589 stm.is_hidden = !stm.is_hidden;
590 } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
591 (opcode <= DW_LNE_MONO__extensions_end)) {
592 ; // reserved for future extensions
593 } else {
594 g_warning ("Unknown extended opcode %x in LNT", opcode);
597 ptr = end_ptr;
598 continue;
599 } else if (opcode < stm.opcode_base) {
600 switch (opcode) {
601 case DW_LNS_copy:
602 add_line (&stm, il_offset_array, line_number_array, source_file_array, hidden_array);
603 break;
604 case DW_LNS_advance_pc:
605 stm.offset += read_leb128 (ptr, &ptr);
606 break;
607 case DW_LNS_advance_line:
608 stm.line += read_leb128 (ptr, &ptr);
609 break;
610 case DW_LNS_set_file:
611 stm.file = read_leb128 (ptr, &ptr);
612 break;
613 case DW_LNS_const_add_pc:
614 stm.offset += stm.max_address_incr;
615 break;
616 default:
617 g_warning ("Unknown standard opcode %x in LNT", opcode);
618 g_assert_not_reached ();
620 } else {
621 opcode -= stm.opcode_base;
623 stm.offset += opcode / stm.line_range;
624 stm.line += stm.line_base + (opcode % stm.line_range);
626 add_line (&stm, il_offset_array, line_number_array, source_file_array, hidden_array);
630 if (!stm.file && stm.first_file)
631 stm.file = stm.first_file;
633 if (stm.file && source_file) {
634 int offset = read32(&(stm.symfile->offset_table->_source_table_offset)) +
635 (stm.file - 1) * sizeof (MonoSymbolFileSourceEntry);
636 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
637 (stm.symfile->raw_contents + offset);
639 if (source_file)
640 *source_file = read_string (stm.symfile->raw_contents + read32(&(se->_data_offset)), NULL);
643 if (source_file_list) {
644 int file, last_file = 0;
646 *source_file_list = g_ptr_array_new ();
647 if (source_files)
648 *source_files = (int *)g_malloc (il_offset_array->len * sizeof (int));
650 for (i = 0; i < il_offset_array->len; ++i) {
651 file = GPOINTER_TO_UINT (g_ptr_array_index (source_file_array, i));
652 if (file && file != last_file) {
653 MonoDebugSourceInfo *info = get_source_info (symfile, file);
655 g_ptr_array_add (*source_file_list, info);
657 last_file = file;
658 if (source_files)
659 (*source_files) [i] = (*source_file_list)->len - 1;
663 if (n_seq_points) {
664 g_assert (seq_points);
666 n = il_offset_array->len;
667 for (i = 0; i < il_offset_array->len; i++) {
668 if (GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
669 n --;
673 *n_seq_points = n;
674 *seq_points = sps = g_new0 (MonoSymSeqPoint, n);
675 j = 0;
676 for (i = 0; i < il_offset_array->len; ++i) {
677 MonoSymSeqPoint *sp = &(sps [j]);
678 if (!GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
679 sp->il_offset = GPOINTER_TO_UINT (g_ptr_array_index (il_offset_array, i));
680 sp->line = GPOINTER_TO_UINT (g_ptr_array_index (line_number_array, i));
681 sp->column = -1;
682 sp->end_line = -1;
683 sp->end_column = -1;
684 j ++;
688 if (has_column_info) {
689 j = 0;
690 for (i = 0; i < il_offset_array->len; ++i) {
691 MonoSymSeqPoint *sp = &(sps [j]);
692 int column = read_leb128 (ptr, &ptr);
693 if (!GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
694 sp->column = column;
695 j++;
700 if (has_end_info) {
701 j = 0;
702 for (i = 0; i < il_offset_array->len; ++i) {
703 MonoSymSeqPoint *sp = &(sps [j]);
704 int end_row, end_column = -1;
706 end_row = read_leb128 (ptr, &ptr);
707 if (end_row != 0xffffff) {
708 end_row += GPOINTER_TO_UINT (g_ptr_array_index (line_number_array, i));
709 end_column = read_leb128 (ptr, &ptr);
710 if (!GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
711 sp->end_line = end_row;
712 sp->end_column = end_column;
713 j++;
720 g_ptr_array_free (il_offset_array, TRUE);
721 g_ptr_array_free (line_number_array, TRUE);
722 g_ptr_array_free (hidden_array, TRUE);
724 mono_debugger_unlock ();
725 return;
728 static int
729 compare_method (const void *key, const void *object)
731 uint32_t token = GPOINTER_TO_UINT (key);
732 MonoSymbolFileMethodEntry *me = (MonoSymbolFileMethodEntry*)object;
734 return token - read32(&(me->_token));
738 * mono_debug_symfile_lookup_method:
740 MonoDebugMethodInfo *
741 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
743 MonoSymbolFileMethodEntry *first_ie, *ie;
744 MonoDebugMethodInfo *minfo;
745 MonoSymbolFile *symfile = handle->symfile;
747 if (!symfile->method_hash)
748 return NULL;
750 if (handle->image != mono_class_get_image (mono_method_get_class (method)))
751 return NULL;
753 mono_debugger_lock ();
755 minfo = (MonoDebugMethodInfo *)g_hash_table_lookup (symfile->method_hash, method);
756 if (minfo) {
757 mono_debugger_unlock ();
758 return minfo;
761 first_ie = (MonoSymbolFileMethodEntry *)
762 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
764 ie = (MonoSymbolFileMethodEntry *)mono_binary_search (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
765 read32(&(symfile->offset_table->_method_count)),
766 sizeof (MonoSymbolFileMethodEntry), compare_method);
768 if (!ie) {
769 mono_debugger_unlock ();
770 return NULL;
773 minfo = g_new0 (MonoDebugMethodInfo, 1);
774 minfo->index = (ie - first_ie) + 1;
775 minfo->method = method;
776 minfo->handle = handle;
778 minfo->data_offset = read32 (&(ie->_data_offset));
779 minfo->lnt_offset = read32 (&(ie->_line_number_table));
781 g_hash_table_insert (symfile->method_hash, method, minfo);
783 mono_debugger_unlock ();
784 return minfo;
788 * mono_debug_symfile_lookup_locals:
790 * Return information about the local variables of \p minfo from the symbol file.
791 * Return NULL if no information can be found.
792 * The result should be freed using \c mono_debug_symfile_free_locals.
794 MonoDebugLocalsInfo*
795 mono_debug_symfile_lookup_locals (MonoDebugMethodInfo *minfo)
797 MonoSymbolFile *symfile = minfo->handle->symfile;
798 const uint8_t *p;
799 int i, len, locals_offset, num_locals, block_index;
800 int code_block_table_offset;
801 MonoDebugLocalsInfo *res;
803 if (!symfile)
804 return NULL;
806 p = symfile->raw_contents + minfo->data_offset;
808 /* compile_unit_index = */ read_leb128 (p, &p);
809 locals_offset = read_leb128 (p, &p);
810 /* namespace_id = */ read_leb128 (p, &p);
811 code_block_table_offset = read_leb128 (p, &p);
813 res = g_new0 (MonoDebugLocalsInfo, 1);
815 p = symfile->raw_contents + code_block_table_offset;
816 res->num_blocks = read_leb128 (p, &p);
817 res->code_blocks = g_new0 (MonoDebugCodeBlock, res->num_blocks);
818 for (i = 0; i < res->num_blocks; ++i) {
819 res->code_blocks [i].type = read_leb128 (p, &p);
820 res->code_blocks [i].parent = read_leb128 (p, &p);
821 res->code_blocks [i].start_offset = read_leb128 (p, &p);
822 res->code_blocks [i].end_offset = read_leb128 (p, &p);
825 p = symfile->raw_contents + locals_offset;
826 num_locals = read_leb128 (p, &p);
828 res->num_locals = num_locals;
829 res->locals = g_new0 (MonoDebugLocalVar, num_locals);
831 for (i = 0; i < num_locals; ++i) {
832 res->locals [i].index = read_leb128 (p, &p);
833 len = read_leb128 (p, &p);
834 res->locals [i].name = (char *)g_malloc (len + 1);
835 memcpy (res->locals [i].name, p, len);
836 res->locals [i].name [len] = '\0';
837 p += len;
838 block_index = read_leb128 (p, &p);
839 if (block_index >= 1 && block_index <= res->num_blocks)
840 res->locals [i].block = &res->code_blocks [block_index - 1];
843 return res;
846 #else /* DISABLE_MDB */
848 MonoSymbolFile *
849 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const uint8_t *raw_contents,
850 int size, gboolean in_the_debugger)
852 return NULL;
855 void
856 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
860 mono_bool
861 mono_debug_symfile_is_loaded (MonoSymbolFile *symfile)
863 return FALSE;
866 MonoDebugMethodInfo *
867 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
869 return NULL;
872 void
873 mono_debug_symfile_get_seq_points (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int **source_files, MonoSymSeqPoint **seq_points, int *n_seq_points)
875 g_assert_not_reached ();
878 MonoDebugSourceLocation *
879 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, uint32_t offset)
881 return NULL;
884 MonoDebugLocalsInfo*
885 mono_debug_symfile_lookup_locals (MonoDebugMethodInfo *minfo)
887 return NULL;
890 void
891 mono_debug_symfile_free_location (MonoDebugSourceLocation *location)
895 #endif