예제 #1
0
        /// <summary>
        /// Casts the specified variable to type that is usable by this class if it was naked pointer.
        /// </summary>
        /// <param name="variable">The variable.</param>
        internal static Variable CastIfNecessary(Variable variable)
        {
            if (variable.GetCodeType() is NakedPointerCodeType && !variable.IsNull())
            {
                // TODO: CodeType newCodeType = CodeType.Create<T>();
                CodeType newCodeType = BuiltinCodeTypes.GetCodeType <T>(variable.GetCodeType().Module);

                newCodeType = newCodeType.PointerToType;
                return(variable.CastAs(newCodeType));
            }

            return(variable);
        }
예제 #2
0
        /// <summary>
        /// Reads the precalculated array.
        /// </summary>
        private unsafe IReadOnlyList <T> ReadArray()
        {
            // Check if we have phisical constructor in delegates
            var delegates = GetDelegates();

            if (delegates != null && delegates.PhysicalConstructor != null)
            {
                var address     = variable.GetPointerAddress();
                var elementType = variable.GetCodeType().ElementType;

                if (elementType.Module.Process.DumpFileMemoryReader != null)
                {
                    var buffer = Debugger.ReadMemory(elementType.Module.Process, address, (uint)(Length * elementType.Size));

                    return(new BufferedElementCreatorReadOnlyList(delegates, elementType, buffer, address, address));
                }

                return(new ElementCreatorReadOnlyList(delegates, elementType, address));
            }

            // Check if type is basic type and we can read it directly with marshaler.
            Type type = typeof(T);

            if (type.IsPrimitive)
            {
                // Try to use binary conversion of memory blocks
                try
                {
                    CodeType builtinCodeType = BuiltinCodeTypes.GetCodeType <T>(variable.GetCodeType().Module);
                    CodeType elementType     = variable.GetCodeType().ElementType;

                    if ((builtinCodeType == elementType) ||
                        (builtinCodeType.Size == elementType.Size && elementType.IsSimple && builtinCodeType.IsDouble == elementType.IsDouble && builtinCodeType.IsFloat == elementType.IsFloat))
                    {
                        var  address    = variable.GetPointerAddress();
                        uint bytesCount = (uint)(Length * elementType.Size);
                        var  buffer     = Debugger.ReadMemory(elementType.Module.Process, address, bytesCount);

                        if (elementType.Module.Process.DumpFileMemoryReader == null)
                        {
                            if (type != typeof(byte))
                            {
                                T[] result = new T[Length];

                                Buffer.BlockCopy(buffer.Bytes, 0, result, 0, buffer.Bytes.Length);
                                return(result);
                            }

                            return((T[])(object)buffer.Bytes);
                        }
                        else
                        {
                            T[] result = new T[Length];

                            var handle = System.Runtime.InteropServices.GCHandle.Alloc(result, System.Runtime.InteropServices.GCHandleType.Pinned);
                            try
                            {
                                void *pointer = handle.AddrOfPinnedObject().ToPointer();

                                MemoryBuffer.MemCpy(pointer, buffer.BytePointer, bytesCount);
                            }
                            finally
                            {
                                handle.Free();
                            }
                            return(result);
                        }
                    }
                }
                catch
                {
                }
            }

            return(null);
        }