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 MySourceInfo
30                 {
31                         private MyMethodInfo[] _methods;
32                         public readonly string FileName;
33
34                         public MySourceInfo (string filename)
35                         {
36                                 this.FileName = filename;
37
38                                 this._methods = new MyMethodInfo [0];
39                         }
40
41                         public MyMethodInfo[] GetMethods ()
42                         {
43                                 return _methods;
44                         }
45
46                         public void AddMethod (MyMethodInfo method)
47                         {
48                                 int i = _methods.Length;
49                                 MyMethodInfo[] new_m = new MyMethodInfo [i + 1];
50                                 Array.Copy (_methods, new_m, i);
51                                 new_m [i] = method;
52                                 _methods = new_m;
53                         }
54                 }
55
56                 protected struct MyMethodInfo
57                 {
58                         public MyMethodInfo (MethodInfo method_info, MySourceInfo source_file) {
59                                 this.MethodInfo = method_info;
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 MethodInfo MethodInfo;
69                         public readonly MySourceInfo 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, MethodInfo method_info,
180                                         string source_file)
181                 {
182                         int token = symbol_token.GetToken ();
183                         MySourceInfo source_info;
184
185                         if (methods.ContainsKey (token))
186                                 methods.Remove (token);
187
188                         if (sources.ContainsKey (source_file))
189                                 source_info = (MySourceInfo) sources [source_file];
190                         else {
191                                 source_info = new MySourceInfo (source_file);
192                                 sources.Add (source_file, source_info);
193                         }
194
195                         current_method = new MyMethodInfo (method_info, source_info);
196
197                         source_info.AddMethod ((MyMethodInfo) current_method);
198
199                         methods.Add (token, current_method);
200
201                         OpenMethod (symbol_token);
202                 }
203
204                 public void SetMethodSourceRange (ISymbolDocumentWriter startDoc,
205                                                   int startLine, int startColumn,
206                                                   ISymbolDocumentWriter endDoc,
207                                                   int endLine, int endColumn)
208                 {
209                         if ((startDoc == null) || (endDoc == null))
210                                 throw new NullReferenceException ();
211
212                         if (!(startDoc is MonoSymbolDocumentWriter) || !(endDoc is MonoSymbolDocumentWriter))
213                                 throw new NotSupportedException ("both startDoc and endDoc must be of type "
214                                                                  + "MonoSymbolDocumentWriter");
215
216                         if (!startDoc.Equals (endDoc))
217                                 throw new NotSupportedException ("startDoc and endDoc must be the same");
218
219                         if (current_method != null)
220                                 ((MyMethodInfo) current_method).SetSourceRange (startLine, startColumn,
221                                                                               endLine, endColumn);
222                 }
223
224                 public void CloseMethod () {
225                         current_method = null;
226                 }
227
228                 public void OpenNamespace (string name)
229                 {
230                 }
231
232                 public int OpenScope (int startOffset)
233                 {
234                         throw new NotImplementedException ();
235                 }
236
237                 public void SetScopeRange (int scopeID, int startOffset, int endOffset)
238                 {
239                 }
240
241                 public void SetSymAttribute (SymbolToken parent, string name, byte[] data)
242                 {
243                 }
244
245                 public void SetUnderlyingWriter (IntPtr underlyingWriter)
246                 {
247                 }
248
249                 public void SetUserEntryPoint (SymbolToken entryMethod)
250                 {
251                 }
252
253                 public void UsingNamespace (string fullName)
254                 {
255                 }
256
257                 //
258                 // MonoSymbolWriter implementation
259                 //
260                 protected void WriteMethod (DwarfFileWriter.DieCompileUnit parent_die, MyMethodInfo method)
261                 {
262                         Console.WriteLine ("WRITING METHOD: " + method.MethodInfo.Name);
263
264                         DwarfFileWriter.DieSubProgram die;
265
266                         die = new DwarfFileWriter.DieSubProgram (parent_die, method.MethodInfo);
267                 }
268
269                 protected void WriteSource (DwarfFileWriter writer, MySourceInfo source)
270                 {
271                         Console.WriteLine ("WRITING SOURCE: " + source.FileName);
272
273                         DwarfFileWriter.CompileUnit compile_unit = new DwarfFileWriter.CompileUnit (
274                                 writer, source.FileName);
275
276                         DwarfFileWriter.DieCompileUnit die = new DwarfFileWriter.DieCompileUnit (compile_unit);
277
278                         foreach (MyMethodInfo method in source.GetMethods ())
279                                 WriteMethod (die, method);
280                 }
281
282                 protected void CreateDwarfFile (string filename)
283                 {
284                         Console.WriteLine ("WRITING DWARF FILE: " + filename);
285
286                         DwarfFileWriter writer = new DwarfFileWriter (filename);
287
288                         foreach (MySourceInfo source in sources.Values)
289                                 WriteSource (writer, source);
290
291                         writer.Close ();
292
293                         Console.WriteLine ("DONE WRITING DWARF FILE");
294
295                 }
296         }
297 }