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