コード例 #1
0
ファイル: NativeImports.cs プロジェクト: xsword911/Cosmos
        //
        /// <summary>
        /// Read the log message from the target.
        /// </summary>
        /// <param name="reader">interface to access debuggee memory</param>
        /// <returns>string containing message or null if not available</returns>
        public string ReadMessageFromTarget(IMemoryReader reader)
        {
            try
            {
                bool isUnicode = (fUnicode != 0);

                int    cbCharSize = (isUnicode) ? 2 : 1;
                byte[] buffer     = new byte[nDebugStringLength * cbCharSize];
                reader.ReadMemory(lpDebugStringData, buffer);

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                for (int i = 0; i < buffer.Length; i += cbCharSize)
                {
                    int val;
                    if (isUnicode)
                    {
                        val = (int)buffer[i] + ((int)buffer[i + 1] << 8);
                    }
                    else
                    {
                        val = buffer[i];
                    }
                    sb.Append((char)val);
                }
                return(sb.ToString());
            }
            catch (InvalidOperationException)
            {
                return(null);
            }
        }
コード例 #2
0
ファイル: NativeImports.cs プロジェクト: xsword911/Cosmos
        // Helper to read an IntPtr from the target
        IntPtr ReadIntPtrFromTarget(IMemoryReader reader, IntPtr ptr)
        {
            // This is not cross-platform: it assumes host and target are the same size.
            byte[] buffer = new byte[IntPtr.Size];
            reader.ReadMemory(ptr, buffer);

            System.UInt64 val = 0;
            // Note: this is dependent on endienness.
            for (int i = buffer.Length - 1; i >= 0; i--)
            {
                val <<= 8;
                val  += buffer[i];
            }
            IntPtr newptr = new IntPtr(unchecked ((long)val));

            return(newptr);
        }
コード例 #3
0
ファイル: MemoryKernelReader.cs プロジェクト: elydan/Elena
        public MemoryKernelReader(Process ff7)
        {
            this.MemoryReader = new NativeMemoryReader(ff7);

            var index = this.MemoryReader.ReadMemory(new IntPtr(KernelSegmentIndexPointer), 27 * 4);

            var addresses      = new Dictionary <KernelSection, int>();
            var sectionLengths = new[]
            {
                256, 3584, 3988, 2876, 3584, 5632, 1152, 512, 1920
            };

            var kernelData = new Dictionary <KernelSection, byte[]>();

            for (var i = 0; i < (int)KernelSection.MateriaData; ++i)
            {
                var loc = BitConverter.ToInt32(index, i * 4);
                addresses.Add((KernelSection)i + 1, loc);
                var data = MemoryReader.ReadMemory(new IntPtr(loc), sectionLengths[i]);
                kernelData.Add((KernelSection)1 + i, data);
            }

            this.KernelData = kernelData;
        }
コード例 #4
0
ファイル: NativeImports.cs プロジェクト: xsword911/Cosmos
        /// <summary>
        /// Read the image name from the target.
        /// </summary>
        /// <param name="reader">access to target's memory</param>
        /// <returns>String for full path to image. Null if name not available</returns>
        /// <remarks>MSDN says this will never be provided for during Attach scenarios; nor for the first 1 or 2 dlls.</remarks>
        public string ReadImageNameFromTarget(IMemoryReader reader)
        {
            string moduleName;
            bool   bUnicode = (fUnicode != 0);

            if (lpImageName == IntPtr.Zero)
            {
                return(null);
            }
            else
            {
                try
                {
                    IntPtr newptr = ReadIntPtrFromTarget(reader, lpImageName);

                    if (newptr == IntPtr.Zero)
                    {
                        return(null);
                    }
                    else
                    {
                        int    charSize = (bUnicode) ? 2 : 1;
                        byte[] buffer   = new byte[charSize];

                        System.Text.StringBuilder sb = new System.Text.StringBuilder();

                        while (true)
                        {
                            // Read 1 character at a time. This is extremely inefficient,
                            // but we don't know the whole length of the string and it ensures we don't
                            // read off a page.
                            reader.ReadMemory(newptr, buffer);

                            int b;
                            if (bUnicode)
                            {
                                b = (int)buffer[0] + ((int)buffer[1] << 8);
                            }
                            else
                            {
                                b = (int)buffer[0];
                            }

                            if (b == 0) // string is null-terminated
                            {
                                break;
                            }
                            sb.Append((char)b);
                            newptr = new IntPtr(newptr.ToInt64() + charSize); // move to next character
                        }

                        moduleName = sb.ToString();
                    }
                }
                catch (System.DataMisalignedException)
                {
                    return(null);
                }
                catch (InvalidOperationException) // ignore failures to read
                {
                    return(null);
                }
            }

            return(moduleName);
        }
