示例#1
0
        public static bool LoadFileToMemory(string fileName, DkmProcess process, long fromAddress, long lengthToWrite)
        {
            bool bRes = false;

            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                int    bufferSize = 4096;
                byte[] buffer     = new byte[bufferSize];
                int    read;
                long   i = 0;
                for (i = 0; (i < lengthToWrite); i += read)
                {
                    read = fs.Read(buffer, 0, (int)Math.Min(lengthToWrite - i, buffer.Length));

                    if (read != bufferSize)
                    {
                        byte[] tmp = new byte[read];
                        Buffer.BlockCopy(buffer, 0, tmp, 0, read);

                        process.WriteMemory((ulong)(fromAddress + i), tmp);
                    }
                    else
                    {
                        process.WriteMemory((ulong)(fromAddress + i), buffer);
                    }
                }

                if (i == lengthToWrite)
                {
                    bRes = true;
                }
            }

            return(bRes);
        }
        public static PyUnicodeObject33 Create(DkmProcess process, string value)
        {
            var allocator = process.GetDataItem <PyObjectAllocator>();

            Debug.Assert(allocator != null);

            var result = allocator.Allocate <PyUnicodeObject33>(value.Length * sizeof(char));

            result._asciiObject.hash.Write(-1);
            result._asciiObject.length.Write(value.Length);
            result._compactObject.wstr_length.Write(value.Length);

            var state = new State {
                interned = Interned.SSTATE_NOT_INTERNED,
                kind     = PyUnicode_Kind.PyUnicode_2BYTE_KIND,
                compact  = true,
                ascii    = false,
                ready    = true
            };

            result._asciiObject.state.Write((byte)state);

            ulong dataPtr = result.Address.OffsetBy(StructProxy.SizeOf <PyCompactUnicodeObject>(process));

            result._asciiObject.wstr.Write(dataPtr);
            process.WriteMemory(dataPtr, Encoding.Unicode.GetBytes(value));

            return(result);
        }
示例#3
0
        public static PyBytesObject Create(DkmProcess process, AsciiString value)
        {
            var allocator = process.GetDataItem <PyObjectAllocator>();

            Debug.Assert(allocator != null);

            var result = allocator.Allocate <PyBytesObject>(value.Bytes.Length);

            result.ob_size.Write(value.Bytes.Length);
            process.WriteMemory(result.ob_sval.Address, value.Bytes);

            return(result);
        }
示例#4
0
        internal static bool TryWriteDoubleVariable(DkmProcess process, ulong address, double value)
        {
            try
            {
                process.WriteMemory(address, BitConverter.GetBytes(value));
            }
            catch (DkmException)
            {
                return(false);
            }

            return(true);
        }
示例#5
0
        internal static bool TryWriteRawBytes(DkmProcess process, ulong address, byte[] value)
        {
            try
            {
                process.WriteMemory(address, value);
            }
            catch (DkmException)
            {
                return(false);
            }

            return(true);
        }
示例#6
0
        internal static bool TryWriteByteVariable(DkmProcess process, ulong address, byte value)
        {
            try
            {
                process.WriteMemory(address, new byte[1] {
                    value
                });
            }
            catch (DkmException)
            {
                return(false);
            }

            return(true);
        }
示例#7
0
        public unsafe void QueueForDecRef(PyObject obj)
        {
            byte[] buf = new byte[sizeof(ObjectToRelease)];
            fixed(byte *p = buf)
            {
                var otr = (ObjectToRelease *)p;

                otr->pyObject = obj.Address;
                otr->next     = _objectsToRelease.Read();
            }

            ulong otrPtr = _process.AllocateVirtualMemory(0, sizeof(ObjectToRelease), NativeMethods.MEM_COMMIT | NativeMethods.MEM_RESERVE, NativeMethods.PAGE_READWRITE);

            _process.WriteMemory(otrPtr, buf);
            _objectsToRelease.Write(otrPtr);
        }
        public static PyUnicodeObject27 Create(DkmProcess process, string value)
        {
            // Allocate string buffer together with the object itself in a single block.
            var allocator = process.GetDataItem <PyObjectAllocator>();

            Debug.Assert(allocator != null);

            var result = allocator.Allocate <PyUnicodeObject27>(value.Length * 2);

            result.length.Write(value.Length);

            var str = result.Address.OffsetBy(StructProxy.SizeOf <PyUnicodeObject27>(process));

            result.str.Raw.Write(str);

            var buf = Encoding.Unicode.GetBytes(value);

            process.WriteMemory(str, buf);

            return(result);
        }
