2003-02-18 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / Mono.CSharp.Debugger / MonoSymbolFile.cs
1 //
2 // System.Diagnostics.SymbolStore/MonoSymbolFile.cs
3 //
4 // Author:
5 //   Martin Baulig (martin@gnome.org)
6 //
7 // (C) 2003 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Reflection;
12 using System.Reflection.Emit;
13 using System.Collections;
14 using System.Text;
15 using System.IO;
16         
17 namespace Mono.CSharp.Debugger
18 {
19         public class MonoSymbolFileException : Exception
20         {
21                 public MonoSymbolFileException ()
22                         : base ()
23                 { }
24
25                 public MonoSymbolFileException (string message, params object[] args)
26                         : base (String.Format (message, args))
27                 { }
28         }
29
30         internal class MyMemoryStream : Stream
31         {
32                 int length;
33                 int real_length;
34                 int position;
35
36                 int chunk_size = 4096;
37                 ArrayList chunks = new ArrayList ();
38
39                 private struct Chunk {
40                         public readonly int Offset;
41                         public readonly int Length;
42                         public byte[] Buffer;
43
44                         public Chunk (int offset, int length)
45                         {
46                                 this.Offset = offset;
47                                 this.Length = length;
48                                 this.Buffer = new Byte [length];
49                         }
50                 }
51
52                 public override long Position {
53                         get { return position; }
54
55                         set {
56                                 if (value > length)
57                                         throw new ArgumentOutOfRangeException ();
58
59                                 position = (int) value;
60                         }
61                 }
62
63                 public override long Length {
64                         get { return length; }
65                 }
66
67                 public override bool CanRead {
68                         get { return true; }
69                 }
70
71                 public override bool CanWrite {
72                         get { return true; }
73                 }
74
75                 public override bool CanSeek {
76                         get { return true; }
77                 }
78
79                 public override void SetLength (long new_length)
80                 {
81                         if (new_length < length)
82                                 throw new ArgumentException ();
83
84                         while (new_length >= real_length) {
85                                 Chunk new_chunk = new Chunk (real_length, chunk_size);
86                                 chunks.Add (new_chunk);
87                                 real_length += chunk_size;
88                         }
89
90                         length = (int) new_length;
91                 }
92
93                 public override void Flush ()
94                 { }
95
96                 public override long Seek (long offset, SeekOrigin origin)
97                 {
98                         int ref_point;
99
100                         switch (origin) {
101                         case SeekOrigin.Begin:
102                                 ref_point = 0;
103                                 break;
104                         case SeekOrigin.Current:
105                                 ref_point = position;
106                                 break;
107                         case SeekOrigin.End:
108                                 ref_point = length;
109                                 break;
110                         default:
111                                 throw new ArgumentException ("Invalid SeekOrigin");
112                         }
113
114                         if ((ref_point + offset < 0) || (offset > real_length))
115                                 throw new ArgumentOutOfRangeException ();
116
117                         position = ref_point + (int) offset;
118
119                         return position;
120                 }
121
122                 Chunk FindChunk (int offset)
123                 {
124                         return (Chunk) chunks [offset / chunk_size];
125                 }
126
127                 public override int Read (byte[] buffer, int offset, int count)
128                 {
129                         int old_count = count;
130
131                         while (count > 0) {
132                                 Chunk chunk = FindChunk (position);
133                                 int coffset = position - chunk.Offset;
134                                 int rest = chunk.Length - coffset;
135                                 int size = System.Math.Min (count, rest);
136
137                                 Array.Copy (chunk.Buffer, coffset, buffer, offset, size);
138                                 position += size;
139                                 offset += size;
140                                 count -= size;
141                         }
142
143                         return old_count;
144                 }
145
146                 public override void Write (byte[] buffer, int offset, int count)
147                 {
148                         if (position + count > length)
149                                 SetLength (position + count);
150
151                         while (count > 0) {
152                                 Chunk chunk = FindChunk (position);
153                                 int coffset = position - chunk.Offset;
154                                 int rest = chunk.Length - coffset;
155                                 int size = System.Math.Min (count, rest);
156
157                                 Array.Copy (buffer, offset, chunk.Buffer, coffset, size);
158                                 position += size;
159                                 offset += size;
160                                 count -= size;
161                         }
162                 }
163
164                 public byte[] GetContents ()
165                 {
166                         byte[] retval = new byte [length];
167                         position = 0;
168                         Read (retval, 0, length);
169                         return retval;
170                 }
171         }
172
173         public class MonoSymbolFile : IDisposable
174         {
175                 ArrayList methods = new ArrayList ();
176                 ArrayList sources = new ArrayList ();
177                 Hashtable method_source_hash = new Hashtable ();
178                 Hashtable type_hash = new Hashtable ();
179
180                 OffsetTable ot;
181                 int last_type_index;
182                 int last_method_index;
183                 int last_source_index;
184                 int last_namespace_index;
185
186                 public MonoSymbolFile ()
187                 { }
188
189                 internal int AddSource (SourceFileEntry source)
190                 {
191                         sources.Add (source);
192                         return ++last_source_index;
193                 }
194
195                 internal int DefineType (Type type)
196                 {
197                         if (type_hash.Contains (type))
198                                 return (int) type_hash [type];
199
200                         int index = ++last_type_index;
201                         type_hash.Add (type, index);
202                         return index;
203                 }
204
205                 internal void AddMethod (MethodEntry entry)
206                 {
207                         methods.Add (entry);
208                 }
209
210                 internal int GetNextTypeIndex ()
211                 {
212                         return ++last_type_index;
213                 }
214
215                 internal int GetNextMethodIndex ()
216                 {
217                         return ++last_method_index;
218                 }
219
220                 internal int GetNextNamespaceIndex ()
221                 {
222                         return ++last_namespace_index;
223                 }
224
225                 internal void WriteString (BinaryWriter bw, string text)
226                 {
227                         byte[] data = Encoding.UTF8.GetBytes (text);
228                         bw.Write ((int) data.Length);
229                         bw.Write (data);
230                         StringSize += data.Length;
231                 }
232
233                 internal string ReadString (int offset)
234                 {
235                         int old_pos = (int) reader.BaseStream.Position;
236                         reader.BaseStream.Position = offset;
237                         int length = reader.ReadInt32 ();
238
239                         byte[] data = reader.ReadBytes (length);
240                         string text = Encoding.UTF8.GetString (data);
241                         reader.BaseStream.Position = old_pos;
242                         return text;
243                 }
244
245                 void Write (BinaryWriter bw)
246                 {
247                         // Magic number and file version.
248                         bw.Write (OffsetTable.Magic);
249                         bw.Write (OffsetTable.Version);
250
251                         //
252                         // Offsets of file sections; we must write this after we're done
253                         // writing the whole file, so we just reserve the space for it here.
254                         //
255                         long offset_table_offset = bw.BaseStream.Position;
256                         ot.Write (bw);
257
258                         //
259                         // Write data sections.
260                         //
261                         ot.DataSectionOffset = (int) bw.BaseStream.Position;
262                         foreach (SourceFileEntry source in sources)
263                                 source.WriteData (bw);
264                         ot.DataSectionSize = (int) bw.BaseStream.Position - ot.DataSectionOffset;
265
266                         //
267                         // Write method table.
268                         //
269                         ot.MethodTableOffset = (int) bw.BaseStream.Position;
270                         for (int i = 0; i < methods.Count; i++) {
271                                 MethodEntry entry = (MethodEntry) methods [i];
272                                 entry.WriteIndex (bw);
273                         }
274                         ot.MethodTableSize = (int) bw.BaseStream.Position - ot.MethodTableOffset;
275
276                         //
277                         // Write source table.
278                         //
279                         ot.SourceTableOffset = (int) bw.BaseStream.Position;
280                         for (int i = 0; i < sources.Count; i++) {
281                                 SourceFileEntry source = (SourceFileEntry) sources [i];
282                                 source.Write (bw);
283                         }
284                         ot.SourceTableSize = (int) bw.BaseStream.Position - ot.SourceTableOffset;
285
286                         //
287                         // Fixup offset table.
288                         //
289                         ot.TypeCount = last_type_index;
290                         ot.MethodCount = methods.Count;
291                         ot.SourceCount = sources.Count;
292
293                         //
294                         // Write offset table.
295                         //
296                         ot.TotalFileSize = (int) bw.BaseStream.Position;
297                         bw.Seek ((int) offset_table_offset, SeekOrigin.Begin);
298                         ot.Write (bw);
299                         bw.Seek (0, SeekOrigin.End);
300                 }
301
302                 public byte[] CreateSymbolFile ()
303                 {
304                         if (reader != null)
305                                 throw new InvalidOperationException ();
306
307                         using (MyMemoryStream stream = new MyMemoryStream ()) {
308                                 Write (new BinaryWriter (stream));
309                                 Console.WriteLine ("WROTE SYMFILE: {0} sources, {1} methods, {2} types, " +
310                                                    "{3} line numbers, {4} locals, {5} namespaces, " +
311                                                    "{6} bytes of string data",
312                                                    SourceCount, MethodCount, TypeCount, LineNumberCount,
313                                                    LocalCount, NamespaceCount, StringSize);
314                                 Console.WriteLine (ot);
315                                 return stream.GetContents ();
316                         }
317                 }
318
319                 Assembly assembly;
320                 BinaryReader reader;
321                 Hashtable method_hash;
322                 Hashtable source_file_hash;
323
324                 Hashtable method_token_hash;
325                 Hashtable method_name_hash;
326                 Hashtable source_name_hash;
327
328                 protected MonoSymbolFile (Assembly assembly, Stream stream)
329                 {
330                         this.assembly = assembly;
331
332                         reader = new BinaryReader (stream);
333
334                         try {
335                                 long magic = reader.ReadInt64 ();
336                                 long version = reader.ReadInt32 ();
337                                 if ((magic != OffsetTable.Magic) || (version != OffsetTable.Version))
338                                         throw new MonoSymbolFileException ();
339                                 ot = new OffsetTable (reader);
340                         } catch {
341                                 throw new MonoSymbolFileException ();
342                         }
343
344                         method_hash = new Hashtable ();
345                         source_file_hash = new Hashtable ();
346                 }
347
348                 public static MonoSymbolFile ReadSymbolFile (Assembly assembly)
349                 {
350                         Stream stream = assembly.GetManifestResourceStream ("MonoSymbolFile");
351                         if (stream == null)
352                                 return null;
353
354                         return new MonoSymbolFile (assembly, stream);
355                 }
356
357                 public Assembly Assembly {
358                         get { return assembly; }
359                 }
360
361                 public int SourceCount {
362                         get { return ot.SourceCount; }
363                 }
364
365                 public int MethodCount {
366                         get { return ot.MethodCount; }
367                 }
368
369                 public int TypeCount {
370                         get { return ot.TypeCount; }
371                 }
372
373                 public int NamespaceCount {
374                         get { return last_namespace_index; }
375                 }
376
377                 internal int LineNumberCount = 0;
378                 internal int LocalCount = 0;
379                 internal int StringSize = 0;
380
381                 public SourceFileEntry GetSourceFile (int index)
382                 {
383                         if ((index < 1) || (index > ot.SourceCount))
384                                 throw new ArgumentException ();
385                         if (reader == null)
386                                 throw new InvalidOperationException ();
387
388                         SourceFileEntry source = (SourceFileEntry) source_file_hash [index];
389                         if (source != null)
390                                 return source;
391
392                         reader.BaseStream.Position = ot.SourceTableOffset +
393                                 SourceFileEntry.Size * (index - 1);
394                         source = new SourceFileEntry (this, reader);
395                         source_file_hash.Add (index, source);
396                         return source;
397                 }
398
399                 public SourceFileEntry[] Sources {
400                         get {
401                                 if (reader == null)
402                                         throw new InvalidOperationException ();
403
404                                 SourceFileEntry[] retval = new SourceFileEntry [SourceCount];
405                                 for (int i = 0; i < SourceCount; i++)
406                                         retval [i] = GetSourceFile (i + 1);
407                                 return retval;
408                         }
409                 }
410
411                 public MethodIndexEntry GetMethodIndexEntry (int index)
412                 {
413                         int old_pos = (int) reader.BaseStream.Position;
414                         reader.BaseStream.Position = ot.MethodTableOffset +
415                                 MethodIndexEntry.Size * (index - 1);
416                         MethodIndexEntry ie = new MethodIndexEntry (reader);
417                         reader.BaseStream.Position = old_pos;
418                         return ie;
419                 }
420
421                 public MethodEntry GetMethodByToken (int token)
422                 {
423                         if (reader == null)
424                                 throw new InvalidOperationException ();
425
426                         if (method_token_hash == null) {
427                                 method_token_hash = new Hashtable ();
428
429                                 for (int i = 0; i < MethodCount; i++) {
430                                         MethodIndexEntry ie = GetMethodIndexEntry (i);
431
432                                         method_token_hash.Add (ie.Token, i);
433                                 }
434                         }
435
436                         object value = method_token_hash [token];
437                         if (value == null)
438                                 return null;
439
440                         return GetMethod ((int) value);
441                 }
442
443                 public MethodEntry GetMethod (MethodBase method)
444                 {
445                         if (reader == null)
446                                 throw new InvalidOperationException ();
447                         int token = assembly.MonoDebugger_GetMethodToken (method);
448                         return GetMethodByToken (token);
449                 }
450
451                 public MethodEntry GetMethod (int index)
452                 {
453                         if ((index < 1) || (index > ot.MethodCount))
454                                 throw new ArgumentException ();
455                         if (reader == null)
456                                 throw new InvalidOperationException ();
457
458                         MethodEntry entry = (MethodEntry) method_hash [index];
459                         if (entry != null)
460                                 return entry;
461
462                         MethodIndexEntry ie = GetMethodIndexEntry (index);
463                         reader.BaseStream.Position = ie.FileOffset;
464
465                         entry = new MethodEntry (this, reader, index);
466                         method_hash.Add (index, entry);
467                         return entry;
468                 }
469
470                 public MethodEntry[] Methods {
471                         get {
472                                 if (reader == null)
473                                         throw new InvalidOperationException ();
474
475                                 MethodEntry[] retval = new MethodEntry [MethodCount];
476                                 for (int i = 0; i < MethodCount; i++)
477                                         retval [i] = GetMethod (i + 1);
478                                 return retval;
479                         }
480                 }
481
482                 public MethodSourceEntry GetMethodSource (int index)
483                 {
484                         if ((index < 1) || (index > ot.MethodCount))
485                                 throw new ArgumentException ();
486                         if (reader == null)
487                                 throw new InvalidOperationException ();
488
489                         object entry = method_source_hash [index];
490                         if (entry != null)
491                                 return (MethodSourceEntry) entry;
492
493                         MethodEntry method = GetMethod (index);
494                         foreach (MethodSourceEntry source in method.SourceFile.Methods) {
495                                 if (source.Index == index) {
496                                         method_source_hash.Add (index, source);
497                                         return source;
498                                 }
499                         }
500
501                         throw new MonoSymbolFileException ("Internal error.");
502                 }
503
504                 public int FindMethod (string full_name)
505                 {
506                         if (reader == null)
507                                 throw new InvalidOperationException ();
508
509                         if (method_name_hash == null) {
510                                 method_name_hash = new Hashtable ();
511
512                                 for (int i = 0; i < ot.MethodCount; i++) {
513                                         MethodIndexEntry ie = GetMethodIndexEntry (i);
514                                         string name = ReadString (ie.FullNameOffset);
515
516                                         method_name_hash.Add (name, i + 1);
517                                 }
518                         }
519
520                         object value = method_name_hash [full_name];
521                         if (value == null)
522                                 return -1;
523                         return (int) value;
524                 }
525
526                 public int FindSource (string file_name)
527                 {
528                         if (reader == null)
529                                 throw new InvalidOperationException ();
530
531                         if (source_name_hash == null) {
532                                 source_name_hash = new Hashtable ();
533
534                                 for (int i = 0; i < ot.SourceCount; i++) {
535                                         SourceFileEntry source = GetSourceFile (i + 1);
536
537                                         source_name_hash.Add (source.FileName, i);
538                                 }
539                         }
540
541                         object value = source_name_hash [file_name];
542                         if (value == null)
543                                 return -1;
544                         return (int) value;
545                 }
546
547                 internal BinaryReader BinaryReader {
548                         get {
549                                 if (reader == null)
550                                         throw new InvalidOperationException ();
551
552                                 return reader;
553                         }
554                 }
555
556                 void IDisposable.Dispose ()
557                 {
558                         Dispose (true);
559                 }
560
561                 protected virtual void Dispose (bool disposing)
562                 {
563                         if (disposing) {
564                                 if (reader != null) {
565                                         reader.Close ();
566                                         reader = null;
567                                 }
568                         }
569                 }
570         }
571 }