PdbReader.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // PdbReader.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using Microsoft.Cci.Pdb;
  32. using Mono.Cecil.Cil;
  33. namespace Mono.Cecil.Pdb
  34. {
  35. public class PdbReader : ISymbolReader
  36. {
  37. int age;
  38. Guid guid;
  39. readonly Stream pdb_file;
  40. readonly Dictionary<string, Document> documents = new Dictionary<string, Document>();
  41. readonly Dictionary<uint, PdbFunction> functions = new Dictionary<uint, PdbFunction>(new uintCompare());
  42. internal PdbReader(Stream file)
  43. {
  44. this.pdb_file = file;
  45. }
  46. /*
  47. uint Magic = 0x53445352;
  48. Guid Signature;
  49. uint Age;
  50. string FileName;
  51. */
  52. public bool ProcessDebugHeader(ImageDebugDirectory directory, byte[] header)
  53. {
  54. if (header.Length < 24)
  55. return false;
  56. var magic = ReadInt32(header, 0);
  57. if (magic != 0x53445352)
  58. return false;
  59. var guid_bytes = new byte[16];
  60. Buffer.BlockCopy(header, 4, guid_bytes, 0, 16);
  61. this.guid = new Guid(guid_bytes);
  62. this.age = ReadInt32(header, 20);
  63. return PopulateFunctions();
  64. }
  65. static int ReadInt32(byte[] bytes, int start)
  66. {
  67. return (bytes[start]
  68. | (bytes[start + 1] << 8)
  69. | (bytes[start + 2] << 16)
  70. | (bytes[start + 3] << 24));
  71. }
  72. bool PopulateFunctions()
  73. {
  74. using (pdb_file)
  75. {
  76. int age;
  77. Guid guid;
  78. var funcs = PdbFile.LoadFunctions(pdb_file, true, out age, out guid);
  79. if (this.age != 0 && this.guid != guid)
  80. return false;
  81. foreach (PdbFunction function in funcs)
  82. functions.Add(function.token, function);
  83. }
  84. return true;
  85. }
  86. public void Read(MethodBody body, InstructionMapper mapper)
  87. {
  88. var method_token = body.Method.MetadataToken;
  89. PdbFunction function;
  90. if (!functions.TryGetValue(method_token.ToUInt32(), out function))
  91. return;
  92. ReadSequencePoints(function, mapper);
  93. ReadScopeAndLocals(function.scopes, null, body, mapper);
  94. }
  95. static void ReadScopeAndLocals(PdbScope[] scopes, Scope parent, MethodBody body, InstructionMapper mapper)
  96. {
  97. foreach (PdbScope scope in scopes)
  98. ReadScopeAndLocals(scope, parent, body, mapper);
  99. CreateRootScope(body);
  100. }
  101. static void CreateRootScope(MethodBody body)
  102. {
  103. if (!body.HasVariables)
  104. return;
  105. var instructions = body.Instructions;
  106. var root = new Scope();
  107. root.Start = instructions[0];
  108. root.End = instructions[instructions.Count - 1];
  109. var variables = body.Variables;
  110. for (int i = 0; i < variables.Count; i++)
  111. root.Variables.Add(variables[i]);
  112. body.Scope = root;
  113. }
  114. static void ReadScopeAndLocals(PdbScope scope, Scope parent, MethodBody body, InstructionMapper mapper)
  115. {
  116. //Scope s = new Scope ();
  117. //s.Start = GetInstruction (body, instructions, (int) scope.address);
  118. //s.End = GetInstruction (body, instructions, (int) scope.length - 1);
  119. //if (parent != null)
  120. // parent.Scopes.Add (s);
  121. //else
  122. // body.Scopes.Add (s);
  123. if (scope == null)
  124. return;
  125. foreach (PdbSlot slot in scope.slots)
  126. {
  127. int index = (int)slot.slot;
  128. if (index < 0 || index >= body.Variables.Count)
  129. continue;
  130. VariableDefinition variable = body.Variables[index];
  131. variable.Name = slot.name;
  132. //s.Variables.Add (variable);
  133. }
  134. ReadScopeAndLocals(scope.scopes, null /* s */, body, mapper);
  135. }
  136. void ReadSequencePoints(PdbFunction function, InstructionMapper mapper)
  137. {
  138. if (function.lines == null)
  139. return;
  140. foreach (PdbLines lines in function.lines)
  141. ReadLines(lines, mapper);
  142. }
  143. void ReadLines(PdbLines lines, InstructionMapper mapper)
  144. {
  145. var document = GetDocument(lines.file);
  146. foreach (var line in lines.lines)
  147. ReadLine(line, document, mapper);
  148. }
  149. static void ReadLine(PdbLine line, Document document, InstructionMapper mapper)
  150. {
  151. var instruction = mapper((int)line.offset);
  152. if (instruction == null)
  153. return;
  154. var sequence_point = new SequencePoint(document);
  155. sequence_point.StartLine = (int)line.lineBegin;
  156. sequence_point.StartColumn = (int)line.colBegin;
  157. sequence_point.EndLine = (int)line.lineEnd;
  158. sequence_point.EndColumn = (int)line.colEnd;
  159. instruction.SequencePoint = sequence_point;
  160. }
  161. Document GetDocument(PdbSource source)
  162. {
  163. string name = source.name;
  164. Document document;
  165. if (documents.TryGetValue(name, out document))
  166. return document;
  167. document = new Document(name)
  168. {
  169. Language = GuidMapping.ToLanguage(source.language),
  170. LanguageVendor = GuidMapping.ToVendor(source.vendor),
  171. Type = GuidMapping.ToType(source.doctype),
  172. };
  173. documents.Add(name, document);
  174. return document;
  175. }
  176. public void Read(MethodSymbols symbols)
  177. {
  178. PdbFunction function;
  179. if (!functions.TryGetValue(symbols.MethodToken.ToUInt32(), out function))
  180. return;
  181. ReadSequencePoints(function, symbols);
  182. ReadLocals(function.scopes, symbols);
  183. }
  184. void ReadLocals(PdbScope[] scopes, MethodSymbols symbols)
  185. {
  186. foreach (var scope in scopes)
  187. ReadLocals(scope, symbols);
  188. }
  189. void ReadLocals(PdbScope scope, MethodSymbols symbols)
  190. {
  191. if (scope == null)
  192. return;
  193. foreach (var slot in scope.slots)
  194. {
  195. int index = (int)slot.slot;
  196. if (index < 0 || index >= symbols.Variables.Count)
  197. continue;
  198. var variable = symbols.Variables[index];
  199. variable.Name = slot.name;
  200. }
  201. ReadLocals(scope.scopes, symbols);
  202. }
  203. void ReadSequencePoints(PdbFunction function, MethodSymbols symbols)
  204. {
  205. if (function.lines == null)
  206. return;
  207. foreach (PdbLines lines in function.lines)
  208. ReadLines(lines, symbols);
  209. }
  210. void ReadLines(PdbLines lines, MethodSymbols symbols)
  211. {
  212. for (int i = 0; i < lines.lines.Length; i++)
  213. {
  214. var line = lines.lines[i];
  215. symbols.Instructions.Add(new InstructionSymbol((int)line.offset, new SequencePoint(GetDocument(lines.file))
  216. {
  217. StartLine = (int)line.lineBegin,
  218. StartColumn = (int)line.colBegin,
  219. EndLine = (int)line.lineEnd,
  220. EndColumn = (int)line.colEnd,
  221. }));
  222. }
  223. }
  224. public void Dispose()
  225. {
  226. pdb_file.Close();
  227. }
  228. }
  229. }