Example #1
0
        public static ISymbolReader GetSymbolReaderForFile(SymbolBinder binder, string pathModule, string searchPath)
        {
            // Guids for imported metadata interfaces.
            Guid dispenserClassID = new Guid(0xe5cb7a31, 0x7512, 0x11d2, 0x89, 0xce, 0x00, 0x80, 0xc7, 0x92, 0xe5, 0xd8); // CLSID_CorMetaDataDispenser
            Guid dispenserIID = new Guid(0x809c652e, 0x7396, 0x11d2, 0x97, 0x71, 0x00, 0xa0, 0xc9, 0xb4, 0xd5, 0x0c); // IID_IMetaDataDispenser
            Guid importerIID = new Guid(0x7dac8207, 0xd3ae, 0x4c75, 0x9b, 0x67, 0x92, 0x80, 0x1a, 0x49, 0x7d, 0x44); // IID_IMetaDataImport

            // First create the Metadata dispenser.
            object objDispenser;
            NativeMethods.CoCreateInstance(ref dispenserClassID, null, 1, ref dispenserIID, out objDispenser);

            // Now open an Importer on the given filename. We'll end up passing this importer straight
            // through to the Binder.
            object objImporter;
            IMetaDataDispenser dispenser = (IMetaDataDispenser)objDispenser;
            dispenser.OpenScope(pathModule, 0, ref importerIID, out objImporter);

            IntPtr importerPtr = IntPtr.Zero;
            ISymbolReader reader;
            try
            {
                // This will manually AddRef the underlying object, so we need to be very careful to Release it.
                importerPtr = Marshal.GetComInterfaceForObject(objImporter, typeof(IMetadataImport));

                reader = binder.GetReader(importerPtr, pathModule, searchPath);
            }
            finally
            {
                if (importerPtr != IntPtr.Zero)
                {
                    Marshal.Release(importerPtr);
                }
            }
            return reader;
        }
Example #2
0
        /*
         * This private function provides implementation for the two public versions.
         * searchPath is a simicolon-delimited list of paths on which to search for pathModule.
         * If searchPath is null, pathModule must be a full path to the assembly.
         */
        private static ISymbolReader GetReaderForFile(SymbolBinder binder, string pathModule, string searchPath)
        {
            // First create the Metadata dispenser.
            object objDispenser;

            NativeMethods.CoCreateInstance(ref dispenserClassID, null, 1, ref dispenserIID, out objDispenser);

            // Now open an Importer on the given filename. We'll end up passing this importer straight
            // through to the Binder.
            object objImporter;
            IMetaDataDispenserPrivate dispenser = (IMetaDataDispenserPrivate)objDispenser;

            dispenser.OpenScope(pathModule, OPEN_READ, ref importerIID, out objImporter);

            IntPtr        importerPtr = IntPtr.Zero;
            ISymbolReader reader;

            try
            {
                // This will manually AddRef the underlying object, so we need to be very careful to Release it.
                importerPtr = Marshal.GetComInterfaceForObject(objImporter, typeof(IMetadataImportPrivateComVisible));

                reader = binder.GetReader(importerPtr, pathModule, searchPath);
            }
            finally
            {
                if (importerPtr != IntPtr.Zero)
                {
                    Marshal.Release(importerPtr);
                }
            }
            return(reader);
        }
Example #3
0
        public ISymbolReader GetReaderFromStream(IStream stream)
        {
            var metadata_import = this.GetMetaDataInterface <IMetadataImport>();
            var binder          = new Microsoft.Samples.Debugging.CorSymbolStore.SymbolBinder();

            return(binder.GetReaderFromStream(metadata_import, stream));
        }
