GetReader() public méthode

public GetReader ( int importer, string filename, string searchPath ) : ISymbolReader
importer int
filename string
searchPath string
Résultat ISymbolReader
Exemple #1
0
        public static ISymbolReader GetSymbolReaderForFile(
            System.Diagnostics.SymbolStore.SymBinder 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 = null;
            IMetaDataDispenser dispenser   = (IMetaDataDispenser)objDispenser;

            try
            {
                dispenser.OpenScope(pathModule, 0, ref importerIID, out objImporter);
            }
            catch
            {
                Console.WriteLine(" ERROR: Failed dispenser.OpenScope() - perhaps this app needs to be compiled in x86?");
                return(null);
            }

            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);
        }
        public static ISymbolReader GetSymbolReader(SymBinder binder, string pathModule)
        {
            try
            {

                object objDispenser;
                CoCreateInstance(ref CLSID_CorMetaDataDispenser,
                    null,
                    1,
                    ref IID_IMetaDataDispenser,
                    out objDispenser);

                object objImporter;
                IMetaDataDispenser dispenser = (IMetaDataDispenser)objDispenser;
                dispenser.OpenScope(pathModule, 0, ref IID_IMetaDataImport, out objImporter);

                IntPtr importerPtr = IntPtr.Zero;
                ISymbolReader reader;
                try
                {
                    importerPtr = Marshal.GetComInterfaceForObject(objImporter, typeof(IMetadataImport));

                    reader = binder.GetReader(importerPtr, pathModule, null);
                }
                finally
                {
                    if (importerPtr != IntPtr.Zero)
                    {
                        Marshal.Release(importerPtr);
                    }
                }
                return reader;
            }
            catch(Exception)
            {
                return null;
            }
        }
Exemple #3
0
            private string GetLocalVariableName(ulong instructionPointer, uint localIndex)
            {
                ClrMethod method = _context.Runtime.GetMethodByAddress(instructionPointer);
                ClrModule module = method.Type.Module;
                string pdbLocation = _context.SymbolLocator.FindPdb(module.Pdb);
                IntPtr iunkMetadataImport = Marshal.GetIUnknownForObject(module.MetadataImport);
                ISymbolReader reader = null;
                ISymbolMethod symMethod = null;
                try
                {
                    using (var binder = new SymBinder())
                    {
                        reader = binder.GetReader(
                            iunkMetadataImport, module.FileName, Path.GetDirectoryName(pdbLocation));

                        symMethod = reader.GetMethod(new SymbolToken((int)method.MetadataToken));
                        return GetLocalVariableName(symMethod.RootScope, localIndex);
                    }
                }
                catch (COMException comEx)
                {
                    // E_FAIL occasionally occurs in ISymbolReader.GetMethod. Nothing we can do about it.
                    if ((uint)comEx.HResult == 0x80004005)
                        return "";

                    // 0x806D0005 occurs when the PDB cannot be found or doesn't contain the necessary
                    // information to create a symbol reader. It's OK to ignore.
                    if ((uint)comEx.HResult == 0x806D0005)
                        return "";

                    throw;
                }
                finally
                {
                    // These interfaces aren't IDisposable, but the underlying objects are. And it is
                    // important to dispose of them properly, because if their finalizer runs on exit,
                    // it crashes with an access violation.
                    if (reader != null)
                        ((IDisposable)reader).Dispose();
                    if (symMethod != null)
                        ((IDisposable)symMethod).Dispose();

                    Marshal.Release(iunkMetadataImport);
                }
            }
Exemple #4
0
        static ISymbolReader InstantiateReader(SymBinder binder, string filename, object objImporter)
        {
            IntPtr importerPtr = IntPtr.Zero;
            ISymbolReader reader;
            try {
                importerPtr = Marshal.GetComInterfaceForObject (objImporter, typeof (IMetadataImport));

                reader = binder.GetReader (importerPtr, filename, null);
            } finally {
                if (importerPtr != IntPtr.Zero)
                    Marshal.Release (importerPtr);
            }

            return reader;
        }
        static ISymbolReader GetReaderFor(string path)
        {
            CorMetaDataDispenser dispenser = null;
            object objImporter = null;
            IntPtr pImporter = IntPtr.Zero;
            Guid iidImporter = typeof(IMetadataImport).GUID;

            try {
                dispenser = new CorMetaDataDispenser();
                dispenser.OpenScope(path, 0, ref iidImporter, out objImporter);

                pImporter = Marshal.GetComInterfaceForObject(objImporter, typeof(IMetadataImport));
                var binder = new SymBinder();
                return binder.GetReader(pImporter, path, null);
            }
            finally {
                if (pImporter != IntPtr.Zero)
                    Marshal.Release(pImporter);
                if (objImporter != null)
                    Marshal.ReleaseComObject(objImporter);
                if (dispenser != null)
                    Marshal.ReleaseComObject(dispenser);
            }
        }
