Beispiel #1
0
    private void DebuggerStart(EventWaitHandle startWaitHandle, Func<ProcessInformation> processCreator, Action<ProcessInformation> callback, Action<Exception> errorCallback) {
      try {
        _debuggerThread = Thread.CurrentThread;
        // Note: processCreator is responsible for creating the process in DEBUG mode.
        // Note: If processCreator throws an exception after creating the
        // process, we may end up in a state where we received debugging events
        // without having "_processInformation" set. We need to be able to deal
        // with that.
        _processInformation = processCreator();
        callback(_processInformation);
      }
      catch (Exception e) {
        errorCallback(e);
        return;
      }
      finally {
        startWaitHandle.Set();
      }

      _running = true;
      try {
        DebuggerLoop();
      }
      finally {
        _running = false;
      }
    }
Beispiel #2
0
    private ProcessInformation CreateProcessWithStartInfo(SimpleProcessStartupInfo simpleProcessStartupInfo, CreateProcessOptions options) {
      Logger.LogInfo("CreateProcessWithStartInfo: Entry point.");
      var stringBuilder = BuildCommandLine(simpleProcessStartupInfo.FileName, simpleProcessStartupInfo.Arguments);
      Logger.LogInfo("CreateProcessWithStartInfo: command line is {0}.", stringBuilder);

      using (var startupInfo = new STARTUPINFO()) {
        Logger.LogInfo("CreateProcessWithStartInfo: Creation flags.");
        ProcessCreationFlags processCreationFlags = 0;
        if (simpleProcessStartupInfo.CreateNoWindow) {
          processCreationFlags |= ProcessCreationFlags.CREATE_NO_WINDOW;
        }
        if ((options & CreateProcessOptions.BreakAwayFromJob) != 0) {
          processCreationFlags |= ProcessCreationFlags.CREATE_BREAKAWAY_FROM_JOB;
        }

        var workingDirectory = simpleProcessStartupInfo.WorkingDirectory;
        if (workingDirectory == string.Empty) {
          workingDirectory = Environment.CurrentDirectory;
        }
        Logger.LogInfo("CreateProcessWithStartInfo: Working directory: {0}.", workingDirectory);

        if ((options & CreateProcessOptions.AttachDebugger) != 0) {
          Logger.LogInfo("CreateProcessWithStartInfo: Setting DEBUG_PROCESS flag.");
          processCreationFlags |= ProcessCreationFlags.DEBUG_PROCESS;
          processCreationFlags |= ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS;
        }

        Logger.LogInfo("CreateProcessWithStartInfo: Calling Win32 CreateProcess.");
        var processInformation = new PROCESS_INFORMATION();
        var environmentPtr = IntPtr.Zero;
        var lastError = 0;
        var success = NativeMethods.CreateProcess(null, stringBuilder, null, null, true, processCreationFlags,
                                                 environmentPtr, workingDirectory, startupInfo, processInformation);
        Logger.LogInfo("CreateProcessWithStartInfo: CreateProcess result: Success={0}-LastError={1}.", success, Marshal.GetLastWin32Error());
        if (!success) {
          lastError = Marshal.GetLastWin32Error();
        }
        // Assign safe handles as quickly as possible to avoid leaks.
        var safeProcessHandle = new SafeProcessHandle(processInformation.hProcess);
        var safeThreadHandle = new SafeProcessHandle(processInformation.hThread);
        if (!success) {
          throw new LastWin32ErrorException(lastError, string.Format("Error creating process from file \"{0}\"", simpleProcessStartupInfo.FileName));
        }

        if (safeProcessHandle.IsInvalid || safeThreadHandle.IsInvalid) {
          Logger.LogInfo("CreateProcessWithStartInfo: Invalid process handle.");
          throw new Exception(string.Format("Error creating process from file \"{0}\" (invalid process handle)", simpleProcessStartupInfo.FileName));
        }

        Logger.LogInfo("CreateProcessWithStartInfo: Creating ProcessResult instance.");
        var processResult = new ProcessInformation {
          ProcessHandle = safeProcessHandle,
          ProcessId = processInformation.dwProcessId
        };
        safeThreadHandle.Close();

        Logger.LogInfo("CreateProcessWithStartInfo: Success!");
        return processResult;
      }
    }
