Пример #1
0
        private IDebugProperty2 GetVsDebugProperty(string expression)
        {
            StringBuilder sb = new StringBuilder();

            IDebugExpressionContext2 debugContext;

            _stackFrame.GetExpressionContext(out debugContext).ThrowOnFailure();
            IDebugExpression2 debugExpression;
            string            errorString;
            uint pichError;


            debugContext.ParseText(expression,
                                   enum_PARSEFLAGS.PARSE_EXPRESSION | enum_PARSEFLAGS.PARSE_DESIGN_TIME_EXPR_EVAL,
                                   10,
                                   out debugExpression,
                                   out errorString,
                                   out pichError).ThrowOnFailure();


            IDebugProperty2 debugProperty;

            debugExpression.EvaluateSync(
                enum_EVALFLAGS.EVAL_NOSIDEEFFECTS |
                enum_EVALFLAGS.EVAL_ALLOW_IMPLICIT_VARS |
                enum_EVALFLAGS.EVAL_ALLOWERRORREPORT,
                Configuration.DefaultTimeoutForVSCalls,
                null,
                out debugProperty).ThrowOnFailure();
            return(debugProperty);
        }
        //TODO
        public object RetrieveObject(string addressExpressionString)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            StackFrame2 currentFrame2 = this.m_Dte2.Debugger.CurrentStackFrame as StackFrame2;

            if (currentFrame2 == null)
            {
                return(null);
            }

            // Depth property is 1-based.
            uint currentFrameDepth = currentFrame2.Depth - 1;

            // Get frame info enum interface.
            IDebugThread2        currentThread2 = this.DebugThread;
            IEnumDebugFrameInfo2 enumDebugFrameInfo2;

            if (VSConstants.S_OK != currentThread2.EnumFrameInfo((uint)enum_FRAMEINFO_FLAGS.FIF_FRAME, 0, out enumDebugFrameInfo2))
            {
                return(null);
            }

            // Skip frames above the current one.
            enumDebugFrameInfo2.Reset();
            if (VSConstants.S_OK != enumDebugFrameInfo2.Skip(currentFrameDepth))
            {
                return(null);
            }

            // Get the current frame.
            FRAMEINFO[] frameInfo = new FRAMEINFO[1];
            uint        fetched   = 0;
            int         hr        = enumDebugFrameInfo2.Next(1, frameInfo, ref fetched);

            if (hr != VSConstants.S_OK || fetched != 1)
            {
                return(null);
            }

            IDebugStackFrame2 stackFrame = frameInfo[0].m_pFrame;

            if (stackFrame == null)
            {
                return(null);
            }

            // Get a context for evaluating expressions.
            IDebugExpressionContext2 expressionContext;

            if (VSConstants.S_OK != stackFrame.GetExpressionContext(out expressionContext))
            {
                return(null);
            }

            // Parse the expression string.
            IDebugExpression2 expression;
            string            error;
            uint errorCharIndex;

            if (VSConstants.S_OK != expressionContext.ParseText($"{addressExpressionString}.Ptr", (uint)enum_PARSEFLAGS.PARSE_EXPRESSION, 10, out expression, out error, out errorCharIndex))
            {
                return(null);
            }

            // Evaluate the parsed expression.
            IDebugProperty2 debugProperty = null;
            IDebugProperty3 upgrade       = debugProperty as IDebugProperty3;

            if (VSConstants.S_OK != expression.EvaluateSync((uint)enum_EVALFLAGS.EVAL_NOSIDEEFFECTS, unchecked ((uint)Timeout.Infinite), null, out debugProperty))
            {
                return(null);
            }

            DEBUG_PROPERTY_INFO[] test2 = new DEBUG_PROPERTY_INFO[64];
            debugProperty.GetPropertyInfo((uint)(enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ALL), 10, unchecked ((uint)Timeout.Infinite), null, 0, test2);
            uint bmpSize = 0;
            var  re      = debugProperty.GetSize(out bmpSize);

            // Get memory context for the property.
            IDebugReference2 reference;

            debugProperty.GetReference(out reference);

            IDebugMemoryBytes2 bytes2;

            debugProperty.GetMemoryBytes(out bytes2);

            IDebugMemoryContext2 memoryContext;

            if (VSConstants.S_OK != debugProperty.GetMemoryContext(out memoryContext))
            {
                // In practice, this is where it seems to fail if you enter an invalid expression.
                return(null);
            }

            CONTEXT_INFO[] bfnl = new CONTEXT_INFO[64];
            memoryContext.GetInfo((uint)enum_CONTEXT_INFO_FIELDS.CIF_ALLFIELDS, bfnl);

            // Get memory bytes interface.
            IDebugMemoryBytes2 memoryBytes;

            if (VSConstants.S_OK != debugProperty.GetMemoryBytes(out memoryBytes))
            {
                return(null);
            }

            string memoryAddress;

            memoryContext.GetName(out memoryAddress);

            int intValue = Convert.ToInt32(memoryAddress, 16);

            var process = GetDebuggedProcess(this.m_Dte2.Debugger);
            var thread  = process.GetThreads().FirstOrDefault(t => t.SystemPart.Id == this.m_Dte2.Debugger.CurrentThread.ID);
            //var stackRange = thread.GetStackAddressRange();
            var            stack = thread.GetTopStackFrame();
            ICorDebugValue value = null;

            //var getproperty = stack.GetProperty(value, addressExpressionString);

            //byte[] processRam = new byte[1000000];
            //process.ReadMemory((ulong)intValue, DkmReadMemoryFlags.None, processRam);
            //string test23 = System.Text.Encoding.ASCII.GetString(processRam, 0, processRam.Length);

            //byte[] processRam2 = new byte[stack.FrameSize];
            //process.ReadMemory((ulong)stack.FrameBase, DkmReadMemoryFlags.None, processRam2);
            //string test13 = System.Text.Encoding.ASCII.GetString(processRam2, 0, processRam2.Length);

            try
            {
                Emgu.CV.Image <Gray, byte> img431 = new Emgu.CV.Image <Gray, byte>(800, 600, 800, new IntPtr(intValue));



                int      width           = 800;
                int      height          = 600;
                int      pixelFormatSize = Image.GetPixelFormatSize(System.Drawing.Imaging.PixelFormat.Format32bppArgb) / 8;
                int      stride          = width * pixelFormatSize;
                byte[]   bits            = new byte[stride * height];
                GCHandle handle          = GCHandle.Alloc(bits, GCHandleType.Pinned);
                IntPtr   pointer         = Marshal.UnsafeAddrOfPinnedArrayElement(bits, 0);
                Bitmap   bitmap          = new Bitmap(width, height, stride, System.Drawing.Imaging.PixelFormat.Format32bppArgb, pointer);

                //Bitmap bmp = new Bitmap(800, 600);
                //IntPtr parameter = new IntPtr(intValue);
                //GCHandle handle = GCHandle.Alloc(bmp, GCHandleType.Normal);
                //Marshal.Copy(processRam, 0, pointer, processRam.Length);
                handle.Free();


                object o = handle.Target;
            }
            catch (Exception ex)
            {
            }
            //try
            //{
            //    IntPtr ptr = new IntPtr(intValue);
            //    GCHandle imageHandle = GCHandle.FromIntPtr(ptr);
            //    object obj = imageHandle.Target;
            //}
            //catch(Exception ex)
            //{

            //}

            // The number of bytes to read.
            ulong dataSize = 0;
            var   res      = memoryBytes.GetSize(out dataSize);

            unsafe
            {
                dataSize = 1024 * 1024;
            }
            //if (VSConstants.S_OK != res)
            //{
            //    return null;
            //}

            //// Allocate space for the result.
            byte[] data         = new byte[dataSize];
            uint   writtenBytes = 0;

            int size = 0;

            data = this.ReadMemory(memoryContext, memoryBytes, (int)dataSize, 1024, out size);
            Emgu.CV.Image <Gray, byte> img = new Emgu.CV.Image <Gray, byte>(800, 600);
            Marshal.Copy(data, 0, img.Ptr, size);

            // Read data from the debuggee.
            uint unreadable = 0;

            hr = memoryBytes.ReadAt(memoryContext, (uint)dataSize, data, out writtenBytes, ref unreadable);

            if (hr != VSConstants.S_OK)
            {
                // Read failed.

                Marshal.Copy(new IntPtr(intValue), data, 0, data.Length);
            }
            else //if (writtenBytes < dataSize)
            {
                // Read partially succeeded.

                try
                {
                    //Marshal.Copy(new IntPtr(intValue), data, 0, (int)writtenBytes);

                    BinaryFormatter ser = new BinaryFormatter();
                    using (MemoryStream stream = new MemoryStream(data, 0, (int)writtenBytes))
                    {
                        MemoryStream outStream = new MemoryStream();
                        ser.Serialize(outStream, new Bitmap(800, 600));
                        byte[] serializedImage = outStream.GetBuffer();
                        string test            = System.Text.Encoding.UTF8.GetString(serializedImage);
                        string test3           = System.Text.Encoding.ASCII.GetString(serializedImage);

                        //var img = Image.FromStream(stream);
                        object obj = ser.Deserialize(stream);
                    }
                }
                catch (Exception ex)
                {
                    string test  = System.Text.Encoding.UTF8.GetString(data, 0, (int)writtenBytes);
                    string test3 = System.Text.Encoding.ASCII.GetString(data, 0, (int)writtenBytes);
                }

                //IntPtr parameter = new IntPtr(intValue);
                //GCHandle handle = (GCHandle)parameter;

                //object o = handle.Target;
            }
            //else
            //{
            //    // Read successful.
            //}

            return(null);
        }
