Beispiel #1
0
        public unsafe CodeChunkInfo[] GetCodeChunks()
        {
            var c2 = obj as ICorDebugCode2;

            if (c2 == null)
            {
                return(ArrayAddIn.Empty <CodeChunkInfo>());
            }
            int hr = c2.GetCodeChunks(0, out uint cnumChunks, IntPtr.Zero);

            if (hr < 0)
            {
                return(ArrayAddIn.Empty <CodeChunkInfo>());
            }
            var infos = new CodeChunkInfo[cnumChunks];

            if (cnumChunks != 0)
            {
                fixed(void *p = &infos[0])
                hr = c2.GetCodeChunks(cnumChunks, out cnumChunks, new IntPtr(p));

                if (hr < 0)
                {
                    return(ArrayAddIn.Empty <CodeChunkInfo>());
                }
            }
            return(infos);
        }
Beispiel #2
0
        ImageSectionHeader[] GetOrCreateSectionHeaders()
        {
            var h = sectionHeaders;

            if (h != null)
            {
                return(h);
            }

            try {
                ulong addr = module.Address;
                if (addr == 0)
                {
                    return(sectionHeaders = ArrayAddIn.Empty <ImageSectionHeader>());
                }
                var data = new byte[0x1000];
                module.Process.CorProcess.ReadMemory(module.Address, data, 0, data.Length, out int sizeRead);
                using (var peImage = new PEImage(data, !module.IsDynamic && module.IsInMemory ? ImageLayout.File : ImageLayout.Memory, true))
                    return(sectionHeaders = peImage.ImageSectionHeaders.ToArray());
            }
            catch {
                Debug.Fail("Couldn't read section headers");
            }
            return(sectionHeaders = ArrayAddIn.Empty <ImageSectionHeader>());
        }
Beispiel #3
0
        public bool GetTypeAndMethodGenericParameters(out CorType[] typeGenArgs, out CorType[] methGenArgs)
        {
            var func   = Function;
            var module = func?.Module;

            if (module == null)
            {
                typeGenArgs = ArrayAddIn.Empty <CorType>();
                methGenArgs = ArrayAddIn.Empty <CorType>();
                return(false);
            }

            var mdi = module.GetMetaDataInterface <IMetaDataImport>();
            var gas = new List <CorType>(TypeParameters);
            var cls = func.Class;
            int typeGenArgsCount = cls == null ? 0 : GetCountGenericParameters(mdi, cls.Token);
            int methGenArgsCount = GetCountGenericParameters(mdi, func.Token);

            Debug.Assert(typeGenArgsCount + methGenArgsCount == gas.Count);
            typeGenArgs = new CorType[typeGenArgsCount];
            methGenArgs = new CorType[methGenArgsCount];
            int j = 0;

            for (int i = 0; j < gas.Count && i < typeGenArgs.Length; i++, j++)
            {
                typeGenArgs[i] = gas[j];
            }
            for (int i = 0; j < gas.Count && i < methGenArgs.Length; i++, j++)
            {
                methGenArgs[i] = gas[j];
            }

            return(true);
        }
Beispiel #4
0
        private ImageSectionHeader[] GetOrCreateSectionHeaders()
        {
            var h = _sectionHeaders;

            if (h != null)
            {
                return(h);
            }

            try
            {
                ulong addr = _clrModule.ImageBase;
                if (addr == 0)
                {
                    return(_sectionHeaders = ArrayAddIn.Empty <ImageSectionHeader>());
                }
                var data = new byte[0x1000];
                //module.Process.CorProcess.ReadMemory(module.Address, data, 0, data.Length, out int sizeRead);
                MemoryIO.ReadBytes(_processId, (IntPtr)_clrModule.ImageBase, data);
                using (var peImage = new PEImage(data, !IsDynamic && IsInMemory ? ImageLayout.File : ImageLayout.Memory, true))
                    return(_sectionHeaders = peImage.ImageSectionHeaders.ToArray());
            }
            catch
            {
                Debug.Fail("Couldn't read section headers");
            }
            return(_sectionHeaders = ArrayAddIn.Empty <ImageSectionHeader>());
        }
