Exemple #1
0
        private ModuleMetadata(ModuleMetadata metadata)
        {
            this.module = metadata.Module;

            // This instance will not be the owner of the
            // resources backing the metadata. 

            this.isImageOwner = false;
        }
Exemple #2
0
 internal static AssemblyMetadata CreateFromFile(ModuleMetadata manifestModule, string path)
 {
     return(new AssemblyMetadata(manifestModule, moduleName => ModuleMetadata.CreateFromFile(Path.Combine(Path.GetDirectoryName(path) ?? "", moduleName))));
 }
Exemple #3
0
 /// <summary>
 /// Finds all modules of an assembly on a specified path and builds an instance of <see cref="AssemblyMetadata"/> that represents them.
 /// </summary>
 /// <param name="path">The full path to the assembly on disk.</param>
 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
 /// <exception cref="ArgumentException"><paramref name="path"/> is invalid.</exception>
 /// <exception cref="IOException">Error reading file <paramref name="path"/>. See <see cref="Exception.InnerException"/> for details.</exception>
 /// <exception cref="NotSupportedException">Reading from a file path is not supported by the platform.</exception>
 public static AssemblyMetadata CreateFromFile(string path)
 {
     return(CreateFromFile(ModuleMetadata.CreateFromFile(path), path));
 }
Exemple #4
0
 // creates a copy
 private ModuleMetadata(ModuleMetadata metadata)
     : base(isImageOwner: false)
 {
     this.module = metadata.Module;
 }
Exemple #5
0
 /// <summary>
 /// Creates a single-module assembly.
 /// </summary>
 /// <param name="peStream">Manifest module PE image stream.</param>
 /// <param name="leaveOpen">False to close the stream upon disposal of the metadata.</param>
 /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
 public static AssemblyMetadata CreateFromImageStream(Stream peStream, bool leaveOpen = false)
 {
     return(Create(ModuleMetadata.CreateFromImageStream(peStream, leaveOpen)));
 }
Exemple #6
0
 /// <summary>
 /// Creates a single-module assembly.
 /// </summary>
 /// <param name="peImage">
 /// Manifest module image.
 /// </param>
 /// <exception cref="ArgumentException"><paramref name="peImage"/> has the default value.</exception>
 /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
 public static AssemblyMetadata CreateFromImage(ImmutableArray <byte> peImage)
 {
     return(Create(ModuleMetadata.CreateFromImage(peImage)));
 }
 // creates a copy
 private ModuleMetadata(ModuleMetadata metadata)
     : base(isImageOwner: false, id: metadata.Id)
 {
     _module = metadata.Module;
 }
Exemple #8
0
 // creates a copy
 private ModuleMetadata(ModuleMetadata metadata)
     : base(isImageOwner: false, id: metadata.Id)
 {
     _module = metadata.Module;
 }
 public CachedModule(ModuleMetadata metadata)
 {
     Debug.Assert(metadata != null);
     this.Metadata = new WeakReference <ModuleMetadata>(metadata);
 }
Exemple #10
0
 public CachedModule(ModuleMetadata metadata)
 {
     Debug.Assert(metadata != null);
     this.Metadata = new WeakReference<ModuleMetadata>(metadata);
 }
Exemple #11
0
 private AssemblyMetadata(ModuleMetadata module)
 {
     this.Modules = ImmutableArray.Create(module);
     this.Assembly = new PEAssembly(this, ImmutableArray.Create(module.Module));
 }
