Пример #1
0
 public int GetConstants(int cConstants, out int pcConstants, ISymUnmanagedConstant[] constants)
 {
     ((ISymUnmanagedScope2)_scope).GetConstants(cConstants, out pcConstants, constants);
     if (constants != null)
     {
         for (int i = 0; i < pcConstants; i++)
         {
             var    c             = constants[i];
             var    signaturesOpt = _reader._constantSignaturesOpt;
             byte[] signature     = null;
             if (signaturesOpt != null)
             {
                 int length;
                 int hresult = c.GetName(0, out length, null);
                 SymUnmanagedReaderExtensions.ThrowExceptionForHR(hresult);
                 var chars = new char[length];
                 hresult = c.GetName(length, out length, chars);
                 SymUnmanagedReaderExtensions.ThrowExceptionForHR(hresult);
                 var name = new string(chars, 0, length - 1);
                 signaturesOpt.TryGetValue(name, out signature);
             }
             constants[i] = new SymConstant(c, signature);
         }
     }
     return(SymUnmanagedReaderExtensions.S_OK);
 }
Пример #2
0
        private static ISymUnmanagedReader CreateReader(Stream pdbStream, object metadataImporter)
        {
            // NOTE: The product uses a different GUID (Microsoft.CodeAnalysis.ExpressionEvaluator.DkmUtilities.s_symUnmanagedReaderClassId).
            Guid corSymReaderSxS = new Guid("0A3976C5-4529-4ef8-B0B0-42EED37082CD");
            var  reader          = (ISymUnmanagedReader)Activator.CreateInstance(Marshal.GetTypeFromCLSID(corSymReaderSxS));
            int  hr = reader.Initialize(metadataImporter, null, null, new ComStreamWrapper(pdbStream));

            SymUnmanagedReaderExtensions.ThrowExceptionForHR(hr);
            return(reader);
        }
Пример #3
0
        private static ISymUnmanagedReader CreatePortableSymReader(Stream pdbStream, object metadataImporter)
        {
            var binder = new PortablePdb.SymBinder();

            ISymUnmanagedReader reader;
            int hr = binder.GetReaderFromStream(metadataImporter, new ComStreamWrapper(pdbStream), out reader);

            SymUnmanagedReaderExtensions.ThrowExceptionForHR(hr);

            return(reader);
        }
Пример #4
0
        void IDisposable.Dispose()
        {
            if (!_isDisposed)
            {
                for (int i = 0; i < _readerVersions.Length; i++)
                {
                    int hr = (_readerVersions[i] as ISymUnmanagedDispose).Destroy();
                    SymUnmanagedReaderExtensions.ThrowExceptionForHR(hr);
                    _readerVersions[i] = null;
                }

                _isDisposed = true;
            }
        }
        private static bool TryGetConstantValue(this ISymUnmanagedConstant constant, out NamedLocalConstant value)
        {
            value = default(NamedLocalConstant);

            int length;
            int hresult = constant.GetName(0, out length, null);

            SymUnmanagedReaderExtensions.ThrowExceptionForHR(hresult);
            Debug.Assert(length > 0);
            if (length == 0)
            {
                return(false);
            }

            var chars = new char[length];

            hresult = constant.GetName(length, out length, chars);
            SymUnmanagedReaderExtensions.ThrowExceptionForHR(hresult);
            Debug.Assert(chars[length - 1] == 0);
            var name = new string(chars, 0, length - 1);

            constant.GetSignature(0, out length, null);
            Debug.Assert(length > 0);
            if (length == 0)
            {
                return(false);
            }

            var signature = new byte[length];

            constant.GetSignature(length, out length, signature);

            object val;

            constant.GetValue(out val);

            var constantValue = GetConstantValue(signature, val);

            value = new NamedLocalConstant(name, signature, constantValue);
            return(true);
        }
Пример #6
0
        private static ISymUnmanagedReader3 CreateNativeSymReader(Stream pdbStream, object metadataImporter)
        {
            object symReader = null;

            var guid = default(Guid);

            if (IntPtr.Size == 4)
            {
                CreateSymReader32(ref guid, out symReader);
            }
            else
            {
                CreateSymReader64(ref guid, out symReader);
            }

            var reader = (ISymUnmanagedReader3)symReader;
            int hr     = reader.Initialize(metadataImporter, null, null, new ComStreamWrapper(pdbStream));

            SymUnmanagedReaderExtensions.ThrowExceptionForHR(hr);
            return(reader);
        }
Пример #7
0
        public static ISymUnmanagedReader CreateReader(Stream pdbStream, object metadataImporter)
        {
            pdbStream.Position = 0;
            bool isPortable = pdbStream.ReadByte() == 'B' && pdbStream.ReadByte() == 'S' && pdbStream.ReadByte() == 'J' && pdbStream.ReadByte() == 'B';

            pdbStream.Position = 0;

            if (isPortable)
            {
                var binder = new PortablePdb.SymBinder();

                ISymUnmanagedReader reader;
                int hr = binder.GetReaderFromStream(metadataImporter, new ComStreamWrapper(pdbStream), out reader);
                SymUnmanagedReaderExtensions.ThrowExceptionForHR(hr);

                return(reader);
            }
            else
            {
                return(CreateNativeSymReader(pdbStream, metadataImporter));
            }
        }
