LLDB mainline
ProcessLauncherWindows.h
Go to the documentation of this file.
1//===-- ProcessLauncherWindows.h --------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef lldb_Host_windows_ProcessLauncherWindows_h_
10#define lldb_Host_windows_ProcessLauncherWindows_h_
11
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/ErrorOr.h"
18#include "llvm/Support/WindowsError.h"
19
20#include <optional>
21
22namespace lldb_private {
23
25
26/// This class manages the lifetime of a PROC_THREAD_ATTRIBUTE_LIST, which is
27/// used with STARTUPINFOEX.
28///
29/// The attribute list is automatically cleaned up when this object is
30/// destroyed.
32public:
33 /// Allocate memory for the attribute list, initialize it, and sets the
34 /// lpAttributeList member of STARTUPINFOEXW structure.
35 ///
36 /// \param[in,out] startupinfoex
37 /// The STARTUPINFOEXW structure whose lpAttributeList member will be set
38 /// to point to the attribute list. The caller must ensure
39 /// this structure remains valid for the lifetime of the returned object.
40 ///
41 /// \return
42 /// A ProcThreadAttributeList object on success, or an error code on
43 /// failure.
44 static llvm::ErrorOr<ProcThreadAttributeList>
45 Create(STARTUPINFOEXW &startupinfoex);
46
47 /// Setup the PseudoConsole handle in the underlying
48 /// LPPROC_THREAD_ATTRIBUTE_LIST.
49 ///
50 /// \param hPC
51 /// The handle to the PseudoConsole.
52 llvm::Error SetupPseudoConsole(HPCON hPC);
53
55 if (lpAttributeList) {
56 DeleteProcThreadAttributeList(lpAttributeList);
57 free(lpAttributeList);
58 }
59 }
60
61 /// ProcThreadAttributeList is not copyable.
62 /// @{
65 /// @}
66
68 : lpAttributeList(other.lpAttributeList) {
69 other.lpAttributeList = nullptr;
70 }
71
72private:
73 explicit ProcThreadAttributeList(LPPROC_THREAD_ATTRIBUTE_LIST list)
74 : lpAttributeList(list) {}
75
76 LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList;
77};
78
80public:
82 Status &error) override;
83
84protected:
85 /// Get the list of Windows handles that should be inherited by the child
86 /// process and update `STARTUPINFOEXW` with the handle list.
87 ///
88 /// If no handles need to be inherited, an empty vector is returned.
89 ///
90 /// Otherwise, the function populates the
91 /// `PROC_THREAD_ATTRIBUTE_HANDLE_LIST` attribute in `startupinfoex` with the
92 /// collected handles using `UpdateProcThreadAttribute`. On success, the
93 /// vector of inherited handles is returned.
94 ///
95 /// \param startupinfoex
96 /// The extended STARTUPINFO structure for the process being created.
97 ///
98 /// \param launch_info
99 /// The process launch configuration.
100 ///
101 /// \param stdout_handle
102 /// \param stderr_handle
103 /// \param stdin_handle
104 /// Optional explicit standard stream handles to use for the child process.
105 ///
106 /// \returns
107 /// `std::vector<HANDLE>` containing all handles that the child must
108 /// inherit.
109 static llvm::ErrorOr<std::vector<HANDLE>>
110 GetInheritedHandles(STARTUPINFOEXW &startupinfoex,
111 const ProcessLaunchInfo *launch_info = nullptr,
112 HANDLE stdout_handle = NULL, HANDLE stderr_handle = NULL,
113 HANDLE stdin_handle = NULL);
114
115 static HANDLE GetStdioHandle(const ProcessLaunchInfo &launch_info, int fd);
116
117 /// Creates a file handle suitable for redirecting stdin, stdout,
118 /// or stderr of a child process.
119 ///
120 /// \param path The file path to open. If empty, returns NULL (no
121 /// redirection).
122 /// \param fd The file descriptor type: STDIN_FILENO, STDOUT_FILENO, or
123 /// STDERR_FILENO.
124 ///
125 /// \return A handle to the opened file, or NULL if the path is empty or the
126 /// file
127 /// cannot be opened (INVALID_HANDLE_VALUE is converted to NULL).
128 ///
129 /// Behavior by file descriptor:
130 /// - STDIN_FILENO: Opens existing file for reading (GENERIC_READ,
131 /// OPEN_EXISTING).
132 /// - STDOUT_FILENO: Creates/truncates file for writing (GENERIC_WRITE,
133 /// CREATE_ALWAYS).
134 /// - STDERR_FILENO: Creates/truncates file for writing with write-through
135 /// (FILE_FLAG_WRITE_THROUGH ensures immediate disk writes,
136 /// bypassing system cache for error messages).
137 ///
138 /// All handles are created with:
139 /// - Inheritance enabled (bInheritHandle = TRUE) so child processes can use
140 /// them.
141 /// - Shared read/write/delete access to allow other processes to access the
142 /// file.
143 static HANDLE GetStdioHandle(const llvm::StringRef path, int fd);
144};
145
146/// Flattens an Args object into a Windows command-line wide string.
147///
148/// Returns an empty string if args is empty.
149///
150/// \param args The Args object to flatten.
151/// \returns A wide string containing the flattened command line.
152llvm::ErrorOr<std::wstring> GetFlattenedWindowsCommandStringW(const Args &args);
153
154llvm::ErrorOr<std::wstring>
155GetFlattenedWindowsCommandStringW(llvm::ArrayRef<const char *> args);
156}
157
158#endif
static llvm::raw_ostream & error(Stream &strm)
void * HPCON
void * HANDLE
A command line argument class.
Definition Args.h:33
ProcThreadAttributeList(LPPROC_THREAD_ATTRIBUTE_LIST list)
ProcThreadAttributeList(ProcThreadAttributeList &&other) noexcept
ProcThreadAttributeList & operator=(const ProcThreadAttributeList &)=delete
static llvm::ErrorOr< ProcThreadAttributeList > Create(STARTUPINFOEXW &startupinfoex)
Allocate memory for the attribute list, initialize it, and sets the lpAttributeList member of STARTUP...
llvm::Error SetupPseudoConsole(HPCON hPC)
Setup the PseudoConsole handle in the underlying LPPROC_THREAD_ATTRIBUTE_LIST.
LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList
ProcThreadAttributeList(const ProcThreadAttributeList &)=delete
ProcThreadAttributeList is not copyable.
HostProcess LaunchProcess(const ProcessLaunchInfo &launch_info, Status &error) override
static HANDLE GetStdioHandle(const ProcessLaunchInfo &launch_info, int fd)
static llvm::ErrorOr< std::vector< HANDLE > > GetInheritedHandles(STARTUPINFOEXW &startupinfoex, const ProcessLaunchInfo *launch_info=nullptr, HANDLE stdout_handle=NULL, HANDLE stderr_handle=NULL, HANDLE stdin_handle=NULL)
Get the list of Windows handles that should be inherited by the child process and update STARTUPINFOE...
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.
llvm::ErrorOr< std::wstring > GetFlattenedWindowsCommandStringW(const Args &args)
Flattens an Args object into a Windows command-line wide string.