Exemple #6
0
        internal static GotoInfo[] LookupLocationsFromPdb(GotoInfo info, IVsSmartOpenScope vsSmartOpenScope)
        {
            Debug.Assert(info != null && info.MemberInfo != null, "LookupLocationsFromPdb lacks required parameter");
            var sources = new Dictionary<string, Location>();

            if (string.IsNullOrEmpty(info.FilePath) || !File.Exists(info.FilePath))
                return new GotoInfo[0];

            object unkMetaDataImport;
            IntPtr ptrMetaDataImport = IntPtr.Zero;
            ISymbolBinder1 binder;
            ISymbolReader reader;
            ISymbolDocument[] documents;
            int[] lines;
            int[] columns;
            int[] endLines;
            int[] endColumns;
            int[] offsets;
            var methods = new List<MethodBase>();
            Type ty = null;

            try
            {
                int hr = vsSmartOpenScope.OpenScope(info.FilePath, 0, ref IID_MetaDataImport, out unkMetaDataImport);

                if (hr == VSConstants.S_OK)
                {
                    ptrMetaDataImport = Marshal.GetIUnknownForObject(unkMetaDataImport);
                    binder = new SymBinder();
                    reader = binder.GetReader(ptrMetaDataImport, info.FilePath, null);
                    documents = new ISymbolDocument[1];
                    lines = new int[1];
                    columns = new int[1];
                    endLines = new int[1];
                    endColumns = new int[1];
                    offsets = new int[1];
                }
                else
                {
                    Debug.WriteLineIf(TS.TraceWarning,
                        string.Format("Failed to obtain MetaDataImport from VS, hr 0x{0:X8}", hr), TS.DisplayName);

                    return new GotoInfo[0];
                }

                switch (info.MemberInfo.MemberType)
                {
                    case MemberTypes.Constructor:
                    case MemberTypes.Method:
                        MethodBase mb = (MethodBase)info.MemberInfo;

                        // Abstract methods does not contain any code.
                        //
                        if (mb.IsAbstract)
                            methods.AddRange(mb.DeclaringType.GetMethods(DeclaredMembers));
                        else
                            methods.Add(mb);
                        break;

                    case MemberTypes.Property:
                        PropertyInfo pi = (PropertyInfo)info.MemberInfo;
                        methods.AddRange(pi.GetAccessors(true));
                        break;

                    case MemberTypes.Field:
                        methods.AddRange(info.MemberInfo.DeclaringType.GetMethods(DeclaredMembers));
                        break;

                    case MemberTypes.Event:
                        EventInfo ei = (EventInfo)info.MemberInfo;
                        methods.Add(ei.GetAddMethod(true));
                        methods.Add(ei.GetRemoveMethod(true));
                        methods.Add(ei.GetRaiseMethod(true));
                        methods.AddRange(ei.GetOtherMethods(true));
                        break;

                    case MemberTypes.TypeInfo:
                    case MemberTypes.NestedType:
                        ty = (Type)info.MemberInfo;
                        methods.AddRange(ty.GetMethods(DeclaredMembers));
                        methods.AddRange(ty.GetConstructors(DeclaredMembers));
                        break;
                    default:
                        Trace.Fail("Unexpected MemberType " + info.MemberInfo.MemberType);
                        break;
                }

                foreach (MethodBase mb in methods)
                {
                    if (mb == null || Attribute.GetCustomAttribute(mb, typeof(CompilerGeneratedAttribute)) != null)
                        continue;

                    try
                    {
                        SymbolToken token = new SymbolToken(mb.MetadataToken);
                        ISymbolMethod method = reader.GetMethod(token);

                        if (method.SequencePointCount > 0)
                        {
                            method.GetSequencePoints(offsets, documents, lines, columns, endLines, endColumns);

                            var path = documents[0].URL;
                            // We are interested in unique files only.
                            if (File.Exists(path) && (ty == null || mb.DeclaringType.Equals(ty)))
                            {
                                Location value;
                                if (sources.TryGetValue(path, out value))
                                {
                                    if ((value.Column == 0 || value.Line == 0) && lines[0] != 0 && columns[0] != 0)
                                        sources[path] = new Location(path, lines[0], columns[0], endLines[0], endColumns[0]);
                                }
                                else
                                    sources.Add(path, new Location(path, lines[0], columns[0], endLines[0], endColumns[0]));
                            }
                        }
                    }
                    catch (COMException ex)
                    {
                        // Abstract method or not a method at all.
                        // Sequence points are available only for methods.
                        //
                        Trace.WriteLineIf(TS.TraceError,
                            string.Format("({0}) {1}, code 0x{2:X8}", mb.Name, ex.Message, ex.ErrorCode), TS.DisplayName);
                    }
                }
            }
            catch (COMException ex)
            {
                // The file was not found or source locations were stripped from the pdb.
                //
                Trace.WriteLineIf(TS.TraceError,
                    string.Format("{0}, code 0x{1:8X}", ex.Message, ex.ErrorCode), TS.DisplayName);
            }
            finally
            {
                if (ptrMetaDataImport != IntPtr.Zero)
                    Marshal.Release(ptrMetaDataImport);
            }

            return sources.Select(x => new GotoInfo(x.Key, x.Value)).ToArray();
        }
        public ISymbolReader GetSymReader()
        {
            IntPtr uImporter = Marshal.GetIUnknownForObject(GetImporter());
            SymBinder binder = new SymBinder();
            ISymbolReader reader = binder.GetReader(uImporter,
                fullName,
                Environment.GetEnvironmentVariable("_NT_SYMBOL_PATH"));

            return reader;
        }