Пример #3
0
        private int InterpolateVariable(string variable, IDebugStackFrame2 topFrame, uint radix, out string interpolatedVariableStr)
        {
            int hr = HRConstants.S_OK;

            IDebugExpressionContext2 expressionContext;

            hr = topFrame.GetExpressionContext(out expressionContext);
            if (hr < 0)
            {
                interpolatedVariableStr = AD7Resources.Error_InterpolateVariableMissingContext;
                return(hr);
            }

            IDebugExpression2 expressionObject;

            hr = expressionContext.ParseText(variable, enum_PARSEFLAGS.PARSE_EXPRESSION, radix, out expressionObject, out string errStr, out uint errIdx);
            if (hr < 0)
            {
                interpolatedVariableStr = string.Format(CultureInfo.InvariantCulture, "{0} at index {1}", errStr, errIdx);
                return(hr);
            }
            if (expressionObject == null)
            {
                interpolatedVariableStr = AD7Resources.Error_InterpolateVariableMissingExpressionObject;
                return(HRConstants.E_FAIL);
            }

            IDebugProperty2 property;
            enum_EVALFLAGS  flags = enum_EVALFLAGS.EVAL_RETURNVALUE |
                                    enum_EVALFLAGS.EVAL_NOEVENTS |
                                    (enum_EVALFLAGS)enum_EVALFLAGS110.EVAL110_FORCE_REAL_FUNCEVAL |
                                    enum_EVALFLAGS.EVAL_NOSIDEEFFECTS;

            hr = expressionObject.EvaluateSync(flags, Constants.EvaluationTimeout, null, out property);
            if (hr < 0 || property == null)
            {
                interpolatedVariableStr = AD7Resources.Error_InterpolateVariableEvaluateFailed;
                return(hr);
            }

            DEBUG_PROPERTY_INFO[]     propertyInfo      = new DEBUG_PROPERTY_INFO[1];
            enum_DEBUGPROP_INFO_FLAGS propertyInfoFlags = enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_NAME |
                                                          enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE |
                                                          enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_TYPE |
                                                          enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_ATTRIB |
                                                          enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_PROP |
                                                          enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_FULLNAME |
                                                          (enum_DEBUGPROP_INFO_FLAGS)enum_DEBUGPROP_INFO_FLAGS110.DEBUGPROP110_INFO_FORCE_REAL_FUNCEVAL |
                                                          (enum_DEBUGPROP_INFO_FLAGS)enum_DEBUGPROP_INFO_FLAGS110.DEBUGPROP110_INFO_NOSIDEEFFECTS;

            hr = property.GetPropertyInfo(propertyInfoFlags, Constants.EvaluationRadix, Constants.EvaluationTimeout, null, 0, propertyInfo);
            if (hr < 0)
            {
                interpolatedVariableStr = AD7Resources.Error_InterpolateVariableMissingProperties;
                return(hr);
            }

            if ((propertyInfo[0].dwAttrib & enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_ERROR) == enum_DBG_ATTRIB_FLAGS.DBG_ATTRIB_VALUE_ERROR)
            {
                // bstrValue has useful information.
                interpolatedVariableStr = propertyInfo[0].bstrValue;
                return(HRConstants.E_FAIL);
            }

            interpolatedVariableStr = propertyInfo[0].bstrValue;

            return(hr);
        }