コード例 #5
0
        // 
        /// <summary>
        /// Read the log message from the target. 
        /// </summary>
        /// <param name="reader">interface to access debuggee memory</param>
        /// <returns>string containing message or null if not available</returns>
        public string ReadMessageFromTarget(IMemoryReader reader)
        {
            try
            {
                bool isUnicode = (fUnicode != 0);

                int cbCharSize = (isUnicode) ? 2 : 1;
                var buffer = new byte[nDebugStringLength*cbCharSize];
                reader.ReadMemory(lpDebugStringData, buffer);

                var sb = new StringBuilder();
                for (int i = 0; i < buffer.Length; i += cbCharSize)
                {
                    int val;
                    if (isUnicode)
                    {
                        val = buffer[i] + (buffer[i + 1] << 8);
                    }
                    else
                    {
                        val = buffer[i];
                    }
                    sb.Append((char) val);
                }
                return sb.ToString();
            }
            catch (InvalidOperationException)
            {
                return null;
            }
        }
コード例 #6
0
        /// <summary>
        /// Read the image name from the target.
        /// </summary>
        /// <param name="reader">access to target's memory</param>
        /// <returns>String for full path to image. Null if name not available</returns>
        /// <remarks>MSDN says this will never be provided for during Attach scenarios; nor for the first 1 or 2 dlls.</remarks>
        public string ReadImageNameFromTarget(IMemoryReader reader)
        {
            string moduleName;
            bool bUnicode = (fUnicode != 0);

            if (lpImageName == IntPtr.Zero)
            {
                return null;
            }
            else
            {
                try
                {
                    IntPtr newptr = ReadIntPtrFromTarget(reader, lpImageName);

                    if (newptr == IntPtr.Zero)
                    {
                        return null;
                    }
                    else
                    {
                        int charSize = (bUnicode) ? 2 : 1;
                        var buffer = new byte[charSize];

                        var sb = new StringBuilder();

                        while (true)
                        {
                            // Read 1 character at a time. This is extremely inefficient,
                            // but we don't know the whole length of the string and it ensures we don't
                            // read off a page.
                            reader.ReadMemory(newptr, buffer);

                            int b;
                            if (bUnicode)
                            {
                                b = buffer[0] + (buffer[1] << 8);
                            }
                            else
                            {
                                b = buffer[0];
                            }

                            if (b == 0) // string is null-terminated
                            {
                                break;
                            }
                            sb.Append((char) b);
                            newptr = new IntPtr(newptr.ToInt64() + charSize); // move to next character
                        }

                        moduleName = sb.ToString();
                    }
                }
                catch (InvalidOperationException) // ignore failures to read
                {
                    return null;
                }
            }

            return moduleName;
        }
コード例 #7
0
        // Helper to read an IntPtr from the target
        private IntPtr ReadIntPtrFromTarget(IMemoryReader reader, IntPtr ptr)
        {
            // This is not cross-platform: it assumes host and target are the same size.
            var buffer = new byte[IntPtr.Size];
            reader.ReadMemory(ptr, buffer);

            UInt64 val = 0;
            // Note: this is dependent on endienness.
            for (int i = buffer.Length - 1; i >= 0; i--)
            {
                val <<= 8;
                val += buffer[i];
            }
            var newptr = new IntPtr(unchecked((long) val));

            return newptr;
        }