Exemple #12
0
        /// <summary>
        /// Creates a single-module assembly.
        /// </summary>
        /// <param name="module">
        /// Manifest module.
        /// </param>
        /// <remarks>This object disposes <paramref name="module"/> it when it is itself disposed.</remarks>
        public static AssemblyMetadata Create(ModuleMetadata module)
        {
            if (module == null)
            {
                throw new ArgumentNullException("module");
            }

            return new AssemblyMetadata(module);
        }
        public int StopDebuggingPE()
        {
            try
            {
                log.Write("Exit Debug Mode: project '{0}'", _vsProject.DisplayName);
                Debug.Assert(s_breakStateEnteredProjects.Count == 0);

                // EnC service is global (per solution), but the debugger calls this for each project.
                // Avoid ending the debug session if it has already been ended.
                if (_encService.DebuggingSession != null)
                {
                    _encService.EndDebuggingSession();
                    LogEncSession();

                    s_encDebuggingSessionInfo = null;
                    s_readOnlyDocumentTracker.Dispose();
                    s_readOnlyDocumentTracker = null;
                }

                if (_metadata != null)
                {
                    _metadata.Dispose();
                    _metadata = null;

                    s_debugStateProjectCount--;
                }
                else
                {
                    // an error might have been reported:
                    _diagnosticProvider.ClearDiagnostics(_encService.DebuggingSession, _vsProject.VisualStudioWorkspace, EditAndContinueDiagnosticUpdateSource.DebuggerErrorId, _vsProject.Id, documentId: null);
                }

                _activeMethods = null;
                _exceptionRegions = null;
                _committedBaseline = null;
                _activeStatementIds = null;

                if (_pdbReaderObj != null)
                {
                    Marshal.ReleaseComObject(_pdbReaderObj);
                    _pdbReaderObj = null;
                }

                if (_pdbProvider != null)
                {
                    _pdbProvider.Dispose();
                    _pdbProvider = null;
                }

                _pdbReader = null;

                // The HResult is ignored by the debugger.
                return VSConstants.S_OK;
            }
            catch (Exception e) when(FatalError.Report(e))
            {
                throw ExceptionUtilities.Unreachable;
            }
            }
 /// <summary>
 /// Creates a reference to a standalone module image.
 /// </summary>
 /// <param name="metadata">Metadata for the standalone module.</param>
 /// <param name="filePath">Optional path that describes the location of the metadata. The file doesn't need to exist on disk. The path is opaque to the compiler.</param>
 /// <param name="documentation">Provides XML documentation for symbol found in the reference.</param>
 /// <param name="display">Display string for error reporting.</param>
 public MetadataImageReference(ModuleMetadata metadata, DocumentationProvider documentation = null, string filePath = null, string display = null)
     : this(RequireNonNull(metadata, "metadata"), documentation, MetadataReferenceProperties.Module, filePath, display)
 {
 }
        /// <summary>
        /// Invoked when the debugger transitions from Design mode to Run mode or Break mode.
        /// </summary>
        public int StartDebuggingPE()
        {
            try
            {
                log.Write("Enter Debug Mode: project '{0}'", _vsProject.DisplayName);

                // EnC service is global (per solution), but the debugger calls this for each project.
                // Avoid starting the debug session if it has already been started.
                if (_encService.DebuggingSession == null)
                {
                    Debug.Assert(s_debugStateProjectCount == 0);
                    Debug.Assert(s_breakStateProjectCount == 0);
                    Debug.Assert(s_breakStateEnteredProjects.Count == 0);

                    _encService.OnBeforeDebuggingStateChanged(DebuggingState.Design, DebuggingState.Run);

                    _encService.StartDebuggingSession(_vsProject.VisualStudioWorkspace.CurrentSolution);
                    s_encDebuggingSessionInfo = new EncDebuggingSessionInfo();

                    s_readOnlyDocumentTracker = new VsReadOnlyDocumentTracker(_encService, _editorAdaptersFactoryService, _vsProject);
                }

                string outputPath = _vsProject.TryGetObjOutputPath();

                // The project doesn't produce a debuggable binary or we can't read it.
                // Continue on since the debugger ignores HResults and we need to handle subsequent calls.
                if (outputPath != null)
                {
                    try
                    {
                        InjectFault_MvidRead();
                        _metadata = ModuleMetadata.CreateFromFile(outputPath);
                        _metadata.GetModuleVersionId();
                    }
                    catch (FileNotFoundException)
                    {
                        // If the project isn't referenced by the project being debugged it might not be built.
                        // In that case EnC is never allowed for the project, and thus we can assume the project hasn't entered debug state.
                        log.Write("StartDebuggingPE: '{0}' metadata file not found: '{1}'", _vsProject.DisplayName, outputPath);
                        _metadata = null;
                    }
                    catch (Exception e)
                    {
                        log.Write("StartDebuggingPE: error reading MVID of '{0}' ('{1}'): {2}", _vsProject.DisplayName, outputPath, e.Message);
                        _metadata = null;

                        var descriptor = new DiagnosticDescriptor("Metadata", "Metadata", ServicesVSResources.ErrorWhileReading, DiagnosticCategory.EditAndContinue, DiagnosticSeverity.Error, isEnabledByDefault: true, customTags: DiagnosticCustomTags.EditAndContinue);

                        _diagnosticProvider.ReportDiagnostics(_encService.DebuggingSession, EditAndContinueDiagnosticUpdateSource.DebuggerErrorId, _vsProject.Id, _encService.DebuggingSession.InitialSolution,
                            new[]
                            {
                                Diagnostic.Create(descriptor, Location.None, outputPath, e.Message)
                            });
                    }
                }
                else
                {
                    log.Write("StartDebuggingPE: project has no output path '{0}'", _vsProject.DisplayName);
                    _metadata = null;
                }

                if (_metadata != null)
                {
                    // The debugger doesn't call EnterBreakStateOnPE for projects that don't have MVID.
                    // However a project that's initially not loaded (but it might be in future) enters 
                    // both the debug and break states.
                    s_debugStateProjectCount++;
                }

                _activeMethods = new HashSet<uint>();
                _exceptionRegions = new List<VsExceptionRegion>();
                _activeStatementIds = new Dictionary<uint, ActiveStatementId>();

                // The HResult is ignored by the debugger.
                return VSConstants.S_OK;
            }
            catch (Exception e) when (FatalError.ReportWithoutCrash(e))
            {
                return VSConstants.E_FAIL;
            }
        }
        public int StopDebuggingPE()
        {
            try
            {
                log.Write("Exit Debug Mode: project '{0}'", _vsProject.DisplayName);
                Debug.Assert(s_breakStateEnteredProjects.Count == 0);

                // Clear the solution stored while projects were entering break mode. 
                // It should be cleared as soon as all tracked projects enter the break mode 
                // but if the entering break mode fails for some projects we should avoid leaking the solution.
                Debug.Assert(s_breakStateEntrySolution == null);
                s_breakStateEntrySolution = null;

                // EnC service is global (per solution), but the debugger calls this for each project.
                // Avoid ending the debug session if it has already been ended.
                if (_encService.DebuggingSession != null)
                {
                    _encService.OnBeforeDebuggingStateChanged(DebuggingState.Run, DebuggingState.Design);

                    _encService.EndDebuggingSession();
                    LogEncSession();

                    s_encDebuggingSessionInfo = null;
                    s_readOnlyDocumentTracker.Dispose();
                    s_readOnlyDocumentTracker = null;
                }

                if (_metadata != null)
                {
                    _metadata.Dispose();
                    _metadata = null;

                    s_debugStateProjectCount--;
                }
                else
                {
                    // an error might have been reported:
                    _diagnosticProvider.ClearDiagnostics(_encService.DebuggingSession, _vsProject.VisualStudioWorkspace, EditAndContinueDiagnosticUpdateSource.DebuggerErrorId, _vsProject.Id, documentId: null);
                }

                _activeMethods = null;
                _exceptionRegions = null;
                _committedBaseline = null;
                _activeStatementIds = null;

                Debug.Assert((_pdbReaderObjAsStream == IntPtr.Zero) || (_pdbReader == null));

                if (_pdbReader != null)
                {
                    Marshal.ReleaseComObject(_pdbReader);
                    _pdbReader = null;
                }

                // The HResult is ignored by the debugger.
                return VSConstants.S_OK;
            }
            catch (Exception e) when (FatalError.ReportWithoutCrash(e))
            {
                return VSConstants.E_FAIL;
            }
        }