Beispiel #3
0
        private ProcessInformation CreateProcessWithStartInfo(SimpleProcessStartupInfo simpleProcessStartupInfo, CreateProcessOptions options)
        {
            Logger.LogInfo("CreateProcessWithStartInfo: Entry point.");
            var stringBuilder = BuildCommandLine(simpleProcessStartupInfo.FileName, simpleProcessStartupInfo.Arguments);

            Logger.LogInfo("CreateProcessWithStartInfo: command line is {0}.", stringBuilder);

            using (var startupInfo = new STARTUPINFO()) {
                Logger.LogInfo("CreateProcessWithStartInfo: Creation flags.");
                ProcessCreationFlags processCreationFlags = 0;
                if (simpleProcessStartupInfo.CreateNoWindow)
                {
                    processCreationFlags |= ProcessCreationFlags.CREATE_NO_WINDOW;
                }
                if ((options & CreateProcessOptions.BreakAwayFromJob) != 0)
                {
                    processCreationFlags |= ProcessCreationFlags.CREATE_BREAKAWAY_FROM_JOB;
                }

                var workingDirectory = simpleProcessStartupInfo.WorkingDirectory;
                if (workingDirectory == string.Empty)
                {
                    workingDirectory = Environment.CurrentDirectory;
                }
                Logger.LogInfo("CreateProcessWithStartInfo: Working directory: {0}.", workingDirectory);

                if ((options & CreateProcessOptions.AttachDebugger) != 0)
                {
                    Logger.LogInfo("CreateProcessWithStartInfo: Setting DEBUG_PROCESS flag.");
                    processCreationFlags |= ProcessCreationFlags.DEBUG_PROCESS;
                    processCreationFlags |= ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS;
                }

                Logger.LogInfo("CreateProcessWithStartInfo: Calling Win32 CreateProcess.");
                var processInformation = new PROCESS_INFORMATION();
                var environmentPtr     = IntPtr.Zero;
                var lastError          = 0;
                var success            = NativeMethods.CreateProcess(null, stringBuilder, null, null, true, processCreationFlags,
                                                                     environmentPtr, workingDirectory, startupInfo, processInformation);
                Logger.LogInfo("CreateProcessWithStartInfo: CreateProcess result: Success={0}-LastError={1}.", success, Marshal.GetLastWin32Error());
                if (!success)
                {
                    lastError = Marshal.GetLastWin32Error();
                }
                // Assign safe handles as quickly as possible to avoid leaks.
                var safeProcessHandle = new SafeProcessHandle(processInformation.hProcess);
                var safeThreadHandle  = new SafeProcessHandle(processInformation.hThread);
                if (!success)
                {
                    throw new LastWin32ErrorException(lastError, string.Format("Error creating process from file \"{0}\"", simpleProcessStartupInfo.FileName));
                }

                if (safeProcessHandle.IsInvalid || safeThreadHandle.IsInvalid)
                {
                    Logger.LogInfo("CreateProcessWithStartInfo: Invalid process handle.");
                    throw new Exception(string.Format("Error creating process from file \"{0}\" (invalid process handle)", simpleProcessStartupInfo.FileName));
                }

                Logger.LogInfo("CreateProcessWithStartInfo: Creating ProcessResult instance.");
                var processResult = new ProcessInformation {
                    ProcessHandle = safeProcessHandle,
                    ProcessId     = processInformation.dwProcessId
                };
                safeThreadHandle.Close();

                Logger.LogInfo("CreateProcessWithStartInfo: Success!");
                return(processResult);
            }
        }
 public CreateProcessResult(ProcessInformation processInformation, DebuggerObject debuggerObject) {
   _processInformation = processInformation;
   _debuggerObject = debuggerObject;
   _process = Process.GetProcessById(processInformation.ProcessId);
 }
 public CreateProcessResult(ProcessInformation processInformation, DebuggerObject debuggerObject)
 {
     _processInformation = processInformation;
     _debuggerObject     = debuggerObject;
     _process            = Process.GetProcessById(processInformation.ProcessId);
 }