Esempio n. 1
0
            internal static int ExecuteCommandBlocking(
                string command, string parameters, string workingDir, string pathExtension,
                IDebuggerAttacher debuggerAttacher, Action <string> reportOutputLine, Action <int> reportProcessId)
            {
                using (var pipeStream = new ProcessOutputPipeStream())
                {
                    var processInfo = CreateProcess(command, parameters, workingDir, pathExtension, pipeStream._writingEnd);
                    reportProcessId(processInfo.dwProcessId);
                    using (var process = new SafeWaitHandle(processInfo.hProcess, true))
                        using (var thread = new SafeWaitHandle(processInfo.hThread, true))
                        {
                            pipeStream.ConnectedToChildProcess();

                            debuggerAttacher?.AttachDebugger(processInfo.dwProcessId);

                            ResumeThread(thread);

                            using (var reader = new StreamReader(pipeStream, Encoding.Default))
                                while (!reader.EndOfStream)
                                {
                                    reportOutputLine(reader.ReadLine());
                                }

                            WaitForSingleObject(process, INFINITE);

                            int exitCode;
                            if (!GetExitCodeProcess(process, out exitCode))
                            {
                                throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not get exit code of process");
                            }

                            return(exitCode);
                        }
                }
            }
 private void OnAttachDebuggerMessageReceived(NamedPipeConnection <AttachDebuggerMessage, AttachDebuggerMessage> connection,
                                              AttachDebuggerMessage message)
 {
     try
     {
         message.DebuggerAttachedSuccessfully = _debuggerAttacher.AttachDebugger(message.ProcessId);
         if (!message.DebuggerAttachedSuccessfully)
         {
             message.ErrorMessage = $"Could not attach debugger to process {message.ProcessId} for unknown reasons";
         }
     }
     catch (Exception e)
     {
         message.DebuggerAttachedSuccessfully = false;
         message.ErrorMessage = $"Could not attach debugger to process {message.ProcessId} because of exception on server side:{Environment.NewLine}{e}";
     }
     finally
     {
         try
         {
             _server.PushMessage(message);
         }
         catch (Exception e)
         {
             _logger.LogError($"Exception on server side while sending debugging response message:{Environment.NewLine}{e}");
         }
     }
 }
Esempio n. 3
0
        public void AttachDebugger(int processId, DebuggerEngine debuggerEngine)
        {
            bool success = false;

            try
            {
                success = _debuggerAttacher.AttachDebugger(processId, debuggerEngine);
            }
            catch (Exception e)
            {
                ThrowFaultException($"Could not attach debugger to process {processId} because of exception on the server side:{Environment.NewLine}{e}");
            }
            if (!success)
            {
                ThrowFaultException($"Could not attach debugger to process {processId} for unknown reasons");
            }
        }
Esempio n. 4
0
 public static bool AttachDebugger(this IDebuggerAttacher attacher, int processIdToAttachTo)
 {
     return(attacher.AttachDebugger(Process.GetProcessById(processIdToAttachTo)));
 }
Esempio n. 5
0
            internal static int ExecuteCommandBlocking(
                string command, string parameters, string workingDir, string pathExtension,
                IDebuggerAttacher debuggerAttacher, DebuggerEngine debuggerEngine,
                ILogger logger, bool printTestOutput, Action <string> reportOutputLine, Action <int> reportProcessId)
            {
                ProcessOutputPipeStream pipeStream = null;

                try
                {
                    pipeStream = new ProcessOutputPipeStream();

                    var processInfo = CreateProcess(command, parameters, workingDir, pathExtension, pipeStream._writingEnd);
                    reportProcessId(processInfo.dwProcessId);
                    using (var process = new SafeWaitHandle(processInfo.hProcess, true))
                        using (var thread = new SafeWaitHandle(processInfo.hThread, true))
                        {
                            pipeStream.ConnectedToChildProcess();

                            logger.DebugInfo($"Attaching debugger to '{command}' via {debuggerEngine} engine");
                            if (!debuggerAttacher.AttachDebugger(processInfo.dwProcessId, debuggerEngine))
                            {
                                logger.LogError($"Could not attach debugger to process {processInfo.dwProcessId}");
                            }

                            if (printTestOutput)
                            {
                                DotNetProcessExecutor.LogStartOfOutput(logger, command, parameters);
                            }

                            ResumeThread(thread);

                            using (var reader = new StreamReader(pipeStream, Encoding.Default))
                            {
                                pipeStream = null;

                                while (!reader.EndOfStream)
                                {
                                    string line = reader.ReadLine();
                                    reportOutputLine(line);
                                    if (printTestOutput)
                                    {
                                        logger.LogInfo(line);
                                    }
                                }
                            }

                            WaitForSingleObject(process, INFINITE);

                            if (printTestOutput)
                            {
                                DotNetProcessExecutor.LogEndOfOutput(logger);
                            }

                            int exitCode;
                            if (!GetExitCodeProcess(process, out exitCode))
                            {
                                throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not get exit code of process");
                            }

                            return(exitCode);
                        }
                }
                finally
                {
                    pipeStream?.Dispose();
                }
            }