Exemplo n.º 1
0
        public static void AttachVisualStudioToProcess(_DTE instanceSolution, string ProcessName)
        {
            Process[] prss = Process.GetProcessesByName(ProcessName);

            if (prss.Length <= 0)
            {
                MessageBox.Show("Failed to find w3wp.exe process! Make sure you opened the page first!", "Quick Attach", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DTEProcess processToAttachTo = instanceSolution.Debugger.LocalProcesses.Cast <DTEProcess>().FirstOrDefault(process => process.ProcessID == prss[0].Id);

            try
            {
                if (processToAttachTo != null)
                {
                    processToAttachTo.Attach();
                }
                else
                {
                    MessageBox.Show("Failed to attach process to solution!", "Quick Attach", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (SecurityException)
            {
                MessageBox.Show("Admin Permissions are required to attach the process", "Quick Attach", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attaches Visual Studio (2012) to the specified process.
        /// </summary>
        /// <param name="process">The process.</param>
        public static void Attach(this System.Diagnostics.Process process)
        {
            // Reference visual studio core
            DTE dte = (DTE)Marshal.GetActiveObject(@"VisualStudio.DTE.11.0");

            // Register the IOleMessageFilter to handle any threading errors.
            MessageFilter.Register();

            // Try loop - visual studio may not respond the first time.
            int tryCount = 50;

            while (tryCount-- > 0)
            {
                try {
                    EnvDTE.Processes processes  = dte.Debugger.LocalProcesses;
                    Process          DTEprocess = processes.Cast <Process>().Where(
                        proc => proc.ProcessID == process.Id).First();
                    DTEprocess.Attach();
                    break;
                }
                catch (COMException) {
                    System.Threading.Thread.Sleep(500);
                }
            }
            // and turn off the IOleMessageFilter.
            MessageFilter.Revoke();
        }
Exemplo n.º 3
0
 //DebuggerExpressionEvaluationEvents
 public void OnExpressionEvaluation(EnvDTE.Process pProcess, EnvDTE.Thread thread, EnvDTE80.dbgExpressionEvaluationState processState)
 {
     _outputWindowPane.OutputString("DebuggerExpressionEvaluationEvents, OnExpressionEvaluation\n");
     _outputWindowPane.OutputString("\tProcess: " + pProcess.Name + "\n");
     _outputWindowPane.OutputString("\tThread: " + thread.Name + "\n");
     _outputWindowPane.OutputString("\tExpression Evaluation State: " + processState.ToString() + "\n");
 }
Exemplo n.º 4
0
        public void AttachToProcess(int processId)
        {
            var r = Package.GetGlobalService(typeof(SDTE)) as DTE;

            _attachedProcess = r.Debugger.LocalProcesses.Cast <EnvDTE.Process>().FirstOrDefault(process => process.ProcessID == processId);
            _attachedProcess.Attach();
        }
Exemplo n.º 5
0
        /// <summary>
        /// The method to use to attach visual studio to a specified process.
        /// </summary>
        /// <param name="visualStudioProcess">
        /// The visual studio process to attach to.
        /// </param>
        /// <param name="applicationProcess">
        /// The application process that needs to be debugged.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// Thrown when the application process is null.
        /// </exception>
        public static void AttachVisualStudioToProcess(Process visualStudioProcess, Process applicationProcess)
        {
            _DTE visualStudioInstance;

            if (TryGetVsInstance(visualStudioProcess.Id, out visualStudioInstance))
            {
                // Find the process you want the VS instance to attach to...
                DTEProcess processToAttachTo =
                    visualStudioInstance.Debugger.LocalProcesses.Cast <DTEProcess>()
                    .FirstOrDefault(process => process.ProcessID == applicationProcess.Id);

                // Attach to the process.
                if (processToAttachTo != null)
                {
                    processToAttachTo.Attach();

                    ShowWindow((int)visualStudioProcess.MainWindowHandle, 3);
                    SetForegroundWindow(visualStudioProcess.MainWindowHandle);
                }
                else
                {
                    throw new InvalidOperationException(
                              "Visual Studio process cannot find specified application '" + applicationProcess.Id + "'");
                }
            }
        }
Exemplo n.º 6
0
        private EnvDTE.Process FindProcess()
        {
            var dte = this.GetDTE();

            EnvDTE.Process possibleZeroProcess = null;
            EnvDTE.Process betterZeroProcess   = null;
            EnvDTE.Process bestZeroProcess     = null;

            Processes processes = dte.Debugger.LocalProcesses;

            foreach (EnvDTE.Process vsProcess in dte.Debugger.LocalProcesses)
            {
                if (Path.GetFileName(vsProcess.Name) == "ZeroEditor.exe")
                {
                    possibleZeroProcess = vsProcess;

                    var process = System.Diagnostics.Process.GetProcessById(vsProcess.ProcessID);

                    // Within the solution path we should generally be able to find the project name
                    // For example: C:\Users\Trevor\Documents\ZeroProjects\Test123\Plugins\MyPlugin\MyPlugin.sln
                    // Test123 would be the project name

                    var solutionName = dte.Solution.FullName;
                    if (String.IsNullOrWhiteSpace(solutionName) == false)
                    {
                        var pluginDir  = Path.GetDirectoryName(solutionName);
                        var pluginsDir = Path.GetDirectoryName(pluginDir);
                        var projectDir = Path.GetDirectoryName(pluginsDir);

                        var projectName = this.GetZeroProjectName(projectDir);

                        if (process.MainWindowTitle == "Zero Editor - " + projectName)
                        {
                            bestZeroProcess = vsProcess;
                            break;
                        }
                        else if (process.MainWindowTitle.EndsWith(projectName))
                        {
                            betterZeroProcess = vsProcess;
                        }
                    }
                }
            }

            var attachingZeroProcess = bestZeroProcess;

            if (attachingZeroProcess == null)
            {
                attachingZeroProcess = betterZeroProcess;
            }
            if (attachingZeroProcess == null)
            {
                attachingZeroProcess = possibleZeroProcess;
            }
            return(attachingZeroProcess);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Detach from previously attached process.
 /// </summary>
 public void DetachDebugger(int processId)
 {
     EnvDTE.Process process = m_attachedProcesses[processId];
     try
     {
         process.Detach(false); // waitForBreakOnEnd = false.
     }
     finally
     {
         // If there's some problem, do not try removing next time.
         m_attachedProcesses.Remove(processId);
     }
 }
Exemplo n.º 8
0
 public int GetProcessId()
 {
     Debug.WriteLine("Getting into GetProcessId");
     EnvDTE.Process process = dte.Debugger.CurrentProcess;
     if (process == null)
     {
         return(-1);
     }
     else
     {
         Debug.WriteLine("Currently Debugging ID: " + process.ProcessID + " name: " + process.Name);
         return(process.ProcessID);
     }
 }
Exemplo n.º 9
0
 public void Detach()
 {
     if (IsAttached)
     {
         try
         {
             _attachedProcess.Detach();
         }
         catch
         {
             _attachedProcess = null;
         }
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// </summary>
        private void onAttachToDebugee(object sender, EventArgs e)
        {
            DTE dte = (DTE)GetService(typeof(DTE));

            EnvDTE.Debugger debugger = dte.Debugger;
            if (debugger.DebuggedProcesses.Count > 0)
            {
                EnvDTE.Process process = debugger.DebuggedProcesses.Item(1);
                if (AlcantareaHelper.InjectCoreDLL((uint)process.ProcessID, m_addindir))
                {
                    onUIContext((int)UIContext.AlcDebugging, 1);
                }
            }
        }
Exemplo n.º 11
0
        void DisableNoSourceWindow(Process newProcess, Program newProgram, Thread newThread,
                                   StackFrame newStackFrame)
        {
            RecordErrors(() =>
            {
                _taskContext.ThrowIfNotOnMainThread();
                if (newStackFrame != null)
                {
                    Properties debuggingProperties = GetDte().Properties["Debugging", "General"];

                    debuggingProperties.Item(_enableAddressLevelDebugging).Value = true;
                    debuggingProperties.Item(_showDisassembly).Value             = true;
                }
            });
        }
Exemplo n.º 12
0
            internal static void AttachVisualStudioToProcess(Process visualStudioProcess, _DTE visualStudioInstance, Process applicationProcess)
            {
                DTEProcess processToAttachTo = visualStudioInstance.Debugger.LocalProcesses.Cast <DTEProcess>().FirstOrDefault(process => process.ProcessID == applicationProcess.Id);

                if (processToAttachTo != null)
                {
                    processToAttachTo.Attach();

                    ShowWindow((int)visualStudioProcess.MainWindowHandle, 3);
                    SetForegroundWindow(visualStudioProcess.MainWindowHandle);
                }
                else
                {
                    throw new InvalidOperationException("Visual Studio process cannot find specified application '" + applicationProcess.Id + "'");
                }
            }
Exemplo n.º 13
0
 private bool TryAttachToProcess()
 {
     try
     {
         var r         = Package.GetGlobalService(typeof(SDTE)) as DTE;
         var processId = GetActiveServiceProcessId();
         _attachedProcess = r.Debugger.LocalProcesses.Cast <EnvDTE.Process>().FirstOrDefault(process => process.ProcessID == processId);
         _attachedProcess.Attach();
         return(true);
     }
     catch (Exception ex)
     {
         Message(string.Format("Can't attach to service process, exception message - {0}", ex.Message));
         return(false);
     }
 }
Exemplo n.º 14
0
        private void Update(Process newprocess, Program newprogram, Thread newthread, StackFrame newstackframe)
        {
            if (_drawingMode == DrawingMode.Canceled || _drawingMode == DrawingMode.Redrawing)
            {
                return;
            }

            if (newstackframe == null)
            {
                _drawingMode = DrawingMode.NotChanged;
                _form?.Hide();
                return;
            }

            _dispatcherTimer          = new DispatcherTimer();
            _dispatcherTimer.Tick    += DispatcherTimer_Tick;
            _dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 50);
            _dispatcherTimer.Start();
            _drawingMode = DrawingMode.ShouldBeRedrawn;
        }
        public static void AttachVSToProcess(Process vsp, Process applicationProcess)
        {
            var vsi = getVSInstance(vsp.Id);

            if (vsi == null)
            {
                return;
            }
            //Find the process you want the VS instance to attach to...
            DTEProcess tp = vsi.Debugger.LocalProcesses.Cast <DTEProcess>().FirstOrDefault(process => process.ProcessID == applicationProcess.Id);

            //Attach to the process.
            if (tp != null)
            {
                tp.Attach();
                ShowWindow((int)vsp.MainWindowHandle, 3);
                SetForegroundWindow(vsp.MainWindowHandle);
            }
            else
            {
                throw new InvalidOperationException("Visual Studio process cannot find specified application '" + applicationProcess.Id + "'");
            }
        }
Exemplo n.º 16
0
        public void AttachDebugger(int processId)
        {
            EnvDTE.Process process = GetDteProcess(processId);

            try
            {
                process.Attach();
            }
            catch (Exception e)
            {
                Debug.Fail("Exception: " + e.ToString());
                throw;
            }

            if (!m_attachedProcesses.ContainsKey(processId))
            {
                m_attachedProcesses.Add(processId, process);
            }
            else
            {
                Debug.Fail("AttachDebugger: was already attached to process with pid=" + processId.ToString(CultureInfo.InvariantCulture));
            }
        }
Exemplo n.º 17
0
 public void OnContextChanged(EnvDTE.Process NewProcess, Program NewProgram, Thread NewThread, EnvDTE.StackFrame NewStackFrame)
 {
     //CommandCustomWatch.Instance.MenuEnabled = false;
     System.Windows.Forms.MessageBox.Show("Events are attached.");
 }
Exemplo n.º 18
0
 //DebuggerEvents
 public void OnContextChanged(EnvDTE.Process NewProcess, EnvDTE.Program NewProgram, EnvDTE.Thread NewThread, EnvDTE.StackFrame NewStackFrame)
 {
     _outputWindowPane.OutputString("DebuggerEvents, OnContextChanged\n");
 }
Exemplo n.º 19
0
        unsafe public bool ReadProcessM(uint address, uint point_num, uint bytes_per_point, IntPtr result, ref uint point_num_read)
        {
            //detecting how many points we can read
            //detecting how many pages age commitied
            //reading them
            //truncating point_num if necessary
            point_num_read = 0;
            // here we must check if the process is available for debugging
            DTE dte = (DTE)Package.GetGlobalService(typeof(DTE));

            EnvDTE.Debugger dbg             = dte.Debugger;
            EnvDTE.Process  current_process = dbg.CurrentProcess;
            if (current_process == null)
            {
                return(false);
            }

            int    process_id = ((DTE)Package.GetGlobalService(typeof(DTE))).Debugger.CurrentProcess.ProcessID;
            IntPtr handle     = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, false, process_id);

            if (handle.ToInt32() == 0)
            {
                return(false);
            }

            uint   variable_address = address;
            uint   tmp                    = address / PAGE_SIZE; //avoiding optimisation
            uint   base_address           = tmp * PAGE_SIZE;
            IntPtr base_address_param     = new IntPtr(base_address);
            MEMORY_BASIC_INFORMATION info = new MEMORY_BASIC_INFORMATION();
            uint err = VirtualQueryEx(handle, base_address_param, ref info, sizeof(MEMORY_BASIC_INFORMATION));

            if (err == 0)
            {
                return(false);
            }
            if (info.State != MEM_COMMIT)
            {
                return(false);
            }
            //calculating number of points commited
            uint region_size             = info.RegionSize;
            uint variable_address_offset = variable_address - base_address;
            uint bytes_available         = info.RegionSize - variable_address_offset;
            uint point_num_available     = bytes_available / bytes_per_point;

            if (point_num > point_num_available)
            {
                point_num = point_num_available;
            }
            //reading available data
            uint   bytes_requested        = point_num * bytes_per_point;
            int    result_size            = 0x00;
            IntPtr result_size_param      = new IntPtr(&result_size);
            IntPtr variable_address_param = new IntPtr(variable_address);
            bool   r = ReadProcessMemory(handle, variable_address_param, result, bytes_requested, result_size_param);

            if (r == false)
            {
                return(false);
            }
            if (result_size != bytes_requested)
            {
                return(false);
            }
            point_num_read = point_num;
            CloseHandle(handle);
            return(true);
        }
Exemplo n.º 20
0
 //DebuggerProcessEvents
 public void OnProcessStateChanged(EnvDTE.Process NewProcess, EnvDTE80.dbgProcessState processState)
 {
     _outputWindowPane.OutputString("DebuggerProcessEvents, OnProcessStateChanged\n");
     _outputWindowPane.OutputString("\tNew Process: " + NewProcess.Name + "\n");
     _outputWindowPane.OutputString("\tProcess State: " + processState.ToString() + "\n");
 }