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