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