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, FileAccess.Read);
443 reader = new MyBinaryReader (stream);
448 long magic = reader.ReadInt64 ();
449 long version = reader.ReadInt32 ();
450 if (magic != OffsetTable.Magic)
451 throw new MonoSymbolFileException (
452 "Symbol file `{0}' is not a valid " +
453 "Mono symbol file", filename);
454 if (version != OffsetTable.Version)
455 throw new MonoSymbolFileException (
456 "Symbol file `{0}' has version {1}, " +
457 "but expected {2}", filename, version,
458 OffsetTable.Version);
460 guid = new Guid (reader.ReadBytes (16));
462 ot = new OffsetTable (reader);
464 throw new MonoSymbolFileException (
465 "Cannot read symbol file `{0}'", filename);
468 Module[] modules = assembly.GetModules ();
469 Guid assembly_guid = MonoDebuggerSupport.GetGuid (modules [0]);
471 if (guid != assembly_guid)
472 throw new MonoSymbolFileException (
473 "Symbol file `{0}' does not match assembly `{1}'",
474 filename, assembly.Location);
476 method_hash = new Hashtable ();
477 source_file_hash = new Hashtable ();
480 public static MonoSymbolFile ReadSymbolFile (Assembly assembly)
482 string filename = assembly.Location;
483 string name = filename + ".mdb";
485 return new MonoSymbolFile (name, assembly);
488 public Assembly Assembly {
489 get { return assembly; }
492 public int SourceCount {
493 get { return ot.SourceCount; }
496 public int MethodCount {
497 get { return ot.MethodCount; }
500 public int TypeCount {
501 get { return ot.TypeCount; }
504 public int NamespaceCount {
505 get { return last_namespace_index; }
508 internal int LineNumberCount = 0;
509 internal int LocalCount = 0;
510 internal int StringSize = 0;
512 public SourceFileEntry GetSourceFile (int index)
514 if ((index < 1) || (index > ot.SourceCount))
515 throw new ArgumentException ();
517 throw new InvalidOperationException ();
519 SourceFileEntry source = (SourceFileEntry) source_file_hash [index];
523 reader.BaseStream.Position = ot.SourceTableOffset +
524 SourceFileEntry.Size * (index - 1);
525 source = new SourceFileEntry (this, reader);
526 source_file_hash.Add (index, source);
530 public SourceFileEntry[] Sources {
533 throw new InvalidOperationException ();
535 SourceFileEntry[] retval = new SourceFileEntry [SourceCount];
536 for (int i = 0; i < SourceCount; i++)
537 retval [i] = GetSourceFile (i + 1);
542 public MethodIndexEntry GetMethodIndexEntry (int index)
544 int old_pos = (int) reader.BaseStream.Position;
545 reader.BaseStream.Position = ot.MethodTableOffset +
546 MethodIndexEntry.Size * (index - 1);
547 MethodIndexEntry ie = new MethodIndexEntry (reader);
548 reader.BaseStream.Position = old_pos;
552 public MethodEntry GetMethodByToken (int token)
555 throw new InvalidOperationException ();
557 if (method_token_hash == null) {
558 method_token_hash = new Hashtable ();
560 for (int i = 0; i < MethodCount; i++) {
561 MethodIndexEntry ie = GetMethodIndexEntry (i + 1);
563 method_token_hash.Add (ie.Token, i + 1);
567 object value = method_token_hash [token];
571 return GetMethod ((int) value);
574 public MethodEntry GetMethod (MethodBase method)
577 throw new InvalidOperationException ();
578 int token = MonoDebuggerSupport.GetMethodToken (method);
579 return GetMethodByToken (token);
582 public MethodEntry GetMethod (int index)
584 if ((index < 1) || (index > ot.MethodCount))
585 throw new ArgumentException ();
587 throw new InvalidOperationException ();
589 MethodEntry entry = (MethodEntry) method_hash [index];
593 MethodIndexEntry ie = GetMethodIndexEntry (index);
594 reader.BaseStream.Position = ie.FileOffset;
596 entry = new MethodEntry (this, reader, index);
597 method_hash.Add (index, entry);
601 public MethodEntry[] Methods {
604 throw new InvalidOperationException ();
606 MethodEntry[] retval = new MethodEntry [MethodCount];
607 for (int i = 0; i < MethodCount; i++)
608 retval [i] = GetMethod (i + 1);
613 public MethodSourceEntry GetMethodSource (int index)
615 if ((index < 1) || (index > ot.MethodCount))
616 throw new ArgumentException ();
618 throw new InvalidOperationException ();
620 object entry = method_source_hash [index];
622 return (MethodSourceEntry) entry;
624 MethodEntry method = GetMethod (index);
625 foreach (MethodSourceEntry source in method.SourceFile.Methods) {
626 if (source.Index == index) {
627 method_source_hash.Add (index, source);
632 throw new MonoSymbolFileException ("Internal error.");
635 public int FindSource (string file_name)
638 throw new InvalidOperationException ();
640 if (source_name_hash == null) {
641 source_name_hash = new Hashtable ();
643 for (int i = 0; i < ot.SourceCount; i++) {
644 SourceFileEntry source = GetSourceFile (i + 1);
646 source_name_hash.Add (source.FileName, i);
650 object value = source_name_hash [file_name];
656 internal MyBinaryReader BinaryReader {
659 throw new InvalidOperationException ();
665 void IDisposable.Dispose ()
670 protected virtual void Dispose (bool disposing)
673 if (reader != null) {