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