2 // System.Diagnostics.SymbolStore/MonoSymbolFile.cs
5 // Martin Baulig (martin@gnome.org)
7 // (C) 2003 Ximian, Inc. http://www.ximian.com
11 using System.Reflection;
12 using System.Reflection.Emit;
13 using System.Collections;
17 namespace Mono.CSharp.Debugger
19 public class MonoSymbolFileException : Exception
21 public MonoSymbolFileException ()
25 public MonoSymbolFileException (string message, params object[] args)
26 : base (String.Format (message, args))
30 internal class MyMemoryStream : Stream
36 int chunk_size = 4096;
37 ArrayList chunks = new ArrayList ();
39 private struct Chunk {
40 public readonly int Offset;
41 public readonly int Length;
44 public Chunk (int offset, int length)
48 this.Buffer = new Byte [length];
52 public override long Position {
53 get { return position; }
57 throw new ArgumentOutOfRangeException ();
59 position = (int) value;
63 public override long Length {
64 get { return length; }
67 public override bool CanRead {
71 public override bool CanWrite {
75 public override bool CanSeek {
79 public override void SetLength (long new_length)
81 if (new_length < length)
82 throw new ArgumentException ();
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;
90 length = (int) new_length;
93 public override void Flush ()
96 public override long Seek (long offset, SeekOrigin origin)
101 case SeekOrigin.Begin:
104 case SeekOrigin.Current:
105 ref_point = position;
111 throw new ArgumentException ("Invalid SeekOrigin");
114 if ((ref_point + offset < 0) || (offset > real_length))
115 throw new ArgumentOutOfRangeException ();
117 position = ref_point + (int) offset;
122 Chunk FindChunk (int offset)
124 return (Chunk) chunks [offset / chunk_size];
127 public override int Read (byte[] buffer, int offset, int count)
129 int old_count = count;
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);
137 Array.Copy (chunk.Buffer, coffset, buffer, offset, size);
146 public override void Write (byte[] buffer, int offset, int count)
148 if (position + count > length)
149 SetLength (position + 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 (buffer, offset, chunk.Buffer, coffset, size);
164 public byte[] GetContents ()
166 byte[] retval = new byte [length];
168 Read (retval, 0, length);
173 public class MonoSymbolFile : IDisposable
175 ArrayList methods = new ArrayList ();
176 ArrayList sources = new ArrayList ();
177 Hashtable method_source_hash = new Hashtable ();
178 Hashtable type_hash = new Hashtable ();
182 int last_method_index;
183 int last_source_index;
184 int last_namespace_index;
186 public MonoSymbolFile ()
189 internal int AddSource (SourceFileEntry source)
191 sources.Add (source);
192 return ++last_source_index;
195 internal int DefineType (Type type)
197 if (type_hash.Contains (type))
198 return (int) type_hash [type];
200 int index = ++last_type_index;
201 type_hash.Add (type, index);
205 internal void AddMethod (MethodEntry entry)
210 internal int GetNextTypeIndex ()
212 return ++last_type_index;
215 internal int GetNextMethodIndex ()
217 return ++last_method_index;
220 internal int GetNextNamespaceIndex ()
222 return ++last_namespace_index;
225 internal void WriteString (BinaryWriter bw, string text)
227 byte[] data = Encoding.UTF8.GetBytes (text);
228 bw.Write ((int) data.Length);
230 StringSize += data.Length;
233 internal string ReadString (int offset)
235 int old_pos = (int) reader.BaseStream.Position;
236 reader.BaseStream.Position = offset;
237 int length = reader.ReadInt32 ();
239 byte[] data = reader.ReadBytes (length);
240 string text = Encoding.UTF8.GetString (data);
241 reader.BaseStream.Position = old_pos;
245 void Write (BinaryWriter bw)
247 // Magic number and file version.
248 bw.Write (OffsetTable.Magic);
249 bw.Write (OffsetTable.Version);
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.
255 long offset_table_offset = bw.BaseStream.Position;
259 // Write data sections.
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;
267 // Write method table.
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);
274 ot.MethodTableSize = (int) bw.BaseStream.Position - ot.MethodTableOffset;
277 // Write source table.
279 ot.SourceTableOffset = (int) bw.BaseStream.Position;
280 for (int i = 0; i < sources.Count; i++) {
281 SourceFileEntry source = (SourceFileEntry) sources [i];
284 ot.SourceTableSize = (int) bw.BaseStream.Position - ot.SourceTableOffset;
287 // Fixup offset table.
289 ot.TypeCount = last_type_index;
290 ot.MethodCount = methods.Count;
291 ot.SourceCount = sources.Count;
294 // Write offset table.
296 ot.TotalFileSize = (int) bw.BaseStream.Position;
297 bw.Seek ((int) offset_table_offset, SeekOrigin.Begin);
299 bw.Seek (0, SeekOrigin.End);
302 public byte[] CreateSymbolFile ()
305 throw new InvalidOperationException ();
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 ();
321 Hashtable method_hash;
322 Hashtable source_file_hash;
324 Hashtable method_token_hash;
325 Hashtable method_name_hash;
326 Hashtable method_full_name_hash;
327 Hashtable source_name_hash;
329 protected MonoSymbolFile (Assembly assembly, Stream stream)
331 this.assembly = assembly;
333 reader = new BinaryReader (stream);
336 long magic = reader.ReadInt64 ();
337 long version = reader.ReadInt32 ();
338 if ((magic != OffsetTable.Magic) || (version != OffsetTable.Version))
339 throw new MonoSymbolFileException ();
340 ot = new OffsetTable (reader);
342 throw new MonoSymbolFileException ();
345 method_hash = new Hashtable ();
346 source_file_hash = new Hashtable ();
349 public static MonoSymbolFile ReadSymbolFile (Assembly assembly)
351 Stream stream = assembly.GetManifestResourceStream ("MonoSymbolFile");
355 return new MonoSymbolFile (assembly, stream);
358 public Assembly Assembly {
359 get { return assembly; }
362 public int SourceCount {
363 get { return ot.SourceCount; }
366 public int MethodCount {
367 get { return ot.MethodCount; }
370 public int TypeCount {
371 get { return ot.TypeCount; }
374 public int NamespaceCount {
375 get { return last_namespace_index; }
378 internal int LineNumberCount = 0;
379 internal int LocalCount = 0;
380 internal int StringSize = 0;
382 public SourceFileEntry GetSourceFile (int index)
384 if ((index < 1) || (index > ot.SourceCount))
385 throw new ArgumentException ();
387 throw new InvalidOperationException ();
389 SourceFileEntry source = (SourceFileEntry) source_file_hash [index];
393 reader.BaseStream.Position = ot.SourceTableOffset +
394 SourceFileEntry.Size * (index - 1);
395 source = new SourceFileEntry (this, reader);
396 source_file_hash.Add (index, source);
400 public SourceFileEntry[] Sources {
403 throw new InvalidOperationException ();
405 SourceFileEntry[] retval = new SourceFileEntry [SourceCount];
406 for (int i = 0; i < SourceCount; i++)
407 retval [i] = GetSourceFile (i + 1);
412 public MethodIndexEntry GetMethodIndexEntry (int index)
414 int old_pos = (int) reader.BaseStream.Position;
415 reader.BaseStream.Position = ot.MethodTableOffset +
416 MethodIndexEntry.Size * (index - 1);
417 MethodIndexEntry ie = new MethodIndexEntry (reader);
418 reader.BaseStream.Position = old_pos;
422 public MethodEntry GetMethodByToken (int token)
425 throw new InvalidOperationException ();
427 if (method_token_hash == null) {
428 method_token_hash = new Hashtable ();
430 for (int i = 0; i < MethodCount; i++) {
431 MethodIndexEntry ie = GetMethodIndexEntry (i + 1);
433 method_token_hash.Add (ie.Token, i + 1);
437 object value = method_token_hash [token];
441 return GetMethod ((int) value);
444 public MethodEntry GetMethod (MethodBase method)
447 throw new InvalidOperationException ();
448 int token = assembly.MonoDebugger_GetMethodToken (method);
449 return GetMethodByToken (token);
452 public MethodEntry GetMethod (int index)
454 if ((index < 1) || (index > ot.MethodCount))
455 throw new ArgumentException ();
457 throw new InvalidOperationException ();
459 MethodEntry entry = (MethodEntry) method_hash [index];
463 MethodIndexEntry ie = GetMethodIndexEntry (index);
464 reader.BaseStream.Position = ie.FileOffset;
466 entry = new MethodEntry (this, reader, index);
467 method_hash.Add (index, entry);
471 public MethodEntry[] Methods {
474 throw new InvalidOperationException ();
476 MethodEntry[] retval = new MethodEntry [MethodCount];
477 for (int i = 0; i < MethodCount; i++)
478 retval [i] = GetMethod (i + 1);
483 public MethodSourceEntry GetMethodSource (int index)
485 if ((index < 1) || (index > ot.MethodCount))
486 throw new ArgumentException ();
488 throw new InvalidOperationException ();
490 object entry = method_source_hash [index];
492 return (MethodSourceEntry) entry;
494 MethodEntry method = GetMethod (index);
495 foreach (MethodSourceEntry source in method.SourceFile.Methods) {
496 if (source.Index == index) {
497 method_source_hash.Add (index, source);
502 throw new MonoSymbolFileException ("Internal error.");
505 public int FindMethod (string full_name)
508 throw new InvalidOperationException ();
510 if (method_full_name_hash == null) {
511 method_full_name_hash = new Hashtable ();
513 for (int i = 0; i < ot.MethodCount; i++) {
514 MethodIndexEntry ie = GetMethodIndexEntry (i + 1);
515 string name = ReadString (ie.FullNameOffset);
517 method_full_name_hash.Add (name, i + 1);
521 object value = method_full_name_hash [full_name];
527 public int[] MethodLookup (string query)
530 throw new InvalidOperationException ();
533 if (method_name_hash == null) {
534 method_name_hash = new Hashtable ();
536 for (int i = 0; i < ot.MethodCount; i++) {
537 MethodIndexEntry ie = GetMethodIndexEntry (i + 1);
538 string full_name = ReadString (ie.FullNameOffset);
540 int pos = full_name.IndexOf ('(');
541 string name = full_name.Substring (0, pos);
543 list = method_name_hash [name] as ArrayList;
545 list = new ArrayList ();
546 method_name_hash.Add (name, list);
553 list = method_name_hash [query] as ArrayList;
557 int[] retval = new int [list.Count];
558 list.CopyTo (retval, 0);
562 public int FindSource (string file_name)
565 throw new InvalidOperationException ();
567 if (source_name_hash == null) {
568 source_name_hash = new Hashtable ();
570 for (int i = 0; i < ot.SourceCount; i++) {
571 SourceFileEntry source = GetSourceFile (i + 1);
573 source_name_hash.Add (source.FileName, i);
577 object value = source_name_hash [file_name];
583 internal BinaryReader BinaryReader {
586 throw new InvalidOperationException ();
592 void IDisposable.Dispose ()
597 protected virtual void Dispose (bool disposing)
600 if (reader != null) {