Merge pull request #2381 from alexanderkyte/pdb_method_assert
[mono.git] / mono / metadata / debug-mono-ppdb.c
1 /*
2  * debug-mono-ppdb.c: Support for the portable PDB symbol
3  * file format
4  *
5  *
6  * Author:
7  *      Mono Project (http://www.mono-project.com)
8  *
9  * Copyright 2015 Xamarin Inc (http://www.xamarin.com)
10  */
11
12 #include <config.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <mono/metadata/metadata.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/tokentype.h>
20 #include <mono/metadata/debug-helpers.h>
21 #include <mono/metadata/mono-debug.h>
22 #include <mono/metadata/debug-mono-symfile.h>
23 #include <mono/metadata/mono-debug-debugger.h>
24 #include <mono/metadata/mono-endian.h>
25 #include <mono/metadata/metadata-internals.h>
26 #include <mono/metadata/class-internals.h>
27 #include <mono/metadata/cil-coff.h>
28
29 #include "debug-mono-ppdb.h"
30
31 struct _MonoPPDBFile {
32         MonoImage *image;
33         GHashTable *doc_hash;
34         GHashTable *method_hash;
35 };
36
37 /* IMAGE_DEBUG_DIRECTORY structure */
38 typedef struct
39 {
40         gint32 characteristics;
41         gint32 time_date_stamp;
42         gint16 major_version;
43         gint16 minor_version;
44         gint32 type;
45         gint32 size_of_data;
46         gint32 address;
47         gint32 pointer;
48 }  ImageDebugDirectory;
49
50 typedef struct {
51         gint32 signature;
52         guint8 guid [16];
53         gint32 age;
54 } CodeviewDebugDirectory;
55
56 typedef struct {
57         guint8 guid [20];
58         guint32 entry_point;
59         guint64 referenced_tables;
60 } PdbStreamHeader;
61
62 static gboolean
63 get_pe_debug_guid (MonoImage *image, guint8 *out_guid, gint32 *out_age, gint32 *out_timestamp)
64 {
65         MonoPEDirEntry *debug_dir_entry;
66         ImageDebugDirectory *debug_dir;
67
68         debug_dir_entry = &((MonoCLIImageInfo*)image->image_info)->cli_header.datadir.pe_debug;
69         if (!debug_dir_entry->size)
70                 return FALSE;
71
72         int offset = mono_cli_rva_image_map (image, debug_dir_entry->rva);
73         debug_dir = (ImageDebugDirectory*)(image->raw_data + offset);
74         if (debug_dir->type == 2 && debug_dir->major_version == 0x100 && debug_dir->minor_version == 0x504d) {
75                 /* This is a 'CODEVIEW' debug directory */
76                 CodeviewDebugDirectory *dir = (CodeviewDebugDirectory*)(image->raw_data + debug_dir->pointer);
77
78                 if (dir->signature == 0x53445352) {
79                         memcpy (out_guid, dir->guid, 16);
80                         *out_age = dir->age;
81                         *out_timestamp = debug_dir->time_date_stamp;
82                         return TRUE;
83                 }
84         }
85         return FALSE;
86 }
87
88 static void
89 doc_free (gpointer key)
90 {
91         MonoDebugSourceInfo *info = (MonoDebugSourceInfo *)key;
92
93         g_free (info->source_file);
94         g_free (info);
95 }
96
97 MonoPPDBFile*
98 mono_ppdb_load_file (MonoImage *image, const guint8 *raw_contents, int size)
99 {
100         MonoImage *ppdb_image;
101         const char *filename;
102         char *s, *ppdb_filename;
103         MonoImageOpenStatus status;
104         guint8 pe_guid [16];
105         gint32 pe_age;
106         gint32 pe_timestamp;
107         MonoPPDBFile *ppdb;
108
109         if (raw_contents) {
110                 ppdb_image = mono_image_open_from_data_internal ((char*)raw_contents, size, TRUE, &status, FALSE, TRUE, NULL);
111         } else {
112                 /* ppdb files drop the .exe/.dll extension */
113                 filename = mono_image_get_filename (image);
114                 if (strlen (filename) > 4 && (!strcmp (filename + strlen (filename) - 4, ".exe") || !strcmp (filename + strlen (filename) - 4, ".dll"))) {
115                         s = g_strdup (filename);
116                         s [strlen (filename) - 4] = '\0';
117                         ppdb_filename = g_strdup_printf ("%s.pdb", s);
118                         g_free (s);
119                 } else {
120                         ppdb_filename = g_strdup_printf ("%s.pdb", filename);
121                 }
122
123                 ppdb_image = mono_image_open_metadata_only (ppdb_filename, &status);
124                 if (!ppdb_image)
125                         g_free (ppdb_filename);
126         }
127         if (!ppdb_image)
128                 return NULL;
129
130         /*
131          * Check that the images match.
132          * The same id is stored in the Debug Directory of the PE file, and in the
133          * #Pdb stream in the ppdb file.
134          */
135         if (get_pe_debug_guid (image, pe_guid, &pe_age, &pe_timestamp)) {
136                 PdbStreamHeader *pdb_stream = (PdbStreamHeader*)ppdb_image->heap_pdb.data;
137
138                 g_assert (pdb_stream);
139
140                 /* The pdb id is a concentation of the pe guid and the timestamp */
141                 if (memcmp (pe_guid, pdb_stream->guid, 16) != 0 || memcmp (&pe_timestamp, pdb_stream->guid + 16, 4) != 0) {
142                         g_warning ("Symbol file %s doesn't match image %s", ppdb_image->name,
143                                            image->name);
144                         mono_image_close (ppdb_image);
145                         return NULL;
146                 }
147         }
148
149         ppdb = g_new0 (MonoPPDBFile, 1);
150         ppdb->image = ppdb_image;
151         ppdb->doc_hash = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) doc_free);
152         ppdb->method_hash = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_free);
153
154         return ppdb;
155 }
156
157 void
158 mono_ppdb_close (MonoDebugHandle *handle)
159 {
160         MonoPPDBFile *ppdb = handle->ppdb;
161
162         mono_image_close (ppdb->image);
163         g_hash_table_destroy (ppdb->doc_hash);
164         g_hash_table_destroy (ppdb->method_hash);
165         g_free (ppdb);
166 }
167
168 MonoDebugMethodInfo *
169 mono_ppdb_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
170 {
171         MonoDebugMethodInfo *minfo;
172         MonoPPDBFile *ppdb = handle->ppdb;
173
174         if (handle->image != mono_class_get_image (mono_method_get_class (method)))
175                 return NULL;
176
177         mono_debugger_lock ();
178
179         minfo = (MonoDebugMethodInfo *)g_hash_table_lookup (ppdb->method_hash, method);
180         if (minfo) {
181                 mono_debugger_unlock ();
182                 return minfo;
183         }
184
185         minfo = g_new0 (MonoDebugMethodInfo, 1);
186         minfo->index = 0;
187         minfo->method = method;
188         minfo->handle = handle;
189
190         g_hash_table_insert (ppdb->method_hash, method, minfo);
191
192         mono_debugger_unlock ();
193
194         return minfo;
195 }
196
197 static MonoDebugSourceInfo*
198 get_docinfo (MonoPPDBFile *ppdb, MonoImage *image, int docidx)
199 {
200         MonoTableInfo *tables = image->tables;
201         guint32 cols [MONO_DOCUMENT_SIZE];
202         const char *ptr;
203         const char *start;
204         const char *part_ptr;
205         int size, part_size, partidx, nparts;
206         char sep;
207         GString *s;
208         MonoDebugSourceInfo *res, *cached;
209
210         mono_debugger_lock ();
211         cached = (MonoDebugSourceInfo *)g_hash_table_lookup (ppdb->doc_hash, GUINT_TO_POINTER (docidx));
212         mono_debugger_unlock ();
213         if (cached)
214                 return cached;
215
216         mono_metadata_decode_row (&tables [MONO_TABLE_DOCUMENT], docidx-1, cols, MONO_DOCUMENT_SIZE);
217
218         ptr = mono_metadata_blob_heap (image, cols [MONO_DOCUMENT_NAME]);
219         size = mono_metadata_decode_blob_size (ptr, &ptr);
220         start = ptr;
221
222         // FIXME: UTF8
223         sep = ptr [0];
224         ptr ++;
225
226         s = g_string_new ("");
227
228         nparts = 0;
229         while (ptr < start + size) {
230                 partidx = mono_metadata_decode_value (ptr, &ptr);
231                 if (nparts)
232                         g_string_append_c (s, sep);
233                 if (partidx) {
234                         part_ptr = mono_metadata_blob_heap (image, partidx);
235                         part_size = mono_metadata_decode_blob_size (part_ptr, &part_ptr);
236
237                         // FIXME: UTF8
238                         g_string_append_len (s, part_ptr, part_size);
239                 }
240                 nparts ++;
241         }
242
243         res = g_new0 (MonoDebugSourceInfo, 1);
244         res->source_file = g_string_free (s, FALSE);
245         res->guid = NULL;
246         res->hash = (guint8*)mono_metadata_blob_heap (image, cols [MONO_DOCUMENT_HASH]);
247
248         mono_debugger_lock ();
249         cached = (MonoDebugSourceInfo *)g_hash_table_lookup (ppdb->doc_hash, GUINT_TO_POINTER (docidx));
250         if (!cached) {
251                 g_hash_table_insert (ppdb->doc_hash, GUINT_TO_POINTER (docidx), res);
252         } else {
253                 doc_free (res);
254                 res = cached;
255         }
256         mono_debugger_unlock ();
257         return res;
258 }
259
260 static char*
261 get_docname (MonoPPDBFile *ppdb, MonoImage *image, int docidx)
262 {
263         MonoDebugSourceInfo *info;
264
265         info = get_docinfo (ppdb, image, docidx);
266         return g_strdup (info->source_file);
267 }
268
269 /**
270  * mono_ppdb_lookup_location:
271  * @minfo: A `MonoDebugMethodInfo' which can be retrieved by
272  *         mono_debug_lookup_method().
273  * @offset: IL offset within the corresponding method's CIL code.
274  *
275  * This function is similar to mono_debug_lookup_location(), but we
276  * already looked up the method and also already did the
277  * `native address -> IL offset' mapping.
278  */
279 MonoDebugSourceLocation *
280 mono_ppdb_lookup_location (MonoDebugMethodInfo *minfo, uint32_t offset)
281 {
282         MonoPPDBFile *ppdb = minfo->handle->ppdb;
283         MonoImage *image = ppdb->image;
284         MonoMethod *method = minfo->method;
285         MonoTableInfo *tables = image->tables;
286         guint32 cols [MONO_METHODBODY_SIZE];
287         const char *ptr;
288         const char *end;
289         char *docname;
290         int idx, size, docidx, iloffset, delta_il, delta_lines, delta_cols, start_line, start_col, adv_line, adv_col;
291         gboolean first = TRUE, first_non_hidden = TRUE;
292         MonoDebugSourceLocation *location;
293
294         if (!method->token)
295                 return NULL;
296
297         idx = mono_metadata_token_index (method->token);
298
299         mono_metadata_decode_row (&tables [MONO_TABLE_METHODBODY], idx-1, cols, MONO_METHODBODY_SIZE);
300
301         docidx = cols [MONO_METHODBODY_DOCUMENT];
302
303         if (!cols [MONO_METHODBODY_SEQ_POINTS])
304                 return NULL;
305         ptr = mono_metadata_blob_heap (image, cols [MONO_METHODBODY_SEQ_POINTS]);
306         size = mono_metadata_decode_blob_size (ptr, &ptr);
307         end = ptr + size;
308
309         /* Header */
310         /* LocalSignature */
311         mono_metadata_decode_value (ptr, &ptr);
312         if (docidx == 0)
313                 docidx = mono_metadata_decode_value (ptr, &ptr);
314         docname = get_docname (ppdb, image, docidx);
315
316         iloffset = 0;
317         start_line = 0;
318         start_col = 0;
319         while (ptr < end) {
320                 delta_il = mono_metadata_decode_value (ptr, &ptr);
321                 if (!first && delta_il == 0) {
322                         /* document-record */
323                         docidx = mono_metadata_decode_value (ptr, &ptr);
324                         docname = get_docname (ppdb, image, docidx);
325                         continue;
326                 }
327                 if (!first && iloffset + delta_il > offset)
328                         break;
329                 iloffset += delta_il;
330                 first = FALSE;
331
332                 delta_lines = mono_metadata_decode_value (ptr, &ptr);
333                 if (delta_lines == 0)
334                         delta_cols = mono_metadata_decode_value (ptr, &ptr);
335                 else
336                         delta_cols = mono_metadata_decode_signed_value (ptr, &ptr);
337                 if (delta_lines == 0 && delta_cols == 0)
338                         /* hidden-sequence-point-record */
339                         continue;
340                 if (first_non_hidden) {
341                         start_line = mono_metadata_decode_value (ptr, &ptr);
342                         start_col = mono_metadata_decode_value (ptr, &ptr);
343                 } else {
344                         adv_line = mono_metadata_decode_signed_value (ptr, &ptr);
345                         adv_col = mono_metadata_decode_signed_value (ptr, &ptr);
346                         start_line += adv_line;
347                         start_col += adv_col;
348                 }
349                 first_non_hidden = FALSE;
350         }
351
352         location = g_new0 (MonoDebugSourceLocation, 1);
353         location->source_file = docname;
354         location->row = start_line;
355         location->il_offset = iloffset;
356
357         return location;
358 }
359
360 void
361 mono_ppdb_get_seq_points (MonoDebugMethodInfo *minfo, char **source_file, GPtrArray **source_file_list, int **source_files, MonoSymSeqPoint **seq_points, int *n_seq_points)
362 {
363         MonoPPDBFile *ppdb = minfo->handle->ppdb;
364         MonoImage *image = ppdb->image;
365         MonoMethod *method = minfo->method;
366         MonoTableInfo *tables = image->tables;
367         guint32 cols [MONO_METHODBODY_SIZE];
368         const char *ptr;
369         const char *end;
370         MonoDebugSourceInfo *docinfo;
371         int i, method_idx, size, docidx, iloffset, delta_il, delta_lines, delta_cols, start_line, start_col, adv_line, adv_col;
372         gboolean first = TRUE, first_non_hidden = TRUE;
373         GArray *sps;
374         MonoSymSeqPoint sp;
375         GPtrArray *sfiles = NULL;
376         GPtrArray *sindexes = NULL;
377
378         if (source_file)
379                 *source_file = NULL;
380         if (source_file_list)
381                 *source_file_list = NULL;
382         if (source_files)
383                 *source_files = NULL;
384         if (seq_points)
385                 *seq_points = NULL;
386         if (n_seq_points)
387                 *n_seq_points = 0;
388
389         if (source_file_list)
390                 *source_file_list = sfiles = g_ptr_array_new ();
391         if (source_files)
392                 sindexes = g_ptr_array_new ();
393
394         if (!method->token)
395                 return;
396
397         method_idx = mono_metadata_token_index (method->token);
398
399         mono_metadata_decode_row (&tables [MONO_TABLE_METHODBODY], method_idx-1, cols, MONO_METHODBODY_SIZE);
400
401         docidx = cols [MONO_METHODBODY_DOCUMENT];
402
403         if (!cols [MONO_METHODBODY_SEQ_POINTS])
404                 return;
405
406         ptr = mono_metadata_blob_heap (image, cols [MONO_METHODBODY_SEQ_POINTS]);
407         size = mono_metadata_decode_blob_size (ptr, &ptr);
408         end = ptr + size;
409
410         sps = g_array_new (FALSE, TRUE, sizeof (MonoSymSeqPoint));
411
412         /* Header */
413         /* LocalSignature */
414         mono_metadata_decode_value (ptr, &ptr);
415         if (docidx == 0)
416                 docidx = mono_metadata_decode_value (ptr, &ptr);
417         docinfo = get_docinfo (ppdb, image, docidx);
418
419         if (sfiles)
420                 g_ptr_array_add (sfiles, docinfo);
421
422         iloffset = 0;
423         start_line = 0;
424         start_col = 0;
425         while (ptr < end) {
426                 delta_il = mono_metadata_decode_value (ptr, &ptr);
427                 if (!first && delta_il == 0) {
428                         /* subsequent-document-record */
429                         docidx = mono_metadata_decode_value (ptr, &ptr);
430                         docinfo = get_docinfo (ppdb, image, docidx);
431                         if (sfiles)
432                                 g_ptr_array_add (sfiles, docinfo);
433                         continue;
434                 }
435                 iloffset += delta_il;
436                 first = FALSE;
437
438                 delta_lines = mono_metadata_decode_value (ptr, &ptr);
439                 if (delta_lines == 0)
440                         delta_cols = mono_metadata_decode_value (ptr, &ptr);
441                 else
442                         delta_cols = mono_metadata_decode_signed_value (ptr, &ptr);
443
444                 if (delta_lines == 0 && delta_cols == 0) {
445                         /* Hidden sequence point */
446                         continue;
447                 }
448
449                 if (first_non_hidden) {
450                         start_line = mono_metadata_decode_value (ptr, &ptr);
451                         start_col = mono_metadata_decode_value (ptr, &ptr);
452                 } else {
453                         adv_line = mono_metadata_decode_signed_value (ptr, &ptr);
454                         adv_col = mono_metadata_decode_signed_value (ptr, &ptr);
455                         start_line += adv_line;
456                         start_col += adv_col;
457                 }
458                 first_non_hidden = FALSE;
459
460                 memset (&sp, 0, sizeof (sp));
461                 sp.il_offset = iloffset;
462                 sp.line = start_line;
463                 sp.column = start_col;
464                 sp.end_line = start_line + delta_lines;
465                 sp.end_column = start_col + delta_cols;
466
467                 g_array_append_val (sps, sp);
468                 if (source_files)
469                         g_ptr_array_add (sindexes, GUINT_TO_POINTER (sfiles->len - 1));
470         }
471
472         if (n_seq_points) {
473                 *n_seq_points = sps->len;
474                 g_assert (seq_points);
475                 *seq_points = g_new (MonoSymSeqPoint, sps->len);
476                 memcpy (*seq_points, sps->data, sps->len * sizeof (MonoSymSeqPoint));
477         }
478
479         if (source_file)
480                 *source_file = g_strdup (((MonoDebugSourceInfo*)g_ptr_array_index (sfiles, 0))->source_file);
481         if (source_files) {
482                 *source_files = g_new (int, sps->len);
483                 for (i = 0; i < sps->len; ++i)
484                         (*source_files)[i] = GPOINTER_TO_INT (g_ptr_array_index (sindexes, i));
485                 g_ptr_array_free (sindexes, TRUE);
486         }
487
488         g_array_free (sps, TRUE);
489 }
490
491 MonoDebugLocalsInfo*
492 mono_ppdb_lookup_locals (MonoDebugMethodInfo *minfo)
493 {
494         MonoPPDBFile *ppdb = minfo->handle->ppdb;
495         MonoImage *image = ppdb->image;
496         MonoTableInfo *tables = image->tables;
497         MonoMethod *method = minfo->method;
498         guint32 cols [MONO_LOCALSCOPE_SIZE];
499         guint32 locals_cols [MONO_LOCALVARIABLE_SIZE];
500         int i, lindex, sindex, method_idx, start_scope_idx, scope_idx, locals_idx, locals_end_idx, nscopes;
501         MonoDebugLocalsInfo *res;
502         MonoMethodSignature *sig;
503
504         if (!method->token)
505                 return NULL;
506
507         sig = mono_method_signature (method);
508         if (!sig)
509                 return NULL;
510
511         method_idx = mono_metadata_token_index (method->token);
512
513         start_scope_idx = mono_metadata_localscope_from_methoddef (image, method_idx);
514
515         if (!start_scope_idx)
516                 return NULL;
517
518         /* Compute number of locals and scopes */
519         scope_idx = start_scope_idx;
520         mono_metadata_decode_row (&tables [MONO_TABLE_LOCALSCOPE], scope_idx-1, cols, MONO_LOCALSCOPE_SIZE);
521         locals_idx = cols [MONO_LOCALSCOPE_VARIABLELIST];
522         while (scope_idx == tables [MONO_TABLE_LOCALSCOPE].rows) {
523                 mono_metadata_decode_row (&tables [MONO_TABLE_LOCALSCOPE], scope_idx-1, cols, MONO_LOCALSCOPE_SIZE);
524                 if (cols [MONO_LOCALSCOPE_METHOD] != method_idx)
525                         break;
526                 scope_idx ++;
527         }
528         nscopes = scope_idx - start_scope_idx;
529         if (scope_idx == tables [MONO_TABLE_LOCALSCOPE].rows) {
530                 locals_end_idx = tables [MONO_TABLE_LOCALVARIABLE].rows;
531         } else {
532                 locals_end_idx = cols [MONO_LOCALSCOPE_VARIABLELIST];
533         }
534
535         res = g_new0 (MonoDebugLocalsInfo, 1);
536         res->num_blocks = nscopes;
537         res->code_blocks = g_new0 (MonoDebugCodeBlock, res->num_blocks);
538         res->num_locals = locals_end_idx - locals_idx;
539         res->locals = g_new0 (MonoDebugLocalVar, res->num_locals);
540
541         lindex = 0;
542         for (sindex = 0; sindex < nscopes; ++sindex) {
543                 scope_idx = start_scope_idx + sindex;
544                 mono_metadata_decode_row (&tables [MONO_TABLE_LOCALSCOPE], scope_idx-1, cols, MONO_LOCALSCOPE_SIZE);
545
546                 locals_idx = cols [MONO_LOCALSCOPE_VARIABLELIST];
547                 if (scope_idx == tables [MONO_TABLE_LOCALSCOPE].rows) {
548                         locals_end_idx = tables [MONO_TABLE_LOCALVARIABLE].rows;
549                 } else {
550                         locals_end_idx = mono_metadata_decode_row_col (&tables [MONO_TABLE_LOCALSCOPE], scope_idx-1 + 1, MONO_LOCALSCOPE_VARIABLELIST);
551                 }
552
553                 res->code_blocks [sindex].start_offset = cols [MONO_LOCALSCOPE_STARTOFFSET];
554                 res->code_blocks [sindex].end_offset = cols [MONO_LOCALSCOPE_STARTOFFSET] + cols [MONO_LOCALSCOPE_LENGTH];
555
556                 //printf ("Scope: %s %d %d %d-%d\n", mono_method_full_name (method, 1), cols [MONO_LOCALSCOPE_STARTOFFSET], cols [MONO_LOCALSCOPE_LENGTH], locals_idx, locals_end_idx);
557
558                 for (i = locals_idx; i < locals_end_idx; ++i) {
559                         mono_metadata_decode_row (&tables [MONO_TABLE_LOCALVARIABLE], i - 1, locals_cols, MONO_LOCALVARIABLE_SIZE);
560
561                         res->locals [lindex].name = g_strdup (mono_metadata_string_heap (image, locals_cols [MONO_LOCALVARIABLE_NAME]));
562                         res->locals [lindex].index = locals_cols [MONO_LOCALVARIABLE_INDEX];
563                         res->locals [lindex].block = &res->code_blocks [sindex];
564                         lindex ++;
565
566                         //printf ("\t %s %d\n", mono_metadata_string_heap (image, locals_cols [MONO_LOCALVARIABLE_NAME]), locals_cols [MONO_LOCALVARIABLE_INDEX]);
567                 }
568         }
569
570         return res;
571 }