PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs

http://github.com/jbevain/cecil
C# | 127 lines | 87 code | 31 blank | 9 comment | 8 complexity | bb0f161c22e455ecd608a8c45b874fd6 MD5 | raw file
  1//
  2// Author:
  3//   Jb Evain (jbevain@gmail.com)
  4//
  5// Copyright (c) 2008 - 2015 Jb Evain
  6// Copyright (c) 2008 - 2011 Novell, Inc.
  7//
  8// Licensed under the MIT/X11 license.
  9//
 10
 11using System;
 12using System.IO;
 13
 14using Mono.Cecil.Cil;
 15
 16namespace Mono.Cecil.Pdb {
 17
 18	public sealed class NativePdbReaderProvider : ISymbolReaderProvider {
 19
 20		public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
 21		{
 22			Mixin.CheckModule (module);
 23			Mixin.CheckFileName (fileName);
 24
 25			return new NativePdbReader (Disposable.Owned (File.OpenRead (Mixin.GetPdbFileName (fileName)) as Stream));
 26		}
 27
 28		public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
 29		{
 30			Mixin.CheckModule (module);
 31			Mixin.CheckStream (symbolStream);
 32
 33			return new NativePdbReader (Disposable.NotOwned (symbolStream));
 34		}
 35	}
 36
 37	public sealed class PdbReaderProvider : ISymbolReaderProvider {
 38
 39		public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
 40		{
 41			Mixin.CheckModule (module);
 42
 43			if (module.HasDebugHeader) {
 44				var header = module.GetDebugHeader ();
 45				var entry = header.GetEmbeddedPortablePdbEntry ();
 46				if (entry != null)
 47					return new EmbeddedPortablePdbReaderProvider ().GetSymbolReader (module, fileName);
 48			}
 49			
 50			Mixin.CheckFileName (fileName);
 51
 52			return Mixin.IsPortablePdb (Mixin.GetPdbFileName (fileName))
 53				? new PortablePdbReaderProvider ().GetSymbolReader (module, fileName)
 54				: new NativePdbReaderProvider ().GetSymbolReader (module, fileName);
 55		}
 56
 57		public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
 58		{
 59			Mixin.CheckModule (module);
 60			Mixin.CheckStream (symbolStream);
 61			Mixin.CheckReadSeek (symbolStream);
 62
 63			return Mixin.IsPortablePdb (symbolStream)
 64				? new PortablePdbReaderProvider ().GetSymbolReader (module, symbolStream)
 65				: new NativePdbReaderProvider ().GetSymbolReader (module, symbolStream);
 66		}
 67	}
 68
 69	public sealed class NativePdbWriterProvider : ISymbolWriterProvider {
 70
 71		public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
 72		{
 73			Mixin.CheckModule (module);
 74			Mixin.CheckFileName (fileName);
 75
 76			return new NativePdbWriter (module, CreateWriter (module, Mixin.GetPdbFileName (fileName)));
 77		}
 78
 79		static SymWriter CreateWriter (ModuleDefinition module, string pdb)
 80		{
 81			var writer = new SymWriter ();
 82
 83			if (File.Exists (pdb))
 84				File.Delete (pdb);
 85
 86			writer.Initialize (new ModuleMetadata (module), pdb, true);
 87
 88			return writer;
 89		}
 90
 91		public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
 92		{
 93			throw new NotImplementedException ();
 94		}
 95	}
 96
 97	public sealed class PdbWriterProvider : ISymbolWriterProvider {
 98
 99		public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
100		{
101			Mixin.CheckModule (module);
102			Mixin.CheckFileName (fileName);
103
104			if (HasPortablePdbSymbols (module))
105				return new PortablePdbWriterProvider ().GetSymbolWriter (module, fileName);
106
107			return new NativePdbWriterProvider ().GetSymbolWriter (module, fileName);
108		}
109
110		static bool HasPortablePdbSymbols (ModuleDefinition module)
111		{
112			return module.symbol_reader != null && module.symbol_reader is PortablePdbReader;
113		}
114
115		public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
116		{
117			Mixin.CheckModule (module);
118			Mixin.CheckStream (symbolStream);
119			Mixin.CheckReadSeek (symbolStream);
120
121			if (HasPortablePdbSymbols (module))
122				return new PortablePdbWriterProvider ().GetSymbolWriter (module, symbolStream);
123
124			return new NativePdbWriterProvider ().GetSymbolWriter (module, symbolStream);
125		}
126	}
127}