xref: /openbsd-src/gnu/llvm/lldb/source/Host/windows/ProcessLauncherWindows.cpp (revision d0fc3bb68efd6c434b4053cd7adb29023cbec341)
1 //===-- ProcessLauncherWindows.cpp ----------------------------------------===//
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 #include "lldb/Host/windows/ProcessLauncherWindows.h"
10 #include "lldb/Host/HostProcess.h"
11 #include "lldb/Host/ProcessLaunchInfo.h"
12 
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/Support/ConvertUTF.h"
15 #include "llvm/Support/Program.h"
16 
17 #include <string>
18 #include <vector>
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 namespace {
24 void CreateEnvironmentBuffer(const Environment &env,
25                              std::vector<char> &buffer) {
26   // The buffer is a list of null-terminated UTF-16 strings, followed by an
27   // extra L'\0' (two bytes of 0).  An empty environment must have one
28   // empty string, followed by an extra L'\0'.
29   for (const auto &KV : env) {
30     std::wstring warg;
31     if (llvm::ConvertUTF8toWide(Environment::compose(KV), warg)) {
32       buffer.insert(
33           buffer.end(), reinterpret_cast<const char *>(warg.c_str()),
34           reinterpret_cast<const char *>(warg.c_str() + warg.size() + 1));
35     }
36   }
37   // One null wchar_t (to end the block) is two null bytes
38   buffer.push_back(0);
39   buffer.push_back(0);
40   // Insert extra two bytes, just in case the environment was empty.
41   buffer.push_back(0);
42   buffer.push_back(0);
43 }
44 
45 bool GetFlattenedWindowsCommandString(Args args, std::string &command) {
46   if (args.empty())
47     return false;
48 
49   std::vector<llvm::StringRef> args_ref;
50   for (auto &entry : args.entries())
51     args_ref.push_back(entry.ref());
52 
53   command = llvm::sys::flattenWindowsCommandLine(args_ref);
54   return true;
55 }
56 } // namespace
57 
58 HostProcess
59 ProcessLauncherWindows::LaunchProcess(const ProcessLaunchInfo &launch_info,
60                                       Status &error) {
61   error.Clear();
62 
63   std::string executable;
64   std::string commandLine;
65   std::vector<char> environment;
66   STARTUPINFO startupinfo = {};
67   PROCESS_INFORMATION pi = {};
68 
69   HANDLE stdin_handle = GetStdioHandle(launch_info, STDIN_FILENO);
70   HANDLE stdout_handle = GetStdioHandle(launch_info, STDOUT_FILENO);
71   HANDLE stderr_handle = GetStdioHandle(launch_info, STDERR_FILENO);
72 
73   startupinfo.cb = sizeof(startupinfo);
74   startupinfo.dwFlags |= STARTF_USESTDHANDLES;
75   startupinfo.hStdError =
76       stderr_handle ? stderr_handle : ::GetStdHandle(STD_ERROR_HANDLE);
77   startupinfo.hStdInput =
78       stdin_handle ? stdin_handle : ::GetStdHandle(STD_INPUT_HANDLE);
79   startupinfo.hStdOutput =
80       stdout_handle ? stdout_handle : ::GetStdHandle(STD_OUTPUT_HANDLE);
81 
82   const char *hide_console_var =
83       getenv("LLDB_LAUNCH_INFERIORS_WITHOUT_CONSOLE");
84   if (hide_console_var &&
85       llvm::StringRef(hide_console_var).equals_lower("true")) {
86     startupinfo.dwFlags |= STARTF_USESHOWWINDOW;
87     startupinfo.wShowWindow = SW_HIDE;
88   }
89 
90   DWORD flags = CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT;
91   if (launch_info.GetFlags().Test(eLaunchFlagDebug))
92     flags |= DEBUG_ONLY_THIS_PROCESS;
93 
94   if (launch_info.GetFlags().Test(eLaunchFlagDisableSTDIO))
95     flags &= ~CREATE_NEW_CONSOLE;
96 
97   LPVOID env_block = nullptr;
98   ::CreateEnvironmentBuffer(launch_info.GetEnvironment(), environment);
99   env_block = environment.data();
100 
101   executable = launch_info.GetExecutableFile().GetPath();
102   GetFlattenedWindowsCommandString(launch_info.GetArguments(), commandLine);
103 
104   std::wstring wexecutable, wcommandLine, wworkingDirectory;
105   llvm::ConvertUTF8toWide(executable, wexecutable);
106   llvm::ConvertUTF8toWide(commandLine, wcommandLine);
107   llvm::ConvertUTF8toWide(launch_info.GetWorkingDirectory().GetCString(),
108                           wworkingDirectory);
109   // If the command line is empty, it's best to pass a null pointer to tell
110   // CreateProcessW to use the executable name as the command line.  If the
111   // command line is not empty, its contents may be modified by CreateProcessW.
112   WCHAR *pwcommandLine = wcommandLine.empty() ? nullptr : &wcommandLine[0];
113 
114   BOOL result = ::CreateProcessW(
115       wexecutable.c_str(), pwcommandLine, NULL, NULL, TRUE, flags, env_block,
116       wworkingDirectory.size() == 0 ? NULL : wworkingDirectory.c_str(),
117       &startupinfo, &pi);
118 
119   if (!result) {
120     // Call GetLastError before we make any other system calls.
121     error.SetError(::GetLastError(), eErrorTypeWin32);
122     // Note that error 50 ("The request is not supported") will occur if you
123     // try debug a 64-bit inferior from a 32-bit LLDB.
124   }
125 
126   if (result) {
127     // Do not call CloseHandle on pi.hProcess, since we want to pass that back
128     // through the HostProcess.
129     ::CloseHandle(pi.hThread);
130   }
131 
132   if (stdin_handle)
133     ::CloseHandle(stdin_handle);
134   if (stdout_handle)
135     ::CloseHandle(stdout_handle);
136   if (stderr_handle)
137     ::CloseHandle(stderr_handle);
138 
139   if (!result)
140     return HostProcess();
141 
142   return HostProcess(pi.hProcess);
143 }
144 
145 HANDLE
146 ProcessLauncherWindows::GetStdioHandle(const ProcessLaunchInfo &launch_info,
147                                        int fd) {
148   const FileAction *action = launch_info.GetFileActionForFD(fd);
149   if (action == nullptr)
150     return NULL;
151   SECURITY_ATTRIBUTES secattr = {};
152   secattr.nLength = sizeof(SECURITY_ATTRIBUTES);
153   secattr.bInheritHandle = TRUE;
154 
155   llvm::StringRef path = action->GetPath();
156   DWORD access = 0;
157   DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
158   DWORD create = 0;
159   DWORD flags = 0;
160   if (fd == STDIN_FILENO) {
161     access = GENERIC_READ;
162     create = OPEN_EXISTING;
163     flags = FILE_ATTRIBUTE_READONLY;
164   }
165   if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
166     access = GENERIC_WRITE;
167     create = CREATE_ALWAYS;
168     if (fd == STDERR_FILENO)
169       flags = FILE_FLAG_WRITE_THROUGH;
170   }
171 
172   std::wstring wpath;
173   llvm::ConvertUTF8toWide(path, wpath);
174   HANDLE result = ::CreateFileW(wpath.c_str(), access, share, &secattr, create,
175                                 flags, NULL);
176   return (result == INVALID_HANDLE_VALUE) ? NULL : result;
177 }
178