ee4cbd207d7a22b4c472563b64559a51b7a3b7e2
[mono.git] / mcs / class / Mono.CSharp.Debugger / MonoSymbolTable.cs
1 //
2 // System.Diagnostics.SymbolStore/MonoSymbolTable.cs
3 //
4 // Author:
5 //   Martin Baulig (martin@gnome.org)
6 //
7 // (C) 2002 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Reflection;
12 using System.Reflection.Emit;
13 using System.Collections;
14 using System.Text;
15 using System.IO;
16         
17 namespace Mono.CSharp.Debugger
18 {
19         public struct OffsetTable
20         {
21                 public const uint Version = 16;
22                 public const long Magic   = 0x45e82623fd7fa614;
23
24                 public uint total_file_size;
25                 public uint source_table_offset;
26                 public uint source_table_size;
27                 public uint method_table_offset;
28                 public uint method_table_size;
29                 public uint line_number_table_offset;
30                 public uint line_number_table_size;
31
32                 public OffsetTable (BinaryReader reader)
33                 {
34                         total_file_size = reader.ReadUInt32 ();
35                         source_table_offset = reader.ReadUInt32 ();
36                         source_table_size = reader.ReadUInt32 ();
37                         method_table_offset = reader.ReadUInt32 ();
38                         method_table_size = reader.ReadUInt32 ();
39                         line_number_table_offset = reader.ReadUInt32 ();
40                         line_number_table_size = reader.ReadUInt32 ();
41                 }
42
43                 public void Write (BinaryWriter bw)
44                 {
45                         bw.Write (total_file_size);
46                         bw.Write (source_table_offset);
47                         bw.Write (source_table_size);
48                         bw.Write (method_table_offset);
49                         bw.Write (method_table_size);
50                         bw.Write (line_number_table_offset);
51                         bw.Write (line_number_table_size);
52                 }
53         }
54
55         public struct LineNumberEntry
56         {
57                 public uint Row;
58                 public uint Offset;
59                 public uint Address;
60
61                 public static LineNumberEntry Null = new LineNumberEntry (0, 0);
62
63                 public LineNumberEntry (uint row, uint offset)
64                 {
65                         this.Row = row;
66                         this.Offset = offset;
67                         this.Address = 0;
68                 }
69
70                 public LineNumberEntry (ISourceLine line)
71                         : this ((uint) line.Row, (uint) line.Offset)
72                 { }             
73
74                 public LineNumberEntry (BinaryReader reader)
75                 {
76                         Row = reader.ReadUInt32 ();
77                         Offset = reader.ReadUInt32 ();
78                         Address = reader.ReadUInt32 ();
79                 }
80
81                 public bool IsNull {
82                         get {
83                                 return Row == 0;
84                         }
85                 }
86
87                 public void Write (BinaryWriter bw)
88                 {
89                         bw.Write (Row);
90                         bw.Write (Offset);
91                         bw.Write (Address);
92                 }
93
94                 public override string ToString ()
95                 {
96                         return String.Format ("[Line {0}:{1}:{2}]", Row, Offset, Address);
97                 }
98         }
99
100         public struct MethodEntry
101         {
102                 public uint Token;
103                 public uint SourceFileOffset;
104                 public uint LineNumberTableOffset;
105                 public uint StartRow;
106                 public long StartAddress;
107                 public long EndAddress;
108
109                 public readonly string SourceFile;
110                 public readonly LineNumberEntry[] LineNumbers;
111
112                 public MethodEntry (BinaryReader reader)
113                 {
114                         Token = reader.ReadUInt32 ();
115                         SourceFileOffset = reader.ReadUInt32 ();
116                         LineNumberTableOffset = reader.ReadUInt32 ();
117                         StartRow = reader.ReadUInt32 ();
118                         StartAddress = reader.ReadInt64 ();
119                         EndAddress = reader.ReadInt64 ();
120
121                         long old_pos = reader.BaseStream.Position;
122                         reader.BaseStream.Position = LineNumberTableOffset;
123
124                         ArrayList lines = new ArrayList ();
125
126                         while (true) {
127                                 LineNumberEntry lne = new LineNumberEntry (reader);
128                                 if (lne.IsNull)
129                                         break;
130                                 lines.Add (lne);
131                         }
132
133                         reader.BaseStream.Position = SourceFileOffset;
134                         SourceFile = reader.ReadString ();
135                         reader.BaseStream.Position = old_pos;
136
137                         LineNumbers = new LineNumberEntry [lines.Count];
138                         lines.CopyTo (LineNumbers);
139                 }
140
141                 public MethodEntry (uint token, uint sf_offset, uint lnt_offset, uint row)
142                 {
143                         this.Token = token;
144                         this.SourceFileOffset = sf_offset;
145                         this.LineNumberTableOffset = lnt_offset;
146                         this.StartRow = row;
147                         this.StartAddress = 0;
148                         this.EndAddress = 0;
149                         this.SourceFile = null;
150                         this.LineNumbers = new LineNumberEntry [0];
151                 }
152
153                 public void Write (BinaryWriter bw)
154                 {
155                         bw.Write (Token);
156                         bw.Write (SourceFileOffset);
157                         bw.Write (LineNumberTableOffset);
158                         bw.Write (StartRow);
159                         bw.Write (StartAddress);
160                         bw.Write (EndAddress);
161                 }
162
163                 public override string ToString ()
164                 {
165                         return String.Format ("[Method {0}:{1}:{2}:{3}:{4}:{5}]",
166                                               Token, SourceFileOffset, LineNumberTableOffset,
167                                               StartRow, StartAddress, EndAddress);
168                 }
169         }
170 }