Пример #4
0
        /// <summary>
        /// Reads from or writes to the debug target at the evaluated expression.
        /// </summary>
        /// <param name="program">VS internal program</param>
        /// <param name="frame">VS internal frame</param>
        /// <param name="buffer">Buffer to read/write</param>
        /// <param name="length">Length to read/write</param>
        /// <param name="read">True to perform read, false to perform write</param>
        /// <param name="expr">VS debugger expression. Will be casted to void*.</param>
        /// <returns>True on success, false on failure</returns>
        private bool MemoryAction(IDebugProgram2 program, IDebugStackFrame2 frame, ref byte[] buffer, uint length, bool read, string expr)
        {
            if (!IsDebugging)
            {
                ShowError(Resources.NotInDebugContextErrorText);
                return(false);
            }

            // Get the execution context (current frame) to evaluate the context
            IDebugExpressionContext2 expressionContext;

            if (frame.GetExpressionContext(out expressionContext) != VSConstants.S_OK || expressionContext == null)
            {
                ShowError(Resources.CannotGetExprContextErrorText);
                return(false);
            }

            // Try to parse the expression
            IDebugExpression2 expression;
            string            error;
            uint errorCharIndex;

            if (expressionContext.ParseText(string.Format("(void *)({0})", expr), enum_PARSEFLAGS.PARSE_EXPRESSION, 10, out expression, out error, out errorCharIndex) != VSConstants.S_OK || errorCharIndex > 0)
            {
                ShowError(string.Format(Resources.ParseExpressionErrorText, expr, error));
                return(false);
            }

            // Try to evaluate the expression
            IDebugProperty2 debugProperty;

            if (expression.EvaluateSync(enum_EVALFLAGS.EVAL_NOSIDEEFFECTS, uint.MaxValue, null, out debugProperty) != VSConstants.S_OK || debugProperty == null)
            {
                ShowError(Resources.ExpressionEvalErrorText);
                return(false);
            }

            // Get memory context of our evaluated expression
            IDebugMemoryContext2 memoryContext;

            if (debugProperty.GetMemoryContext(out memoryContext) != VSConstants.S_OK || memoryContext == null)
            {
                ShowError(Resources.CannotGetFrameErrorText);
                return(false);
            }

            // Get memory accessor
            // For some reason debugProperty.GetMemoryBytes does not work so we have to do it this way
            IDebugMemoryBytes2 memoryBytes;

            if (program.GetMemoryBytes(out memoryBytes) != VSConstants.S_OK || memoryBytes == null)
            {
                ShowError(Resources.CannotGetMemoryErrorText);
                return(false);
            }

            // Using the memory accessor and context, read or write to debugee memory
            if (read)
            {
                uint numread    = 0;
                uint unreadable = 0;
                memoryBytes.ReadAt(memoryContext, length, buffer, out numread, ref unreadable);
                if (numread != length || unreadable > 0)
                {
                    ShowError(string.Format(Resources.ChunkUnreadableErrorText, unreadable, length));
                    return(false);
                }
            }
            else
            {
                memoryBytes.WriteAt(memoryContext, length, buffer);
            }
            return(true);
        }