2007-04-25 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / debug-mono-symfile.c
1 #include <config.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <signal.h>
7 #include <sys/param.h>
8 #include <sys/stat.h>
9 #include <mono/metadata/metadata.h>
10 #include <mono/metadata/tabledefs.h>
11 #include <mono/metadata/rawbuffer.h>
12 #include <mono/metadata/tokentype.h>
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/exception.h>
15 #include <mono/metadata/debug-helpers.h>
16 #include <mono/metadata/mono-debug.h>
17 #include <mono/metadata/debug-mono-symfile.h>
18 #include <mono/metadata/mono-debug-debugger.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/metadata/metadata-internals.h>
21 #include <mono/metadata/class-internals.h>
22
23 #include <fcntl.h>
24 #include <unistd.h>
25
26 #define RANGE_TABLE_CHUNK_SIZE          256
27 #define CLASS_TABLE_CHUNK_SIZE          256
28 #define TYPE_TABLE_PTR_CHUNK_SIZE       256
29 #define TYPE_TABLE_CHUNK_SIZE           65536
30
31 static void
32 free_method_info (MonoDebugMethodInfo *minfo)
33 {
34         g_free (minfo);
35 }
36
37 static gchar *
38 get_class_name (MonoClass *klass)
39 {
40         MonoClass *nested_in = mono_class_get_nesting_type (klass);
41         const char *name_space;
42         if (nested_in) {
43                 gchar *parent_name = get_class_name (nested_in);
44                 gchar *name = g_strdup_printf ("%s.%s", parent_name, mono_class_get_name (klass));
45                 g_free (parent_name);
46                 return name;
47         }
48
49         name_space = mono_class_get_namespace (klass);
50         return g_strdup_printf ("%s%s%s", name_space,
51                                 name_space [0] ? "." : "", mono_class_get_name (klass));
52 }
53
54 static int
55 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile, gboolean in_the_debugger)
56 {
57         const char *ptr, *start;
58         gchar *guid;
59         guint64 magic;
60         long version;
61
62         ptr = start = (const char*)symfile->raw_contents;
63         if (!ptr)
64                 return FALSE;
65
66         magic = read64(ptr);
67         ptr += sizeof(guint64);
68         if (magic != MONO_SYMBOL_FILE_MAGIC) {
69                 if (!in_the_debugger)
70                         g_warning ("Symbol file %s is not a mono symbol file", symfile->filename);
71                 return FALSE;
72         }
73
74         version = read32(ptr);
75         ptr += sizeof(guint32);
76         if (version != MONO_SYMBOL_FILE_VERSION) {
77                 if (!in_the_debugger)
78                         g_warning ("Symbol file %s has incorrect version "
79                                    "(expected %d, got %ld)", symfile->filename,
80                                    MONO_SYMBOL_FILE_VERSION, version);
81                 return FALSE;
82         }
83
84         guid = mono_guid_to_string ((const guint8 *) ptr);
85         ptr += 16;
86
87         if (strcmp (handle->image->guid, guid)) {
88                 if (!in_the_debugger)
89                         g_warning ("Symbol file %s doesn't match image %s", symfile->filename,
90                                    handle->image_file);
91                 if (guid)
92                         g_free (guid);
93                 return FALSE;
94         }
95
96         symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
97
98         symfile->method_hash = g_hash_table_new_full (
99                 g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) free_method_info);
100
101         g_free (guid);
102         return TRUE;
103 }
104
105 MonoSymbolFile *
106 mono_debug_open_mono_symbols (MonoDebugHandle *handle, const guint8 *raw_contents,
107                               int size, gboolean in_the_debugger)
108 {
109         MonoSymbolFile *symfile;
110         FILE* f;
111
112         mono_debugger_lock ();
113         symfile = g_new0 (MonoSymbolFile, 1);
114
115         if (raw_contents != NULL) {
116                 unsigned char *p;
117                 symfile->raw_contents_size = size;
118                 symfile->raw_contents = p = g_malloc (size);
119                 memcpy (p, raw_contents, size);
120                 symfile->filename = g_strdup_printf ("LoadedFromMemory");
121         } else {
122                 symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
123
124                 if ((f = fopen (symfile->filename, "rb"))) {
125                         struct stat stat_buf;
126                         
127                         if (fstat (fileno (f), &stat_buf) < 0) {
128                                 if (!in_the_debugger)
129                                         g_warning ("stat of %s failed: %s",
130                                                    symfile->filename,  g_strerror (errno));
131                         } else {
132                                 symfile->raw_contents_size = stat_buf.st_size;
133                                 symfile->raw_contents = mono_raw_buffer_load (fileno (f), FALSE, 0, stat_buf.st_size);
134                         }
135
136                         fclose (f);
137                 }
138         }
139         
140         if (load_symfile (handle, symfile, in_the_debugger)) {
141                 mono_debugger_unlock ();
142                 return symfile;
143         } else if (!in_the_debugger) {
144                 mono_debug_close_mono_symbol_file (symfile);
145                 mono_debugger_unlock ();
146                 return NULL;
147         }
148
149         mono_debugger_unlock ();
150         return symfile;
151 }
152
153 void
154 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
155 {
156         if (!symfile)
157                 return;
158
159         mono_debugger_lock ();
160         if (symfile->method_hash)
161                 g_hash_table_destroy (symfile->method_hash);
162
163         if (symfile->raw_contents)
164                 mono_raw_buffer_free ((gpointer) symfile->raw_contents);
165
166         if (symfile->filename)
167                 g_free (symfile->filename);
168         g_free (symfile);
169         mono_debugger_unlock ();
170 }
171
172 static int
173 read_leb128 (const char *ptr, const char **rptr)
174 {
175         int ret = 0;
176         int shift = 0;
177         char b;
178
179         do {
180                 b = *ptr++;
181                                 
182                 ret = ret | ((b & 0x7f) << shift);
183                 shift += 7;
184         } while ((b & 0x80) == 0x80);
185
186         if (rptr)
187                 *rptr = ptr;
188
189         return ret;
190 }
191
192 static gchar *
193 read_string (const char *ptr)
194 {
195         int len = read_leb128 (ptr, &ptr);
196         return g_filename_from_utf8 (ptr, len, NULL, NULL, NULL);
197 }
198
199 /**
200  * mono_debug_symfile_lookup_location:
201  * @minfo: A `MonoDebugMethodInfo' which can be retrieved by
202  *         mono_debug_lookup_method().
203  * @offset: IL offset within the corresponding method's CIL code.
204  *
205  * This function is similar to mono_debug_lookup_location(), but we
206  * already looked up the method and also already did the
207  * `native address -> IL offset' mapping.
208  */
209 MonoDebugSourceLocation *
210 mono_debug_symfile_lookup_location (MonoDebugMethodInfo *minfo, guint32 offset)
211 {
212         MonoSymbolFileLineNumberEntry *lne;
213         MonoSymbolFile *symfile;
214         gchar *source_file = NULL;
215         const unsigned char *ptr;
216         int count, i;
217
218         if ((symfile = minfo->handle->symfile) == NULL)
219                 return NULL;
220
221         mono_debugger_lock ();
222
223         if (read32(&(minfo->entry->_source_index))) {
224                 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
225                         (read32(&(minfo->entry->_source_index)) - 1) * sizeof (MonoSymbolFileSourceEntry);
226                 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *) (symfile->raw_contents + offset);
227
228                 source_file = read_string ((const char*)symfile->raw_contents + read32(&(se->_name_offset)));
229         }
230
231         ptr = symfile->raw_contents + read32(&(minfo->entry->_line_number_table_offset));
232
233         count = read32(&(minfo->entry->_num_line_numbers));
234         lne = ((MonoSymbolFileLineNumberEntry *) ptr) + count - 1;
235
236         for (i = count - 1; i >= 0; i--, lne--) {
237                 MonoDebugSourceLocation *location;
238
239                 if (read32(&(lne->_offset)) > offset)
240                         continue;
241
242                 location = g_new0 (MonoDebugSourceLocation, 1);
243                 location->source_file = source_file;
244                 location->row = read32(&(lne->_row));
245                 location->il_offset = read32(&(lne->_offset));
246
247                 mono_debugger_unlock ();
248                 return location;
249         }
250
251         mono_debugger_unlock ();
252         return NULL;
253 }
254
255 gint32
256 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, guint32 il_offset)
257 {
258         int i;
259
260         if (!jit || !jit->line_numbers)
261                 return -1;
262
263         for (i = jit->num_line_numbers - 1; i >= 0; i--) {
264                 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
265
266                 if (lne.il_offset <= il_offset)
267                         return lne.native_offset;
268         }
269
270         return -1;
271 }
272
273 static int
274 compare_method (const void *key, const void *object)
275 {
276         guint32 token = GPOINTER_TO_UINT (key);
277         MonoSymbolFileMethodIndexEntry *me = (MonoSymbolFileMethodIndexEntry*)object;
278
279         return token - read32(&(me->_token));
280 }
281
282 MonoDebugMethodInfo *
283 mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
284 {
285         MonoSymbolFileMethodEntry *me;
286         MonoSymbolFileMethodIndexEntry *first_ie, *ie;
287         MonoDebugMethodInfo *minfo;
288         MonoSymbolFile *symfile = handle->symfile;
289
290         if (!symfile->method_hash)
291                 return NULL;
292
293         if (handle->image != mono_class_get_image (mono_method_get_class (method)))
294                 return NULL;
295
296         mono_debugger_lock ();
297         first_ie = (MonoSymbolFileMethodIndexEntry *)
298                 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
299
300         ie = bsearch (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
301                                    read32(&(symfile->offset_table->_method_count)),
302                                    sizeof (MonoSymbolFileMethodIndexEntry), compare_method);
303
304         if (!ie) {
305                 mono_debugger_unlock ();
306                 return NULL;
307         }
308
309         me = (MonoSymbolFileMethodEntry *) (symfile->raw_contents + read32(&(ie->_file_offset)));
310
311         minfo = g_new0 (MonoDebugMethodInfo, 1);
312         minfo->index = (ie - first_ie) + 1;
313         minfo->method = method;
314         minfo->handle = handle;
315         minfo->num_il_offsets = read32(&(me->_num_line_numbers));
316         minfo->il_offsets = (MonoSymbolFileLineNumberEntry *)
317                 (symfile->raw_contents + read32(&(me->_line_number_table_offset)));
318         minfo->num_lexical_blocks = read32(&(me->_num_lexical_blocks));
319         minfo->lexical_blocks = (MonoSymbolFileLexicalBlockEntry *)
320                 (symfile->raw_contents + read32(&(me->_lexical_block_table_offset)));
321         minfo->entry = me;
322
323         g_hash_table_insert (symfile->method_hash, method, minfo);
324
325         mono_debugger_unlock ();
326         return minfo;
327 }