2 // Mono.CSharp.Debugger/MonoSymbolWriter.cs
5 // Martin Baulig (martin@ximian.com)
7 // This is the default implementation of the System.Diagnostics.SymbolStore.ISymbolWriter
10 // (C) 2002 Ximian, Inc. http://www.ximian.com
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Runtime.CompilerServices;
36 using System.Collections.Generic;
39 namespace Mono.CompilerServices.SymbolWriter
41 public class MonoSymbolWriter
43 List<SourceMethodBuilder> methods;
44 List<SourceFileEntry> sources;
45 List<CompileUnitEntry> comp_units;
46 protected readonly MonoSymbolFile file;
49 private SourceMethodBuilder current_method;
50 Stack<SourceMethodBuilder> current_method_stack = new Stack<SourceMethodBuilder> ();
52 public MonoSymbolWriter (string filename)
54 this.methods = new List<SourceMethodBuilder> ();
55 this.sources = new List<SourceFileEntry> ();
56 this.comp_units = new List<CompileUnitEntry> ();
57 this.file = new MonoSymbolFile ();
59 this.filename = filename + ".mdb";
62 public MonoSymbolFile SymbolFile {
66 public void CloseNamespace ()
69 public void DefineLocalVariable (int index, string name)
71 if (current_method == null)
74 current_method.AddLocal (index, name);
77 public void DefineCapturedLocal (int scope_id, string name, string captured_name)
79 file.DefineCapturedVariable (scope_id, name, captured_name,
80 CapturedVariable.CapturedKind.Local);
83 public void DefineCapturedParameter (int scope_id, string name, string captured_name)
85 file.DefineCapturedVariable (scope_id, name, captured_name,
86 CapturedVariable.CapturedKind.Parameter);
89 public void DefineCapturedThis (int scope_id, string captured_name)
91 file.DefineCapturedVariable (scope_id, "this", captured_name,
92 CapturedVariable.CapturedKind.This);
95 public void DefineCapturedScope (int scope_id, int id, string captured_name)
97 file.DefineCapturedScope (scope_id, id, captured_name);
100 public void DefineScopeVariable (int scope, int index)
102 if (current_method == null)
105 current_method.AddScopeVariable (scope, index);
108 public void MarkSequencePoint (int offset, SourceFileEntry file, int line, int column,
111 if (current_method == null)
114 current_method.MarkSequencePoint (offset, file, line, column, is_hidden);
117 public SourceMethodBuilder OpenMethod (ICompileUnit file, int ns_id, IMethodDef method)
119 SourceMethodBuilder builder = new SourceMethodBuilder (file, ns_id, method);
120 current_method_stack.Push (current_method);
121 current_method = builder;
122 methods.Add (current_method);
126 public void CloseMethod ()
128 current_method = (SourceMethodBuilder) current_method_stack.Pop ();
131 public SourceFileEntry DefineDocument (string url)
133 SourceFileEntry entry = new SourceFileEntry (file, url);
138 public SourceFileEntry DefineDocument (string url, byte[] guid, byte[] checksum)
140 SourceFileEntry entry = new SourceFileEntry (file, url, guid, checksum);
145 public CompileUnitEntry DefineCompilationUnit (SourceFileEntry source)
147 CompileUnitEntry entry = new CompileUnitEntry (file, source);
148 comp_units.Add (entry);
152 public int DefineNamespace (string name, CompileUnitEntry unit,
153 string[] using_clauses, int parent)
155 if ((unit == null) || (using_clauses == null))
156 throw new NullReferenceException ();
158 return unit.DefineNamespace (name, using_clauses, parent);
161 public int OpenScope (int start_offset)
163 if (current_method == null)
166 current_method.StartBlock (CodeBlockEntry.Type.Lexical, start_offset);
170 public void CloseScope (int end_offset)
172 if (current_method == null)
175 current_method.EndBlock (end_offset);
178 public void OpenCompilerGeneratedBlock (int start_offset)
180 if (current_method == null)
183 current_method.StartBlock (CodeBlockEntry.Type.CompilerGenerated,
187 public void CloseCompilerGeneratedBlock (int end_offset)
189 if (current_method == null)
192 current_method.EndBlock (end_offset);
195 public void StartIteratorBody (int start_offset)
197 current_method.StartBlock (CodeBlockEntry.Type.IteratorBody,
201 public void EndIteratorBody (int end_offset)
203 current_method.EndBlock (end_offset);
206 public void StartIteratorDispatcher (int start_offset)
208 current_method.StartBlock (CodeBlockEntry.Type.IteratorDispatcher,
212 public void EndIteratorDispatcher (int end_offset)
214 current_method.EndBlock (end_offset);
217 public void DefineAnonymousScope (int id)
219 file.DefineAnonymousScope (id);
222 public void WriteSymbolFile (Guid guid)
224 foreach (SourceMethodBuilder method in methods)
225 method.DefineMethod (file);
228 // We mmap the file, so unlink the previous version since it may be in use
229 File.Delete (filename);
231 // We can safely ignore
233 using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write)) {
234 file.CreateSymbolFile (guid, fs);