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