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;
37 namespace Mono.CompilerServices.SymbolWriter
39 public class MonoSymbolFileException : Exception
41 public MonoSymbolFileException ()
45 public MonoSymbolFileException (string message, params object[] args)
46 : base (String.Format (message, args))
50 internal class MyMemoryStream : Stream
56 int chunk_size = 4096;
57 ArrayList chunks = new ArrayList ();
59 private struct Chunk {
60 public readonly int Offset;
61 public readonly int Length;
64 public Chunk (int offset, int length)
68 this.Buffer = new Byte [length];
72 public override long Position {
73 get { return position; }
77 throw new ArgumentOutOfRangeException ();
79 position = (int) value;
83 public override long Length {
84 get { return length; }
87 public override bool CanRead {
91 public override bool CanWrite {
95 public override bool CanSeek {
99 public override void SetLength (long new_length)
101 if (new_length < length)
102 throw new ArgumentException ();
104 while (new_length >= real_length) {
105 Chunk new_chunk = new Chunk (real_length, chunk_size);
106 chunks.Add (new_chunk);
107 real_length += chunk_size;
110 length = (int) new_length;
113 public override void Flush ()
116 public override long Seek (long offset, SeekOrigin origin)
121 case SeekOrigin.Begin:
124 case SeekOrigin.Current:
125 ref_point = position;
131 throw new ArgumentException ("Invalid SeekOrigin");
134 if ((ref_point + offset < 0) || (offset > real_length))
135 throw new ArgumentOutOfRangeException ();
137 position = ref_point + (int) offset;
142 Chunk FindChunk (int offset)
144 return (Chunk) chunks [offset / chunk_size];
147 public override int Read (byte[] buffer, int offset, int count)
149 int old_count = count;
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);
157 Array.Copy (chunk.Buffer, coffset, buffer, offset, size);
166 public override void Write (byte[] buffer, int offset, int count)
168 if (position + count > length)
169 SetLength (position + count);
172 Chunk chunk = FindChunk (position);
173 int coffset = position - chunk.Offset;
174 int rest = chunk.Length - coffset;
175 int size = System.Math.Min (count, rest);
177 Array.Copy (buffer, offset, chunk.Buffer, coffset, size);
184 public byte[] GetContents ()
186 byte[] retval = new byte [length];
188 Read (retval, 0, length);
193 internal class MyBinaryWriter : BinaryWriter
195 public MyBinaryWriter (Stream stream)
199 public void WriteLeb128 (int value)
201 base.Write7BitEncodedInt (value);
205 internal class MyBinaryReader : BinaryReader
207 public MyBinaryReader (Stream stream)
211 public int ReadLeb128 ()
213 return base.Read7BitEncodedInt ();
217 public class MonoDebuggerSupport
219 static GetTypeFunc get_type;
220 static GetMethodTokenFunc get_method_token;
221 static GetMethodFunc get_method;
222 static GetLocalTypeFromSignatureFunc local_type_from_sig;
223 static GetGuidFunc get_guid;
225 delegate Type GetTypeFunc (Assembly assembly, int token);
226 delegate int GetMethodTokenFunc (Assembly assembly, MethodBase method);
227 delegate MethodBase GetMethodFunc (Assembly assembly, int token);
228 delegate Type GetLocalTypeFromSignatureFunc (Assembly assembly, byte[] sig);
229 delegate Guid GetGuidFunc (Module module);
231 static Delegate create_delegate (Type type, Type delegate_type, string name)
233 MethodInfo mi = type.GetMethod (name, BindingFlags.Static |
234 BindingFlags.NonPublic);
236 throw new Exception ("Can't find " + name);
238 return Delegate.CreateDelegate (delegate_type, mi);
241 static MonoDebuggerSupport ()
243 get_type = (GetTypeFunc) create_delegate (
244 typeof (Assembly), typeof (GetTypeFunc),
245 "MonoDebugger_GetType");
247 get_method_token = (GetMethodTokenFunc) create_delegate (
248 typeof (Assembly), typeof (GetMethodTokenFunc),
249 "MonoDebugger_GetMethodToken");
251 get_method = (GetMethodFunc) create_delegate (
252 typeof (Assembly), typeof (GetMethodFunc),
253 "MonoDebugger_GetMethod");
255 local_type_from_sig = (GetLocalTypeFromSignatureFunc) create_delegate (
256 typeof (Assembly), typeof (GetLocalTypeFromSignatureFunc),
257 "MonoDebugger_GetLocalTypeFromSignature");
259 get_guid = (GetGuidFunc) create_delegate (
260 typeof (Module), typeof (GetGuidFunc), "Mono_GetGuid");
263 public static Type GetType (Assembly assembly, int token)
265 return get_type (assembly, token);
268 public static int GetMethodToken (MethodBase method)
270 return get_method_token (method.ReflectedType.Assembly, method);
273 public static MethodBase GetMethod (Assembly assembly, int token)
275 return get_method (assembly, token);
278 public static Type GetLocalTypeFromSignature (Assembly assembly, byte[] sig)
280 return local_type_from_sig (assembly, sig);
283 public static Guid GetGuid (Module module)
285 return get_guid (module);
289 public class MonoSymbolFile : IDisposable
291 ArrayList methods = new ArrayList ();
292 ArrayList sources = new ArrayList ();
293 Hashtable method_source_hash = new Hashtable ();
294 Hashtable type_hash = new Hashtable ();
298 int last_method_index;
299 int last_source_index;
300 int last_namespace_index;
302 public int NumLineNumbers;
304 public MonoSymbolFile ()
307 internal int AddSource (SourceFileEntry source)
309 sources.Add (source);
310 return ++last_source_index;
313 internal int DefineType (Type type)
315 if (type_hash.Contains (type))
316 return (int) type_hash [type];
318 int index = ++last_type_index;
319 type_hash.Add (type, index);
323 internal void AddMethod (MethodEntry entry)
328 internal int GetNextTypeIndex ()
330 return ++last_type_index;
333 internal int GetNextMethodIndex ()
335 return ++last_method_index;
338 internal int GetNextNamespaceIndex ()
340 return ++last_namespace_index;
343 byte [] stringBuffer;
344 int maxCharsPerRound;
345 static Encoding enc = Encoding.UTF8;
347 internal string ReadString (int offset)
349 int old_pos = (int) reader.BaseStream.Position;
350 reader.BaseStream.Position = offset;
352 string text = reader.ReadString ();
354 reader.BaseStream.Position = old_pos;
358 void Write (MyBinaryWriter bw, Guid guid)
360 // Magic number and file version.
361 bw.Write (OffsetTable.Magic);
362 bw.Write (OffsetTable.Version);
364 bw.Write (guid.ToByteArray ());
367 // Offsets of file sections; we must write this after we're done
368 // writing the whole file, so we just reserve the space for it here.
370 long offset_table_offset = bw.BaseStream.Position;
374 // Write data sections.
376 ot.DataSectionOffset = (int) bw.BaseStream.Position;
377 foreach (SourceFileEntry source in sources)
378 source.WriteData (bw);
379 ot.DataSectionSize = (int) bw.BaseStream.Position - ot.DataSectionOffset;
382 // Sort the methods according to their tokens and write
386 ot.MethodTableOffset = (int) bw.BaseStream.Position;
387 for (int i = 0; i < methods.Count; i++) {
388 MethodEntry entry = (MethodEntry) methods [i];
389 entry.WriteIndex (bw);
391 ot.MethodTableSize = (int) bw.BaseStream.Position - ot.MethodTableOffset;
394 // Write source table.
396 ot.SourceTableOffset = (int) bw.BaseStream.Position;
397 for (int i = 0; i < sources.Count; i++) {
398 SourceFileEntry source = (SourceFileEntry) sources [i];
401 ot.SourceTableSize = (int) bw.BaseStream.Position - ot.SourceTableOffset;
404 // Fixup offset table.
406 ot.TypeCount = last_type_index;
407 ot.MethodCount = methods.Count;
408 ot.SourceCount = sources.Count;
411 // Write offset table.
413 ot.TotalFileSize = (int) bw.BaseStream.Position;
414 bw.Seek ((int) offset_table_offset, SeekOrigin.Begin);
416 bw.Seek (0, SeekOrigin.End);
419 public byte[] CreateSymbolFile (Guid guid)
422 throw new InvalidOperationException ();
424 using (MyMemoryStream stream = new MyMemoryStream ()) {
425 Write (new MyBinaryWriter (stream), guid);
426 return stream.GetContents ();
431 MyBinaryReader reader;
432 Hashtable method_hash;
433 Hashtable source_file_hash;
435 Hashtable method_token_hash;
436 Hashtable source_name_hash;
438 protected MonoSymbolFile (string filename, Assembly assembly)
440 this.assembly = assembly;
442 FileStream stream = new FileStream (filename, FileMode.Open);
443 reader = new MyBinaryReader (stream);
446 long magic = reader.ReadInt64 ();
447 long version = reader.ReadInt32 ();
448 if (magic != OffsetTable.Magic)
449 throw new MonoSymbolFileException (
450 "Symbol file `{0}' is not a valid " +
451 "Mono symbol file", filename);
452 if (version != OffsetTable.Version)
453 throw new MonoSymbolFileException (
454 "Symbol file `{0}' has version {1}, " +
455 "but expected {2}", filename, version,
456 OffsetTable.Version);
457 ot = new OffsetTable (reader);
459 throw new MonoSymbolFileException (
460 "Cannot read symbol file `{0}'", filename);
463 Guid guid = new Guid (reader.ReadBytes (16));
464 Module[] modules = assembly.GetModules ();
465 Guid assembly_guid = MonoDebuggerSupport.GetGuid (modules [0]);
467 if (guid != assembly_guid)
468 throw new MonoSymbolFileException (
469 "Symbol file `{0}' does not match assembly `{1}'",
470 filename, assembly.Location);
472 method_hash = new Hashtable ();
473 source_file_hash = new Hashtable ();
476 public static MonoSymbolFile ReadSymbolFile (Assembly assembly)
478 string filename = assembly.Location;
479 string name = filename + ".mdb";
481 return new MonoSymbolFile (filename, assembly);
484 public Assembly Assembly {
485 get { return assembly; }
488 public int SourceCount {
489 get { return ot.SourceCount; }
492 public int MethodCount {
493 get { return ot.MethodCount; }
496 public int TypeCount {
497 get { return ot.TypeCount; }
500 public int NamespaceCount {
501 get { return last_namespace_index; }
504 internal int LineNumberCount = 0;
505 internal int LocalCount = 0;
506 internal int StringSize = 0;
508 public SourceFileEntry GetSourceFile (int index)
510 if ((index < 1) || (index > ot.SourceCount))
511 throw new ArgumentException ();
513 throw new InvalidOperationException ();
515 SourceFileEntry source = (SourceFileEntry) source_file_hash [index];
519 reader.BaseStream.Position = ot.SourceTableOffset +
520 SourceFileEntry.Size * (index - 1);
521 source = new SourceFileEntry (this, reader);
522 source_file_hash.Add (index, source);
526 public SourceFileEntry[] Sources {
529 throw new InvalidOperationException ();
531 SourceFileEntry[] retval = new SourceFileEntry [SourceCount];
532 for (int i = 0; i < SourceCount; i++)
533 retval [i] = GetSourceFile (i + 1);
538 public MethodIndexEntry GetMethodIndexEntry (int index)
540 int old_pos = (int) reader.BaseStream.Position;
541 reader.BaseStream.Position = ot.MethodTableOffset +
542 MethodIndexEntry.Size * (index - 1);
543 MethodIndexEntry ie = new MethodIndexEntry (reader);
544 reader.BaseStream.Position = old_pos;
548 public MethodEntry GetMethodByToken (int token)
551 throw new InvalidOperationException ();
553 if (method_token_hash == null) {
554 method_token_hash = new Hashtable ();
556 for (int i = 0; i < MethodCount; i++) {
557 MethodIndexEntry ie = GetMethodIndexEntry (i + 1);
559 method_token_hash.Add (ie.Token, i + 1);
563 object value = method_token_hash [token];
567 return GetMethod ((int) value);
570 public MethodEntry GetMethod (MethodBase method)
573 throw new InvalidOperationException ();
574 int token = MonoDebuggerSupport.GetMethodToken (method);
575 return GetMethodByToken (token);
578 public MethodEntry GetMethod (int index)
580 if ((index < 1) || (index > ot.MethodCount))
581 throw new ArgumentException ();
583 throw new InvalidOperationException ();
585 MethodEntry entry = (MethodEntry) method_hash [index];
589 MethodIndexEntry ie = GetMethodIndexEntry (index);
590 reader.BaseStream.Position = ie.FileOffset;
592 entry = new MethodEntry (this, reader, index);
593 method_hash.Add (index, entry);
597 public MethodEntry[] Methods {
600 throw new InvalidOperationException ();
602 MethodEntry[] retval = new MethodEntry [MethodCount];
603 for (int i = 0; i < MethodCount; i++)
604 retval [i] = GetMethod (i + 1);
609 public MethodSourceEntry GetMethodSource (int index)
611 if ((index < 1) || (index > ot.MethodCount))
612 throw new ArgumentException ();
614 throw new InvalidOperationException ();
616 object entry = method_source_hash [index];
618 return (MethodSourceEntry) entry;
620 MethodEntry method = GetMethod (index);
621 foreach (MethodSourceEntry source in method.SourceFile.Methods) {
622 if (source.Index == index) {
623 method_source_hash.Add (index, source);
628 throw new MonoSymbolFileException ("Internal error.");
631 public int FindSource (string file_name)
634 throw new InvalidOperationException ();
636 if (source_name_hash == null) {
637 source_name_hash = new Hashtable ();
639 for (int i = 0; i < ot.SourceCount; i++) {
640 SourceFileEntry source = GetSourceFile (i + 1);
642 source_name_hash.Add (source.FileName, i);
646 object value = source_name_hash [file_name];
652 internal MyBinaryReader BinaryReader {
655 throw new InvalidOperationException ();
661 void IDisposable.Dispose ()
666 protected virtual void Dispose (bool disposing)
669 if (reader != null) {