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;
187 public readonly int File;
188 public readonly int Offset;
189 public readonly bool IsHidden; // Obsolete is never used
192 public sealed class LocationComparer : IComparer<LineNumberEntry>
194 public static readonly LocationComparer Default = new LocationComparer ();
196 public int Compare (LineNumberEntry l1, LineNumberEntry l2)
198 return l1.Row == l2.Row ?
199 l1.Column.CompareTo (l2.Column) :
200 l1.Row.CompareTo (l2.Row);
204 public static readonly LineNumberEntry Null = new LineNumberEntry (0, 0, 0, 0);
206 public LineNumberEntry (int file, int row, int column, int offset)
207 : this (file, row, offset, column, false)
211 public LineNumberEntry (int file, int row, int offset)
212 : this (file, row, -1, offset, false)
216 public LineNumberEntry (int file, int row, int column, int offset, bool is_hidden)
220 this.Column = column;
221 this.Offset = offset;
222 this.IsHidden = is_hidden;
225 public override string ToString ()
227 return String.Format ("[Line {0}:{1,2}:{3}]", File, Row, Column, Offset);
231 public class CodeBlockEntry
234 #region This is actually written to the symbol file
236 public Type BlockType;
237 public int StartOffset;
238 public int EndOffset;
243 CompilerGenerated = 2,
245 IteratorDispatcher = 4
248 public CodeBlockEntry (int index, int parent, Type type, int start_offset)
251 this.Parent = parent;
252 this.BlockType = type;
253 this.StartOffset = start_offset;
256 internal CodeBlockEntry (int index, MyBinaryReader reader)
259 int type_flag = reader.ReadLeb128 ();
260 BlockType = (Type) (type_flag & 0x3f);
261 this.Parent = reader.ReadLeb128 ();
262 this.StartOffset = reader.ReadLeb128 ();
263 this.EndOffset = reader.ReadLeb128 ();
265 /* Reserved for future extensions. */
266 if ((type_flag & 0x40) != 0) {
267 int data_size = reader.ReadInt16 ();
268 reader.BaseStream.Position += data_size;
272 public void Close (int end_offset)
274 this.EndOffset = end_offset;
277 internal void Write (MyBinaryWriter bw)
279 bw.WriteLeb128 ((int) BlockType);
280 bw.WriteLeb128 (Parent);
281 bw.WriteLeb128 (StartOffset);
282 bw.WriteLeb128 (EndOffset);
285 public override string ToString ()
287 return String.Format ("[CodeBlock {0}:{1}:{2}:{3}:{4}]",
288 Index, Parent, BlockType, StartOffset, EndOffset);
292 public struct LocalVariableEntry
294 #region This is actually written to the symbol file
295 public readonly int Index;
296 public readonly string Name;
297 public readonly int BlockIndex;
300 public LocalVariableEntry (int index, string name, int block)
304 this.BlockIndex = block;
307 internal LocalVariableEntry (MonoSymbolFile file, MyBinaryReader reader)
309 Index = reader.ReadLeb128 ();
310 Name = reader.ReadString ();
311 BlockIndex = reader.ReadLeb128 ();
314 internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
316 bw.WriteLeb128 (Index);
318 bw.WriteLeb128 (BlockIndex);
321 public override string ToString ()
323 return String.Format ("[LocalVariable {0}:{1}:{2}]",
324 Name, Index, BlockIndex - 1);
328 public struct CapturedVariable
330 #region This is actually written to the symbol file
331 public readonly string Name;
332 public readonly string CapturedName;
333 public readonly CapturedKind Kind;
336 public enum CapturedKind : byte
343 public CapturedVariable (string name, string captured_name,
347 this.CapturedName = captured_name;
351 internal CapturedVariable (MyBinaryReader reader)
353 Name = reader.ReadString ();
354 CapturedName = reader.ReadString ();
355 Kind = (CapturedKind) reader.ReadByte ();
358 internal void Write (MyBinaryWriter bw)
361 bw.Write (CapturedName);
362 bw.Write ((byte) Kind);
365 public override string ToString ()
367 return String.Format ("[CapturedVariable {0}:{1}:{2}]",
368 Name, CapturedName, Kind);
372 public struct CapturedScope
374 #region This is actually written to the symbol file
375 public readonly int Scope;
376 public readonly string CapturedName;
379 public CapturedScope (int scope, string captured_name)
382 this.CapturedName = captured_name;
385 internal CapturedScope (MyBinaryReader reader)
387 Scope = reader.ReadLeb128 ();
388 CapturedName = reader.ReadString ();
391 internal void Write (MyBinaryWriter bw)
393 bw.WriteLeb128 (Scope);
394 bw.Write (CapturedName);
397 public override string ToString ()
399 return String.Format ("[CapturedScope {0}:{1}]",
400 Scope, CapturedName);
404 public struct ScopeVariable
406 #region This is actually written to the symbol file
407 public readonly int Scope;
408 public readonly int Index;
411 public ScopeVariable (int scope, int index)
417 internal ScopeVariable (MyBinaryReader reader)
419 Scope = reader.ReadLeb128 ();
420 Index = reader.ReadLeb128 ();
423 internal void Write (MyBinaryWriter bw)
425 bw.WriteLeb128 (Scope);
426 bw.WriteLeb128 (Index);
429 public override string ToString ()
431 return String.Format ("[ScopeVariable {0}:{1}]", Scope, Index);
435 public class AnonymousScopeEntry
437 #region This is actually written to the symbol file
438 public readonly int ID;
441 List<CapturedVariable> captured_vars = new List<CapturedVariable> ();
442 List<CapturedScope> captured_scopes = new List<CapturedScope> ();
444 public AnonymousScopeEntry (int id)
449 internal AnonymousScopeEntry (MyBinaryReader reader)
451 ID = reader.ReadLeb128 ();
453 int num_captured_vars = reader.ReadLeb128 ();
454 for (int i = 0; i < num_captured_vars; i++)
455 captured_vars.Add (new CapturedVariable (reader));
457 int num_captured_scopes = reader.ReadLeb128 ();
458 for (int i = 0; i < num_captured_scopes; i++)
459 captured_scopes.Add (new CapturedScope (reader));
462 internal void AddCapturedVariable (string name, string captured_name,
463 CapturedVariable.CapturedKind kind)
465 captured_vars.Add (new CapturedVariable (name, captured_name, kind));
468 public CapturedVariable[] CapturedVariables {
470 CapturedVariable[] retval = new CapturedVariable [captured_vars.Count];
471 captured_vars.CopyTo (retval, 0);
476 internal void AddCapturedScope (int scope, string captured_name)
478 captured_scopes.Add (new CapturedScope (scope, captured_name));
481 public CapturedScope[] CapturedScopes {
483 CapturedScope[] retval = new CapturedScope [captured_scopes.Count];
484 captured_scopes.CopyTo (retval, 0);
489 internal void Write (MyBinaryWriter bw)
493 bw.WriteLeb128 (captured_vars.Count);
494 foreach (CapturedVariable cv in captured_vars)
497 bw.WriteLeb128 (captured_scopes.Count);
498 foreach (CapturedScope cs in captured_scopes)
502 public override string ToString ()
504 return String.Format ("[AnonymousScope {0}]", ID);
508 public class CompileUnitEntry : ICompileUnit
510 #region This is actually written to the symbol file
511 public readonly int Index;
516 SourceFileEntry source;
517 List<SourceFileEntry> include_files;
518 List<NamespaceEntry> namespaces;
522 public static int Size {
526 CompileUnitEntry ICompileUnit.Entry {
530 public CompileUnitEntry (MonoSymbolFile file, SourceFileEntry source)
533 this.source = source;
535 this.Index = file.AddCompileUnit (this);
538 namespaces = new List<NamespaceEntry> ();
541 public void AddFile (SourceFileEntry file)
544 throw new InvalidOperationException ();
546 if (include_files == null)
547 include_files = new List<SourceFileEntry> ();
549 include_files.Add (file);
552 public SourceFileEntry SourceFile {
562 public int DefineNamespace (string name, string[] using_clauses, int parent)
565 throw new InvalidOperationException ();
567 int index = file.GetNextNamespaceIndex ();
568 NamespaceEntry ns = new NamespaceEntry (name, index, using_clauses, parent);
573 internal void WriteData (MyBinaryWriter bw)
575 DataOffset = (int) bw.BaseStream.Position;
576 bw.WriteLeb128 (source.Index);
578 int count_includes = include_files != null ? include_files.Count : 0;
579 bw.WriteLeb128 (count_includes);
580 if (include_files != null) {
581 foreach (SourceFileEntry entry in include_files)
582 bw.WriteLeb128 (entry.Index);
585 bw.WriteLeb128 (namespaces.Count);
586 foreach (NamespaceEntry ns in namespaces)
590 internal void Write (BinaryWriter bw)
593 bw.Write (DataOffset);
596 internal CompileUnitEntry (MonoSymbolFile file, MyBinaryReader reader)
600 Index = reader.ReadInt32 ();
601 DataOffset = reader.ReadInt32 ();
607 throw new InvalidOperationException ();
610 if (namespaces != null)
613 MyBinaryReader reader = file.BinaryReader;
614 int old_pos = (int) reader.BaseStream.Position;
616 reader.BaseStream.Position = DataOffset;
618 int source_idx = reader.ReadLeb128 ();
619 source = file.GetSourceFile (source_idx);
621 int count_includes = reader.ReadLeb128 ();
622 if (count_includes > 0) {
623 include_files = new List<SourceFileEntry> ();
624 for (int i = 0; i < count_includes; i++)
625 include_files.Add (file.GetSourceFile (reader.ReadLeb128 ()));
628 int count_ns = reader.ReadLeb128 ();
629 namespaces = new List<NamespaceEntry> ();
630 for (int i = 0; i < count_ns; i ++)
631 namespaces.Add (new NamespaceEntry (file, reader));
633 reader.BaseStream.Position = old_pos;
637 public NamespaceEntry[] Namespaces {
640 NamespaceEntry[] retval = new NamespaceEntry [namespaces.Count];
641 namespaces.CopyTo (retval, 0);
646 public SourceFileEntry[] IncludeFiles {
649 if (include_files == null)
650 return new SourceFileEntry [0];
652 SourceFileEntry[] retval = new SourceFileEntry [include_files.Count];
653 include_files.CopyTo (retval, 0);
659 public class SourceFileEntry
661 #region This is actually written to the symbol file
662 public readonly int Index;
673 public static int Size {
677 public SourceFileEntry (MonoSymbolFile file, string file_name)
680 this.file_name = file_name;
681 this.Index = file.AddSource (this);
686 public SourceFileEntry (MonoSymbolFile file, string file_name,
687 byte[] guid, byte[] checksum)
688 : this (file, file_name)
691 this.hash = checksum;
694 public byte[] Checksum {
700 internal void WriteData (MyBinaryWriter bw)
702 DataOffset = (int) bw.BaseStream.Position;
703 bw.Write (file_name);
706 guid = Guid.NewGuid ().ToByteArray ();
708 using (FileStream fs = new FileStream (file_name, FileMode.Open, FileAccess.Read)) {
709 MD5 md5 = MD5.Create ();
710 hash = md5.ComputeHash (fs);
713 hash = new byte [16];
719 bw.Write ((byte) (auto_generated ? 1 : 0));
722 internal void Write (BinaryWriter bw)
725 bw.Write (DataOffset);
728 internal SourceFileEntry (MonoSymbolFile file, MyBinaryReader reader)
732 Index = reader.ReadInt32 ();
733 DataOffset = reader.ReadInt32 ();
735 int old_pos = (int) reader.BaseStream.Position;
736 reader.BaseStream.Position = DataOffset;
738 file_name = reader.ReadString ();
739 guid = reader.ReadBytes (16);
740 hash = reader.ReadBytes (16);
741 auto_generated = reader.ReadByte () == 1;
743 reader.BaseStream.Position = old_pos;
746 public string FileName {
747 get { return file_name; }
750 public bool AutoGenerated {
751 get { return auto_generated; }
754 public void SetAutoGenerated ()
757 throw new InvalidOperationException ();
759 auto_generated = true;
760 file.OffsetTable.FileFlags |= OffsetTable.Flags.IsAspxSource;
763 public bool CheckChecksum ()
766 using (FileStream fs = new FileStream (file_name, FileMode.Open)) {
767 MD5 md5 = MD5.Create ();
768 byte[] data = md5.ComputeHash (fs);
769 for (int i = 0; i < 16; i++)
770 if (data [i] != hash [i])
779 public override string ToString ()
781 return String.Format ("SourceFileEntry ({0}:{1})", Index, DataOffset);
785 public class LineNumberTable
787 protected LineNumberEntry[] _line_numbers;
788 public LineNumberEntry[] LineNumbers {
789 get { return _line_numbers; }
792 public readonly int LineBase;
793 public readonly int LineRange;
794 public readonly byte OpcodeBase;
795 public readonly int MaxAddressIncrement;
797 #region Configurable constants
798 public const int Default_LineBase = -1;
799 public const int Default_LineRange = 8;
800 public const byte Default_OpcodeBase = 9;
804 public const byte DW_LNS_copy = 1;
805 public const byte DW_LNS_advance_pc = 2;
806 public const byte DW_LNS_advance_line = 3;
807 public const byte DW_LNS_set_file = 4;
808 public const byte DW_LNS_const_add_pc = 8;
810 public const byte DW_LNE_end_sequence = 1;
813 public const byte DW_LNE_MONO_negate_is_hidden = 0x40;
815 internal const byte DW_LNE_MONO__extensions_start = 0x40;
816 internal const byte DW_LNE_MONO__extensions_end = 0x7f;
818 protected LineNumberTable (MonoSymbolFile file)
820 this.LineBase = file.OffsetTable.LineNumberTable_LineBase;
821 this.LineRange = file.OffsetTable.LineNumberTable_LineRange;
822 this.OpcodeBase = (byte) file.OffsetTable.LineNumberTable_OpcodeBase;
823 this.MaxAddressIncrement = (255 - OpcodeBase) / LineRange;
826 internal LineNumberTable (MonoSymbolFile file, LineNumberEntry[] lines)
829 this._line_numbers = lines;
832 internal void Write (MonoSymbolFile file, MyBinaryWriter bw, bool readColumnsInfo)
834 int start = (int) bw.BaseStream.Position;
836 bool last_is_hidden = false;
837 int last_line = 1, last_offset = 0, last_file = 1;
838 for (int i = 0; i < LineNumbers.Length; i++) {
839 int line_inc = LineNumbers [i].Row - last_line;
840 int offset_inc = LineNumbers [i].Offset - last_offset;
842 if (LineNumbers [i].File != last_file) {
843 bw.Write (DW_LNS_set_file);
844 bw.WriteLeb128 (LineNumbers [i].File);
845 last_file = LineNumbers [i].File;
848 if (LineNumbers [i].IsHidden != last_is_hidden) {
851 bw.Write (DW_LNE_MONO_negate_is_hidden);
852 last_is_hidden = LineNumbers [i].IsHidden;
855 if (offset_inc >= MaxAddressIncrement) {
856 if (offset_inc < 2 * MaxAddressIncrement) {
857 bw.Write (DW_LNS_const_add_pc);
858 offset_inc -= MaxAddressIncrement;
860 bw.Write (DW_LNS_advance_pc);
861 bw.WriteLeb128 (offset_inc);
866 if ((line_inc < LineBase) || (line_inc >= LineBase + LineRange)) {
867 bw.Write (DW_LNS_advance_line);
868 bw.WriteLeb128 (line_inc);
869 if (offset_inc != 0) {
870 bw.Write (DW_LNS_advance_pc);
871 bw.WriteLeb128 (offset_inc);
873 bw.Write (DW_LNS_copy);
876 opcode = (byte) (line_inc - LineBase + (LineRange * offset_inc) +
881 last_line = LineNumbers [i].Row;
882 last_offset = LineNumbers [i].Offset;
887 bw.Write (DW_LNE_end_sequence);
889 for (int i = 0; i < LineNumbers.Length; i++) {
890 var ln = LineNumbers [i];
892 bw.WriteLeb128 (ln.Column);
895 file.ExtendedLineNumberSize += (int) bw.BaseStream.Position - start;
898 internal static LineNumberTable Read (MonoSymbolFile file, MyBinaryReader br, bool readColumnsInfo)
900 LineNumberTable lnt = new LineNumberTable (file);
901 lnt.DoRead (file, br, readColumnsInfo);
905 void DoRead (MonoSymbolFile file, MyBinaryReader br, bool includesColumns)
907 var lines = new List<LineNumberEntry> ();
909 bool is_hidden = false, modified = false;
910 int stm_line = 1, stm_offset = 0, stm_file = 1;
912 byte opcode = br.ReadByte ();
915 byte size = br.ReadByte ();
916 long end_pos = br.BaseStream.Position + size;
917 opcode = br.ReadByte ();
919 if (opcode == DW_LNE_end_sequence) {
921 lines.Add (new LineNumberEntry (
922 stm_file, stm_line, -1, stm_offset, is_hidden));
924 } else if (opcode == DW_LNE_MONO_negate_is_hidden) {
925 is_hidden = !is_hidden;
927 } else if ((opcode >= DW_LNE_MONO__extensions_start) &&
928 (opcode <= DW_LNE_MONO__extensions_end)) {
929 ; // reserved for future extensions
931 throw new MonoSymbolFileException ("Unknown extended opcode {0:x}", opcode);
934 br.BaseStream.Position = end_pos;
936 } else if (opcode < OpcodeBase) {
939 lines.Add (new LineNumberEntry (
940 stm_file, stm_line, -1, stm_offset, is_hidden));
943 case DW_LNS_advance_pc:
944 stm_offset += br.ReadLeb128 ();
947 case DW_LNS_advance_line:
948 stm_line += br.ReadLeb128 ();
951 case DW_LNS_set_file:
952 stm_file = br.ReadLeb128 ();
955 case DW_LNS_const_add_pc:
956 stm_offset += MaxAddressIncrement;
960 throw new MonoSymbolFileException (
961 "Unknown standard opcode {0:x} in LNT",
965 opcode -= OpcodeBase;
967 stm_offset += opcode / LineRange;
968 stm_line += LineBase + (opcode % LineRange);
969 lines.Add (new LineNumberEntry (
970 stm_file, stm_line, -1, stm_offset, is_hidden));
975 _line_numbers = lines.ToArray ();
977 if (includesColumns) {
978 for (int i = 0; i < _line_numbers.Length; ++i) {
979 var ln = _line_numbers[i];
981 ln.Column = br.ReadLeb128 ();
986 public bool GetMethodBounds (out LineNumberEntry start, out LineNumberEntry end)
988 if (_line_numbers.Length > 1) {
989 start = _line_numbers [0];
990 end = _line_numbers [_line_numbers.Length - 1];
994 start = LineNumberEntry.Null;
995 end = LineNumberEntry.Null;
1000 public class MethodEntry : IComparable
1002 #region This is actually written to the symbol file
1003 public readonly int CompileUnitIndex;
1004 public readonly int Token;
1005 public readonly int NamespaceID;
1008 int LocalVariableTableOffset;
1009 int LineNumberTableOffset;
1010 int CodeBlockTableOffset;
1011 int ScopeVariableTableOffset;
1018 public Flags MethodFlags {
1019 get { return flags; }
1022 public readonly CompileUnitEntry CompileUnit;
1024 LocalVariableEntry[] locals;
1025 CodeBlockEntry[] code_blocks;
1026 ScopeVariable[] scope_vars;
1027 LineNumberTable lnt;
1030 public readonly MonoSymbolFile SymbolFile;
1033 get { return index; }
1034 set { index = value; }
1040 LocalNamesAmbiguous = 1,
1041 ColumnsInfoIncluded = 1 << 1
1044 public const int Size = 12;
1046 internal MethodEntry (MonoSymbolFile file, MyBinaryReader reader, int index)
1048 this.SymbolFile = file;
1051 Token = reader.ReadInt32 ();
1052 DataOffset = reader.ReadInt32 ();
1053 LineNumberTableOffset = reader.ReadInt32 ();
1055 long old_pos = reader.BaseStream.Position;
1056 reader.BaseStream.Position = DataOffset;
1058 CompileUnitIndex = reader.ReadLeb128 ();
1059 LocalVariableTableOffset = reader.ReadLeb128 ();
1060 NamespaceID = reader.ReadLeb128 ();
1062 CodeBlockTableOffset = reader.ReadLeb128 ();
1063 ScopeVariableTableOffset = reader.ReadLeb128 ();
1065 RealNameOffset = reader.ReadLeb128 ();
1067 flags = (Flags) reader.ReadLeb128 ();
1069 reader.BaseStream.Position = old_pos;
1071 CompileUnit = file.GetCompileUnit (CompileUnitIndex);
1074 internal MethodEntry (MonoSymbolFile file, CompileUnitEntry comp_unit,
1075 int token, ScopeVariable[] scope_vars,
1076 LocalVariableEntry[] locals, LineNumberEntry[] lines,
1077 CodeBlockEntry[] code_blocks, string real_name,
1078 Flags flags, int namespace_id)
1080 this.SymbolFile = file;
1081 this.real_name = real_name;
1082 this.locals = locals;
1083 this.code_blocks = code_blocks;
1084 this.scope_vars = scope_vars;
1090 CompileUnitIndex = comp_unit.Index;
1091 CompileUnit = comp_unit;
1092 NamespaceID = namespace_id;
1094 CheckLineNumberTable (lines);
1095 lnt = new LineNumberTable (file, lines);
1096 file.NumLineNumbers += lines.Length;
1098 int num_locals = locals != null ? locals.Length : 0;
1100 if (num_locals <= 32) {
1101 // Most of the time, the O(n^2) factor is actually
1102 // less than the cost of allocating the hash table,
1103 // 32 is a rough number obtained through some testing.
1105 for (int i = 0; i < num_locals; i ++) {
1106 string nm = locals [i].Name;
1108 for (int j = i + 1; j < num_locals; j ++) {
1109 if (locals [j].Name == nm) {
1110 flags |= Flags.LocalNamesAmbiguous;
1111 goto locals_check_done;
1118 var local_names = new Dictionary<string, LocalVariableEntry> ();
1119 foreach (LocalVariableEntry local in locals) {
1120 if (local_names.ContainsKey (local.Name)) {
1121 flags |= Flags.LocalNamesAmbiguous;
1124 local_names.Add (local.Name, local);
1129 static void CheckLineNumberTable (LineNumberEntry[] line_numbers)
1131 int last_offset = -1;
1134 if (line_numbers == null)
1137 for (int i = 0; i < line_numbers.Length; i++) {
1138 LineNumberEntry line = line_numbers [i];
1140 if (line.Equals (LineNumberEntry.Null))
1141 throw new MonoSymbolFileException ();
1143 if (line.Offset < last_offset)
1144 throw new MonoSymbolFileException ();
1146 if (line.Offset > last_offset) {
1147 last_row = line.Row;
1148 last_offset = line.Offset;
1149 } else if (line.Row > last_row) {
1150 last_row = line.Row;
1155 internal void Write (MyBinaryWriter bw)
1157 if ((index <= 0) || (DataOffset == 0))
1158 throw new InvalidOperationException ();
1161 bw.Write (DataOffset);
1162 bw.Write (LineNumberTableOffset);
1165 internal void WriteData (MonoSymbolFile file, MyBinaryWriter bw)
1168 throw new InvalidOperationException ();
1170 LocalVariableTableOffset = (int) bw.BaseStream.Position;
1171 int num_locals = locals != null ? locals.Length : 0;
1172 bw.WriteLeb128 (num_locals);
1173 for (int i = 0; i < num_locals; i++)
1174 locals [i].Write (file, bw);
1175 file.LocalCount += num_locals;
1177 CodeBlockTableOffset = (int) bw.BaseStream.Position;
1178 int num_code_blocks = code_blocks != null ? code_blocks.Length : 0;
1179 bw.WriteLeb128 (num_code_blocks);
1180 for (int i = 0; i < num_code_blocks; i++)
1181 code_blocks [i].Write (bw);
1183 ScopeVariableTableOffset = (int) bw.BaseStream.Position;
1184 int num_scope_vars = scope_vars != null ? scope_vars.Length : 0;
1185 bw.WriteLeb128 (num_scope_vars);
1186 for (int i = 0; i < num_scope_vars; i++)
1187 scope_vars [i].Write (bw);
1189 if (real_name != null) {
1190 RealNameOffset = (int) bw.BaseStream.Position;
1191 bw.Write (real_name);
1194 LineNumberTableOffset = (int) bw.BaseStream.Position;
1195 lnt.Write (file, bw, (flags & Flags.ColumnsInfoIncluded) != 0);
1197 DataOffset = (int) bw.BaseStream.Position;
1199 bw.WriteLeb128 (CompileUnitIndex);
1200 bw.WriteLeb128 (LocalVariableTableOffset);
1201 bw.WriteLeb128 (NamespaceID);
1203 bw.WriteLeb128 (CodeBlockTableOffset);
1204 bw.WriteLeb128 (ScopeVariableTableOffset);
1206 bw.WriteLeb128 (RealNameOffset);
1207 bw.WriteLeb128 ((int) flags);
1210 public LineNumberTable GetLineNumberTable ()
1216 if (LineNumberTableOffset == 0)
1219 MyBinaryReader reader = SymbolFile.BinaryReader;
1220 long old_pos = reader.BaseStream.Position;
1221 reader.BaseStream.Position = LineNumberTableOffset;
1223 lnt = LineNumberTable.Read (SymbolFile, reader, (flags & Flags.ColumnsInfoIncluded) != 0);
1225 reader.BaseStream.Position = old_pos;
1230 public LocalVariableEntry[] GetLocals ()
1236 if (LocalVariableTableOffset == 0)
1239 MyBinaryReader reader = SymbolFile.BinaryReader;
1240 long old_pos = reader.BaseStream.Position;
1241 reader.BaseStream.Position = LocalVariableTableOffset;
1243 int num_locals = reader.ReadLeb128 ();
1244 locals = new LocalVariableEntry [num_locals];
1246 for (int i = 0; i < num_locals; i++)
1247 locals [i] = new LocalVariableEntry (SymbolFile, reader);
1249 reader.BaseStream.Position = old_pos;
1254 public CodeBlockEntry[] GetCodeBlocks ()
1257 if (code_blocks != null)
1260 if (CodeBlockTableOffset == 0)
1263 MyBinaryReader reader = SymbolFile.BinaryReader;
1264 long old_pos = reader.BaseStream.Position;
1265 reader.BaseStream.Position = CodeBlockTableOffset;
1267 int num_code_blocks = reader.ReadLeb128 ();
1268 code_blocks = new CodeBlockEntry [num_code_blocks];
1270 for (int i = 0; i < num_code_blocks; i++)
1271 code_blocks [i] = new CodeBlockEntry (i, reader);
1273 reader.BaseStream.Position = old_pos;
1278 public ScopeVariable[] GetScopeVariables ()
1281 if (scope_vars != null)
1284 if (ScopeVariableTableOffset == 0)
1287 MyBinaryReader reader = SymbolFile.BinaryReader;
1288 long old_pos = reader.BaseStream.Position;
1289 reader.BaseStream.Position = ScopeVariableTableOffset;
1291 int num_scope_vars = reader.ReadLeb128 ();
1292 scope_vars = new ScopeVariable [num_scope_vars];
1294 for (int i = 0; i < num_scope_vars; i++)
1295 scope_vars [i] = new ScopeVariable (reader);
1297 reader.BaseStream.Position = old_pos;
1302 public string GetRealName ()
1305 if (real_name != null)
1308 if (RealNameOffset == 0)
1311 real_name = SymbolFile.BinaryReader.ReadString (RealNameOffset);
1316 public int CompareTo (object obj)
1318 MethodEntry method = (MethodEntry) obj;
1320 if (method.Token < Token)
1322 else if (method.Token > Token)
1328 public override string ToString ()
1330 return String.Format ("[Method {0}:{1:x}:{2}:{3}]",
1331 index, Token, CompileUnitIndex, CompileUnit);
1335 public struct NamespaceEntry
1337 #region This is actually written to the symbol file
1338 public readonly string Name;
1339 public readonly int Index;
1340 public readonly int Parent;
1341 public readonly string[] UsingClauses;
1344 public NamespaceEntry (string name, int index, string[] using_clauses, int parent)
1348 this.Parent = parent;
1349 this.UsingClauses = using_clauses != null ? using_clauses : new string [0];
1352 internal NamespaceEntry (MonoSymbolFile file, MyBinaryReader reader)
1354 Name = reader.ReadString ();
1355 Index = reader.ReadLeb128 ();
1356 Parent = reader.ReadLeb128 ();
1358 int count = reader.ReadLeb128 ();
1359 UsingClauses = new string [count];
1360 for (int i = 0; i < count; i++)
1361 UsingClauses [i] = reader.ReadString ();
1364 internal void Write (MonoSymbolFile file, MyBinaryWriter bw)
1367 bw.WriteLeb128 (Index);
1368 bw.WriteLeb128 (Parent);
1369 bw.WriteLeb128 (UsingClauses.Length);
1370 foreach (string uc in UsingClauses)
1374 public override string ToString ()
1376 return String.Format ("[Namespace {0}:{1}:{2}]", Name, Index, Parent);