Tue Aug 3 17:54:17 CEST 2004 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / debug-mono-symfile.c
1 #include <config.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <sys/param.h>
6 #include <mono/metadata/metadata.h>
7 #include <mono/metadata/tabledefs.h>
8 #include <mono/metadata/rawbuffer.h>
9 #include <mono/metadata/tokentype.h>
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/exception.h>
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/mono-debug.h>
14 #include <mono/metadata/debug-mono-symfile.h>
15 #include <mono/metadata/mono-endian.h>
16 #include <mono/metadata/metadata-internals.h>
17
18 #include <fcntl.h>
19 #include <unistd.h>
20
21 #define RANGE_TABLE_CHUNK_SIZE          256
22 #define CLASS_TABLE_CHUNK_SIZE          256
23 #define TYPE_TABLE_PTR_CHUNK_SIZE       256
24 #define TYPE_TABLE_CHUNK_SIZE           65536
25
26 static void
27 free_method_info (MonoDebugMethodInfo *minfo)
28 {
29         g_free (minfo);
30 }
31
32 static gchar *
33 get_class_name (MonoClass *klass)
34 {
35         MonoClass *nested_in = mono_class_get_nesting_type (klass);
36         const char *name_space;
37         if (nested_in) {
38                 gchar *parent_name = get_class_name (nested_in);
39                 gchar *name = g_strdup_printf ("%s.%s", parent_name, mono_class_get_name (klass));
40                 g_free (parent_name);
41                 return name;
42         }
43
44         name_space = mono_class_get_namespace (klass);
45         return g_strdup_printf ("%s%s%s", name_space,
46                                 name_space [0] ? "." : "", mono_class_get_name (klass));
47 }
48
49 static int
50 load_symfile (MonoDebugHandle *handle, MonoSymbolFile *symfile)
51 {
52         const char *ptr, *start;
53         gchar *guid;
54         guint64 magic;
55         long version;
56
57         ptr = start = symfile->raw_contents;
58         if (!ptr)
59                 return FALSE;
60
61         magic = read64(ptr);
62         ptr += sizeof(guint64);
63         if (magic != MONO_SYMBOL_FILE_MAGIC) {
64                 g_warning ("Symbol file %s is not a mono symbol file", symfile->filename);
65                 return FALSE;
66         }
67
68         version = read32(ptr);
69         ptr += sizeof(guint32);
70         if (version != MONO_SYMBOL_FILE_VERSION) {
71                 g_warning ("Symbol file %s has incorrect version "
72                            "(expected %d, got %ld)", symfile->filename,
73                            MONO_SYMBOL_FILE_VERSION, version);
74                 return FALSE;
75         }
76
77         guid = mono_guid_to_string ((const guint8 *) ptr);
78         ptr += 16;
79
80         if (strcmp (handle->image->guid, guid)) {
81                 g_warning ("Symbol file %s doesn't match image %s", symfile->filename,
82                            handle->image_file);
83                 return FALSE;
84         }
85
86         symfile->offset_table = (MonoSymbolFileOffsetTable *) ptr;
87
88         symfile->method_hash = g_hash_table_new_full (
89                 g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) free_method_info);
90
91         return TRUE;
92 }
93
94 MonoSymbolFile *
95 mono_debug_open_mono_symbol_file (MonoDebugHandle *handle, gboolean create_symfile)
96 {
97         MonoSymbolFile *symfile;
98
99         symfile = g_new0 (MonoSymbolFile, 1);
100
101         symfile->filename = g_strdup_printf ("%s.mdb", mono_image_get_filename (handle->image));
102
103         if (!g_file_get_contents (symfile->filename, &symfile->raw_contents,
104                                   &symfile->raw_contents_size, NULL))
105                 symfile->raw_contents = NULL;
106
107         if (load_symfile (handle, symfile))
108                 return symfile;
109         else if (!create_symfile) {
110                 mono_debug_close_mono_symbol_file (symfile);
111                 return NULL;
112         }
113
114         return symfile;
115 }
116
117 void
118 mono_debug_close_mono_symbol_file (MonoSymbolFile *symfile)
119 {
120         if (!symfile)
121                 return;
122
123         if (symfile->method_hash)
124                 g_hash_table_destroy (symfile->method_hash);
125
126         g_free (symfile);
127 }
128
129 static int
130 read_leb128 (const char *ptr, const char **rptr)
131 {
132         int ret = 0;
133         int shift = 0;
134         char b;
135
136         do {
137                 b = *ptr++;
138                                 
139                 ret = ret | ((b & 0x7f) << shift);
140                 shift += 7;
141         } while ((b & 0x80) == 0x80);
142
143         if (rptr)
144                 *rptr = ptr;
145
146         return ret;
147 }
148
149 static gchar *
150 read_string (const char *ptr)
151 {
152         int len = read_leb128 (ptr, &ptr);
153         return g_filename_from_utf8 (ptr, len, NULL, NULL, NULL);
154 }
155
156 gchar *
157 mono_debug_find_source_location (MonoSymbolFile *symfile, MonoMethod *method, guint32 offset,
158                                  guint32 *line_number)
159 {
160         MonoSymbolFileLineNumberEntry *lne;
161         MonoDebugMethodInfo *minfo;
162         gchar *source_file = NULL;
163         const char *ptr;
164         int i;
165
166         if (!symfile->method_hash)
167                 return NULL;
168
169         minfo = g_hash_table_lookup (symfile->method_hash, method);
170         if (!minfo)
171                 return NULL;
172
173         if (read32(&(minfo->entry->_source_index))) {
174                 int offset = read32(&(symfile->offset_table->_source_table_offset)) +
175                         (read32(&(minfo->entry->_source_index)) - 1) * sizeof (MonoSymbolFileSourceEntry);
176                 MonoSymbolFileSourceEntry *se = (MonoSymbolFileSourceEntry *) (symfile->raw_contents + offset);
177
178                 source_file = read_string (symfile->raw_contents + read32(&(se->_name_offset)));
179         }
180
181         ptr = symfile->raw_contents + read32(&(minfo->entry->_line_number_table_offset));
182
183         lne = (MonoSymbolFileLineNumberEntry *) ptr;
184
185         for (i = 0; i < read32(&(minfo->entry->_num_line_numbers)); i++, lne++) {
186                 if (read32(&(lne->_offset)) < offset)
187                         continue;
188
189                 if (line_number) {
190                         *line_number = read32(&(lne->_row));
191                         if (source_file)
192                                 return source_file;
193                         else
194                                 return NULL;
195                 } else if (source_file) {
196                         gchar *retval = g_strdup_printf ("%s:%d", source_file, read32(&(lne->_row)));
197                         g_free (source_file);
198                         return retval;
199                 } else
200                         return g_strdup_printf ("%d", read32(&(lne->_row)));
201         }
202
203         return NULL;
204 }
205
206 gint32
207 _mono_debug_address_from_il_offset (MonoDebugMethodJitInfo *jit, guint32 il_offset)
208 {
209         int i;
210
211         if (!jit || !jit->line_numbers)
212                 return -1;
213
214         for (i = jit->line_numbers->len - 1; i >= 0; i--) {
215                 MonoDebugLineNumberEntry lne = g_array_index (
216                         jit->line_numbers, MonoDebugLineNumberEntry, i);
217
218                 if (lne.offset <= il_offset)
219                         return lne.address;
220         }
221
222         return -1;
223 }
224
225 static int
226 compare_method (const void *key, const void *object)
227 {
228         guint32 token = GPOINTER_TO_UINT (key);
229         MonoSymbolFileMethodIndexEntry *me = (MonoSymbolFileMethodIndexEntry*)object;
230
231         return token - read32(&(me->_token));
232 }
233
234 MonoDebugMethodInfo *
235 mono_debug_find_method (MonoDebugHandle *handle, MonoMethod *method)
236 {
237         MonoSymbolFileMethodEntry *me;
238         MonoSymbolFileMethodIndexEntry *first_ie, *ie;
239         MonoDebugMethodInfo *minfo;
240         MonoSymbolFile *symfile = handle->symfile;
241
242         if (!symfile->method_hash)
243                 return NULL;
244
245         if (handle->image != mono_class_get_image (mono_method_get_class (method)))
246                 return NULL;
247
248         first_ie = (MonoSymbolFileMethodIndexEntry *)
249                 (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
250
251         ie = bsearch (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
252                                    read32(&(symfile->offset_table->_method_count)),
253                                    sizeof (MonoSymbolFileMethodIndexEntry), compare_method);
254
255         if (!ie)
256                 return NULL;
257
258         me = (MonoSymbolFileMethodEntry *) (symfile->raw_contents + read32(&(ie->_file_offset)));
259
260         minfo = g_new0 (MonoDebugMethodInfo, 1);
261         minfo->index = (ie - first_ie) + 1;
262         minfo->method = method;
263         minfo->handle = handle;
264         minfo->num_il_offsets = read32(&(me->_num_line_numbers));
265         minfo->il_offsets = (MonoSymbolFileLineNumberEntry *)
266                 (symfile->raw_contents + read32(&(me->_line_number_table_offset)));
267         minfo->entry = me;
268
269         g_hash_table_insert (symfile->method_hash, method, minfo);
270
271         return minfo;
272 }