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