Пример #8
0
        public unsafe static MethodDebugInfo <TTypeSymbol, TLocalSymbol> ReadMethodDebugInfo(
            ISymUnmanagedReader symReader,
            EESymbolProvider <TTypeSymbol, TLocalSymbol> symbolProviderOpt, // TODO: only null in DTEE case where we looking for default namesapace
            int methodToken,
            int methodVersion,
            int ilOffset,
            bool isVisualBasicMethod)
        {
            // no symbols
            if (symReader == null)
            {
                return(None);
            }

            var symReader4 = symReader as ISymUnmanagedReader4;

            if (symReader4 != null) // TODO: VB Portable PDBs
            {
                byte *metadata;
                int   size;

                // TODO: version
                int hr = symReader4.GetPortableDebugMetadata(out metadata, out size);
                SymUnmanagedReaderExtensions.ThrowExceptionForHR(hr);

                if (metadata != null)
                {
                    var mdReader = new MetadataReader(metadata, size);
                    try
                    {
                        return(ReadFromPortable(mdReader, methodToken, ilOffset, symbolProviderOpt, isVisualBasicMethod));
                    }
                    catch (BadImageFormatException)
                    {
                        // bad CDI, ignore
                        return(None);
                    }
                }
            }

            var allScopes = ArrayBuilder <ISymUnmanagedScope> .GetInstance();

            var containingScopes = ArrayBuilder <ISymUnmanagedScope> .GetInstance();

            try
            {
                ImmutableArray <HoistedLocalScopeRecord>        hoistedLocalScopeRecords;
                ImmutableArray <ImmutableArray <ImportRecord> > importRecordGroups;
                ImmutableArray <ExternAliasRecord> externAliasRecords;
                ImmutableDictionary <int, ImmutableArray <bool> >    dynamicLocalMap;
                ImmutableDictionary <string, ImmutableArray <bool> > dynamicLocalConstantMap;
                string defaultNamespaceName;

                var symMethod = symReader.GetMethodByVersion(methodToken, methodVersion);
                if (symMethod != null)
                {
                    symMethod.GetAllScopes(allScopes, containingScopes, ilOffset, isScopeEndInclusive: isVisualBasicMethod);
                }

                if (isVisualBasicMethod)
                {
                    ReadVisualBasicImportsDebugInfo(
                        symReader,
                        methodToken,
                        methodVersion,
                        out importRecordGroups,
                        out defaultNamespaceName);

                    hoistedLocalScopeRecords = ImmutableArray <HoistedLocalScopeRecord> .Empty;
                    externAliasRecords       = ImmutableArray <ExternAliasRecord> .Empty;
                    dynamicLocalMap          = null;
                    dynamicLocalConstantMap  = null;
                }
                else
                {
                    Debug.Assert(symbolProviderOpt != null);

                    ReadCSharpNativeImportsInfo(
                        symReader,
                        symbolProviderOpt,
                        methodToken,
                        methodVersion,
                        out importRecordGroups,
                        out externAliasRecords);

                    ReadCSharpNativeCustomDebugInfo(
                        symReader,
                        methodToken,
                        methodVersion,
                        allScopes,
                        out hoistedLocalScopeRecords,
                        out dynamicLocalMap,
                        out dynamicLocalConstantMap);

                    defaultNamespaceName = "";
                }

                var constantsBuilder = ArrayBuilder <TLocalSymbol> .GetInstance();

                if (symbolProviderOpt != null) // TODO
                {
                    GetConstants(constantsBuilder, symbolProviderOpt, containingScopes, dynamicLocalConstantMap);
                }

                var reuseSpan = GetReuseSpan(allScopes, ilOffset, isVisualBasicMethod);

                return(new MethodDebugInfo <TTypeSymbol, TLocalSymbol>(
                           hoistedLocalScopeRecords,
                           importRecordGroups,
                           externAliasRecords,
                           dynamicLocalMap,
                           defaultNamespaceName,
                           containingScopes.GetLocalNames(),
                           constantsBuilder.ToImmutableAndFree(),
                           reuseSpan));
            }
            catch (InvalidOperationException)
            {
                // bad CDI, ignore
                return(None);
            }
            finally
            {
                allScopes.Free();
                containingScopes.Free();
            }
        }
