2002-03-19 Martin Baulig <martin@gnome.org>
[mono.git] / mcs / class / Mono.CSharp.Debugger / MonoSymbolWriter.cs
1 //
2 // System.Diagnostics.SymbolStore/MonoSymbolWriter.cs
3 //
4 // Author:
5 //   Martin Baulig (martin@gnome.org)
6 //
7 // This is the default implementation of the System.Diagnostics.SymbolStore.ISymbolWriter
8 // interface.
9 //
10 // (C) 2002 Ximian, Inc.  http://www.ximian.com
11 //
12
13 using System;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Diagnostics.SymbolStore;
17 using System.Collections;
18 using System.IO;
19         
20 namespace Mono.CSharp.Debugger
21 {
22
23         public class MonoSymbolWriter : IMonoSymbolWriter
24         {
25                 protected string output_filename = null;
26                 protected Hashtable methods = null;
27                 protected Hashtable sources = null;
28
29                 protected class SourceInfo
30                 {
31                         private MethodInfo[] _methods;
32                         public readonly string FileName;
33
34                         public SourceInfo (string filename)
35                         {
36                                 this.FileName = filename;
37
38                                 this._methods = new MethodInfo [0];
39                         }
40
41                         public MethodInfo[] GetMethods ()
42                         {
43                                 return _methods;
44                         }
45
46                         public void AddMethod (MethodInfo method)
47                         {
48                                 int i = _methods.Length;
49                                 MethodInfo[] new_m = new MethodInfo [i + 1];
50                                 Array.Copy (_methods, new_m, i);
51                                 new_m [i] = method;
52                                 _methods = new_m;
53                         }
54                 }
55
56                 protected struct MethodInfo
57                 {
58                         public MethodInfo (string name, SourceInfo source_file) {
59                                 this.Name = name;
60                                 this.SourceFile = source_file;
61                         }
62
63                         public void SetSourceRange (int startLine, int startColumn,
64                                                     int endLine, int endColumn)
65                         {
66                         }
67
68                         public readonly string Name;
69                         public readonly SourceInfo SourceFile;
70                 }
71
72                 protected Object current_method = null;
73
74                 //
75                 // Interface IMonoSymbolWriter
76                 //
77
78                 public MonoSymbolWriter ()
79                 {
80                         methods = new Hashtable ();
81                         sources = new Hashtable ();
82                 }
83
84                 public void Close () {
85                         CreateDwarfFile (output_filename);
86                 }
87
88                 public void CloseNamespace () {
89                 }
90
91                 public void CloseScope (int endOffset) {
92                 }
93
94                 // Create and return a new IMonoSymbolDocumentWriter.
95                 public ISymbolDocumentWriter DefineDocument (string url,
96                                                              Guid language,
97                                                              Guid languageVendor,
98                                                              Guid documentType)
99                 {
100                         return new MonoSymbolDocumentWriter (url);
101                 }
102
103                 public void DefineField (
104                         SymbolToken parent,
105                         string name,
106                         FieldAttributes attributes,
107                         byte[] signature,
108                         SymAddressKind addrKind,
109                         int addr1,
110                         int addr2,
111                         int addr3)
112                 {
113                 }
114
115                 public void DefineGlobalVariable (
116                         string name,
117                         FieldAttributes attributes,
118                         byte[] signature,
119                         SymAddressKind addrKind,
120                         int addr1,
121                         int addr2,
122                         int addr3)
123                 {
124                 }
125
126                 public void DefineLocalVariable (string name,
127                                                  FieldAttributes attributes,
128                                                  byte[] signature,
129                                                  SymAddressKind addrKind,
130                                                  int addr1,
131                                                  int addr2,
132                                                  int addr3,
133                                                  int startOffset,
134                                                  int endOffset)
135                 {
136                         Console.WriteLine ("WRITE LOCAL: " + name + " " + addr1);
137                 }
138
139                 public void DefineParameter (
140                         string name,
141                         ParameterAttributes attributes,
142                         int sequence,
143                         SymAddressKind addrKind,
144                         int addr1,
145                         int addr2,
146                         int addr3)
147                 {
148                 }
149
150                 public void DefineSequencePoints (
151                         ISymbolDocumentWriter document,
152                         int[] offsets,
153                         int[] lines,
154                         int[] columns,
155                         int[] endLines,
156                         int[] endColumns)
157                 {
158                 }
159
160                 public void Initialize (IntPtr emitter, string filename, bool fFullBuild)
161                 {
162                         throw new NotSupportedException ("Please use the 'Initialize (string filename)' "
163                                                          + "constructor and read the documentation in "
164                                                          + "Mono.CSharp.Debugger/IMonoSymbolWriter.cs");
165                 }
166
167                 // This is documented in IMonoSymbolWriter.cs
168                 public void Initialize (string filename)
169                 {
170                         this.output_filename = filename;
171                 }
172
173                 public void OpenMethod (SymbolToken method)
174                 {
175                         // do nothing
176                 }
177
178                 // This is documented in IMonoSymbolWriter.cs
179                 public void OpenMethod (SymbolToken symbol_token, string name, string source_file)
180                 {
181                         int token = symbol_token.GetToken ();
182                         SourceInfo source_info;
183
184                         if (methods.ContainsKey (token))
185                                 methods.Remove (token);
186
187                         if (sources.ContainsKey (source_file))
188                                 source_info = (SourceInfo) sources [source_file];
189                         else {
190                                 source_info = new SourceInfo (source_file);
191                                 sources.Add (source_file, source_info);
192                         }
193
194                         current_method = new MethodInfo (name, source_info);
195
196                         source_info.AddMethod ((MethodInfo) current_method);
197
198                         methods.Add (token, current_method);
199
200                         OpenMethod (symbol_token);
201                 }
202
203                 public void SetMethodSourceRange (ISymbolDocumentWriter startDoc,
204                                                   int startLine, int startColumn,
205                                                   ISymbolDocumentWriter endDoc,
206                                                   int endLine, int endColumn)
207                 {
208                         if ((startDoc == null) || (endDoc == null))
209                                 throw new NullReferenceException ();
210
211                         if (!(startDoc is MonoSymbolDocumentWriter) || !(endDoc is MonoSymbolDocumentWriter))
212                                 throw new NotSupportedException ("both startDoc and endDoc must be of type "
213                                                                  + "MonoSymbolDocumentWriter");
214
215                         if (!startDoc.Equals (endDoc))
216                                 throw new NotSupportedException ("startDoc and endDoc must be the same");
217
218                         if (current_method != null)
219                                 ((MethodInfo) current_method).SetSourceRange (startLine, startColumn,
220                                                                               endLine, endColumn);
221                 }
222
223                 public void CloseMethod () {
224                         current_method = null;
225                 }
226
227                 public void OpenNamespace (string name)
228                 {
229                 }
230
231                 public int OpenScope (int startOffset)
232                 {
233                         throw new NotImplementedException ();
234                 }
235
236                 public void SetScopeRange (int scopeID, int startOffset, int endOffset)
237                 {
238                 }
239
240                 public void SetSymAttribute (SymbolToken parent, string name, byte[] data)
241                 {
242                 }
243
244                 public void SetUnderlyingWriter (IntPtr underlyingWriter)
245                 {
246                 }
247
248                 public void SetUserEntryPoint (SymbolToken entryMethod)
249                 {
250                 }
251
252                 public void UsingNamespace (string fullName)
253                 {
254                 }
255
256                 //
257                 // MonoSymbolWriter implementation
258                 //
259                 protected void WriteMethod (DwarfFileWriter.DieCompileUnit parent_die, MethodInfo method)
260                 {
261                         Console.WriteLine ("WRITING METHOD: " + method.Name);
262
263                         DwarfFileWriter.DieSubProgram die;
264
265                         die = new DwarfFileWriter.DieSubProgram (parent_die, method.Name);
266                 }
267
268                 protected void WriteSource (DwarfFileWriter writer, SourceInfo source)
269                 {
270                         Console.WriteLine ("WRITING SOURCE: " + source.FileName);
271
272                         DwarfFileWriter.CompileUnit compile_unit = new DwarfFileWriter.CompileUnit (
273                                 writer, source.FileName);
274
275                         DwarfFileWriter.DieCompileUnit die = new DwarfFileWriter.DieCompileUnit (compile_unit);
276
277                         foreach (MethodInfo method in source.GetMethods ())
278                                 WriteMethod (die, method);
279                 }
280
281                 protected void CreateDwarfFile (string filename)
282                 {
283                         Console.WriteLine ("WRITING DWARF FILE: " + filename);
284
285                         DwarfFileWriter writer = new DwarfFileWriter (filename);
286
287                         foreach (SourceInfo source in sources.Values)
288                                 WriteSource (writer, source);
289
290                         writer.Close ();
291
292                         Console.WriteLine ("DONE WRITING DWARF FILE");
293
294                 }
295         }
296 }