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