Пример #9
0
        public unsafe static MethodDebugInfo <TTypeSymbol, TLocalSymbol> ReadMethodDebugInfo(
            ISymUnmanagedReader3 symReader,
            EESymbolProvider <TTypeSymbol, TLocalSymbol> symbolProviderOpt, // TODO: only null in DTEE case where we looking for default namesapace
            int methodToken,
            int methodVersion,
            int ilOffset,
            bool isVisualBasicMethod)
        {
            // no symbols
            if (symReader == null)
            {
                return(None);
            }

            if (symReader is ISymUnmanagedReader5 symReader5)
            {
                int hr = symReader5.GetPortableDebugMetadataByVersion(methodVersion, out byte *metadata, out int size);
                SymUnmanagedReaderExtensions.ThrowExceptionForHR(hr);

                if (hr == 0)
                {
                    var mdReader = new MetadataReader(metadata, size);
                    try
                    {
                        return(ReadFromPortable(mdReader, methodToken, ilOffset, symbolProviderOpt, isVisualBasicMethod));
                    }
                    catch (BadImageFormatException)
                    {
                        // bad CDI, ignore
                        return(None);
                    }
                }
            }

            var allScopes = ArrayBuilder <ISymUnmanagedScope> .GetInstance();

            var containingScopes = ArrayBuilder <ISymUnmanagedScope> .GetInstance();

            try
            {
                var symMethod = symReader.GetMethodByVersion(methodToken, methodVersion);
                if (symMethod != null)
                {
                    symMethod.GetAllScopes(allScopes, containingScopes, ilOffset, isScopeEndInclusive: isVisualBasicMethod);
                }

                ImmutableArray <ImmutableArray <ImportRecord> > importRecordGroups;
                ImmutableArray <ExternAliasRecord> externAliasRecords;
                string defaultNamespaceName;

                if (isVisualBasicMethod)
                {
                    ReadVisualBasicImportsDebugInfo(
                        symReader,
                        methodToken,
                        methodVersion,
                        out importRecordGroups,
                        out defaultNamespaceName);

                    externAliasRecords = ImmutableArray <ExternAliasRecord> .Empty;
                }
                else
                {
                    Debug.Assert(symbolProviderOpt != null);

                    ReadCSharpNativeImportsInfo(
                        symReader,
                        symbolProviderOpt,
                        methodToken,
                        methodVersion,
                        out importRecordGroups,
                        out externAliasRecords);

                    defaultNamespaceName = "";
                }

                // VB should read hoisted scope information from local variables:
                var hoistedLocalScopeRecords = isVisualBasicMethod ?
                                               default(ImmutableArray <HoistedLocalScopeRecord>) :
                                               ImmutableArray <HoistedLocalScopeRecord> .Empty;

                ImmutableDictionary <int, ImmutableArray <bool> >    dynamicLocalMap                    = null;
                ImmutableDictionary <string, ImmutableArray <bool> > dynamicLocalConstantMap            = null;
                ImmutableDictionary <int, ImmutableArray <string> >  tupleLocalMap                      = null;
                ImmutableDictionary <LocalNameAndScope, ImmutableArray <string> > tupleLocalConstantMap = null;

                byte[] customDebugInfo = symReader.GetCustomDebugInfoBytes(methodToken, methodVersion);
                if (customDebugInfo != null)
                {
                    if (!isVisualBasicMethod)
                    {
                        var customDebugInfoRecord = CustomDebugInfoReader.TryGetCustomDebugInfoRecord(customDebugInfo, CustomDebugInfoKind.StateMachineHoistedLocalScopes);
                        if (!customDebugInfoRecord.IsDefault)
                        {
                            hoistedLocalScopeRecords = CustomDebugInfoReader.DecodeStateMachineHoistedLocalScopesRecord(customDebugInfoRecord)
                                                       .SelectAsArray(s => new HoistedLocalScopeRecord(s.StartOffset, s.Length));
                        }

                        GetCSharpDynamicLocalInfo(
                            customDebugInfo,
                            allScopes,
                            out dynamicLocalMap,
                            out dynamicLocalConstantMap);
                    }

                    GetTupleElementNamesLocalInfo(
                        customDebugInfo,
                        out tupleLocalMap,
                        out tupleLocalConstantMap);
                }

                var constantsBuilder = ArrayBuilder <TLocalSymbol> .GetInstance();

                if (symbolProviderOpt != null) // TODO
                {
                    GetConstants(constantsBuilder, symbolProviderOpt, containingScopes, dynamicLocalConstantMap, tupleLocalConstantMap);
                }

                var reuseSpan = GetReuseSpan(allScopes, ilOffset, isVisualBasicMethod);

                return(new MethodDebugInfo <TTypeSymbol, TLocalSymbol>(
                           hoistedLocalScopeRecords,
                           importRecordGroups,
                           externAliasRecords,
                           dynamicLocalMap,
                           tupleLocalMap,
                           defaultNamespaceName,
                           containingScopes.GetLocalNames(),
                           constantsBuilder.ToImmutableAndFree(),
                           reuseSpan));
            }
            catch (InvalidOperationException)
            {
                // bad CDI, ignore
                return(None);
            }
            finally
            {
                allScopes.Free();
                containingScopes.Free();
            }
        }