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