Example #4
0
 private ISymbolReader getSymbolReader(CorModule module)
 {
     ISymbolReader reader = null;
     string sympath = Path.GetDirectoryName(module.Name);
     string moduleName = module.Name;
     try
     {
         SymbolBinder binder = new SymbolBinder();
         var importer = new CorMetadataImport(module);
         reader = binder.GetReaderForFile(importer.RawCOMObject, moduleName, sympath);
     }
     catch (COMException ex)
     {
         if (ex.ErrorCode == unchecked((int)0x806D0014))  // E_PDB_CORRUPT
         {
             // Ignore it.
             // This may happen for mismatched pdbs
         }
         else
             throw;
     }
     return reader;
 }
		void OnUpdateModuleSymbols (object sender, CorUpdateModuleSymbolsEventArgs e)
		{
			SymbolBinder binder = new SymbolBinder ();
			CorMetadataImport mi = new CorMetadataImport (e.Module);
			ISymbolReader reader = binder.GetReaderFromStream (mi.RawCOMObject, e.Stream);
			foreach (ISymbolDocument doc in reader.GetDocuments ()) {
				Console.WriteLine (doc.URL);
			}
			e.Continue = true;
		}
Example #6
0
        /*
         * Implementation which allows customization of the SymbolBinder to use.
         * searchPath is a simicolon-delimited list of paths on which to search for pathModule.
         * If searchPath is null, pathModule must be a full path to the assembly.
         */
         public static ISymbolReader GetReaderForFile(SymbolFormat symFormat, string pathModule, string searchPath)
        {
            // First create the Metadata dispenser.
            // Create the appropriate symbol binder
            SymbolBinder binder;
            if (symFormat == SymbolFormat.PDB)
                binder = new SymbolBinder();
            else if (symFormat == SymbolFormat.ILDB)
                binder = new IldbSymbolBinder();
            else
                throw new ArgumentException("Invalid format", "symFormat");

            // Create the Metadata dispenser.
            object objDispenser;
            NativeMethods.CoCreateInstance(ref dispenserClassID, null, 1, ref dispenserIID, out objDispenser);

            // Now open an Importer on the given filename. We'll end up passing this importer straight
            // through to the Binder.
            object objImporter;
            IMetaDataDispenserPrivate dispenser = (IMetaDataDispenserPrivate)objDispenser;
            dispenser.OpenScope(pathModule, OPEN_READ, ref importerIID, out objImporter);

            IntPtr importerPtr = IntPtr.Zero;
            ISymbolReader reader;
            try
            {
                // This will manually AddRef the underlying object, so we need to be very careful to Release it.
                importerPtr = Marshal.GetComInterfaceForObject(objImporter, typeof(IMetadataImportPrivateComVisible));

                reader = binder.GetReader(importerPtr, pathModule, searchPath);
            }
            finally
            {
                if (importerPtr != IntPtr.Zero)
                {
                    Marshal.Release(importerPtr);
                }
            }
            return reader;
        }
