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