2 // Mono.CSharp.Debugger/MonoSymbolFile.cs
5 // Martin Baulig (martin@ximian.com)
7 // (C) 2003 Ximian, Inc. http://www.ximian.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Reflection;
33 using System.Collections;
35 using System.Threading;
38 namespace Mono.CompilerServices.SymbolWriter
40 public class MonoSymbolFileException : Exception
42 public MonoSymbolFileException ()
46 public MonoSymbolFileException (string message, params object[] args)
47 : base (String.Format (message, args))
51 internal class MyMemoryStream : Stream
57 int chunk_size = 4096;
58 ArrayList chunks = new ArrayList ();
60 private struct Chunk {
61 public readonly int Offset;
62 public readonly int Length;
65 public Chunk (int offset, int length)
69 this.Buffer = new Byte [length];
73 public override long Position {
74 get { return position; }
78 throw new ArgumentOutOfRangeException ();
80 position = (int) value;
84 public override long Length {
85 get { return length; }
88 public override bool CanRead {
92 public override bool CanWrite {
96 public override bool CanSeek {
100 public override void SetLength (long new_length)
102 if (new_length < length)
103 throw new ArgumentException ();
105 while (new_length >= real_length) {
106 Chunk new_chunk = new Chunk (real_length, chunk_size);
107 chunks.Add (new_chunk);
108 real_length += chunk_size;
111 length = (int) new_length;
114 public override void Flush ()
117 public override long Seek (long offset, SeekOrigin origin)
122 case SeekOrigin.Begin:
125 case SeekOrigin.Current:
126 ref_point = position;
132 throw new ArgumentException ("Invalid SeekOrigin");
135 if ((ref_point + offset < 0) || (offset > real_length))
136 throw new ArgumentOutOfRangeException ();
138 position = ref_point + (int) offset;
143 Chunk FindChunk (int offset)
145 return (Chunk) chunks [offset / chunk_size];
148 public override int Read (byte[] buffer, int offset, int count)
150 int old_count = count;
153 Chunk chunk = FindChunk (position);
154 int coffset = position - chunk.Offset;
155 int rest = chunk.Length - coffset;
156 int size = System.Math.Min (count, rest);
158 Array.Copy (chunk.Buffer, coffset, buffer, offset, size);
167 public override void Write (byte[] buffer, int offset, int count)
169 if (position + count > length)
170 SetLength (position + count);
173 Chunk chunk = FindChunk (position);
174 int coffset = position - chunk.Offset;
175 int rest = chunk.Length - coffset;
176 int size = System.Math.Min (count, rest);
178 Array.Copy (buffer, offset, chunk.Buffer, coffset, size);
185 public byte[] GetContents ()
187 byte[] retval = new byte [length];
189 Read (retval, 0, length);
194 internal class MyBinaryWriter : BinaryWriter
196 public MyBinaryWriter (Stream stream)
200 public void WriteLeb128 (int value)
202 base.Write7BitEncodedInt (value);
206 internal class MyBinaryReader : BinaryReader
208 public MyBinaryReader (Stream stream)
212 public int ReadLeb128 ()
214 return base.Read7BitEncodedInt ();
218 public class MonoDebuggerSupport
220 static GetTypeFunc get_type;
221 static GetMethodTokenFunc get_method_token;
222 static GetMethodFunc get_method;
223 static GetLocalTypeFromSignatureFunc local_type_from_sig;
224 static GetGuidFunc get_guid;
225 static CheckRuntimeVersionFunc check_runtime_version;
227 delegate Type GetTypeFunc (Assembly assembly, int token);
228 delegate int GetMethodTokenFunc (Assembly assembly, MethodBase method);
229 delegate MethodBase GetMethodFunc (Assembly assembly, int token);
230 delegate Type GetLocalTypeFromSignatureFunc (Assembly assembly, byte[] sig);
231 delegate Guid GetGuidFunc (Module module);
232 delegate string CheckRuntimeVersionFunc (string filename);
234 static Delegate create_delegate (Type type, Type delegate_type, string name)
236 MethodInfo mi = type.GetMethod (name, BindingFlags.Static |
237 BindingFlags.NonPublic);
239 throw new Exception ("Can't find " + name);
241 return Delegate.CreateDelegate (delegate_type, mi);
244 static MonoDebuggerSupport ()
246 get_type = (GetTypeFunc) create_delegate (
247 typeof (Assembly), typeof (GetTypeFunc),
248 "MonoDebugger_GetType");
250 get_method_token = (GetMethodTokenFunc) create_delegate (
251 typeof (Assembly), typeof (GetMethodTokenFunc),
252 "MonoDebugger_GetMethodToken");
254 get_method = (GetMethodFunc) create_delegate (
255 typeof (Assembly), typeof (GetMethodFunc),
256 "MonoDebugger_GetMethod");
258 local_type_from_sig = (GetLocalTypeFromSignatureFunc) create_delegate (
259 typeof (Assembly), typeof (GetLocalTypeFromSignatureFunc),
260 "MonoDebugger_GetLocalTypeFromSignature");
262 get_guid = (GetGuidFunc) create_delegate (
263 typeof (Module), typeof (GetGuidFunc), "Mono_GetGuid");
265 check_runtime_version = (CheckRuntimeVersionFunc) create_delegate (
266 typeof (Assembly), typeof (CheckRuntimeVersionFunc),
267 "MonoDebugger_CheckRuntimeVersion");
270 public static Type GetType (Assembly assembly, int token)
272 return get_type (assembly, token);
275 public static int GetMethodToken (MethodBase method)
277 return get_method_token (method.ReflectedType.Assembly, method);
280 public static MethodBase GetMethod (Assembly assembly, int token)
282 return get_method (assembly, token);
285 public static Type GetLocalTypeFromSignature (Assembly assembly, byte[] sig)
287 return local_type_from_sig (assembly, sig);
290 public static string CheckRuntimeVersion (string filename)
292 return check_runtime_version (filename);
295 public static Guid GetGuid (Module module)
297 return get_guid (module);
301 public class MonoSymbolFile : IDisposable
303 ArrayList methods = new ArrayList ();
304 ArrayList sources = new ArrayList ();
305 Hashtable method_source_hash = new Hashtable ();
306 Hashtable type_hash = new Hashtable ();
310 int last_method_index;
311 int last_source_index;
312 int last_namespace_index;
314 public int NumLineNumbers;
316 public MonoSymbolFile ()
319 internal int AddSource (SourceFileEntry source)
321 sources.Add (source);
322 return ++last_source_index;
325 internal int DefineType (Type type)
327 if (type_hash.Contains (type))
328 return (int) type_hash [type];
330 int index = ++last_type_index;
331 type_hash.Add (type, index);
335 internal void AddMethod (MethodEntry entry)
340 internal int GetNextTypeIndex ()
342 return ++last_type_index;
345 internal int GetNextMethodIndex ()
347 return ++last_method_index;
350 internal int GetNextNamespaceIndex ()
352 return ++last_namespace_index;
355 byte [] stringBuffer;
356 int maxCharsPerRound;
357 static Encoding enc = Encoding.UTF8;
359 internal string ReadString (int offset)
361 int old_pos = (int) reader.BaseStream.Position;
362 reader.BaseStream.Position = offset;
364 string text = reader.ReadString ();
366 reader.BaseStream.Position = old_pos;
370 void Write (MyBinaryWriter bw, Guid guid)
372 // Magic number and file version.
373 bw.Write (OffsetTable.Magic);
374 bw.Write (OffsetTable.Version);
376 bw.Write (guid.ToByteArray ());
379 // Offsets of file sections; we must write this after we're done
380 // writing the whole file, so we just reserve the space for it here.
382 long offset_table_offset = bw.BaseStream.Position;
386 // Write data sections.
388 ot.DataSectionOffset = (int) bw.BaseStream.Position;
389 foreach (SourceFileEntry source in sources)
390 source.WriteData (bw);
391 ot.DataSectionSize = (int) bw.BaseStream.Position - ot.DataSectionOffset;
394 // Sort the methods according to their tokens and write
398 ot.MethodTableOffset = (int) bw.BaseStream.Position;
399 for (int i = 0; i < methods.Count; i++) {
400 MethodEntry entry = (MethodEntry) methods [i];
401 entry.WriteIndex (bw);
403 ot.MethodTableSize = (int) bw.BaseStream.Position - ot.MethodTableOffset;
406 // Write source table.
408 ot.SourceTableOffset = (int) bw.BaseStream.Position;
409 for (int i = 0; i < sources.Count; i++) {
410 SourceFileEntry source = (SourceFileEntry) sources [i];
413 ot.SourceTableSize = (int) bw.BaseStream.Position - ot.SourceTableOffset;
416 // Fixup offset table.
418 ot.TypeCount = last_type_index;
419 ot.MethodCount = methods.Count;
420 ot.SourceCount = sources.Count;
423 // Write offset table.
425 ot.TotalFileSize = (int) bw.BaseStream.Position;
426 bw.Seek ((int) offset_table_offset, SeekOrigin.Begin);
428 bw.Seek (0, SeekOrigin.End);
431 public byte[] CreateSymbolFile (Guid guid)
434 throw new InvalidOperationException ();
436 using (MyMemoryStream stream = new MyMemoryStream ()) {
437 Write (new MyBinaryWriter (stream), guid);
438 return stream.GetContents ();
443 MyBinaryReader reader;
444 Hashtable method_hash;
445 Hashtable source_file_hash;
447 Hashtable method_token_hash;
448 Hashtable source_name_hash;
450 protected MonoSymbolFile (string filename, Assembly assembly)
452 this.assembly = assembly;
454 FileStream stream = new FileStream (filename, FileMode.Open, FileAccess.Read);
455 reader = new MyBinaryReader (stream);
460 long magic = reader.ReadInt64 ();
461 long version = reader.ReadInt32 ();
462 if (magic != OffsetTable.Magic)
463 throw new MonoSymbolFileException (
464 "Symbol file `{0}' is not a valid " +
465 "Mono symbol file", filename);
466 if (version != OffsetTable.Version)
467 throw new MonoSymbolFileException (
468 "Symbol file `{0}' has version {1}, " +
469 "but expected {2}", filename, version,
470 OffsetTable.Version);
472 guid = new Guid (reader.ReadBytes (16));
474 ot = new OffsetTable (reader);
476 throw new MonoSymbolFileException (
477 "Cannot read symbol file `{0}'", filename);
480 Module[] modules = assembly.GetModules ();
481 Guid assembly_guid = MonoDebuggerSupport.GetGuid (modules [0]);
483 if (guid != assembly_guid)
484 throw new MonoSymbolFileException (
485 "Symbol file `{0}' does not match assembly `{1}'",
486 filename, assembly.Location);
488 method_hash = new Hashtable ();
489 source_file_hash = new Hashtable ();
492 public static MonoSymbolFile ReadSymbolFile (Assembly assembly)
494 string filename = assembly.Location;
495 string name = filename + ".mdb";
497 return new MonoSymbolFile (name, assembly);
500 public Assembly Assembly {
501 get { return assembly; }
504 public int SourceCount {
505 get { return ot.SourceCount; }
508 public int MethodCount {
509 get { return ot.MethodCount; }
512 public int TypeCount {
513 get { return ot.TypeCount; }
516 public int NamespaceCount {
517 get { return last_namespace_index; }
520 internal int LineNumberCount = 0;
521 internal int LocalCount = 0;
522 internal int StringSize = 0;
524 public SourceFileEntry GetSourceFile (int index)
526 if ((index < 1) || (index > ot.SourceCount))
527 throw new ArgumentException ();
529 throw new InvalidOperationException ();
531 SourceFileEntry source = (SourceFileEntry) source_file_hash [index];
535 reader.BaseStream.Position = ot.SourceTableOffset +
536 SourceFileEntry.Size * (index - 1);
537 source = new SourceFileEntry (this, reader);
538 source_file_hash.Add (index, source);
542 public SourceFileEntry[] Sources {
545 throw new InvalidOperationException ();
547 SourceFileEntry[] retval = new SourceFileEntry [SourceCount];
548 for (int i = 0; i < SourceCount; i++)
549 retval [i] = GetSourceFile (i + 1);
554 public MethodIndexEntry GetMethodIndexEntry (int index)
556 int old_pos = (int) reader.BaseStream.Position;
557 reader.BaseStream.Position = ot.MethodTableOffset +
558 MethodIndexEntry.Size * (index - 1);
559 MethodIndexEntry ie = new MethodIndexEntry (reader);
560 reader.BaseStream.Position = old_pos;
564 public MethodEntry GetMethodByToken (int token)
567 throw new InvalidOperationException ();
569 if (method_token_hash == null) {
570 method_token_hash = new Hashtable ();
572 for (int i = 0; i < MethodCount; i++) {
573 MethodIndexEntry ie = GetMethodIndexEntry (i + 1);
575 method_token_hash.Add (ie.Token, i + 1);
579 object value = method_token_hash [token];
583 return GetMethod ((int) value);
586 public MethodEntry GetMethod (MethodBase method)
589 throw new InvalidOperationException ();
590 int token = MonoDebuggerSupport.GetMethodToken (method);
591 return GetMethodByToken (token);
594 public MethodEntry GetMethod (int index)
596 if ((index < 1) || (index > ot.MethodCount))
597 throw new ArgumentException ();
599 throw new InvalidOperationException ();
601 MethodEntry entry = (MethodEntry) method_hash [index];
605 MethodIndexEntry ie = GetMethodIndexEntry (index);
606 reader.BaseStream.Position = ie.FileOffset;
608 entry = new MethodEntry (this, reader, index);
609 method_hash.Add (index, entry);
613 public MethodEntry[] Methods {
616 throw new InvalidOperationException ();
618 MethodEntry[] retval = new MethodEntry [MethodCount];
619 for (int i = 0; i < MethodCount; i++)
620 retval [i] = GetMethod (i + 1);
625 public MethodSourceEntry GetMethodSource (int index)
627 if ((index < 1) || (index > ot.MethodCount))
628 throw new ArgumentException ();
630 throw new InvalidOperationException ();
632 object entry = method_source_hash [index];
634 return (MethodSourceEntry) entry;
636 MethodEntry method = GetMethod (index);
637 foreach (MethodSourceEntry source in method.SourceFile.Methods) {
638 if (source.Index == index) {
639 method_source_hash.Add (index, source);
644 throw new MonoSymbolFileException ("Internal error.");
647 public int FindSource (string file_name)
650 throw new InvalidOperationException ();
652 if (source_name_hash == null) {
653 source_name_hash = new Hashtable ();
655 for (int i = 0; i < ot.SourceCount; i++) {
656 SourceFileEntry source = GetSourceFile (i + 1);
658 source_name_hash.Add (source.FileName, i);
662 object value = source_name_hash [file_name];
668 internal MyBinaryReader BinaryReader {
671 throw new InvalidOperationException ();
677 void IDisposable.Dispose ()
682 protected virtual void Dispose (bool disposing)
685 if (reader != null) {