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