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
84 /// Get the list of Windows handles that should be inherited by the child
85 /// process and update `STARTUPINFOEXW` with the handle list.
86 ///
87 /// If no handles need to be inherited, an empty vector is returned.
88 ///
89 /// Otherwise, the function populates the
90 /// `PROC_THREAD_ATTRIBUTE_HANDLE_LIST` attribute in `startupinfoex` with the
91 /// collected handles using `UpdateProcThreadAttribute`. On success, the
92 /// vector of inherited handles is returned.
93 ///
94 /// \param startupinfoex
95 /// The extended STARTUPINFO structure for the process being created.
96 ///
97 /// \param launch_info
98 /// The process launch configuration.
99 ///
100 /// \param stdout_handle
101 /// \param stderr_handle
102 /// \param stdin_handle
103 /// Optional explicit standard stream handles to use for the child process.
104 ///
105 /// \returns
106 /// `std::vector<HANDLE>` containing all handles that the child must
107 /// inherit.
108 static llvm::ErrorOr<std::vector<HANDLE>>
109 GetInheritedHandles(STARTUPINFOEXW &startupinfoex,
110 const ProcessLaunchInfo *launch_info = nullptr,
111 HANDLE stdout_handle = NULL, HANDLE stderr_handle = NULL,
112 HANDLE stdin_handle = NULL);
113
114 static HANDLE GetStdioHandle(const ProcessLaunchInfo &launch_info, int fd);
115
116 /// Creates a file handle suitable for redirecting stdin, stdout,
117 /// or stderr of a child process.
118 ///
119 /// \param path The file path to open. If empty, returns NULL (no
120 /// redirection).
121 /// \param fd The file descriptor type: STDIN_FILENO, STDOUT_FILENO, or
122 /// STDERR_FILENO.
123 ///
124 /// \return A handle to the opened file, or NULL if the path is empty or the
125 /// file
126 /// cannot be opened (INVALID_HANDLE_VALUE is converted to NULL).
127 ///
128 /// Behavior by file descriptor:
129 /// - STDIN_FILENO: Opens existing file for reading (GENERIC_READ,
130 /// OPEN_EXISTING).
131 /// - STDOUT_FILENO: Creates/truncates file for writing (GENERIC_WRITE,
132 /// CREATE_ALWAYS).
133 /// - STDERR_FILENO: Creates/truncates file for writing with write-through
134 /// (FILE_FLAG_WRITE_THROUGH ensures immediate disk writes,
135 /// bypassing system cache for error messages).
136 ///
137 /// All handles are created with:
138 /// - Inheritance enabled (bInheritHandle = TRUE) so child processes can use
139 /// them.
140 /// - Shared read/write/delete access to allow other processes to access the
141 /// file.
142 static HANDLE GetStdioHandle(const llvm::StringRef path, int fd);
143};
144
145/// Flattens an Args object into a Windows command-line wide string.
146///
147/// Returns an empty string if args is empty.
148///
149/// \param args The Args object to flatten.
150/// \returns A wide string containing the flattened command line.
151llvm::ErrorOr<std::wstring> GetFlattenedWindowsCommandStringW(const Args &args);
152
153llvm::ErrorOr<std::wstring>
154GetFlattenedWindowsCommandStringW(llvm::ArrayRef<const char *> args);
155}
156
157#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.