2 // System.Diagnostics.SymbolStore/MonoSymbolWriter.cs
5 // Martin Baulig (martin@gnome.org)
7 // This is the default implementation of the System.Diagnostics.SymbolStore.ISymbolWriter
10 // (C) 2002 Ximian, Inc. http://www.ximian.com
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Runtime.CompilerServices;
17 using System.Diagnostics.SymbolStore;
18 using System.Collections;
21 namespace Mono.CSharp.Debugger
23 internal class SourceFile : ISourceFile
25 private ArrayList _methods = new ArrayList ();
26 private string _file_name;
28 public SourceFile (string filename)
30 this._file_name = filename;
33 public override string ToString ()
38 // interface ISourceFile
40 public string FileName {
46 public ISourceMethod[] Methods {
48 ISourceMethod[] retval = new ISourceMethod [_methods.Count];
49 _methods.CopyTo (retval);
54 public void AddMethod (ISourceMethod method)
56 _methods.Add (method);
60 internal class SourceBlock : ISourceBlock
62 static private int next_index;
63 private readonly int _index;
65 public SourceBlock (ISourceMethod method, ISourceLine start, ISourceLine end)
67 this._method = method;
70 this._index = ++next_index;
73 internal SourceBlock (ISourceMethod method, int startOffset)
75 this._method = method;
76 this._start = new SourceLine (startOffset);
77 this._index = ++next_index;
80 public override string ToString ()
82 return "SourceBlock #" + ID + " (" + Start + " - " + End + ")";
85 private readonly ISourceMethod _method;
86 private ArrayList _blocks = new ArrayList ();
87 internal ISourceLine _start;
88 internal ISourceLine _end;
90 private ArrayList _locals = new ArrayList ();
92 public ISourceMethod SourceMethod {
98 public ISourceBlock[] Blocks {
100 ISourceBlock[] retval = new ISourceBlock [_blocks.Count];
101 _blocks.CopyTo (retval);
106 public void AddBlock (ISourceBlock block)
111 public ISourceLine Start {
117 public ISourceLine End {
129 public ILocalVariable[] Locals {
131 ILocalVariable[] retval = new ILocalVariable [_locals.Count];
132 _locals.CopyTo (retval);
137 public void AddLocal (ILocalVariable local)
143 internal class SourceLine : ISourceLine
145 public SourceLine (int row, int column)
146 : this (0, row, column)
148 this._type = SourceOffsetType.OFFSET_NONE;
151 public SourceLine (int offset, int row, int column)
153 this._offset = offset;
155 this._column = column;
156 this._type = SourceOffsetType.OFFSET_IL;
159 internal SourceLine (int offset)
160 : this (offset, 0, 0)
163 public override string ToString ()
165 return "SourceLine (" + _offset + "@" + _row + ":" + _column + ")";
168 internal SourceOffsetType _type;
169 internal int _offset;
171 internal int _column;
173 // interface ISourceLine
175 public SourceOffsetType OffsetType {
200 internal class Variable : IVariable
202 public Variable (string name, ITypeHandle handle, ISourceMethod method, int index)
203 : this (name, handle, method, index, null)
206 public Variable (string name, ITypeHandle handle, ISourceMethod method,
207 int index, ISourceLine line)
210 this._handle = handle;
211 this._method = method;
216 private readonly string _name;
217 private readonly ITypeHandle _handle;
218 private readonly ISourceMethod _method;
219 private readonly ISourceLine _line;
220 private readonly int _index;
222 // interface IVariable
230 public ISourceMethod Method {
242 public ITypeHandle TypeHandle {
248 public ISourceLine Line {
255 internal class LocalVariable : Variable, ILocalVariable
257 public LocalVariable (string name, ITypeHandle handle, ISourceMethod method,
258 int index, ISourceLine line)
259 : base (name, handle, method, index, line)
262 public override string ToString ()
264 return "LocalVariable (" + Index + "," + Name + ")";
268 internal class SourceMethod : ISourceMethod
270 private ArrayList _lines = new ArrayList ();
271 private ArrayList _blocks = new ArrayList ();
272 private Hashtable _block_hash = new Hashtable ();
273 private Stack _block_stack = new Stack ();
275 internal readonly MethodBase _method_base;
276 internal ISourceFile _source_file;
279 private SourceBlock _implicit_block;
281 public SourceMethod (MethodBase method_base, ISourceFile source_file)
284 this._source_file = source_file;
287 internal SourceMethod (MethodBase method_base)
289 this._method_base = method_base;
291 this._implicit_block = new SourceBlock (this, 0);
294 public void SetSourceRange (ISourceFile sourceFile,
295 int startLine, int startColumn,
296 int endLine, int endColumn)
298 _source_file = sourceFile;
299 _implicit_block._start = new SourceLine (startLine, startColumn);
300 _implicit_block._end = new SourceLine (endLine, endColumn);
304 public void StartBlock (ISourceBlock block)
306 _block_stack.Push (block);
309 public void EndBlock (int endOffset) {
310 SourceBlock block = (SourceBlock) _block_stack.Pop ();
312 block._end = new SourceLine (endOffset);
314 if (_block_stack.Count > 0) {
315 ISourceBlock parent = (ISourceBlock) _block_stack.Peek ();
317 parent.AddBlock (block);
321 _block_hash.Add (block.ID, block);
324 public void SetBlockRange (int BlockID, int startOffset, int endOffset)
326 SourceBlock block = (SourceBlock) _block_hash [BlockID];
327 ((SourceLine) block.Start)._offset = startOffset;
328 ((SourceLine) block.End)._offset = endOffset;
331 public ISourceBlock CurrentBlock {
333 if (_block_stack.Count > 0)
334 return (ISourceBlock) _block_stack.Peek ();
336 return _implicit_block;
340 // interface ISourceMethod
342 public ISourceLine[] Lines {
344 ISourceLine[] retval = new ISourceLine [_lines.Count];
345 _lines.CopyTo (retval);
350 public void AddLine (ISourceLine line)
355 public ISourceBlock[] Blocks {
357 ISourceBlock[] retval = new ISourceBlock [_blocks.Count];
358 _blocks.CopyTo (retval);
363 public ILocalVariable[] Locals {
365 return _implicit_block.Locals;
369 public void AddLocal (ILocalVariable local)
371 _implicit_block.AddLocal (local);
374 public MethodBase MethodBase {
380 public string FullName {
382 return _method_base.DeclaringType.FullName + "." + _method_base.Name;
386 public Type ReturnType {
388 if (_method_base is MethodInfo)
389 return ((MethodInfo)_method_base).ReturnType;
390 else if (_method_base is ConstructorInfo)
391 return _method_base.DeclaringType;
393 throw new NotSupportedException ();
397 public ParameterInfo[] Parameters {
399 if (_method_base == null)
400 return new ParameterInfo [0];
402 ParameterInfo [] retval = _method_base.GetParameters ();
404 return new ParameterInfo [0];
410 public ISourceFile SourceFile {
421 throw new NotSupportedException ();
425 public ISourceLine Start {
427 return _implicit_block.Start;
431 public ISourceLine End {
433 return _implicit_block.End;
438 public class MonoSymbolWriter : IMonoSymbolWriter
440 protected Assembly assembly;
441 protected ModuleBuilder module_builder;
442 protected ArrayList locals = null;
443 protected ArrayList orphant_methods = null;
444 protected ArrayList methods = null;
445 protected Hashtable sources = null;
446 private ArrayList mbuilder_array = null;
448 internal ISourceMethod[] Methods {
450 ISourceMethod[] retval = new ISourceMethod [methods.Count];
451 methods.CopyTo (retval);
456 internal ISourceFile[] Sources {
458 ISourceFile[] retval = new ISourceFile [sources.Count];
459 sources.Values.CopyTo (retval, 0);
464 protected SourceMethod current_method = null;
465 private string assembly_filename = null;
466 private string output_filename = null;
469 // Interface IMonoSymbolWriter
472 public MonoSymbolWriter (ModuleBuilder mb, string filename, ArrayList mbuilder_array)
474 this.assembly_filename = filename;
475 this.module_builder = mb;
476 this.methods = new ArrayList ();
477 this.sources = new Hashtable ();
478 this.orphant_methods = new ArrayList ();
479 this.locals = new ArrayList ();
480 this.mbuilder_array = mbuilder_array;
483 public void Close () {
484 if (assembly == null)
485 assembly = Assembly.LoadFrom (assembly_filename);
489 CreateOutput (assembly);
492 public void CloseNamespace () {
495 // Create and return a new IMonoSymbolDocumentWriter.
496 public ISymbolDocumentWriter DefineDocument (string url,
501 return new MonoSymbolDocumentWriter (url);
504 public void DefineField (
507 FieldAttributes attributes,
509 SymAddressKind addrKind,
516 public void DefineGlobalVariable (
518 FieldAttributes attributes,
520 SymAddressKind addrKind,
527 public void DefineLocalVariable (string name,
528 FieldAttributes attributes,
530 SymAddressKind addrKind,
537 throw new NotSupportedException ();
540 public void DefineLocalVariable (string name,
542 FieldAttributes attributes,
550 public void DefineParameter (string name,
551 ParameterAttributes attributes,
553 SymAddressKind addrKind,
558 throw new NotSupportedException ();
561 public void DefineSequencePoints (ISymbolDocumentWriter document,
568 SourceLine source_line = new SourceLine (offsets [0], lines [0], columns [0]);
570 if (current_method != null)
571 current_method.AddLine (source_line);
574 public void Initialize (IntPtr emitter, string filename, bool fFullBuild)
576 throw new NotSupportedException ();
579 public void Initialize (string assembly_filename, string filename, string[] args)
581 this.output_filename = filename;
582 this.assembly_filename = assembly_filename;
585 public void OpenMethod (SymbolToken symbol_token)
587 int token = symbol_token.GetToken ();
589 if ((token & 0xff000000) != 0x06000000)
590 throw new ArgumentException ();
592 int index = (token & 0xffffff) - 1;
594 MethodBuilder mb = (MethodBuilder) mbuilder_array [index];
596 current_method = new SourceMethod (mb);
598 methods.Add (current_method);
601 public void SetMethodSourceRange (ISymbolDocumentWriter startDoc,
602 int startLine, int startColumn,
603 ISymbolDocumentWriter endDoc,
604 int endLine, int endColumn)
606 if (current_method == null)
609 if ((startDoc == null) || (endDoc == null))
610 throw new NullReferenceException ();
612 if (!(startDoc is MonoSymbolDocumentWriter) || !(endDoc is MonoSymbolDocumentWriter))
613 throw new NotSupportedException ("both startDoc and endDoc must be of type "
614 + "MonoSymbolDocumentWriter");
616 if (!startDoc.Equals (endDoc))
617 throw new NotSupportedException ("startDoc and endDoc must be the same");
619 string source_file = ((MonoSymbolDocumentWriter) startDoc).FileName;
620 SourceFile source_info;
622 if (sources.ContainsKey (source_file))
623 source_info = (SourceFile) sources [source_file];
625 source_info = new SourceFile (source_file);
626 sources.Add (source_file, source_info);
629 current_method.SetSourceRange (source_info, startLine, startColumn,
632 source_info.AddMethod (current_method);
635 public void CloseMethod () {
636 current_method = null;
639 public void OpenNamespace (string name)
643 public int OpenScope (int startOffset)
645 if (current_method == null)
648 ISourceBlock block = new SourceBlock (current_method, startOffset);
649 current_method.StartBlock (block);
654 public void CloseScope (int endOffset) {
655 if (current_method == null)
658 current_method.EndBlock (endOffset);
661 public void SetScopeRange (int scopeID, int startOffset, int endOffset)
663 if (current_method == null)
666 current_method.SetBlockRange (scopeID, startOffset, endOffset);
669 public void SetSymAttribute (SymbolToken parent, string name, byte[] data)
673 public void SetUnderlyingWriter (IntPtr underlyingWriter)
675 throw new NotSupportedException ();
678 public void SetUserEntryPoint (SymbolToken entryMethod)
682 public void UsingNamespace (string fullName)
687 // MonoSymbolWriter implementation
689 protected void DoFixups (Assembly assembly)
691 foreach (SourceMethod method in methods) {
692 if (method._method_base is MethodBuilder) {
693 MethodBuilder mb = (MethodBuilder) method._method_base;
694 method._token = mb.GetToken ().Token;
695 } else if (method._method_base is ConstructorBuilder) {
696 ConstructorBuilder cb = (ConstructorBuilder) method._method_base;
697 method._token = cb.GetToken ().Token;
699 throw new NotSupportedException ();
701 if (method.SourceFile == null)
702 orphant_methods.Add (method);
706 protected void CreateOutput (Assembly assembly)
708 using (MonoSymbolTableWriter writer = new MonoSymbolTableWriter (output_filename))
709 writer.WriteSymbolTable (this);