Cosmetic fixes.
[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 = 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 = 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 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
184                 public MonoSymbolFile ()
185                 { }
186
187                 public SourceFileEntry DefineSource (string source_file)
188                 {
189                         if (reader != null)
190                                 throw new InvalidOperationException ();
191
192                         SourceFileEntry source = (SourceFileEntry) source_hash [source_file];
193                         if (source == null) {
194                                 source = new SourceFileEntry (this, source_file, sources.Count + 1);
195                                 source_hash.Add (source_file, source);
196                                 sources.Add (source);
197                         }
198                         return source;
199                 }
200
201                 internal int DefineType (Type type)
202                 {
203                         if (type_hash.Contains (type))
204                                 return (int) type_hash [type];
205
206                         int index = ++last_type_index;
207                         type_hash.Add (type, index);
208                         return index;
209                 }
210
211                 internal void AddMethod (MethodEntry entry)
212                 {
213                         methods.Add (entry);
214                 }
215
216                 internal int GetNextTypeIndex ()
217                 {
218                         return ++last_type_index;
219                 }
220
221                 internal int GetNextMethodIndex ()
222                 {
223                         return ++last_method_index;
224                 }
225
226                 internal void WriteString (BinaryWriter bw, string text)
227                 {
228                         byte[] data = Encoding.UTF8.GetBytes (text);
229                         bw.Write ((int) data.Length);
230                         bw.Write (data);
231                         StringSize += data.Length;
232                 }
233
234                 internal string ReadString (int offset)
235                 {
236                         int old_pos = (int) reader.BaseStream.Position;
237                         reader.BaseStream.Position = offset;
238                         int length = reader.ReadInt32 ();
239
240                         byte[] data = reader.ReadBytes (length);
241                         string text = Encoding.UTF8.GetString (data);
242                         reader.BaseStream.Position = old_pos;
243                         return text;
244                 }
245
246                 void Write (BinaryWriter bw)
247                 {
248                         // Magic number and file version.
249                         bw.Write (OffsetTable.Magic);
250                         bw.Write (OffsetTable.Version);
251
252                         //
253                         // Offsets of file sections; we must write this after we're done
254                         // writing the whole file, so we just reserve the space for it here.
255                         //
256                         long offset_table_offset = bw.BaseStream.Position;
257                         ot.Write (bw);
258
259                         //
260                         // Write data sections.
261                         //
262                         ot.DataSectionOffset = (int) bw.BaseStream.Position;
263                         foreach (SourceFileEntry source in sources)
264                                 source.WriteData (bw);
265                         ot.DataSectionSize = (int) bw.BaseStream.Position - ot.DataSectionOffset;
266
267                         //
268                         // Write method table.
269                         //
270                         ot.MethodTableOffset = (int) bw.BaseStream.Position;
271                         for (int i = 0; i < methods.Count; i++) {
272                                 MethodEntry entry = (MethodEntry) methods [i];
273                                 entry.WriteIndex (bw);
274                         }
275                         ot.MethodTableSize = (int) bw.BaseStream.Position - ot.MethodTableOffset;
276
277                         //
278                         // Write source table.
279                         //
280                         ot.SourceTableOffset = (int) bw.BaseStream.Position;
281                         for (int i = 0; i < sources.Count; i++) {
282                                 SourceFileEntry source = (SourceFileEntry) sources [i];
283                                 source.Write (bw);
284                         }
285                         ot.SourceTableSize = (int) bw.BaseStream.Position - ot.SourceTableOffset;
286
287                         //
288                         // Fixup offset table.
289                         //
290                         ot.TypeCount = last_type_index;
291                         ot.MethodCount = methods.Count;
292                         ot.SourceCount = sources.Count;
293
294                         //
295                         // Write offset table.
296                         //
297                         ot.TotalFileSize = (int) bw.BaseStream.Position;
298                         bw.Seek ((int) offset_table_offset, SeekOrigin.Begin);
299                         ot.Write (bw);
300                         bw.Seek (0, SeekOrigin.End);
301                 }
302
303                 public byte[] CreateSymbolFile ()
304                 {
305                         if (reader != null)
306                                 throw new InvalidOperationException ();
307
308                         using (MyMemoryStream stream = new MyMemoryStream ()) {
309                                 Write (new BinaryWriter (stream));
310                                 Console.WriteLine ("WROTE SYMFILE: {0} sources, {1} methods, {2} types, " +
311                                                    "{3} line numbers, {4} locals, {5} bytes of string data",
312                                                    SourceCount, MethodCount, TypeCount, LineNumberCount,
313                                                    LocalCount, StringSize);
314                                 Console.WriteLine (ot);
315                                 return stream.GetContents ();
316                         }
317                 }
318
319                 // Stream stream;
320                 BinaryReader reader;
321                 Hashtable method_hash;
322                 Hashtable source_file_hash;
323
324                 Hashtable method_name_hash;
325                 Hashtable source_name_hash;
326
327                 protected MonoSymbolFile (Stream stream)
328                 {
329                         reader = new BinaryReader (stream);
330
331                         try {
332                                 long magic = reader.ReadInt64 ();
333                                 long version = reader.ReadInt32 ();
334                                 if ((magic != OffsetTable.Magic) || (version != OffsetTable.Version))
335                                         throw new MonoSymbolFileException ();
336                                 ot = new OffsetTable (reader);
337                         } catch {
338                                 throw new MonoSymbolFileException ();
339                         }
340
341                         method_hash = new Hashtable ();
342                         source_file_hash = new Hashtable ();
343                 }
344
345                 public static MonoSymbolFile ReadSymbolFile (Assembly assembly)
346                 {
347                         Stream stream = assembly.GetManifestResourceStream ("MonoSymbolFile");
348                         if (stream == null)
349                                 return null;
350
351                         return new MonoSymbolFile (stream);
352                 }
353
354                 public int SourceCount {
355                         get { return ot.SourceCount; }
356                 }
357
358                 public int MethodCount {
359                         get { return ot.MethodCount; }
360                 }
361
362                 public int TypeCount {
363                         get { return ot.TypeCount; }
364                 }
365
366                 internal int LineNumberCount = 0;
367                 internal int LocalCount = 0;
368                 internal int StringSize = 0;
369
370                 public SourceFileEntry GetSourceFile (int index)
371                 {
372                         if ((index < 1) || (index > ot.SourceCount))
373                                 throw new ArgumentException ();
374                         if (reader == null)
375                                 throw new InvalidOperationException ();
376
377                         SourceFileEntry source = (SourceFileEntry) source_file_hash [index];
378                         if (source != null)
379                                 return source;
380
381                         reader.BaseStream.Position = ot.SourceTableOffset +
382                                 SourceFileEntry.Size * (index - 1);
383                         source = new SourceFileEntry (this, reader);
384                         source_file_hash.Add (index, source);
385                         return source;
386                 }
387
388                 public MethodEntry GetMethod (int index)
389                 {
390                         if ((index < 1) || (index > ot.MethodCount))
391                                 throw new ArgumentException ();
392                         if (reader == null)
393                                 throw new InvalidOperationException ();
394
395                         MethodEntry entry = (MethodEntry) method_hash [index];
396                         if (entry != null)
397                                 return entry;
398
399                         reader.BaseStream.Position = ot.MethodTableOffset + 8 * (index - 1);
400                         reader.BaseStream.Position = reader.ReadInt32 ();
401
402                         entry = new MethodEntry (this, reader, index);
403                         method_hash.Add (index, entry);
404                         return entry;
405                 }
406
407                 public int FindMethod (string full_name)
408                 {
409                         if (reader == null)
410                                 throw new InvalidOperationException ();
411
412                         if (method_name_hash == null) {
413                                 method_name_hash = new Hashtable ();
414
415                                 for (int i = 0; i < ot.MethodCount; i++) {
416                                         reader.BaseStream.Position = ot.MethodTableOffset + 8 * i;
417
418                                         int offset = reader.ReadInt32 ();
419                                         int name_offset = reader.ReadInt32 ();
420                                         string name = ReadString (name_offset);
421
422                                         method_name_hash.Add (name, i);
423                                 }
424                         }
425
426                         object value = method_name_hash [full_name];
427                         if (value == null)
428                                 return -1;
429                         return (int) value;
430                 }
431
432                 public int FindSource (string file_name)
433                 {
434                         if (reader == null)
435                                 throw new InvalidOperationException ();
436
437                         if (source_name_hash == null) {
438                                 source_name_hash = new Hashtable ();
439
440                                 for (int i = 0; i < ot.SourceCount; i++) {
441                                         SourceFileEntry source = GetSourceFile (i + 1);
442
443                                         source_name_hash.Add (source.FileName, i);
444                                 }
445                         }
446
447                         object value = source_name_hash [file_name];
448                         if (value == null)
449                                 return -1;
450                         return (int) value;
451                 }
452
453                 internal BinaryReader BinaryReader {
454                         get {
455                                 if (reader == null)
456                                         throw new InvalidOperationException ();
457
458                                 return reader;
459                         }
460                 }
461
462                 void IDisposable.Dispose ()
463                 {
464                         Dispose (true);
465                 }
466
467                 protected virtual void Dispose (bool disposing)
468                 {
469                         if (disposing) {
470                                 if (reader != null) {
471                                         reader.Close ();
472                                         reader = null;
473                                 }
474                         }
475                 }
476         }
477 }