Merge pull request #5675 from mono/glib-debug-symbols
[mono.git] / mono / metadata / debug-mono-symfile.c
1 /**
2  * \file
3  *
4  *   Support for reading debug info from .mdb files.
5  *
6  * Author:
7  *      Mono Project (http://www.mono-project.com)
8  *
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.
12  */
13
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>
36
37 #ifndef DISABLE_MDB
38
39 #include <fcntl.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
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
48
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;
60 };
61
62 static void
63 free_method_info (MonoDebugMethodInfo *minfo)
64 {
65         g_free (minfo);
66 }
67
68 static void
69 free_source_info (MonoDebugSourceInfo *sinfo)
70 {
71         g_free (sinfo->source_file);
72         g_free (sinfo->guid);
73         g_free (sinfo->hash);
74         g_free (sinfo);
75 }
76
77 static int
78 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile, mono_bool in_the_debugger)
79 {
80         const char *ptr, *start;
81         gchar *guid;
82         uint64_t magic;
83         int minor, major;
84
85         ptr = start = (const char*)symfile->raw_contents;
86         if (!ptr)
87                 return FALSE;
88
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;
95         }
96
97         major = read32(ptr);
98         ptr += sizeof(uint32_t);
99         minor = read32(ptr);
100         ptr += sizeof(uint32_t);
101
102         /*
103          * 50.0 is the frozen version for Mono 2.0.
104          *
105          * Nobody except me (Martin) is allowed to check the minor version.
106          */
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;
113         }
114
115         guid = mono_guid_to_string ((const uint8_t *) ptr);
116         ptr += 16;
117
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;
125         }
126
127         symfile->major_version = major;
128         symfile->minor_version = minor;
129
130         symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
131
132         symfile->method_hash = g_hash_table_new_full (
133                 NULL, NULL, NULL, (GDestroyNotify) free_method_info);
134
135         symfile->source_hash = g_hash_table_new_full (
136                 NULL, NULL, NULL, (GDestroyNotify) free_source_info);
137
138         g_free (guid);
139         return TRUE;
140 }
141
142 /**
143  * mono_debug_open_mono_symbols:
144  */
145 MonoSymbolFile *
146 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const uint8_t *raw_contents,
147                               int size, gboolean in_the_debugger)
148 {
149         MonoSymbolFile *symfile;
150
151         mono_debugger_lock ();
152         symfile = g_new0 (MonoSymbolFile, 1);
153
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;
163
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);
174                         }
175
176                         mono_file_map_close (f);
177                 }
178         }
179         
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;
187         }
188
189         mono_debugger_unlock ();
190         return symfile;
191 }
192
193 /**
194  * mono_debug_close_mono_symbol_file:
195  */
196 void
197 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
198 {
199         if (!symfile)
200                 return;
201
202         mono_debugger_lock ();
203         if (symfile->method_hash)
204                 g_hash_table_destroy (symfile->method_hash);
205
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);
211         }
212
213         if (symfile->filename)
214                 g_free (symfile->filename);
215         g_free (symfile);
216         mono_debugger_unlock ();
217 }
218
219 /**
220  * mono_debug_symfile_is_loaded:
221  */
222 mono_bool
223 mono_debug_symfile_is_loaded (MonoSymbolFile *symfile)
224 {
225         return symfile && symfile->offset_table;
226 }
227
228 static int
229 read_leb128 (const uint8_t *ptr, const uint8_t **rptr)
230 {
231         int ret = 0;
232         int shift = 0;
233         char b;
234
235         do {
236                 b = *ptr++;
237                                 
238                 ret = ret | ((b & 0x7f) << shift);
239                 shift += 7;
240         } while ((b & 0x80) == 0x80);
241
242         if (rptr)
243                 *rptr = ptr;
244
245         return ret;
246 }
247
248 static gchar *
249 read_string (const uint8_t *ptr, const uint8_t **endp)
250 {
251         gchar *s;
252         int len = read_leb128 (ptr, &ptr);
253
254         s = g_filename_from_utf8 ((const char *) ptr, len, NULL, NULL, NULL);
255         ptr += len;
256         if (endp)
257                 *endp = ptr;
258         return s;
259 }
260
261 typedef struct {
262         MonoSymbolFile *symfile;
263         int line_base, line_range, max_address_incr;
264         uint8_t opcode_base;
265         uint32_t last_line, last_file, last_offset;
266         uint32_t first_file;
267         int line, file, offset;
268         gboolean is_hidden;
269 } StatementMachine;
270
271 static gboolean
272 check_line (StatementMachine *stm, int offset, MonoDebugSourceLocation **location)
273 {
274         gchar *source_file = NULL;
275
276         if (stm->offset <= offset) {
277                 stm->last_offset = stm->offset;
278                 stm->last_file = stm->file;
279                 if (stm->line != 0xfeefee)
280                         stm->last_line = stm->line;
281                 return FALSE;
282         }
283
284         if (stm->last_file) {
285                 int offset = read32(&(stm->symfile->offset_table->_source_table_offset)) +
286                         (stm->last_file - 1) * sizeof (MonoSymbolFileSourceEntry);
287                 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
288                         (stm->symfile->raw_contents + offset);
289
290                 source_file = read_string (stm->symfile->raw_contents + read32(&(se->_data_offset)), NULL);
291         }
292
293         if (stm->last_line == 0) {
294                 /* 
295                  * The IL offset is less than the first IL offset which has a corresponding
296                  * source line.
297                  */
298                 *location = NULL;
299                 return TRUE;
300         }
301
302         *location = g_new0 (MonoDebugSourceLocation, 1);
303         (*location)->source_file = source_file;
304         (*location)->row = stm->last_line;
305         (*location)->il_offset = stm->last_offset;
306         return TRUE;
307 }
308
309 /**
310  * mono_debug_symfile_lookup_location:
311  * \param minfo A \c MonoDebugMethodInfo which can be retrieved by \c mono_debug_lookup_method.
312  * \param offset IL offset within the corresponding method's CIL code.
313  *
314  * This function is similar to \c mono_debug_lookup_location, but we
315  * already looked up the method and also already did the
316  * native address -> IL offset mapping.
317  */
318 MonoDebugSourceLocation *
319 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, uint32_t offset)
320 {
321         MonoDebugSourceLocation *location = NULL;
322         MonoSymbolFile *symfile;
323         const unsigned char *ptr;
324         StatementMachine stm;
325
326 #define DW_LNS_copy 1
327 #define DW_LNS_advance_pc 2
328 #define DW_LNS_advance_line 3
329 #define DW_LNS_set_file 4
330 #define DW_LNS_const_add_pc 8
331
332 #define DW_LNE_end_sequence 1
333 #define DW_LNE_MONO_negate_is_hidden 0x40
334
335 #define DW_LNE_MONO__extensions_start 0x40
336 #define DW_LNE_MONO__extensions_end 0x7f
337
338         if ((symfile = minfo->handle->symfile) == NULL)
339                 return NULL;
340
341         stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
342         stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
343         stm.opcode_base = (uint8_t) read32 (&symfile->offset_table->_line_number_table_opcode_base);
344         stm.max_address_incr = (255 - stm.opcode_base) / stm.line_range;
345
346         mono_debugger_lock ();
347
348         ptr = symfile->raw_contents + minfo->lnt_offset;
349
350         stm.symfile = symfile;
351         stm.offset = stm.last_offset = 0;
352         stm.last_file = 0;
353         stm.last_line = 0;
354         stm.first_file = 0;
355         stm.file = 1;
356         stm.line = 1;
357         stm.is_hidden = FALSE;
358
359         while (TRUE) {
360                 uint8_t opcode = *ptr++;
361
362                 if (opcode == 0) {
363                         uint8_t size = *ptr++;
364                         const unsigned char *end_ptr = ptr + size;
365
366                         opcode = *ptr++;
367
368                         if (opcode == DW_LNE_end_sequence) {
369                                 if (check_line (&stm, -1, &location))
370                                         goto out_success;
371                                 break;
372                         } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
373                                 stm.is_hidden = !stm.is_hidden;
374                         } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
375                                    (opcode <= DW_LNE_MONO__extensions_end)) {
376                                 ; // reserved for future extensions
377                         } else {
378                                 g_warning ("Unknown extended opcode %x in LNT", opcode);
379                         }
380
381                         ptr = end_ptr;
382                         continue;
383                 } else if (opcode < stm.opcode_base) {
384                         switch (opcode) {
385                         case DW_LNS_copy:
386                                 if (check_line (&stm, offset, &location))
387                                         goto out_success;
388                                 break;
389                         case DW_LNS_advance_pc:
390                                 stm.offset += read_leb128 (ptr, &ptr);
391                                 break;
392                         case DW_LNS_advance_line:
393                                 stm.line += read_leb128 (ptr, &ptr);
394                                 break;
395                         case DW_LNS_set_file:
396                                 stm.file = read_leb128 (ptr, &ptr);
397                                 break;
398                         case DW_LNS_const_add_pc:
399                                 stm.offset += stm.max_address_incr;
400                                 break;
401                         default:
402                                 g_warning ("Unknown standard opcode %x in LNT", opcode);
403                                 goto error_out;
404                         }
405                 } else {
406                         opcode -= stm.opcode_base;
407
408                         stm.offset += opcode / stm.line_range;
409                         stm.line += stm.line_base + (opcode % stm.line_range);
410
411                         if (check_line (&stm, offset, &location))
412                                 goto out_success;
413                 }
414         }
415
416  error_out:
417         mono_debugger_unlock ();
418         return NULL;
419
420  out_success:
421         mono_debugger_unlock ();
422         return location;
423 }
424
425 static void
426 add_line (StatementMachine *stm, GPtrArray *il_offset_array, GPtrArray *line_number_array, GPtrArray *source_file_array, GPtrArray *hidden_array)
427 {
428         g_ptr_array_add (il_offset_array, GUINT_TO_POINTER (stm->offset));
429         g_ptr_array_add (line_number_array, GUINT_TO_POINTER (stm->line));
430         g_ptr_array_add (source_file_array, GUINT_TO_POINTER (stm->file));
431         g_ptr_array_add (hidden_array, GUINT_TO_POINTER (stm->is_hidden || stm->line <= 0));
432
433         if (!stm->is_hidden && !stm->first_file)
434                 stm->first_file = stm->file;
435 }
436
437 /**
438  * mono_debug_symfile_free_location:
439  *
440  * Free a \c MonoDebugSourceLocation returned by
441  * \c mono_debug_symfile_lookup_location
442  */
443 void
444 mono_debug_symfile_free_location (MonoDebugSourceLocation  *location)
445 {
446         g_free (location->source_file);
447         g_free (location);
448 }
449
450 /*
451  * LOCKING: Assumes the debugger lock is held.
452  */
453 static MonoDebugSourceInfo*
454 get_source_info (MonoSymbolFile *symfile, int index)
455 {
456         MonoDebugSourceInfo *info;
457
458         info = (MonoDebugSourceInfo *)g_hash_table_lookup (symfile->source_hash, GUINT_TO_POINTER (index));
459         if (!info) {
460                 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
461                         (index - 1) * sizeof (MonoSymbolFileSourceEntry);
462                 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
463                         (symfile->raw_contents + offset);
464                 const uint8_t *ptr = symfile->raw_contents + read32(&(se->_data_offset));
465
466                 info = g_new0 (MonoDebugSourceInfo, 1);
467                 info->source_file = read_string (ptr, &ptr);
468                 info->guid = (guint8 *)g_malloc0 (16);
469                 memcpy (info->guid, ptr, 16);
470                 ptr += 16;
471                 info->hash = (guint8 *)g_malloc0 (16);
472                 memcpy (info->hash, ptr, 16);
473                 ptr += 16;
474                 g_hash_table_insert (symfile->source_hash, GUINT_TO_POINTER (index), info);
475         }
476         return info;
477 }
478
479 typedef enum {
480         LNT_FLAG_HAS_COLUMN_INFO = 1 << 1,
481         LNT_FLAG_HAS_END_INFO = 1 << 2,
482 } LineNumberTableFlags;
483
484 static LineNumberTableFlags
485 method_get_lnt_flags (MonoDebugMethodInfo *minfo)
486 {
487         MonoSymbolFile *symfile;
488         const unsigned char *ptr;
489         guint32 flags;
490
491         if ((symfile = minfo->handle->symfile) == NULL)
492                 return (LineNumberTableFlags)0;
493
494         ptr = symfile->raw_contents + minfo->data_offset;
495
496         /* Has to read 'flags' which is preceeded by a bunch of other data */
497         /* compile_unit_index */
498         read_leb128 (ptr, &ptr);
499         /* local variable table offset */
500         read_leb128 (ptr, &ptr);
501         /* namespace id */
502         read_leb128 (ptr, &ptr);
503         /* code block table offset */
504         read_leb128 (ptr, &ptr);
505         /* scope variable table offset */
506         read_leb128 (ptr, &ptr);
507         /* real name offset */
508         read_leb128 (ptr, &ptr);
509
510         flags = read_leb128 (ptr, &ptr);
511         return (LineNumberTableFlags)flags;
512 }
513
514 /*
515  * mono_debug_symfile_get_seq_points:
516  *
517  * On return, SOURCE_FILE_LIST will point to a GPtrArray of MonoDebugSourceFile
518  * structures, and SOURCE_FILES will contain indexes into this array.
519  * The MonoDebugSourceFile structures are owned by this module.
520  */
521 void
522 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 {
524         // FIXME: Unify this with mono_debug_symfile_lookup_location
525         MonoSymbolFile *symfile;
526         const unsigned char *ptr;
527         StatementMachine stm;
528         uint32_t i, j, n;
529         LineNumberTableFlags flags;
530         GPtrArray *il_offset_array, *line_number_array, *source_file_array, *hidden_array;
531         gboolean has_column_info, has_end_info;
532         MonoSymSeqPoint *sps;
533
534         if (source_file_list)
535                 *source_file_list = NULL;
536         if (seq_points)
537                 *seq_points = NULL;
538         if (n_seq_points)
539                 *n_seq_points = 0;
540         if (source_files)
541                 *source_files = NULL;
542         if (source_file)
543                 *source_file = NULL;
544
545         if ((symfile = minfo->handle->symfile) == NULL)
546                 return;
547
548         flags = method_get_lnt_flags (minfo);
549         has_column_info = (flags & LNT_FLAG_HAS_COLUMN_INFO) > 0;
550         has_end_info = (flags & LNT_FLAG_HAS_END_INFO) > 0;
551
552         il_offset_array = g_ptr_array_new ();
553         line_number_array = g_ptr_array_new ();
554         source_file_array = g_ptr_array_new ();
555         hidden_array = g_ptr_array_new();
556
557         stm.line_base = read32 (&symfile->offset_table->_line_number_table_line_base);
558         stm.line_range = read32 (&symfile->offset_table->_line_number_table_line_range);
559         stm.opcode_base = (uint8_t) read32 (&symfile->offset_table->_line_number_table_opcode_base);
560         stm.max_address_incr = (255 - stm.opcode_base) / stm.line_range;
561
562         mono_debugger_lock ();
563
564         ptr = symfile->raw_contents + minfo->lnt_offset;
565
566         stm.symfile = symfile;
567         stm.offset = stm.last_offset = 0;
568         stm.last_file = 0;
569         stm.last_line = 0;
570         stm.first_file = 0;
571         stm.file = 1;
572         stm.line = 1;
573         stm.is_hidden = FALSE;
574
575         while (TRUE) {
576                 uint8_t opcode = *ptr++;
577
578                 if (opcode == 0) {
579                         uint8_t size = *ptr++;
580                         const unsigned char *end_ptr = ptr + size;
581
582                         opcode = *ptr++;
583
584                         if (opcode == DW_LNE_end_sequence) {
585                                 if (il_offset_array->len == 0)
586                                         /* Empty table */
587                                         break;
588                                 break;
589                         } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
590                                 stm.is_hidden = !stm.is_hidden;
591                         } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
592                                    (opcode <= DW_LNE_MONO__extensions_end)) {
593                                 ; // reserved for future extensions
594                         } else {
595                                 g_warning ("Unknown extended opcode %x in LNT", opcode);
596                         }
597
598                         ptr = end_ptr;
599                         continue;
600                 } else if (opcode < stm.opcode_base) {
601                         switch (opcode) {
602                         case DW_LNS_copy:
603                                 add_line (&stm, il_offset_array, line_number_array, source_file_array, hidden_array);
604                                 break;
605                         case DW_LNS_advance_pc:
606                                 stm.offset += read_leb128 (ptr, &ptr);
607                                 break;
608                         case DW_LNS_advance_line:
609                                 stm.line += read_leb128 (ptr, &ptr);
610                                 break;
611                         case DW_LNS_set_file:
612                                 stm.file = read_leb128 (ptr, &ptr);
613                                 break;
614                         case DW_LNS_const_add_pc:
615                                 stm.offset += stm.max_address_incr;
616                                 break;
617                         default:
618                                 g_warning ("Unknown standard opcode %x in LNT", opcode);
619                                 g_assert_not_reached ();
620                         }
621                 } else {
622                         opcode -= stm.opcode_base;
623
624                         stm.offset += opcode / stm.line_range;
625                         stm.line += stm.line_base + (opcode % stm.line_range);
626
627                         add_line (&stm, il_offset_array, line_number_array, source_file_array, hidden_array);
628                 }
629         }
630
631         if (!stm.file && stm.first_file)
632                 stm.file = stm.first_file;
633
634         if (stm.file && source_file) {
635                 int offset = read32(&(stm.symfile->offset_table->_source_table_offset)) +
636                         (stm.file - 1) * sizeof (MonoSymbolFileSourceEntry);
637                 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *)
638                         (stm.symfile->raw_contents + offset);
639
640                 if (source_file)
641                         *source_file = read_string (stm.symfile->raw_contents + read32(&(se->_data_offset)), NULL);
642         }
643
644         if (source_file_list) {
645                 int file, last_file = 0;
646
647                 *source_file_list = g_ptr_array_new ();
648                 if (source_files)
649                         *source_files = (int *)g_malloc (il_offset_array->len * sizeof (int));
650
651                 for (i = 0; i < il_offset_array->len; ++i) {
652                         file = GPOINTER_TO_UINT (g_ptr_array_index (source_file_array, i));
653                         if (file && file != last_file) {
654                                 MonoDebugSourceInfo *info = get_source_info (symfile, file);
655
656                                 g_ptr_array_add (*source_file_list, info);
657                         }
658                         last_file = file;
659                         if (source_files)
660                                 (*source_files) [i] = (*source_file_list)->len - 1;
661                 }
662         }                               
663
664         if (n_seq_points) {
665                 g_assert (seq_points);
666
667                 n = il_offset_array->len;
668                 for (i = 0; i < il_offset_array->len; i++) {
669                         if (GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
670                                 n --;
671                         }
672                 }
673
674                 *n_seq_points = n;
675                 *seq_points = sps = g_new0 (MonoSymSeqPoint, n);
676                 j = 0;
677                 for (i = 0; i < il_offset_array->len; ++i) {
678                         MonoSymSeqPoint *sp = &(sps [j]);
679                         if (!GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
680                                 sp->il_offset = GPOINTER_TO_UINT (g_ptr_array_index (il_offset_array, i));
681                                 sp->line = GPOINTER_TO_UINT (g_ptr_array_index (line_number_array, i));
682                                 sp->column = -1;
683                                 sp->end_line = -1;
684                                 sp->end_column = -1;
685                                 j ++;
686                         }
687                 }
688
689                 if (has_column_info) {
690                         j = 0;
691                         for (i = 0; i < il_offset_array->len; ++i) {
692                                 MonoSymSeqPoint *sp = &(sps [j]);
693                                 int column = read_leb128 (ptr, &ptr);
694                                 if (!GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
695                                         sp->column = column;
696                                         j++;
697                                 }
698                         }
699                 }
700
701                 if (has_end_info) {
702                         j = 0;
703                         for (i = 0; i < il_offset_array->len; ++i) {
704                                 MonoSymSeqPoint *sp = &(sps [j]);
705                                 int end_row, end_column = -1;
706
707                                 end_row = read_leb128 (ptr, &ptr);
708                                 if (end_row != 0xffffff) {
709                                         end_row += GPOINTER_TO_UINT (g_ptr_array_index (line_number_array, i));
710                                         end_column = read_leb128 (ptr, &ptr);
711                                         if (!GPOINTER_TO_UINT (g_ptr_array_index (hidden_array, i))) {
712                                                 sp->end_line = end_row;
713                                                 sp->end_column = end_column;
714                                                 j++;
715                                         }
716                                 }
717                         }
718                 }
719         }
720
721         g_ptr_array_free (il_offset_array, TRUE);
722         g_ptr_array_free (line_number_array, TRUE);
723         g_ptr_array_free (hidden_array, TRUE);
724
725         mono_debugger_unlock ();
726         return;
727 }
728
729 static int
730 compare_method (const void *key, const void *object)
731 {
732         uint32_t token = GPOINTER_TO_UINT (key);
733         MonoSymbolFileMethodEntry *me = (MonoSymbolFileMethodEntry*)object;
734
735         return token - read32(&(me->_token));
736 }
737
738 /**
739  * mono_debug_symfile_lookup_method:
740  */
741 MonoDebugMethodInfo *
742 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
743 {
744         MonoSymbolFileMethodEntry *first_ie, *ie;
745         MonoDebugMethodInfo *minfo;
746         MonoSymbolFile *symfile = handle->symfile;
747
748         if (!symfile->method_hash)
749                 return NULL;
750
751         if (handle->image != mono_class_get_image (mono_method_get_class (method)))
752                 return NULL;
753
754         mono_debugger_lock ();
755
756         minfo = (MonoDebugMethodInfo *)g_hash_table_lookup (symfile->method_hash, method);
757         if (minfo) {
758                 mono_debugger_unlock ();
759                 return minfo;
760         }
761
762         first_ie = (MonoSymbolFileMethodEntry *)
763                 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
764
765         ie = (MonoSymbolFileMethodEntry *)mono_binary_search (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
766                                    read32(&(symfile->offset_table->_method_count)),
767                                    sizeof (MonoSymbolFileMethodEntry), compare_method);
768
769         if (!ie) {
770                 mono_debugger_unlock ();
771                 return NULL;
772         }
773
774         minfo = g_new0 (MonoDebugMethodInfo, 1);
775         minfo->index = (ie - first_ie) + 1;
776         minfo->method = method;
777         minfo->handle = handle;
778
779         minfo->data_offset = read32 (&(ie->_data_offset));
780         minfo->lnt_offset = read32 (&(ie->_line_number_table));
781
782         g_hash_table_insert (symfile->method_hash, method, minfo);
783
784         mono_debugger_unlock ();
785         return minfo;
786 }
787
788 /**
789  * mono_debug_symfile_lookup_locals:
790  *
791  * Return information about the local variables of \p minfo from the symbol file.
792  * Return NULL if no information can be found.
793  * The result should be freed using \c mono_debug_symfile_free_locals.
794  */
795 MonoDebugLocalsInfo*
796 mono_debug_symfile_lookup_locals (MonoDebugMethodInfo *minfo)
797 {
798         MonoSymbolFile *symfile = minfo->handle->symfile;
799         const uint8_t *p;
800         int i, len, locals_offset, num_locals, block_index;
801         int code_block_table_offset;
802         MonoDebugLocalsInfo *res;
803
804         if (!symfile)
805                 return NULL;
806
807         p = symfile->raw_contents + minfo->data_offset;
808
809         /* compile_unit_index = */ read_leb128 (p, &p);
810         locals_offset = read_leb128 (p, &p);
811         /* namespace_id = */ read_leb128 (p, &p);
812         code_block_table_offset = read_leb128 (p, &p);
813
814         res = g_new0 (MonoDebugLocalsInfo, 1);
815
816         p = symfile->raw_contents + code_block_table_offset;
817         res->num_blocks = read_leb128 (p, &p);
818         res->code_blocks = g_new0 (MonoDebugCodeBlock, res->num_blocks);
819         for (i = 0; i < res->num_blocks; ++i) {
820                 res->code_blocks [i].type = read_leb128 (p, &p);
821                 res->code_blocks [i].parent = read_leb128 (p, &p);
822                 res->code_blocks [i].start_offset = read_leb128 (p, &p);
823                 res->code_blocks [i].end_offset = read_leb128 (p, &p);
824         }
825
826         p = symfile->raw_contents + locals_offset;
827         num_locals = read_leb128 (p, &p);
828
829         res->num_locals = num_locals;
830         res->locals = g_new0 (MonoDebugLocalVar, num_locals);
831
832         for (i = 0; i < num_locals; ++i) {
833                 res->locals [i].index = read_leb128 (p, &p);
834                 len = read_leb128 (p, &p);
835                 res->locals [i].name = (char *)g_malloc (len + 1);
836                 memcpy (res->locals [i].name, p, len);
837                 res->locals [i].name [len] = '\0';
838                 p += len;
839                 block_index = read_leb128 (p, &p);
840                 if (block_index >= 1 && block_index <= res->num_blocks)
841                         res->locals [i].block = &res->code_blocks [block_index - 1];
842         }
843
844         return res;
845 }
846
847 #else /* DISABLE_MDB */
848
849 MonoSymbolFile *
850 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const uint8_t *raw_contents,
851                               int size, gboolean in_the_debugger)
852 {
853         return NULL;
854 }
855
856 void
857 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
858 {
859 }
860
861 mono_bool
862 mono_debug_symfile_is_loaded (MonoSymbolFile *symfile)
863 {
864         return FALSE;
865 }
866
867 MonoDebugMethodInfo *
868 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
869 {
870         return NULL;
871 }
872
873 void
874 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 {
876         g_assert_not_reached ();
877 }
878
879 MonoDebugSourceLocation *
880 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, uint32_t offset)
881 {
882         return NULL;
883 }
884
885 MonoDebugLocalsInfo*
886 mono_debug_symfile_lookup_locals (MonoDebugMethodInfo *minfo)
887 {
888         return NULL;
889 }
890
891 void
892 mono_debug_symfile_free_location (MonoDebugSourceLocation  *location)
893 {
894 }
895
896 #endif