Beispiel #5
0
        /// <summary>
        /// Searches for CoreCLR runtimes in a process
        /// </summary>
        /// <param name="pid">Process ID</param>
        /// <param name="runtimePath">Path of CoreCLR.dll or path of the CoreCLR runtime. This is
        /// used to find <c>dbgshim.dll</c> if <paramref name="dbgshimPath"/> is null</param>
        /// <param name="dbgshimPath">Filename of dbgshim.dll or null if we should look in
        /// <paramref name="runtimePath"/></param>
        /// <returns></returns>
        public unsafe static CoreCLRInfo[] GetCoreCLRInfos(int pid, string runtimePath, string dbgshimPath)
        {
            var dbgShimState = GetOrCreateDbgShimState(runtimePath, dbgshimPath);

            if (dbgShimState == null)
            {
                return(ArrayAddIn.Empty <CoreCLRInfo>());
            }
            int hr = dbgShimState.EnumerateCLRs((uint)pid, out var pHandleArray, out var pStringArray, out uint dwArrayLength);

            if (hr < 0 || dwArrayLength == 0)
            {
                return(ArrayAddIn.Empty <CoreCLRInfo>());
            }
            try {
                var ary = new CoreCLRInfo[dwArrayLength];
                var psa = (IntPtr *)pStringArray;
                for (int i = 0; i < ary.Length; i++)
                {
                    var version = GetVersionStringFromModule(dbgShimState, (uint)pid, psa[i], out string coreclrFilename);
                    ary[i] = new CoreCLRInfo(pid, coreclrFilename, version, dbgShimState.Filename);
                }

                return(ary);
            }
            finally {
                hr = dbgShimState.CloseCLREnumeration(pHandleArray, pStringArray, dwArrayLength);
                Debug.Assert(hr >= 0);
            }
        }
Beispiel #6
0
        void InitNameAndAttrs_NoLock()
        {
            var  mdai  = readerModule.MetaDataAssemblyImport;
            uint token = OriginalToken.Raw;

            Flags     = MDAPI.GetFileAttributes(mdai, token) ?? 0;
            Name      = MDAPI.GetFileName(mdai, token) ?? string.Empty;
            HashValue = MDAPI.GetFileHash(mdai, token) ?? ArrayAddIn.Empty <byte>();
        }
        protected override void InitializeSecurityAttributes()
        {
            var  mdi       = readerModule.MetaDataImport;
            uint token     = OriginalToken.Raw;
            var  data      = MDAPI.GetPermissionSetBlob(mdi, token) ?? ArrayAddIn.Empty <byte>();
            var  gpContext = new GenericParamContext();
            var  tmp       = DeclSecurityReader.Read(readerModule, data, gpContext);

            Interlocked.CompareExchange(ref securityAttributes, tmp, null);
        }
Beispiel #8
0
        public unsafe uint[] GetReturnValueLiveOffset(uint ilOffset)
        {
            var c3 = obj as ICorDebugCode3;

            if (c3 == null)
            {
                return(ArrayAddIn.Empty <uint>());
            }
            int hr = c3.GetReturnValueLiveOffset(ilOffset, 0, out uint totalSize, null);
            // E_UNEXPECTED if it returns void
            const int E_UNEXPECTED = unchecked ((int)0x8000FFFF);
            // E_FAIL if nothing is found
            const int E_FAIL = unchecked ((int)0x80004005);

            Debug.Assert(hr == 0 || hr == CordbgErrors.CORDBG_E_INVALID_OPCODE || hr == CordbgErrors.CORDBG_E_UNSUPPORTED || hr == E_UNEXPECTED || hr == E_FAIL);
            if (hr < 0)
            {
                return(ArrayAddIn.Empty <uint>());
            }
            if (totalSize == 0)
            {
                return(ArrayAddIn.Empty <uint>());
            }
            var res = new uint[totalSize];

            hr = c3.GetReturnValueLiveOffset(ilOffset, (uint)res.Length, out uint fetched, res);
            if (hr < 0)
            {
                return(ArrayAddIn.Empty <uint>());
            }
            if (fetched != (uint)res.Length)
            {
                Array.Resize(ref res, (int)fetched);
            }
            return(res);
        }
        public override byte[] GetBlob()
        {
            if (blob != null)
            {
                return(blob);
            }
            var  mdi   = readerModule.MetaDataImport;
            uint token = OriginalToken.Raw;

            Interlocked.CompareExchange(ref blob, MDAPI.GetPermissionSetBlob(mdi, token) ?? ArrayAddIn.Empty <byte>(), null);
            return(blob);
        }