2 // Mono.CSharp.Debugger/MonoSymbolTable.cs
5 // Martin Baulig (martin@ximian.com)
7 // (C) 2002 Ximian, Inc. http://www.ximian.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Security.Cryptography;
33 using System.Collections.Generic;
38 // Parts which are actually written into the symbol file are marked with
40 // #region This is actually written to the symbol file
43 // Please do not modify these regions without previously talking to me.
45 // All changes to the file format must be synchronized in several places:
47 // a) The fields in these regions (and their order) must match the actual
48 // contents of the symbol file.
50 // This helps people to understand the symbol file format without reading
51 // too much source code, ie. you look at the appropriate region and then
52 // you know what's actually in the file.
54 // It is also required to help me enforce b).
56 // b) The regions must be kept in sync with the unmanaged code in
57 // mono/metadata/debug-mono-symfile.h
59 // When making changes to the file format, you must also increase two version
62 // i) OffsetTable.Version in this file.
63 // ii) MONO_SYMBOL_FILE_VERSION in mono/metadata/debug-mono-symfile.h
65 // After doing so, recompile everything, including the debugger. Symbol files
66 // with different versions are incompatible to each other and the debugger and
67 // the runtime enfore this, so you need to recompile all your assemblies after
68 // changing the file format.
71 namespace Mono.CompilerServices.SymbolWriter
73 public class OffsetTable
75 public const int MajorVersion = 50;
76 public const int MinorVersion = 0;
77 public const long Magic = 0x45e82623fd7fa614;
79 #region This is actually written to the symbol file
80 public int TotalFileSize;
81 public int DataSectionOffset;
82 public int DataSectionSize;
83 public int CompileUnitCount;
84 public int CompileUnitTableOffset;
85 public int CompileUnitTableSize;
86 public int SourceCount;
87 public int SourceTableOffset;
88 public int SourceTableSize;
89 public int MethodCount;
90 public int MethodTableOffset;
91 public int MethodTableSize;
93 public int AnonymousScopeCount;
94 public int AnonymousScopeTableOffset;
95 public int AnonymousScopeTableSize;
104 public Flags FileFlags;
106 public int LineNumberTable_LineBase = LineNumberTable.Default_LineBase;
107 public int LineNumberTable_LineRange = LineNumberTable.Default_LineRange;
108 public int LineNumberTable_OpcodeBase = LineNumberTable.Default_OpcodeBase;
111 internal OffsetTable ()
113 int platform = (int) Environment.OSVersion.Platform;
114 if ((platform != 4) && (platform != 128))
115 FileFlags |= Flags.WindowsFileNames;
118 internal OffsetTable (BinaryReader reader, int major_version, int minor_version)
120 TotalFileSize = reader.ReadInt32 ();
121 DataSectionOffset = reader.ReadInt32 ();
122 DataSectionSize = reader.ReadInt32 ();
123 CompileUnitCount = reader.ReadInt32 ();
124 CompileUnitTableOffset = reader.ReadInt32 ();
125 CompileUnitTableSize = reader.ReadInt32 ();
126 SourceCount = reader.ReadInt32 ();
127 SourceTableOffset = reader.ReadInt32 ();
128 SourceTableSize = reader.ReadInt32 ();
129 MethodCount = reader.ReadInt32 ();
130 MethodTableOffset = reader.ReadInt32 ();
131 MethodTableSize = reader.ReadInt32 ();
132 TypeCount = reader.ReadInt32 ();
134 AnonymousScopeCount = reader.ReadInt32 ();
135 AnonymousScopeTableOffset = reader.ReadInt32 ();
136 AnonymousScopeTableSize = reader.ReadInt32 ();
138 LineNumberTable_LineBase = reader.ReadInt32 ();
139 LineNumberTable_LineRange = reader.ReadInt32 ();
140 LineNumberTable_OpcodeBase = reader.ReadInt32 ();
142 FileFlags = (Flags) reader.ReadInt32 ();
145 internal void Write (BinaryWriter bw, int major_version, int minor_version)
147 bw.Write (TotalFileSize);
148 bw.Write (DataSectionOffset);
149 bw.Write (DataSectionSize);
150 bw.Write (CompileUnitCount);
151 bw.Write (CompileUnitTableOffset);
152 bw.Write (CompileUnitTableSize);
153 bw.Write (SourceCount);
154 bw.Write (SourceTableOffset);
155 bw.Write (SourceTableSize);
156 bw.Write (MethodCount);
157 bw.Write (MethodTableOffset);
158 bw.Write (MethodTableSize);
159 bw.Write (TypeCount);
161 bw.Write (AnonymousScopeCount);
162 bw.Write (AnonymousScopeTableOffset);
163 bw.Write (AnonymousScopeTableSize);
165 bw.Write (LineNumberTable_LineBase);
166 bw.Write (LineNumberTable_LineRange);
167 bw.Write (LineNumberTable_OpcodeBase);
169 bw.Write ((int) FileFlags);
172 public override string ToString ()
174 return String.Format (
175 "OffsetTable [{0} - {1}:{2} - {3}:{4}:{5} - {6}:{7}:{8} - {9}]",
176 TotalFileSize, DataSectionOffset, DataSectionSize, SourceCount,
177 SourceTableOffset, SourceTableSize, MethodCount, MethodTableOffset,
178 MethodTableSize, TypeCount);
182 public class LineNumberEntry
184 #region This is actually written to the symbol file
185 public readonly int Row;
186 public readonly int File;
187 public readonly int Offset;
188 public readonly bool IsHidden; // Obsolete is never used
191 public sealed class LocationComparer : IComparer<LineNumberEntry>
193 public static readonly LocationComparer Default = new LocationComparer ();
195 public int Compare (LineNumberEntry l1, LineNumberEntry l2)
197 return l1.Row == l2.Row ?
198 l1.Offset.CompareTo (l2.Offset) :
199 l1.Row.CompareTo (l2.Row);
203 public static readonly LineNumberEntry Null = new LineNumberEntry (0, 0, 0);
205 public LineNumberEntry (int file, int row, int offset)
206 : this (file, row, offset, false)
209 public LineNumberEntry (int file, int row, int offset, bool is_hidden)
213 this.Offset = offset;
214 this.IsHidden = is_hidden;
217 public override string ToString ()
219 return String.Format ("[Line {0}:{1}:{2}]", File, Row, Offset);
223 public class CodeBlockEntry
226 #region This is actually written to the symbol file
228 public Type BlockType;
229 public int StartOffset;
230 public int EndOffset;
235 CompilerGenerated = 2,
237 IteratorDispatcher = 4
240 public CodeBlockEntry (int index, int parent, Type type, int start_offset)
243 this.Parent = parent;
244 this.BlockType = type;
245 this.StartOffset = start_offset;
248 internal CodeBlockEntry (int index, MyBinaryReader reader)
251 int type_flag = reader.ReadLeb128 ();
252 BlockType = (Type) (type_flag & 0x3f);
253 this.Parent = reader.ReadLeb128 ();
254 this.StartOffset = reader.ReadLeb128 ();
255 this.EndOffset = reader.ReadLeb128 ();
257 /* Reserved for future extensions. */
258 if ((type_flag & 0x40) != 0) {
259 int data_size = reader.ReadInt16 ();
260 reader.BaseStream.Position += data_size;
264 public void Close (int end_offset)
266 this.EndOffset = end_offset;
269 internal void Write (MyBinaryWriter bw)
271 bw.WriteLeb128 ((int) BlockType);
272 bw.WriteLeb128 (Parent);
273 bw.WriteLeb128 (StartOffset);
274 bw.WriteLeb128 (EndOffset);
277 public override string ToString ()
279 return String.Format ("[CodeBlock {0}:{1}:{2}:{3}:{4}]",
280 Index, Parent, BlockType, StartOffset, EndOffset);
284 public struct LocalVariableEntry
286 #region This is actually written to the symbol file
287 public readonly int Index;
288 public readonly string Name;
289 public readonly int BlockIndex;
292 public LocalVariableEntry (int index, string name, int block)
296 this.BlockIndex = block;
299 internal LocalVariableEntry (MonoSymbolFile file, MyBinaryReader reader)
301 Index = reader.ReadLeb128 ();
302 Name = reader.ReadString ();
303 BlockIndex = reader.ReadLeb128 ();
306 internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
308 bw.WriteLeb128 (Index);
310 bw.WriteLeb128 (BlockIndex);
313 public override string ToString ()
315 return String.Format ("[LocalVariable {0}:{1}:{2}]",
316 Name, Index, BlockIndex - 1);
320 public struct CapturedVariable
322 #region This is actually written to the symbol file
323 public readonly string Name;
324 public readonly string CapturedName;
325 public readonly CapturedKind Kind;
328 public enum CapturedKind : byte
335 public CapturedVariable (string name, string captured_name,
339 this.CapturedName = captured_name;
343 internal CapturedVariable (MyBinaryReader reader)
345 Name = reader.ReadString ();
346 CapturedName = reader.ReadString ();
347 Kind = (CapturedKind) reader.ReadByte ();
350 internal void Write (MyBinaryWriter bw)
353 bw.Write (CapturedName);
354 bw.Write ((byte) Kind);
357 public override string ToString ()
359 return String.Format ("[CapturedVariable {0}:{1}:{2}]",
360 Name, CapturedName, Kind);
364 public struct CapturedScope
366 #region This is actually written to the symbol file
367 public readonly int Scope;
368 public readonly string CapturedName;
371 public CapturedScope (int scope, string captured_name)
374 this.CapturedName = captured_name;
377 internal CapturedScope (MyBinaryReader reader)
379 Scope = reader.ReadLeb128 ();
380 CapturedName = reader.ReadString ();
383 internal void Write (MyBinaryWriter bw)
385 bw.WriteLeb128 (Scope);
386 bw.Write (CapturedName);
389 public override string ToString ()
391 return String.Format ("[CapturedScope {0}:{1}]",
392 Scope, CapturedName);
396 public struct ScopeVariable
398 #region This is actually written to the symbol file
399 public readonly int Scope;
400 public readonly int Index;
403 public ScopeVariable (int scope, int index)
409 internal ScopeVariable (MyBinaryReader reader)
411 Scope = reader.ReadLeb128 ();
412 Index = reader.ReadLeb128 ();
415 internal void Write (MyBinaryWriter bw)
417 bw.WriteLeb128 (Scope);
418 bw.WriteLeb128 (Index);
421 public override string ToString ()
423 return String.Format ("[ScopeVariable {0}:{1}]", Scope, Index);
427 public class AnonymousScopeEntry
429 #region This is actually written to the symbol file
430 public readonly int ID;
433 List<CapturedVariable> captured_vars = new List<CapturedVariable> ();
434 List<CapturedScope> captured_scopes = new List<CapturedScope> ();
436 public AnonymousScopeEntry (int id)
441 internal AnonymousScopeEntry (MyBinaryReader reader)
443 ID = reader.ReadLeb128 ();
445 int num_captured_vars = reader.ReadLeb128 ();
446 for (int i = 0; i < num_captured_vars; i++)
447 captured_vars.Add (new CapturedVariable (reader));
449 int num_captured_scopes = reader.ReadLeb128 ();
450 for (int i = 0; i < num_captured_scopes; i++)
451 captured_scopes.Add (new CapturedScope (reader));
454 internal void AddCapturedVariable (string name, string captured_name,
455 CapturedVariable.CapturedKind kind)
457 captured_vars.Add (new CapturedVariable (name, captured_name, kind));
460 public CapturedVariable[] CapturedVariables {
462 CapturedVariable[] retval = new CapturedVariable [captured_vars.Count];
463 captured_vars.CopyTo (retval, 0);
468 internal void AddCapturedScope (int scope, string captured_name)
470 captured_scopes.Add (new CapturedScope (scope, captured_name));
473 public CapturedScope[] CapturedScopes {
475 CapturedScope[] retval = new CapturedScope [captured_scopes.Count];
476 captured_scopes.CopyTo (retval, 0);
481 internal void Write (MyBinaryWriter bw)
485 bw.WriteLeb128 (captured_vars.Count);
486 foreach (CapturedVariable cv in captured_vars)
489 bw.WriteLeb128 (captured_scopes.Count);
490 foreach (CapturedScope cs in captured_scopes)
494 public override string ToString ()
496 return String.Format ("[AnonymousScope {0}]", ID);
500 public class CompileUnitEntry : ICompileUnit
502 #region This is actually written to the symbol file
503 public readonly int Index;
508 SourceFileEntry source;
509 List<SourceFileEntry> include_files;
510 List<NamespaceEntry> namespaces;
514 public static int Size {
518 CompileUnitEntry ICompileUnit.Entry {
522 public CompileUnitEntry (MonoSymbolFile file, SourceFileEntry source)
525 this.source = source;
527 this.Index = file.AddCompileUnit (this);
530 namespaces = new List<NamespaceEntry> ();
533 public void AddFile (SourceFileEntry file)
536 throw new InvalidOperationException ();
538 if (include_files == null)
539 include_files = new List<SourceFileEntry> ();
541 include_files.Add (file);
544 public SourceFileEntry SourceFile {
554 public int DefineNamespace (string name, string[] using_clauses, int parent)
557 throw new InvalidOperationException ();
559 int index = file.GetNextNamespaceIndex ();
560 NamespaceEntry ns = new NamespaceEntry (name, index, using_clauses, parent);
565 internal void WriteData (MyBinaryWriter bw)
567 DataOffset = (int) bw.BaseStream.Position;
568 bw.WriteLeb128 (source.Index);
570 int count_includes = include_files != null ? include_files.Count : 0;
571 bw.WriteLeb128 (count_includes);
572 if (include_files != null) {
573 foreach (SourceFileEntry entry in include_files)
574 bw.WriteLeb128 (entry.Index);
577 bw.WriteLeb128 (namespaces.Count);
578 foreach (NamespaceEntry ns in namespaces)
582 internal void Write (BinaryWriter bw)
585 bw.Write (DataOffset);
588 internal CompileUnitEntry (MonoSymbolFile file, MyBinaryReader reader)
592 Index = reader.ReadInt32 ();
593 DataOffset = reader.ReadInt32 ();
599 throw new InvalidOperationException ();
602 if (namespaces != null)
605 MyBinaryReader reader = file.BinaryReader;
606 int old_pos = (int) reader.BaseStream.Position;
608 reader.BaseStream.Position = DataOffset;
610 int source_idx = reader.ReadLeb128 ();
611 source = file.GetSourceFile (source_idx);
613 int count_includes = reader.ReadLeb128 ();
614 if (count_includes > 0) {
615 include_files = new List<SourceFileEntry> ();
616 for (int i = 0; i < count_includes; i++)
617 include_files.Add (file.GetSourceFile (reader.ReadLeb128 ()));
620 int count_ns = reader.ReadLeb128 ();
621 namespaces = new List<NamespaceEntry> ();
622 for (int i = 0; i < count_ns; i ++)
623 namespaces.Add (new NamespaceEntry (file, reader));
625 reader.BaseStream.Position = old_pos;
629 public NamespaceEntry[] Namespaces {
632 NamespaceEntry[] retval = new NamespaceEntry [namespaces.Count];
633 namespaces.CopyTo (retval, 0);
638 public SourceFileEntry[] IncludeFiles {
641 if (include_files == null)
642 return new SourceFileEntry [0];
644 SourceFileEntry[] retval = new SourceFileEntry [include_files.Count];
645 include_files.CopyTo (retval, 0);
651 public class SourceFileEntry
653 #region This is actually written to the symbol file
654 public readonly int Index;
665 public static int Size {
669 public SourceFileEntry (MonoSymbolFile file, string file_name)
672 this.file_name = file_name;
673 this.Index = file.AddSource (this);
678 public SourceFileEntry (MonoSymbolFile file, string file_name,
679 byte[] guid, byte[] checksum)
680 : this (file, file_name)
683 this.hash = checksum;
686 public byte[] Checksum {
692 internal void WriteData (MyBinaryWriter bw)
694 DataOffset = (int) bw.BaseStream.Position;
695 bw.Write (file_name);
698 guid = Guid.NewGuid ().ToByteArray ();
700 using (FileStream fs = new FileStream (file_name, FileMode.Open, FileAccess.Read)) {
701 MD5 md5 = MD5.Create ();
702 hash = md5.ComputeHash (fs);
705 hash = new byte [16];
711 bw.Write ((byte) (auto_generated ? 1 : 0));
714 internal void Write (BinaryWriter bw)
717 bw.Write (DataOffset);
720 internal SourceFileEntry (MonoSymbolFile file, MyBinaryReader reader)
724 Index = reader.ReadInt32 ();
725 DataOffset = reader.ReadInt32 ();
727 int old_pos = (int) reader.BaseStream.Position;
728 reader.BaseStream.Position = DataOffset;
730 file_name = reader.ReadString ();
731 guid = reader.ReadBytes (16);
732 hash = reader.ReadBytes (16);
733 auto_generated = reader.ReadByte () == 1;
735 reader.BaseStream.Position = old_pos;
738 public string FileName {
739 get { return file_name; }
742 public bool AutoGenerated {
743 get { return auto_generated; }
746 public void SetAutoGenerated ()
749 throw new InvalidOperationException ();
751 auto_generated = true;
752 file.OffsetTable.FileFlags |= OffsetTable.Flags.IsAspxSource;
755 public bool CheckChecksum ()
758 using (FileStream fs = new FileStream (file_name, FileMode.Open)) {
759 MD5 md5 = MD5.Create ();
760 byte[] data = md5.ComputeHash (fs);
761 for (int i = 0; i < 16; i++)
762 if (data [i] != hash [i])
771 public override string ToString ()
773 return String.Format ("SourceFileEntry ({0}:{1})", Index, DataOffset);
777 public class LineNumberTable
779 protected LineNumberEntry[] _line_numbers;
780 public LineNumberEntry[] LineNumbers {
781 get { return _line_numbers; }
784 public readonly int LineBase;
785 public readonly int LineRange;
786 public readonly byte OpcodeBase;
787 public readonly int MaxAddressIncrement;
789 #region Configurable constants
790 public const int Default_LineBase = -1;
791 public const int Default_LineRange = 8;
792 public const byte Default_OpcodeBase = 9;
794 public const bool SuppressDuplicates = true;
797 public const byte DW_LNS_copy = 1;
798 public const byte DW_LNS_advance_pc = 2;
799 public const byte DW_LNS_advance_line = 3;
800 public const byte DW_LNS_set_file = 4;
801 public const byte DW_LNS_const_add_pc = 8;
803 public const byte DW_LNE_end_sequence = 1;
806 public const byte DW_LNE_MONO_negate_is_hidden = 0x40;
808 internal const byte DW_LNE_MONO__extensions_start = 0x40;
809 internal const byte DW_LNE_MONO__extensions_end = 0x7f;
811 protected LineNumberTable (MonoSymbolFile file)
813 this.LineBase = file.OffsetTable.LineNumberTable_LineBase;
814 this.LineRange = file.OffsetTable.LineNumberTable_LineRange;
815 this.OpcodeBase = (byte) file.OffsetTable.LineNumberTable_OpcodeBase;
816 this.MaxAddressIncrement = (255 - OpcodeBase) / LineRange;
819 internal LineNumberTable (MonoSymbolFile file, LineNumberEntry[] lines)
822 this._line_numbers = lines;
825 internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
827 int start = (int) bw.BaseStream.Position;
829 bool last_is_hidden = false;
830 int last_line = 1, last_offset = 0, last_file = 1;
831 for (int i = 0; i < LineNumbers.Length; i++) {
832 int line_inc = LineNumbers [i].Row - last_line;
833 int offset_inc = LineNumbers [i].Offset - last_offset;
835 if (SuppressDuplicates && (i+1 < LineNumbers.Length)) {
836 if (LineNumbers [i+1].Equals (LineNumbers [i]))
840 if (LineNumbers [i].File != last_file) {
841 bw.Write (DW_LNS_set_file);
842 bw.WriteLeb128 (LineNumbers [i].File);
843 last_file = LineNumbers [i].File;
846 if (LineNumbers [i].IsHidden != last_is_hidden) {
849 bw.Write (DW_LNE_MONO_negate_is_hidden);
850 last_is_hidden = LineNumbers [i].IsHidden;
853 if (offset_inc >= MaxAddressIncrement) {
854 if (offset_inc < 2 * MaxAddressIncrement) {
855 bw.Write (DW_LNS_const_add_pc);
856 offset_inc -= MaxAddressIncrement;
858 bw.Write (DW_LNS_advance_pc);
859 bw.WriteLeb128 (offset_inc);
864 if ((line_inc < LineBase) || (line_inc >= LineBase + LineRange)) {
865 bw.Write (DW_LNS_advance_line);
866 bw.WriteLeb128 (line_inc);
867 if (offset_inc != 0) {
868 bw.Write (DW_LNS_advance_pc);
869 bw.WriteLeb128 (offset_inc);
871 bw.Write (DW_LNS_copy);
874 opcode = (byte) (line_inc - LineBase + (LineRange * offset_inc) +
879 last_line = LineNumbers [i].Row;
880 last_offset = LineNumbers [i].Offset;
885 bw.Write (DW_LNE_end_sequence);
887 file.ExtendedLineNumberSize += (int) bw.BaseStream.Position - start;
890 internal static LineNumberTable Read (MonoSymbolFile file, MyBinaryReader br)
892 LineNumberTable lnt = new LineNumberTable (file);
893 lnt.DoRead (file, br);
897 void DoRead (MonoSymbolFile file, MyBinaryReader br)
899 var lines = new List<LineNumberEntry> ();
901 bool is_hidden = false, modified = false;
902 int stm_line = 1, stm_offset = 0, stm_file = 1;
904 byte opcode = br.ReadByte ();
907 byte size = br.ReadByte ();
908 long end_pos = br.BaseStream.Position + size;
909 opcode = br.ReadByte ();
911 if (opcode == DW_LNE_end_sequence) {
913 lines.Add (new LineNumberEntry (
914 stm_file, stm_line, stm_offset, is_hidden));
916 } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
917 is_hidden = !is_hidden;
919 } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
920 (opcode <= DW_LNE_MONO__extensions_end)) {
921 ; // reserved for future extensions
923 throw new MonoSymbolFileException ("Unknown extended opcode {0:x}", opcode);
926 br.BaseStream.Position = end_pos;
928 } else if (opcode < OpcodeBase) {
931 lines.Add (new LineNumberEntry (
932 stm_file, stm_line, stm_offset, is_hidden));
935 case DW_LNS_advance_pc:
936 stm_offset += br.ReadLeb128 ();
939 case DW_LNS_advance_line:
940 stm_line += br.ReadLeb128 ();
943 case DW_LNS_set_file:
944 stm_file = br.ReadLeb128 ();
947 case DW_LNS_const_add_pc:
948 stm_offset += MaxAddressIncrement;
952 throw new MonoSymbolFileException (
953 "Unknown standard opcode {0:x} in LNT",
957 opcode -= OpcodeBase;
959 stm_offset += opcode / LineRange;
960 stm_line += LineBase + (opcode % LineRange);
961 lines.Add (new LineNumberEntry (
962 stm_file, stm_line, stm_offset, is_hidden));
967 _line_numbers = new LineNumberEntry [lines.Count];
968 lines.CopyTo (_line_numbers, 0);
971 public bool GetMethodBounds (out LineNumberEntry start, out LineNumberEntry end)
973 if (_line_numbers.Length > 1) {
974 start = _line_numbers [0];
975 end = _line_numbers [_line_numbers.Length - 1];
979 start = LineNumberEntry.Null;
980 end = LineNumberEntry.Null;
985 public class MethodEntry : IComparable
987 #region This is actually written to the symbol file
988 public readonly int CompileUnitIndex;
989 public readonly int Token;
990 public readonly int NamespaceID;
993 int LocalVariableTableOffset;
994 int LineNumberTableOffset;
995 int CodeBlockTableOffset;
996 int ScopeVariableTableOffset;
1003 public Flags MethodFlags {
1004 get { return flags; }
1007 public readonly CompileUnitEntry CompileUnit;
1009 LocalVariableEntry[] locals;
1010 CodeBlockEntry[] code_blocks;
1011 ScopeVariable[] scope_vars;
1012 LineNumberTable lnt;
1015 public readonly MonoSymbolFile SymbolFile;
1018 get { return index; }
1019 set { index = value; }
1025 LocalNamesAmbiguous = 1
1028 public const int Size = 12;
1030 internal MethodEntry (MonoSymbolFile file, MyBinaryReader reader, int index)
1032 this.SymbolFile = file;
1035 Token = reader.ReadInt32 ();
1036 DataOffset = reader.ReadInt32 ();
1037 LineNumberTableOffset = reader.ReadInt32 ();
1039 long old_pos = reader.BaseStream.Position;
1040 reader.BaseStream.Position = DataOffset;
1042 CompileUnitIndex = reader.ReadLeb128 ();
1043 LocalVariableTableOffset = reader.ReadLeb128 ();
1044 NamespaceID = reader.ReadLeb128 ();
1046 CodeBlockTableOffset = reader.ReadLeb128 ();
1047 ScopeVariableTableOffset = reader.ReadLeb128 ();
1049 RealNameOffset = reader.ReadLeb128 ();
1051 flags = (Flags) reader.ReadLeb128 ();
1053 reader.BaseStream.Position = old_pos;
1055 CompileUnit = file.GetCompileUnit (CompileUnitIndex);
1058 internal MethodEntry (MonoSymbolFile file, CompileUnitEntry comp_unit,
1059 int token, ScopeVariable[] scope_vars,
1060 LocalVariableEntry[] locals, LineNumberEntry[] lines,
1061 CodeBlockEntry[] code_blocks, string real_name,
1062 Flags flags, int namespace_id)
1064 this.SymbolFile = file;
1065 this.real_name = real_name;
1066 this.locals = locals;
1067 this.code_blocks = code_blocks;
1068 this.scope_vars = scope_vars;
1074 CompileUnitIndex = comp_unit.Index;
1075 CompileUnit = comp_unit;
1076 NamespaceID = namespace_id;
1078 CheckLineNumberTable (lines);
1079 lnt = new LineNumberTable (file, lines);
1080 file.NumLineNumbers += lines.Length;
1082 int num_locals = locals != null ? locals.Length : 0;
1084 if (num_locals <= 32) {
1085 // Most of the time, the O(n^2) factor is actually
1086 // less than the cost of allocating the hash table,
1087 // 32 is a rough number obtained through some testing.
1089 for (int i = 0; i < num_locals; i ++) {
1090 string nm = locals [i].Name;
1092 for (int j = i + 1; j < num_locals; j ++) {
1093 if (locals [j].Name == nm) {
1094 flags |= Flags.LocalNamesAmbiguous;
1095 goto locals_check_done;
1102 var local_names = new Dictionary<string, LocalVariableEntry> ();
1103 foreach (LocalVariableEntry local in locals) {
1104 if (local_names.ContainsKey (local.Name)) {
1105 flags |= Flags.LocalNamesAmbiguous;
1108 local_names.Add (local.Name, local);
1113 static void CheckLineNumberTable (LineNumberEntry[] line_numbers)
1115 int last_offset = -1;
1118 if (line_numbers == null)
1121 for (int i = 0; i < line_numbers.Length; i++) {
1122 LineNumberEntry line = line_numbers [i];
1124 if (line.Equals (LineNumberEntry.Null))
1125 throw new MonoSymbolFileException ();
1127 if (line.Offset < last_offset)
1128 throw new MonoSymbolFileException ();
1130 if (line.Offset > last_offset) {
1131 last_row = line.Row;
1132 last_offset = line.Offset;
1133 } else if (line.Row > last_row) {
1134 last_row = line.Row;
1139 internal void Write (MyBinaryWriter bw)
1141 if ((index <= 0) || (DataOffset == 0))
1142 throw new InvalidOperationException ();
1145 bw.Write (DataOffset);
1146 bw.Write (LineNumberTableOffset);
1149 internal void WriteData (MonoSymbolFile file, MyBinaryWriter bw)
1152 throw new InvalidOperationException ();
1154 LocalVariableTableOffset = (int) bw.BaseStream.Position;
1155 int num_locals = locals != null ? locals.Length : 0;
1156 bw.WriteLeb128 (num_locals);
1157 for (int i = 0; i < num_locals; i++)
1158 locals [i].Write (file, bw);
1159 file.LocalCount += num_locals;
1161 CodeBlockTableOffset = (int) bw.BaseStream.Position;
1162 int num_code_blocks = code_blocks != null ? code_blocks.Length : 0;
1163 bw.WriteLeb128 (num_code_blocks);
1164 for (int i = 0; i < num_code_blocks; i++)
1165 code_blocks [i].Write (bw);
1167 ScopeVariableTableOffset = (int) bw.BaseStream.Position;
1168 int num_scope_vars = scope_vars != null ? scope_vars.Length : 0;
1169 bw.WriteLeb128 (num_scope_vars);
1170 for (int i = 0; i < num_scope_vars; i++)
1171 scope_vars [i].Write (bw);
1173 if (real_name != null) {
1174 RealNameOffset = (int) bw.BaseStream.Position;
1175 bw.Write (real_name);
1178 LineNumberTableOffset = (int) bw.BaseStream.Position;
1179 lnt.Write (file, bw);
1181 DataOffset = (int) bw.BaseStream.Position;
1183 bw.WriteLeb128 (CompileUnitIndex);
1184 bw.WriteLeb128 (LocalVariableTableOffset);
1185 bw.WriteLeb128 (NamespaceID);
1187 bw.WriteLeb128 (CodeBlockTableOffset);
1188 bw.WriteLeb128 (ScopeVariableTableOffset);
1190 bw.WriteLeb128 (RealNameOffset);
1191 bw.WriteLeb128 ((int) flags);
1194 public LineNumberTable GetLineNumberTable ()
1200 if (LineNumberTableOffset == 0)
1203 MyBinaryReader reader = SymbolFile.BinaryReader;
1204 long old_pos = reader.BaseStream.Position;
1205 reader.BaseStream.Position = LineNumberTableOffset;
1207 lnt = LineNumberTable.Read (SymbolFile, reader);
1209 reader.BaseStream.Position = old_pos;
1214 public LocalVariableEntry[] GetLocals ()
1220 if (LocalVariableTableOffset == 0)
1223 MyBinaryReader reader = SymbolFile.BinaryReader;
1224 long old_pos = reader.BaseStream.Position;
1225 reader.BaseStream.Position = LocalVariableTableOffset;
1227 int num_locals = reader.ReadLeb128 ();
1228 locals = new LocalVariableEntry [num_locals];
1230 for (int i = 0; i < num_locals; i++)
1231 locals [i] = new LocalVariableEntry (SymbolFile, reader);
1233 reader.BaseStream.Position = old_pos;
1238 public CodeBlockEntry[] GetCodeBlocks ()
1241 if (code_blocks != null)
1244 if (CodeBlockTableOffset == 0)
1247 MyBinaryReader reader = SymbolFile.BinaryReader;
1248 long old_pos = reader.BaseStream.Position;
1249 reader.BaseStream.Position = CodeBlockTableOffset;
1251 int num_code_blocks = reader.ReadLeb128 ();
1252 code_blocks = new CodeBlockEntry [num_code_blocks];
1254 for (int i = 0; i < num_code_blocks; i++)
1255 code_blocks [i] = new CodeBlockEntry (i, reader);
1257 reader.BaseStream.Position = old_pos;
1262 public ScopeVariable[] GetScopeVariables ()
1265 if (scope_vars != null)
1268 if (ScopeVariableTableOffset == 0)
1271 MyBinaryReader reader = SymbolFile.BinaryReader;
1272 long old_pos = reader.BaseStream.Position;
1273 reader.BaseStream.Position = ScopeVariableTableOffset;
1275 int num_scope_vars = reader.ReadLeb128 ();
1276 scope_vars = new ScopeVariable [num_scope_vars];
1278 for (int i = 0; i < num_scope_vars; i++)
1279 scope_vars [i] = new ScopeVariable (reader);
1281 reader.BaseStream.Position = old_pos;
1286 public string GetRealName ()
1289 if (real_name != null)
1292 if (RealNameOffset == 0)
1295 real_name = SymbolFile.BinaryReader.ReadString (RealNameOffset);
1300 public int CompareTo (object obj)
1302 MethodEntry method = (MethodEntry) obj;
1304 if (method.Token < Token)
1306 else if (method.Token > Token)
1312 public override string ToString ()
1314 return String.Format ("[Method {0}:{1:x}:{2}:{3}]",
1315 index, Token, CompileUnitIndex, CompileUnit);
1319 public struct NamespaceEntry
1321 #region This is actually written to the symbol file
1322 public readonly string Name;
1323 public readonly int Index;
1324 public readonly int Parent;
1325 public readonly string[] UsingClauses;
1328 public NamespaceEntry (string name, int index, string[] using_clauses, int parent)
1332 this.Parent = parent;
1333 this.UsingClauses = using_clauses != null ? using_clauses : new string [0];
1336 internal NamespaceEntry (MonoSymbolFile file, MyBinaryReader reader)
1338 Name = reader.ReadString ();
1339 Index = reader.ReadLeb128 ();
1340 Parent = reader.ReadLeb128 ();
1342 int count = reader.ReadLeb128 ();
1343 UsingClauses = new string [count];
1344 for (int i = 0; i < count; i++)
1345 UsingClauses [i] = reader.ReadString ();
1348 internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
1351 bw.WriteLeb128 (Index);
1352 bw.WriteLeb128 (Parent);
1353 bw.WriteLeb128 (UsingClauses.Length);
1354 foreach (string uc in UsingClauses)
1358 public override string ToString ()
1360 return String.Format ("[Namespace {0}:{1}:{2}]", Name, Index, Parent);