Example #1
0
 public ulong TebAddress() {
     IDebugProperty2 debugProperty = this.EvaluateExpression(this.CurrentThread, "@tib");
     if (debugProperty != null) {
         using (new DisposableComReference(debugProperty)) {
             DEBUG_PROPERTY_INFO[] debugPropertyInfo = new DEBUG_PROPERTY_INFO[1];
             if (debugProperty.GetPropertyInfo((uint)enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE, 16, evaluateExpressionTimeout, null, 0, debugPropertyInfo) == S_OK) {
                 IDebugMemoryContext2 memoryContext = null;
                 if (debugProperty.GetMemoryContext(out memoryContext) == S_OK) {
                     string hexString;
                     memoryContext.GetName(out hexString);
                     hexString = hexString.Substring(2);  // Strip '0x' for conversion to ulong
                     return ulong.Parse(hexString, NumberStyles.HexNumber);
                 }
             }
         }
     }
     return 0;
 }
Example #2
0
 int IDebugProperty3.GetMemoryContext(out IDebugMemoryContext2 ppMemory)
 {
     return(IDebugProperty2.GetMemoryContext(out ppMemory));
 }
        //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);
        }
Example #4
0
        public int Event(IDebugEngine2 engine, IDebugProcess2 process, IDebugProgram2 program, IDebugThread2 thread, IDebugEvent2 debugEvent, ref Guid riidEvent, uint attributes) {
            bool savedProgram = false;
            bool savedThread = false;

            if (riidEvent == processCreateEvent) {
                AD_PROCESS_ID[] pdwProcessId = new AD_PROCESS_ID[1];
                process.GetPhysicalProcessId(pdwProcessId);
                Debug.Assert(!this.attachedProcesses.Contains(pdwProcessId[0].dwProcessId));
                this.attachedProcesses.Add(pdwProcessId[0].dwProcessId);
            } else if (riidEvent == processDestroyEvent) {
                AD_PROCESS_ID[] pdwProcessId = new AD_PROCESS_ID[1];
                process.GetPhysicalProcessId(pdwProcessId);
                Debug.Assert(this.attachedProcesses.Contains(pdwProcessId[0].dwProcessId));
                this.attachedProcesses.Remove(pdwProcessId[0].dwProcessId);
            } else if (riidEvent == breakInEvent && this.currentDebugProgram != program && program != null && thread != null) {
                // Evaluate an expression get access to the memory context and the bitness.
                IDebugProperty2 debugProperty = this.EvaluateExpression(thread, "(void**)0x0 + 1");
                if (debugProperty != null) {
                    using (new DisposableComReference(debugProperty)) {
                        DEBUG_PROPERTY_INFO[] debugPropertyInfo = new DEBUG_PROPERTY_INFO[1];
                        if (debugProperty.GetPropertyInfo((uint)enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE, 16, evaluateExpressionTimeout, null, 0, debugPropertyInfo) == S_OK) {
                            IDebugMemoryContext2 memoryContext = null;
                            IDebugMemoryBytes2 memoryBytes = null;
                            if (debugProperty.GetMemoryContext(out memoryContext) == S_OK && debugProperty.GetMemoryBytes(out memoryBytes) == S_OK) {
                                DisposableComReference.SetReference(ref this.currentDebugProgram, program);
                                DisposableComReference.SetReference(ref this.currentThread, thread);
                                DisposableComReference.SetReference(ref this.memoryContext, memoryContext);
                                DisposableComReference.SetReference(ref this.memoryBytes, memoryBytes);
                                ulong offset = ulong.Parse(debugPropertyInfo[0].bstrValue.Substring("0x".Length), System.Globalization.NumberStyles.AllowHexSpecifier);

                                // Adjust the memory context and calculate the bitness.
                                this.memoryContext.Subtract(offset, out memoryContext);
                                DisposableComReference.SetReference(ref this.memoryContext, memoryContext);
                                this.isPointer64Bit = (offset == 8);

                                this.engine.NotifyDebuggerStatusChange(DebuggerChangeEventArgs.DebuggerStatus.Detaching);
                                this.engine.DiaLoader.ClearSymbols();
                                savedProgram = true;
                                savedThread = true;
                            } else {
                                DisposableComReference.ReleaseIfNotNull(ref memoryContext);
                                DisposableComReference.ReleaseIfNotNull(ref memoryBytes);
                            }
                        }
                    }
                }
            } else if (riidEvent == stopDebugEvent) {
                // The debugger stopped.  Clear the references.
                DisposableComReference.ReleaseIfNotNull(ref this.currentDebugProgram);
                DisposableComReference.ReleaseIfNotNull(ref this.memoryContext);
                DisposableComReference.ReleaseIfNotNull(ref this.memoryBytes);
                this.engine.NotifyDebuggerStatusChange(DebuggerChangeEventArgs.DebuggerStatus.Detaching);
                this.engine.DiaLoader.ClearSymbols();
            } else if (riidEvent == breakInEvent) {
                // The debugger broke in, notify the client.
                this.engine.NotifyDebuggerStatusChange(DebuggerChangeEventArgs.DebuggerStatus.Break);
            } else if (riidEvent == threadSwitchEvent) {
                // The user switched the current thread.
                DisposableComReference.SetReference(ref this.currentThread, thread);
                savedThread = true;
                if (this.currentThread != null) {
                    uint threadId;
                    thread.GetThreadId(out threadId);
                    this.TargetThreadSystemId = threadId;
                }

                bool processChanged = false;
                if (process != null) {
                    AD_PROCESS_ID[] pdwProcessId = new AD_PROCESS_ID[1];
                    process.GetPhysicalProcessId(pdwProcessId);
                    if (this.TargetProcessSystemId != 0) {
                        if (pdwProcessId[0].dwProcessId != this.TargetProcessSystemId) {
                            this.TargetProcessSystemId = pdwProcessId[0].dwProcessId;
                            processChanged = true;
                        }
                    } else {
                        this.TargetProcessSystemId = pdwProcessId[0].dwProcessId;
                        if (this.TargetProcessSystemId != 0) {
                            processChanged = true;
                        }
                    }
                } else if (this.TargetProcessSystemId != 0) {
                    this.TargetProcessSystemId = 0;
                    processChanged = true;
                }

                if (processChanged) {
                    DisposableComReference.SetReference(ref this.currentDebugProgram, program);
                    savedProgram = true;

                    if (program != null) {
                        // Evaluate an expression get access to the memory context and the bitness.
                        IDebugProperty2 debugProperty = this.EvaluateExpression(thread, "(void**)0x0 + 1");
                        if (debugProperty != null) {
                            using (new DisposableComReference(debugProperty)) {
                                DEBUG_PROPERTY_INFO[] debugPropertyInfo = new DEBUG_PROPERTY_INFO[1];
                                if (debugProperty.GetPropertyInfo((uint)enum_DEBUGPROP_INFO_FLAGS.DEBUGPROP_INFO_VALUE, 16, evaluateExpressionTimeout, null, 0, debugPropertyInfo) == S_OK) {
                                    IDebugMemoryContext2 memoryContext = null;
                                    IDebugMemoryBytes2 memoryBytes = null;
                                    if ((debugProperty.GetMemoryContext(out memoryContext) == S_OK) && (debugProperty.GetMemoryBytes(out memoryBytes) == S_OK)) {
                                        DisposableComReference.SetReference(ref this.memoryContext, memoryContext);
                                        DisposableComReference.SetReference(ref this.memoryBytes, memoryBytes);
                                        ulong offset = ulong.Parse(debugPropertyInfo[0].bstrValue.Substring("0x".Length), System.Globalization.NumberStyles.AllowHexSpecifier);

                                        // Adjust the memory context and calculate the bitness.
                                        this.memoryContext.Subtract(offset, out memoryContext);
                                        DisposableComReference.SetReference(ref this.memoryContext, memoryContext);
                                        this.isPointer64Bit = (offset == 8);
                                    } else {
                                        DisposableComReference.ReleaseIfNotNull(ref memoryContext);
                                        DisposableComReference.ReleaseIfNotNull(ref memoryBytes);
                                    }
                                }
                            }
                        }

                        this.engine.NotifyDebuggerStatusChange(DebuggerChangeEventArgs.DebuggerStatus.ChangingProcess);
                    }
                } else {
                    this.engine.NotifyDebuggerStatusChange(DebuggerChangeEventArgs.DebuggerStatus.ChangingThread);
                }
            }

            if (!savedProgram) {
                DisposableComReference.ReleaseIfNotNull(ref program);
            }
            if (!savedThread) {
                DisposableComReference.ReleaseIfNotNull(ref thread);
            }
            DisposableComReference.ReleaseIfNotNull(ref engine);
            DisposableComReference.ReleaseIfNotNull(ref process);
            DisposableComReference.ReleaseIfNotNull(ref debugEvent);

            return S_OK;
        }
Example #5
0
 int IDebugProperty3.GetMemoryContext(out IDebugMemoryContext2 ppMemory) => IDebugProperty2.GetMemoryContext(out ppMemory);