示例#9
0
        private void WriteBreakpoints()
        {
            int maxLineNumber     = _breakpoints.Keys.Select(loc => loc.LineNumber).DefaultIfEmpty().Max();
            var lineNumbersStream = new MemoryStream((maxLineNumber + 1) * sizeof(int));
            var lineNumbersWriter = new BinaryWriter(lineNumbersStream);

            var stringsStream = new MemoryStream();
            var stringsWriter = new BinaryWriter(stringsStream);
            var stringOffsets = new Dictionary <string, int>();

            stringsWriter.Write((int)0);
            foreach (var s in _breakpoints.Keys.Select(loc => loc.FileName).Distinct())
            {
                stringOffsets[s] = (int)stringsStream.Position;
                stringsWriter.Write((int)s.Length);
                foreach (char c in s)
                {
                    stringsWriter.Write((ushort)c);
                }
                stringsWriter.Write((ushort)0);
            }

            var fileNamesOffsetsStream  = new MemoryStream();
            var fileNamesOffsetsWriter  = new BinaryWriter(fileNamesOffsetsStream);
            var fileNamesOffsetsIndices = new Dictionary <int[], int>(new StructuralArrayEqualityComparer <int>());

            fileNamesOffsetsWriter.Write((int)0);
            foreach (var g in _breakpoints.Keys.GroupBy(loc => loc.LineNumber))
            {
                var lineNumber = g.Key;

                var fileNamesOffsets = g.Select(loc => stringOffsets[loc.FileName]).ToArray();
                Array.Sort(fileNamesOffsets);

                if (!fileNamesOffsetsIndices.TryGetValue(fileNamesOffsets, out global::System.Int32 fileNamesOffsetsIndex))
                {
                    fileNamesOffsetsIndex = (int)fileNamesOffsetsStream.Position / sizeof(int);
                    foreach (int offset in fileNamesOffsets)
                    {
                        fileNamesOffsetsWriter.Write(offset);
                    }
                    fileNamesOffsetsWriter.Write((int)0);
                    fileNamesOffsetsIndices.Add(fileNamesOffsets, fileNamesOffsetsIndex);
                }

                lineNumbersStream.Position = lineNumber * sizeof(int);
                lineNumbersWriter.Write(fileNamesOffsetsIndex);
            }

            byte breakpointDataInUseByTraceFunc = _breakpointDataInUseByTraceFunc.Read();
            byte currentBreakpointData          = (breakpointDataInUseByTraceFunc == 0) ? (byte)1 : (byte)0;

            _currentBreakpointData.Write(currentBreakpointData);

            CliStructProxy <BreakpointData> bpDataProxy = _breakpointData[currentBreakpointData];
            BreakpointData bpData = bpDataProxy.Read();

            if (bpData.lineNumbers != 0)
            {
                _process.FreeVirtualMemory(bpData.lineNumbers, 0, NativeMethods.MEM_RELEASE);
            }
            if (bpData.fileNamesOffsets != 0)
            {
                _process.FreeVirtualMemory(bpData.fileNamesOffsets, 0, NativeMethods.MEM_RELEASE);
            }
            if (bpData.strings != 0)
            {
                _process.FreeVirtualMemory(bpData.strings, 0, NativeMethods.MEM_RELEASE);
            }

            bpData.maxLineNumber = maxLineNumber;
            if (lineNumbersStream.Length > 0)
            {
                bpData.lineNumbers = _process.AllocateVirtualMemory(0, (int)lineNumbersStream.Length, NativeMethods.MEM_COMMIT | NativeMethods.MEM_RESERVE, NativeMethods.PAGE_READWRITE);
                _process.WriteMemory(bpData.lineNumbers, lineNumbersStream.ToArray());
            }
            else
            {
                bpData.lineNumbers = 0;
            }

            bpData.fileNamesOffsets = _process.AllocateVirtualMemory(0, (int)fileNamesOffsetsStream.Length, NativeMethods.MEM_COMMIT | NativeMethods.MEM_RESERVE, NativeMethods.PAGE_READWRITE);
            _process.WriteMemory(bpData.fileNamesOffsets, fileNamesOffsetsStream.ToArray());

            bpData.strings = _process.AllocateVirtualMemory(0, (int)stringsStream.Length, NativeMethods.MEM_COMMIT | NativeMethods.MEM_RESERVE, NativeMethods.PAGE_READWRITE);
            _process.WriteMemory(bpData.strings, stringsStream.ToArray());

            bpDataProxy.Write(bpData);
        }