Exemple #17
0
 internal static AssemblyMetadata CreateFromFile(ModuleMetadata manifestModule, string path)
 {
     return new AssemblyMetadata(manifestModule, moduleName => ModuleMetadata.CreateFromFile(Path.Combine(Path.GetDirectoryName(path), moduleName)));
 }
Exemple #18
0
 private static ModuleMetadata CreateModuleMetadata(FileStream stream)
 {
     // The Stream is held by the ModuleMetadata to read metadata on demand.
     // We hand off the responsibility for closing the stream to the metadata object.
     return(ModuleMetadata.CreateFromStream(stream, leaveOpen: false));
 }
Exemple #19
0
        /// <summary>
        /// Creates a single-module assembly.
        /// </summary>
        /// <param name="module">
        /// Manifest module.
        /// </param>
        /// <remarks>This object disposes <paramref name="module"/> it when it is itself disposed.</remarks>
        public static AssemblyMetadata Create(ModuleMetadata module)
        {
            if (module == null)
            {
                throw new ArgumentNullException(nameof(module));
            }

            return new AssemblyMetadata(ImmutableArray.Create(module));
        }
Exemple #20
0
 private AssemblyMetadata(ModuleMetadata module)
 {
     this.Modules  = ImmutableArray.Create(module);
     this.Assembly = new PEAssembly(this, ImmutableArray.Create(module.Module));
 }
