コード例 #1
0
ファイル: SymbolHandler.cs プロジェクト: wisdark/Lunar
        internal Symbol GetSymbol(string symbolName)
        {
            // Initialise a native symbol handler

            if (!Dbghelp.SymSetOptions(SymbolOptions.UndecorateName).HasFlag(SymbolOptions.UndecorateName))
            {
                throw new Win32Exception();
            }

            if (!Dbghelp.SymInitialize(Kernel32.GetCurrentProcess(), null, false))
            {
                throw new Win32Exception();
            }

            try
            {
                const int pseudoDllAddress = 0x1000;

                // Load the symbol file into the symbol handler

                var symbolFileSize = new FileInfo(_pdbFilePath).Length;

                var symbolTableAddress = Dbghelp.SymLoadModuleEx(Kernel32.GetCurrentProcess(), IntPtr.Zero, _pdbFilePath, null, pseudoDllAddress, (int)symbolFileSize, IntPtr.Zero, 0);

                if (symbolTableAddress == 0)
                {
                    throw new Win32Exception();
                }

                try
                {
                    // Initialise a buffer to store the symbol information

                    Span <byte> symbolInformationBytes = stackalloc byte[(Unsafe.SizeOf <SymbolInfo>() + sizeof(char) * Constants.MaxSymbolNameLength + sizeof(long) - 1) / sizeof(long)];

                    MemoryMarshal.Write(symbolInformationBytes, ref Unsafe.AsRef(new SymbolInfo(Unsafe.SizeOf <SymbolInfo>(), 0, Constants.MaxSymbolNameLength)));

                    // Retrieve the symbol information

                    if (!Dbghelp.SymFromName(Kernel32.GetCurrentProcess(), symbolName, out symbolInformationBytes[0]))
                    {
                        throw new Win32Exception();
                    }

                    var symbolInformation = MemoryMarshal.Read <SymbolInfo>(symbolInformationBytes);

                    return(new Symbol((int)(symbolInformation.Address - pseudoDllAddress)));
                }

                finally
                {
                    Dbghelp.SymUnloadModule64(Kernel32.GetCurrentProcess(), symbolTableAddress);
                }
            }

            finally
            {
                Dbghelp.SymCleanup(Kernel32.GetCurrentProcess());
            }
        }
コード例 #2
0
ファイル: PdbFile.cs プロジェクト: ohio813/Bleak
        private async Task <Dictionary <string, IntPtr> > ParseSymbols(bool isWow64)
        {
            // Initialise a symbol handler for the local process

            var localProcessHandle = Process.GetCurrentProcess().SafeHandle;

            if (!Dbghelp.SymInitialize(localProcessHandle, IntPtr.Zero, false))
            {
                throw new PInvokeException("Failed to call SymInitialize");
            }

            // Load the symbol table for the PDB

            var pdbPathBuffer = Marshal.StringToHGlobalAnsi(await DownloadPdb(isWow64));

            var symbolTableBaseAddress = Dbghelp.SymLoadModuleEx(localProcessHandle, IntPtr.Zero, pdbPathBuffer, IntPtr.Zero, _module.BaseAddress, int.MaxValue, IntPtr.Zero, 0);

            if (symbolTableBaseAddress == IntPtr.Zero)
            {
                throw new PInvokeException("Failed to call SymLoadModuleEx");
            }

            // Initialise the callback used during SymEnumSymbols

            var symbolAddresses = new List <IntPtr>();

            var symbolNames = new List <string>();

            bool Callback(IntPtr symbolInfo, int symbolSize, IntPtr userContext)
            {
                symbolAddresses.Add((IntPtr)Marshal.PtrToStructure <SymbolInfo>(symbolInfo).Address);

                symbolNames.Add(Marshal.PtrToStringAnsi(symbolInfo + Marshal.SizeOf <SymbolInfo>()));

                return(true);
            }

            var callBackPointer = Marshal.GetFunctionPointerForDelegate(new Callbacks.EnumerateSymbolsCallback(Callback));

            // Enumerate the PDB symbols

            if (!Dbghelp.SymEnumSymbols(localProcessHandle, symbolTableBaseAddress, IntPtr.Zero, callBackPointer, IntPtr.Zero))
            {
                throw new PInvokeException("Failed to call SymEnumSymbols");
            }

            Dbghelp.SymUnloadModule64(localProcessHandle, symbolTableBaseAddress);

            var symbols = new Dictionary <string, IntPtr>();

            for (var symbolIndex = 0; symbolIndex < symbolNames.Count; symbolIndex++)
            {
                symbols.TryAdd(symbolNames[symbolIndex], symbolAddresses[symbolIndex]);
            }

            return(symbols);
        }
コード例 #3
0
        private Dictionary <string, IntPtr> ParseSymbols(string pdbPath, IntPtr moduleAddress)
        {
            // Initialise a symbol handler for the local process

            var localProcessHandle = Process.GetCurrentProcess().SafeHandle;

            if (!Dbghelp.SymInitialize(localProcessHandle, IntPtr.Zero, false))
            {
                throw new PInvokeException("Failed to call SymInitialize");
            }

            // Load the symbol table for the PDB

            var pdbPathBuffer = Marshal.StringToHGlobalAnsi(pdbPath);

            var symbolTableBaseAddress = Dbghelp.SymLoadModuleEx(localProcessHandle, IntPtr.Zero, pdbPathBuffer, IntPtr.Zero, moduleAddress, int.MaxValue, IntPtr.Zero, 0);

            if (symbolTableBaseAddress == IntPtr.Zero)
            {
                throw new PInvokeException("Failed to call SymLoadModuleEx");
            }

            // Initialise the callback used during the SymEnumSymbols call

            var symbols = new Dictionary <string, IntPtr>();

            bool Callback(IntPtr symbolInfo, int symbolSize, IntPtr userContext)
            {
                symbols.TryAdd(Marshal.PtrToStringAnsi(symbolInfo + Marshal.SizeOf <SymbolInfo>()), (IntPtr)Marshal.PtrToStructure <SymbolInfo>(symbolInfo).Address);

                return(true);
            }

            var callbackDelegate = new Prototypes.EnumerateSymbolsCallback(Callback);

            var callbackPointer = Marshal.GetFunctionPointerForDelegate(callbackDelegate);

            // Enumerate the PDB symbols

            if (!Dbghelp.SymEnumSymbols(localProcessHandle, symbolTableBaseAddress, IntPtr.Zero, callbackPointer, IntPtr.Zero))
            {
                throw new PInvokeException("Failed to call SymEnumSymbols");
            }

            Dbghelp.SymUnloadModule64(localProcessHandle, symbolTableBaseAddress);

            return(symbols);
        }