Example #7
0
        /// <summary>
        /// Read the pdb file for this module and frame
        /// Retrieve infomation about the function
        /// </summary>
        /// <remarks>
        /// When an unmanaged app like reflector loads CLR, "Function.Module.Name"
        /// doesn't return a valid value and so this function returns null.
        /// </remarks>
        /// <returns>SourcePosition of the function</returns>
        private SourcePosition GetMetaDataInfo(CorMetadataImport importer)
        {
            SourcePosition functionPos = null; //position in this function where we are
            try
            {
                moduleFullName = thisFrame.Function.Module.Name;
                moduleShortName = System.IO.Path.GetFileName(moduleFullName);
            }
            catch (ArgumentException)
            {
                moduleFullName = "";
                moduleShortName = "";
                return null;
            }

            //TODO: Implement a better method to determine the symbol path than just assuming it's in the same
            //      directory
            string sympath = ".";

            //dealing with readinf the source in the module
            ISymbolReader metaReader = null;
            ISymbolBinder1 symbolBinder = new SymbolBinder();

            try
            {
                if (moduleFullName.Length > 0)
                {
                    metaReader = (symbolBinder as ISymbolBinder2).
                        GetReaderForFile(importer.RawCOMObject, moduleFullName, sympath);
                }
            }
            catch (COMException)
            {
                //Debug.WriteLine(ed.ToString(CultureInfo.CurrentCulture.NumberFormat));
                //will get here for any function which we cant read the .pdb file for
                //its not a big deal we just wont have source and line info
            }

            if (metaReader != null)
            {
                ISymbolMethod symMethod = null;
                try
                {
                    symMethod = metaReader.GetMethod(new SymbolToken((int)thisFrame.Function.Token), thisFrame.Function.Version);
                    int sequenceCount = symMethod.SequencePointCount;
                    symDocs = new ISymbolDocument[sequenceCount];
                    offsets = new int[sequenceCount];
                    startLines = new int[sequenceCount];
                    startColumns = new int[sequenceCount];
                    endLines = new int[sequenceCount];
                    endColumns = new int[sequenceCount];

                    //Get the sequence points and store them in the apporpriate arrays. Seqeunce points
                    //represent the different points in the files which correlate to il instruction and lines
                    symMethod.GetSequencePoints(offsets, symDocs, startLines, startColumns, endLines, endColumns);

                    functionPos = GetSourcePositionFromFrame();
                }
                catch (COMException)
                {
                    functionPos = null;
                }
                finally
                {
                    symDocs = null;
                    symMethod = null;
                }
            }

            CorType ctype = GetClassType();
            if (ctype != null)
            {
                StringBuilder sb = new StringBuilder();
                GetFunctionClassPath(sb, ctype);
                functionFullName = sb.ToString();
            }
            else
            {
                functionFullName = "";
            }

            MethodInfo methIn = importer.GetMethodInfo(thisFrame.Function.Token);
            functionFullName += "." + methIn.Name;
            functionShortName = methIn.Name;
            return functionPos;
        }
Example #8
0
        public void SetBreakPoint(int lineNumber, string moduleNameHint, string fileNameHint)
        {
            if (state == ProcessState.Terminated) throw new InvalidProcessException();
            List<CorModule> matchingModules =
                modules.Where(module => module.Name.Contains(moduleNameHint)).ToList();
            if (matchingModules.Count > 1) throw new MultipleMatchException();
            if (matchingModules.Count == 0) throw new NoMatchException();
            CorModule corModule = matchingModules.First();
            var pdbFile = new FileInfo(corModule.Name);
            var binder = new SymbolBinder();
            var metadataImport = corModule.GetMetaDataInterface<IMetadataImport>();
            ISymbolReader reader = binder.GetReaderForFile(metadataImport, pdbFile.FullName, null,
                                                           SymSearchPolicies.AllowOriginalPathAccess |
                                                           SymSearchPolicies.AllowReferencePathAccess);

            Index(reader, corModule);
            ISymbolDocument[] documents = reader.GetDocuments();
            List<ISymbolDocument> matchingDocuments =
                documents.Where(document => Regex.Match(document.URL, fileNameHint).Success).ToList();
            if (matchingDocuments.Count > 1) throw new MultipleMatchException();
            if (matchingDocuments.Count == 0) throw new NoMatchException();
            ISymbolDocument source = matchingDocuments.First();
            int line = source.FindClosestLine(lineNumber);
            ISymbolMethod method = reader.GetMethodFromDocumentPosition(source, line, 1);
            CorFunction function = corModule.GetFunctionFromToken(method.Token.GetToken());
            SequencePoints points = SequencePoints.From(method);
            SequencePoint pointToBreak =
                points.Where(point => lineNumber >= point.StartLine && line <= point.EndLine).First();

            CorFunctionBreakpoint breakpoint = function.ILCode.CreateBreakpoint(pointToBreak.Offset);
            breakpoints.Add(breakpoint);
            breakpoint.Activate(true);
            subscriber.Published(string.Format("Successfully set breakpoint on line {0} in file {1} in module {2}",
                                               lineNumber, source.URL, corModule.Name));
        }
 public SymbolService()
 {
     this.binder = new SymbolBinder();
 }