Exemple #21
0
        internal AssemblyMetadata(ModuleMetadata manifestModule, Func<string, ModuleMetadata> moduleFactory)
            : base(isImageOwner: true, id: new MetadataId())
        {
            Debug.Assert(manifestModule != null);
            Debug.Assert(moduleFactory != null);

            _initialModules = ImmutableArray.Create(manifestModule);
            _moduleFactoryOpt = moduleFactory;
        }
Exemple #22
0
 /// <summary>
 /// Creates a single-module assembly.
 /// </summary>
 /// <param name="peImage">
 /// Manifest module image.
 /// </param>
 /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
 public static AssemblyMetadata CreateFromImage(IEnumerable <byte> peImage)
 {
     return(Create(ModuleMetadata.CreateFromImage(peImage)));
 }
        /// <exception cref="IOException"/>
        /// <exception cref="BadImageFormatException" />
        private AssemblyMetadata CreateAssemblyMetadata(
            FileKey fileKey, ModuleMetadata manifestModule, List<ITemporaryStreamStorage> storages,
            Func<FileKey, List<ITemporaryStreamStorage>, ModuleMetadata> moduleMetadataFactory)
        {
            ImmutableArray<ModuleMetadata>.Builder moduleBuilder = null;

            string assemblyDir = null;
            foreach (string moduleName in manifestModule.GetModuleNames())
            {
                if (moduleBuilder == null)
                {
                    moduleBuilder = ImmutableArray.CreateBuilder<ModuleMetadata>();
                    moduleBuilder.Add(manifestModule);
                    assemblyDir = Path.GetDirectoryName(fileKey.FullPath);
                }

                var moduleFileKey = FileKey.Create(PathUtilities.CombineAbsoluteAndRelativePaths(assemblyDir, moduleName));
                var metadata = moduleMetadataFactory(moduleFileKey, storages);

                moduleBuilder.Add(metadata);
            }

            var modules = (moduleBuilder != null) ? moduleBuilder.ToImmutable() : ImmutableArray.Create(manifestModule);
            return AssemblyMetadata.Create(modules);
        }
Exemple #24
0
 /// <summary>
 /// Creates a single-module assembly.
 /// </summary>
 /// <param name="peStream">Manifest module PE image stream.</param>
 /// <param name="options">False to close the stream upon disposal of the metadata.</param>
 /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
 public static AssemblyMetadata CreateFromImageStream(Stream peStream, PEStreamOptions options)
 {
     return(Create(ModuleMetadata.CreateFromImageStream(peStream, options)));
 }
Exemple #25
0
        private AssemblyMetadata CreateAssemblyMetadata(ShadowCopy manifestModuleCopy)
        {
            // We don't need to use the global metadata cache here since the shadow copy
            // won't change and is private to us - only users of the same shadow copy provider see it.

            ArrayBuilder <ModuleMetadata> moduleBuilder = null;

            bool           fault          = true;
            ModuleMetadata manifestModule = null;

            try
            {
                manifestModule = CreateModuleMetadata(manifestModuleCopy);

                string originalDirectory = null, shadowCopyDirectory = null;
                foreach (string moduleName in manifestModule.GetModuleNames())
                {
                    if (moduleBuilder == null)
                    {
                        moduleBuilder = ArrayBuilder <ModuleMetadata> .GetInstance();

                        moduleBuilder.Add(manifestModule);
                        originalDirectory   = Path.GetDirectoryName(manifestModuleCopy.OriginalPath);
                        shadowCopyDirectory = Path.GetDirectoryName(manifestModuleCopy.FullPath);
                    }

                    string originalPath   = Path.Combine(originalDirectory, moduleName);
                    string shadowCopyPath = Path.Combine(shadowCopyDirectory, moduleName);

                    var moduleCopy = CopyFile(originalPath, shadowCopyPath);
                    moduleBuilder.Add(CreateModuleMetadata(moduleCopy));
                }

                var modules = (moduleBuilder != null) ? moduleBuilder.ToImmutable() : ImmutableArray.Create(manifestModule);

                fault = false;
                return(new AssemblyMetadata(modules));
            }
            finally
            {
                if (fault)
                {
                    if (manifestModule != null)
                    {
                        manifestModule.Dispose();
                    }

                    if (moduleBuilder != null)
                    {
                        for (int i = 1; i < moduleBuilder.Count; i++)
                        {
                            moduleBuilder[i].Dispose();
                        }
                    }
                }

                if (moduleBuilder != null)
                {
                    moduleBuilder.Free();
                }
            }
        }