2005-12-22 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-endian.h>
19 #include <mono/metadata/metadata-internals.h>
20 #include <mono/metadata/class-internals.h>
21 #include <mono/metadata/mono-debug-debugger.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 = 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                 return FALSE;
92         }
93
94         symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
95
96         symfile->method_hash = g_hash_table_new_full (
97                 g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) free_method_info);
98
99         return TRUE;
100 }
101
102 MonoSymbolFile *
103 mono_debug_open_mono_symbol_file (MonoDebugHandle *handle, gboolean in_the_debugger)
104 {
105         MonoSymbolFile *symfile;
106         FILE* f;
107
108         mono_debugger_lock ();
109         symfile = g_new0 (MonoSymbolFile, 1);
110
111         symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
112
113         if ((f = fopen (symfile->filename, "rb")) > 0) {
114                 struct stat stat_buf;
115                         
116                 if (fstat (fileno (f), &stat_buf) < 0) {
117                         if (!in_the_debugger)
118                                 g_warning ("stat of %s failed: %s",
119                                            symfile->filename,  g_strerror (errno));
120                 } else {        
121                         symfile->raw_contents_size = stat_buf.st_size;
122                         symfile->raw_contents = mono_raw_buffer_load (fileno (f), FALSE, 0, stat_buf.st_size);
123                 }
124                 
125                 fclose (f);
126         }
127         
128         if (load_symfile (handle, symfile, in_the_debugger)) {
129                 mono_debugger_unlock ();
130                 return symfile;
131         } else if (!in_the_debugger) {
132                 mono_debug_close_mono_symbol_file (symfile);
133                 mono_debugger_unlock ();
134                 return NULL;
135         }
136
137         mono_debugger_unlock ();
138         return symfile;
139 }
140
141 void
142 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
143 {
144         if (!symfile)
145                 return;
146
147         mono_debugger_lock ();
148         if (symfile->method_hash)
149                 g_hash_table_destroy (symfile->method_hash);
150
151         if (symfile->raw_contents)
152                 mono_raw_buffer_free ((gpointer) symfile->raw_contents);
153         
154         g_free (symfile);
155         mono_debugger_unlock ();
156 }
157
158 static int
159 read_leb128 (const char *ptr, const char **rptr)
160 {
161         int ret = 0;
162         int shift = 0;
163         char b;
164
165         do {
166                 b = *ptr++;
167                                 
168                 ret = ret | ((b & 0x7f) << shift);
169                 shift += 7;
170         } while ((b & 0x80) == 0x80);
171
172         if (rptr)
173                 *rptr = ptr;
174
175         return ret;
176 }
177
178 static gchar *
179 read_string (const char *ptr)
180 {
181         int len = read_leb128 (ptr, &ptr);
182         return g_filename_from_utf8 (ptr, len, NULL, NULL, NULL);
183 }
184
185 gchar *
186 mono_debug_find_source_location (MonoSymbolFile *symfile, MonoMethod *method, guint32 offset,
187                                  guint32 *line_number)
188 {
189         MonoSymbolFileLineNumberEntry *lne;
190         MonoDebugMethodInfo *minfo;
191         gchar *source_file = NULL;
192         const char *ptr;
193         int i;
194
195         mono_debugger_lock ();
196         if (!symfile->method_hash) {
197                 mono_debugger_unlock ();
198                 return NULL;
199         }
200
201         minfo = g_hash_table_lookup (symfile->method_hash, method);
202         if (!minfo) {
203                 mono_debugger_unlock ();
204                 return NULL;
205         }
206
207         if (read32(&(minfo->entry->_source_index))) {
208                 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
209                         (read32(&(minfo->entry->_source_index)) - 1) * sizeof (MonoSymbolFileSourceEntry);
210                 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *) (symfile->raw_contents + offset);
211
212                 source_file = read_string (symfile->raw_contents + read32(&(se->_name_offset)));
213         }
214
215         ptr = symfile->raw_contents + read32(&(minfo->entry->_line_number_table_offset));
216
217         lne = (MonoSymbolFileLineNumberEntry *) ptr;
218
219         for (i = 0; i < read32(&(minfo->entry->_num_line_numbers)); i++, lne++) {
220                 if (read32(&(lne->_offset)) < offset)
221                         continue;
222
223                 if (line_number) {
224                         *line_number = read32(&(lne->_row));
225                         mono_debugger_unlock ();
226                         if (source_file)
227                                 return source_file;
228                         else
229                                 return NULL;
230                 } else if (source_file) {
231                         gchar *retval = g_strdup_printf ("%s:%d", source_file, read32(&(lne->_row)));
232                         g_free (source_file);
233                         mono_debugger_unlock ();
234                         return retval;
235                 } else {
236                         gchar* retval = g_strdup_printf ("%d", read32(&(lne->_row)));
237                         mono_debugger_unlock ();
238                         return retval;
239                 }
240         }
241
242         mono_debugger_unlock ();
243         return NULL;
244 }
245
246 gint32
247 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, guint32 il_offset)
248 {
249         int i;
250
251         if (!jit || !jit->line_numbers)
252                 return -1;
253
254         for (i = jit->num_line_numbers - 1; i >= 0; i--) {
255                 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
256
257                 if (lne.il_offset <= il_offset)
258                         return lne.native_offset;
259         }
260
261         return -1;
262 }
263
264 static int
265 compare_method (const void *key, const void *object)
266 {
267         guint32 token = GPOINTER_TO_UINT (key);
268         MonoSymbolFileMethodIndexEntry *me = (MonoSymbolFileMethodIndexEntry*)object;
269
270         return token - read32(&(me->_token));
271 }
272
273 MonoDebugMethodInfo *
274 mono_debug_find_method (MonoDebugHandle *handle, MonoMethod *method)
275 {
276         MonoSymbolFileMethodEntry *me;
277         MonoSymbolFileMethodIndexEntry *first_ie, *ie;
278         MonoDebugMethodInfo *minfo;
279         MonoSymbolFile *symfile = handle->symfile;
280
281         if (!symfile->method_hash)
282                 return NULL;
283
284         if (handle->image != mono_class_get_image (mono_method_get_class (method)))
285                 return NULL;
286
287         mono_debugger_lock ();
288         first_ie = (MonoSymbolFileMethodIndexEntry *)
289                 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
290
291         ie = bsearch (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
292                                    read32(&(symfile->offset_table->_method_count)),
293                                    sizeof (MonoSymbolFileMethodIndexEntry), compare_method);
294
295         if (!ie) {
296                 mono_debugger_unlock ();
297                 return NULL;
298         }
299
300         me = (MonoSymbolFileMethodEntry *) (symfile->raw_contents + read32(&(ie->_file_offset)));
301
302         minfo = g_new0 (MonoDebugMethodInfo, 1);
303         minfo->index = (ie - first_ie) + 1;
304         minfo->method = method;
305         minfo->handle = handle;
306         minfo->num_il_offsets = read32(&(me->_num_line_numbers));
307         minfo->il_offsets = (MonoSymbolFileLineNumberEntry *)
308                 (symfile->raw_contents + read32(&(me->_line_number_table_offset)));
309         minfo->num_lexical_blocks = read32(&(me->_num_lexical_blocks));
310         minfo->lexical_blocks = (MonoSymbolFileLexicalBlockEntry *)
311                 (symfile->raw_contents + read32(&(me->_lexical_block_table_offset)));
312         minfo->entry = me;
313
314         g_hash_table_insert (symfile->method_hash, method, minfo);
315
316         mono_debugger_unlock ();
317         return minfo;
318 }