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 public 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 public 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 public 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 public 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 public 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 public class MethodParameter : Variable, IMethodParameter
270 private static int get_index (ISourceMethod method, ParameterInfo param)
272 return method.MethodBase.IsStatic ? param.Position - 1 :
276 public MethodParameter (DwarfFileWriter writer, ISourceMethod method,
278 : base (param.Name, writer.RegisterType (param.ParameterType),
279 method, get_index (method, param))
281 this._method = method;
285 private readonly ISourceMethod _method;
286 private readonly ParameterInfo _param;
289 public class SourceMethod : ISourceMethod
291 private ArrayList _lines = new ArrayList ();
292 private ArrayList _blocks = new ArrayList ();
293 private Hashtable _block_hash = new Hashtable ();
294 private Stack _block_stack = new Stack ();
296 internal readonly MethodBase _method_base;
297 internal ISourceFile _source_file;
300 private SourceBlock _implicit_block;
302 public SourceMethod (MethodBase method_base, ISourceFile source_file)
305 this._source_file = source_file;
308 internal SourceMethod (MethodBase method_base)
310 this._method_base = method_base;
312 this._implicit_block = new SourceBlock (this, 0);
315 public void SetSourceRange (ISourceFile sourceFile,
316 int startLine, int startColumn,
317 int endLine, int endColumn)
319 _source_file = sourceFile;
320 _implicit_block._start = new SourceLine (startLine, startColumn);
321 _implicit_block._end = new SourceLine (endLine, endColumn);
325 public void StartBlock (ISourceBlock block)
327 _block_stack.Push (block);
330 public void EndBlock (int endOffset) {
331 SourceBlock block = (SourceBlock) _block_stack.Pop ();
333 block._end = new SourceLine (endOffset);
335 if (_block_stack.Count > 0) {
336 ISourceBlock parent = (ISourceBlock) _block_stack.Peek ();
338 parent.AddBlock (block);
342 _block_hash.Add (block.ID, block);
345 public void SetBlockRange (int BlockID, int startOffset, int endOffset)
347 SourceBlock block = (SourceBlock) _block_hash [BlockID];
348 ((SourceLine) block.Start)._offset = startOffset;
349 ((SourceLine) block.End)._offset = endOffset;
352 public ISourceBlock CurrentBlock {
354 if (_block_stack.Count > 0)
355 return (ISourceBlock) _block_stack.Peek ();
357 return _implicit_block;
361 // interface ISourceMethod
363 public ISourceLine[] Lines {
365 ISourceLine[] retval = new ISourceLine [_lines.Count];
366 _lines.CopyTo (retval);
371 public void AddLine (ISourceLine line)
376 public ISourceBlock[] Blocks {
378 ISourceBlock[] retval = new ISourceBlock [_blocks.Count];
379 _blocks.CopyTo (retval);
384 public ILocalVariable[] Locals {
386 return _implicit_block.Locals;
390 public void AddLocal (ILocalVariable local)
392 _implicit_block.AddLocal (local);
395 public MethodBase MethodBase {
401 public string FullName {
403 return _method_base.DeclaringType.FullName + "." + _method_base.Name;
407 public Type ReturnType {
409 if (_method_base is MethodInfo)
410 return ((MethodInfo)_method_base).ReturnType;
411 else if (_method_base is ConstructorInfo)
412 return _method_base.DeclaringType;
414 throw new NotSupportedException ();
418 public ParameterInfo[] Parameters {
420 if (_method_base == null)
421 return new ParameterInfo [0];
423 ParameterInfo [] retval = _method_base.GetParameters ();
425 return new ParameterInfo [0];
431 public ISourceFile SourceFile {
442 throw new NotSupportedException ();
446 public ISourceLine Start {
448 return _implicit_block.Start;
452 public ISourceLine End {
454 return _implicit_block.End;
459 public class MonoSymbolWriter : IMonoSymbolWriter
461 protected Assembly assembly;
462 protected ModuleBuilder module_builder;
463 protected string output_filename = null;
464 protected ArrayList locals = null;
465 protected ArrayList orphant_methods = null;
466 protected ArrayList methods = null;
467 protected Hashtable sources = null;
468 protected DwarfFileWriter writer = null;
469 private ArrayList mbuilder_array = null;
471 public ISourceMethod[] Methods {
473 ISourceMethod[] retval = new ISourceMethod [methods.Count];
474 methods.CopyTo (retval);
479 public ISourceFile[] Sources {
481 ISourceFile[] retval = new ISourceFile [sources.Count];
482 sources.Values.CopyTo (retval, 0);
487 public DwarfFileWriter DwarfFileWriter {
493 protected SourceMethod current_method = null;
494 private readonly string assembly_filename = null;
497 // Interface IMonoSymbolWriter
500 public MonoSymbolWriter (ModuleBuilder mb, string filename, ArrayList mbuilder_array)
502 this.assembly_filename = filename;
503 this.module_builder = mb;
504 this.methods = new ArrayList ();
505 this.sources = new Hashtable ();
506 this.orphant_methods = new ArrayList ();
507 this.locals = new ArrayList ();
508 this.mbuilder_array = mbuilder_array;
511 public void Close () {
512 if (assembly == null)
513 assembly = Assembly.LoadFrom (assembly_filename);
517 CreateDwarfFile (assembly);
520 public void CloseNamespace () {
523 // Create and return a new IMonoSymbolDocumentWriter.
524 public ISymbolDocumentWriter DefineDocument (string url,
529 return new MonoSymbolDocumentWriter (url);
532 public void DefineField (
535 FieldAttributes attributes,
537 SymAddressKind addrKind,
544 public void DefineGlobalVariable (
546 FieldAttributes attributes,
548 SymAddressKind addrKind,
555 public void DefineLocalVariable (string name,
556 FieldAttributes attributes,
558 SymAddressKind addrKind,
565 throw new NotSupportedException ();
568 public void DefineLocalVariable (string name,
570 FieldAttributes attributes,
575 if (current_method == null)
578 ITypeHandle type = writer.RegisterType (local.LocalType);
580 LocalVariable local_info = new LocalVariable (name, type, current_method,
583 current_method.CurrentBlock.AddLocal (local_info);
584 locals.Add (local_info);
588 public void DefineParameter (string name,
589 ParameterAttributes attributes,
591 SymAddressKind addrKind,
596 throw new NotSupportedException ();
599 public void DefineSequencePoints (ISymbolDocumentWriter document,
606 SourceLine source_line = new SourceLine (offsets [0], lines [0], columns [0]);
608 if (current_method != null)
609 current_method.AddLine (source_line);
612 public void Initialize (IntPtr emitter, string filename, bool fFullBuild)
614 this.output_filename = filename;
616 this.writer = new DwarfFileWriter (filename);
619 public void OpenMethod (SymbolToken symbol_token)
621 int token = symbol_token.GetToken ();
623 if ((token & 0xff000000) != 0x06000000)
624 throw new ArgumentException ();
626 int index = (token & 0xffffff) - 1;
628 MethodBuilder mb = (MethodBuilder) mbuilder_array [index];
630 current_method = new SourceMethod (mb);
632 methods.Add (current_method);
635 public void SetMethodSourceRange (ISymbolDocumentWriter startDoc,
636 int startLine, int startColumn,
637 ISymbolDocumentWriter endDoc,
638 int endLine, int endColumn)
640 if (current_method == null)
643 if ((startDoc == null) || (endDoc == null))
644 throw new NullReferenceException ();
646 if (!(startDoc is MonoSymbolDocumentWriter) || !(endDoc is MonoSymbolDocumentWriter))
647 throw new NotSupportedException ("both startDoc and endDoc must be of type "
648 + "MonoSymbolDocumentWriter");
650 if (!startDoc.Equals (endDoc))
651 throw new NotSupportedException ("startDoc and endDoc must be the same");
653 string source_file = ((MonoSymbolDocumentWriter) startDoc).FileName;
654 SourceFile source_info;
656 if (sources.ContainsKey (source_file))
657 source_info = (SourceFile) sources [source_file];
659 source_info = new SourceFile (source_file);
660 sources.Add (source_file, source_info);
663 current_method.SetSourceRange (source_info, startLine, startColumn,
666 source_info.AddMethod (current_method);
669 public void CloseMethod () {
670 current_method = null;
673 public void OpenNamespace (string name)
677 public int OpenScope (int startOffset)
679 if (current_method == null)
682 ISourceBlock block = new SourceBlock (current_method, startOffset);
683 current_method.StartBlock (block);
688 public void CloseScope (int endOffset) {
689 if (current_method == null)
692 current_method.EndBlock (endOffset);
695 public void SetScopeRange (int scopeID, int startOffset, int endOffset)
697 if (current_method == null)
700 current_method.SetBlockRange (scopeID, startOffset, endOffset);
703 public void SetSymAttribute (SymbolToken parent, string name, byte[] data)
707 public void SetUnderlyingWriter (IntPtr underlyingWriter)
709 throw new NotSupportedException ();
712 public void SetUserEntryPoint (SymbolToken entryMethod)
716 public void UsingNamespace (string fullName)
721 // MonoSymbolWriter implementation
723 protected void WriteLocal (DwarfFileWriter.Die parent_die, ILocalVariable local)
725 DwarfFileWriter.DieMethodVariable die;
727 die = new DwarfFileWriter.DieMethodVariable (parent_die, local);
730 protected void WriteBlock (DwarfFileWriter.Die parent_die, ISourceBlock block)
732 DwarfFileWriter.DieLexicalBlock die;
734 die = new DwarfFileWriter.DieLexicalBlock (parent_die, block);
736 foreach (ILocalVariable local in block.Locals)
737 WriteLocal (die, local);
739 foreach (ISourceBlock subblock in block.Blocks)
740 WriteBlock (die, subblock);
743 protected void WriteMethod (ISourceMethod method)
745 DwarfFileWriter.DieCompileUnit parent_die = writer.DieGlobalCompileUnit;
746 DwarfFileWriter.DieSubProgram die;
748 die = new DwarfFileWriter.DieSubProgram (parent_die, method);
750 foreach (ILocalVariable local in method.Locals)
751 WriteLocal (die, local);
753 foreach (ISourceBlock block in method.Blocks)
754 WriteBlock (die, block);
757 protected void WriteSource (DwarfFileWriter writer, ISourceFile source)
759 foreach (ISourceMethod method in source.Methods)
760 WriteMethod (method);
763 protected void DoFixups (Assembly assembly)
765 foreach (SourceMethod method in methods) {
766 if (method._method_base is MethodBuilder) {
767 MethodBuilder mb = (MethodBuilder) method._method_base;
768 method._token = mb.GetToken ().Token;
769 } else if (method._method_base is ConstructorBuilder) {
770 ConstructorBuilder cb = (ConstructorBuilder) method._method_base;
771 method._token = cb.GetToken ().Token;
773 throw new NotSupportedException ();
775 if (method.SourceFile == null)
776 orphant_methods.Add (method);
780 protected void CreateDwarfFile (Assembly assembly)
782 foreach (ISourceFile source in sources.Values)
783 WriteSource (writer, source);
785 if (orphant_methods.Count > 0) {
786 SourceFile source = new SourceFile ("<unknown>");
788 foreach (SourceMethod orphant in orphant_methods) {
789 orphant._source_file = source;
790 source.AddMethod (orphant);
793 WriteSource (writer, source);
796 writer.WriteSymbolTable (this);