1 //===-- ProcessLaunchInfo.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 <climits> 10 11 #include "lldb/Host/Config.h" 12 #include "lldb/Host/FileAction.h" 13 #include "lldb/Host/FileSystem.h" 14 #include "lldb/Host/HostInfo.h" 15 #include "lldb/Host/ProcessLaunchInfo.h" 16 #include "lldb/Utility/Log.h" 17 #include "lldb/Utility/StreamString.h" 18 19 #include "llvm/Support/ConvertUTF.h" 20 #include "llvm/Support/FileSystem.h" 21 22 #if !defined(_WIN32) 23 #include <limits.h> 24 #endif 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 //---------------------------------------------------------------------------- 30 // ProcessLaunchInfo member functions 31 //---------------------------------------------------------------------------- 32 33 ProcessLaunchInfo::ProcessLaunchInfo() 34 : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(0), 35 m_file_actions(), m_pty(new PseudoTerminal), m_resume_count(0), 36 m_monitor_callback(nullptr), m_monitor_callback_baton(nullptr), 37 m_monitor_signals(false), m_listener_sp(), m_hijack_listener_sp() {} 38 39 ProcessLaunchInfo::ProcessLaunchInfo(const FileSpec &stdin_file_spec, 40 const FileSpec &stdout_file_spec, 41 const FileSpec &stderr_file_spec, 42 const FileSpec &working_directory, 43 uint32_t launch_flags) 44 : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(launch_flags), 45 m_file_actions(), m_pty(new PseudoTerminal), m_resume_count(0), 46 m_monitor_callback(nullptr), m_monitor_callback_baton(nullptr), 47 m_monitor_signals(false), m_listener_sp(), m_hijack_listener_sp() { 48 if (stdin_file_spec) { 49 FileAction file_action; 50 const bool read = true; 51 const bool write = false; 52 if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write)) 53 AppendFileAction(file_action); 54 } 55 if (stdout_file_spec) { 56 FileAction file_action; 57 const bool read = false; 58 const bool write = true; 59 if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write)) 60 AppendFileAction(file_action); 61 } 62 if (stderr_file_spec) { 63 FileAction file_action; 64 const bool read = false; 65 const bool write = true; 66 if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write)) 67 AppendFileAction(file_action); 68 } 69 if (working_directory) 70 SetWorkingDirectory(working_directory); 71 } 72 73 bool ProcessLaunchInfo::AppendCloseFileAction(int fd) { 74 FileAction file_action; 75 if (file_action.Close(fd)) { 76 AppendFileAction(file_action); 77 return true; 78 } 79 return false; 80 } 81 82 bool ProcessLaunchInfo::AppendDuplicateFileAction(int fd, int dup_fd) { 83 FileAction file_action; 84 if (file_action.Duplicate(fd, dup_fd)) { 85 AppendFileAction(file_action); 86 return true; 87 } 88 return false; 89 } 90 91 bool ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec, 92 bool read, bool write) { 93 FileAction file_action; 94 if (file_action.Open(fd, file_spec, read, write)) { 95 AppendFileAction(file_action); 96 return true; 97 } 98 return false; 99 } 100 101 bool ProcessLaunchInfo::AppendSuppressFileAction(int fd, bool read, 102 bool write) { 103 FileAction file_action; 104 if (file_action.Open(fd, FileSpec(FileSystem::DEV_NULL), read, write)) { 105 AppendFileAction(file_action); 106 return true; 107 } 108 return false; 109 } 110 111 const FileAction *ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const { 112 if (idx < m_file_actions.size()) 113 return &m_file_actions[idx]; 114 return nullptr; 115 } 116 117 const FileAction *ProcessLaunchInfo::GetFileActionForFD(int fd) const { 118 for (size_t idx = 0, count = m_file_actions.size(); idx < count; ++idx) { 119 if (m_file_actions[idx].GetFD() == fd) 120 return &m_file_actions[idx]; 121 } 122 return nullptr; 123 } 124 125 const FileSpec &ProcessLaunchInfo::GetWorkingDirectory() const { 126 return m_working_dir; 127 } 128 129 void ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir) { 130 m_working_dir = working_dir; 131 } 132 133 const char *ProcessLaunchInfo::GetProcessPluginName() const { 134 return (m_plugin_name.empty() ? nullptr : m_plugin_name.c_str()); 135 } 136 137 void ProcessLaunchInfo::SetProcessPluginName(llvm::StringRef plugin) { 138 m_plugin_name = plugin; 139 } 140 141 const FileSpec &ProcessLaunchInfo::GetShell() const { return m_shell; } 142 143 void ProcessLaunchInfo::SetShell(const FileSpec &shell) { 144 m_shell = shell; 145 if (m_shell) { 146 FileSystem::Instance().ResolveExecutableLocation(m_shell); 147 m_flags.Set(lldb::eLaunchFlagLaunchInShell); 148 } else 149 m_flags.Clear(lldb::eLaunchFlagLaunchInShell); 150 } 151 152 void ProcessLaunchInfo::SetLaunchInSeparateProcessGroup(bool separate) { 153 if (separate) 154 m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup); 155 else 156 m_flags.Clear(lldb::eLaunchFlagLaunchInSeparateProcessGroup); 157 } 158 159 void ProcessLaunchInfo::SetShellExpandArguments(bool expand) { 160 if (expand) 161 m_flags.Set(lldb::eLaunchFlagShellExpandArguments); 162 else 163 m_flags.Clear(lldb::eLaunchFlagShellExpandArguments); 164 } 165 166 void ProcessLaunchInfo::Clear() { 167 ProcessInfo::Clear(); 168 m_working_dir.Clear(); 169 m_plugin_name.clear(); 170 m_shell.Clear(); 171 m_flags.Clear(); 172 m_file_actions.clear(); 173 m_resume_count = 0; 174 m_listener_sp.reset(); 175 m_hijack_listener_sp.reset(); 176 } 177 178 void ProcessLaunchInfo::SetMonitorProcessCallback( 179 const Host::MonitorChildProcessCallback &callback, bool monitor_signals) { 180 m_monitor_callback = callback; 181 m_monitor_signals = monitor_signals; 182 } 183 184 bool ProcessLaunchInfo::NoOpMonitorCallback(lldb::pid_t pid, bool exited, int signal, int status) { 185 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS); 186 LLDB_LOG(log, "pid = {0}, exited = {1}, signal = {2}, status = {3}", pid, 187 exited, signal, status); 188 return true; 189 } 190 191 bool ProcessLaunchInfo::MonitorProcess() const { 192 if (m_monitor_callback && ProcessIDIsValid()) { 193 Host::StartMonitoringChildProcess(m_monitor_callback, GetProcessID(), 194 m_monitor_signals); 195 return true; 196 } 197 return false; 198 } 199 200 void ProcessLaunchInfo::SetDetachOnError(bool enable) { 201 if (enable) 202 m_flags.Set(lldb::eLaunchFlagDetachOnError); 203 else 204 m_flags.Clear(lldb::eLaunchFlagDetachOnError); 205 } 206 207 llvm::Error ProcessLaunchInfo::SetUpPtyRedirection() { 208 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS); 209 LLDB_LOG(log, "Generating a pty to use for stdin/out/err"); 210 211 int open_flags = O_RDWR | O_NOCTTY; 212 #if !defined(_WIN32) 213 // We really shouldn't be specifying platform specific flags that are 214 // intended for a system call in generic code. But this will have to 215 // do for now. 216 open_flags |= O_CLOEXEC; 217 #endif 218 if (!m_pty->OpenFirstAvailableMaster(open_flags, nullptr, 0)) { 219 return llvm::createStringError(llvm::inconvertibleErrorCode(), 220 "PTY::OpenFirstAvailableMaster failed"); 221 } 222 const FileSpec slave_file_spec(m_pty->GetSlaveName(nullptr, 0)); 223 224 // Only use the slave tty if we don't have anything specified for 225 // input and don't have an action for stdin 226 if (GetFileActionForFD(STDIN_FILENO) == nullptr) 227 AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false); 228 229 // Only use the slave tty if we don't have anything specified for 230 // output and don't have an action for stdout 231 if (GetFileActionForFD(STDOUT_FILENO) == nullptr) 232 AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true); 233 234 // Only use the slave tty if we don't have anything specified for 235 // error and don't have an action for stderr 236 if (GetFileActionForFD(STDERR_FILENO) == nullptr) 237 AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true); 238 return llvm::Error::success(); 239 } 240 241 bool ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell( 242 Status &error, bool localhost, bool will_debug, 243 bool first_arg_is_full_shell_command, int32_t num_resumes) { 244 error.Clear(); 245 246 if (GetFlags().Test(eLaunchFlagLaunchInShell)) { 247 if (m_shell) { 248 std::string shell_executable = m_shell.GetPath(); 249 250 const char **argv = GetArguments().GetConstArgumentVector(); 251 if (argv == nullptr || argv[0] == nullptr) 252 return false; 253 Args shell_arguments; 254 std::string safe_arg; 255 shell_arguments.AppendArgument(shell_executable); 256 const llvm::Triple &triple = GetArchitecture().GetTriple(); 257 if (triple.getOS() == llvm::Triple::Win32 && 258 !triple.isWindowsCygwinEnvironment()) 259 shell_arguments.AppendArgument(llvm::StringRef("/C")); 260 else 261 shell_arguments.AppendArgument(llvm::StringRef("-c")); 262 263 StreamString shell_command; 264 if (will_debug) { 265 // Add a modified PATH environment variable in case argv[0] is a 266 // relative path. 267 const char *argv0 = argv[0]; 268 FileSpec arg_spec(argv0); 269 if (arg_spec.IsRelative()) { 270 // We have a relative path to our executable which may not work if we 271 // just try to run "a.out" (without it being converted to "./a.out") 272 FileSpec working_dir = GetWorkingDirectory(); 273 // Be sure to put quotes around PATH's value in case any paths have 274 // spaces... 275 std::string new_path("PATH=\""); 276 const size_t empty_path_len = new_path.size(); 277 278 if (working_dir) { 279 new_path += working_dir.GetPath(); 280 } else { 281 llvm::SmallString<64> cwd; 282 if (! llvm::sys::fs::current_path(cwd)) 283 new_path += cwd; 284 } 285 std::string curr_path; 286 if (HostInfo::GetEnvironmentVar("PATH", curr_path)) { 287 if (new_path.size() > empty_path_len) 288 new_path += ':'; 289 new_path += curr_path; 290 } 291 new_path += "\" "; 292 shell_command.PutCString(new_path); 293 } 294 295 if (triple.getOS() != llvm::Triple::Win32 || 296 triple.isWindowsCygwinEnvironment()) 297 shell_command.PutCString("exec"); 298 299 // Only Apple supports /usr/bin/arch being able to specify the 300 // architecture 301 if (GetArchitecture().IsValid() && // Valid architecture 302 GetArchitecture().GetTriple().getVendor() == 303 llvm::Triple::Apple && // Apple only 304 GetArchitecture().GetCore() != 305 ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h 306 { 307 shell_command.Printf(" /usr/bin/arch -arch %s", 308 GetArchitecture().GetArchitectureName()); 309 // Set the resume count to 2: 310 // 1 - stop in shell 311 // 2 - stop in /usr/bin/arch 312 // 3 - then we will stop in our program 313 SetResumeCount(num_resumes + 1); 314 } else { 315 // Set the resume count to 1: 316 // 1 - stop in shell 317 // 2 - then we will stop in our program 318 SetResumeCount(num_resumes); 319 } 320 } 321 322 if (first_arg_is_full_shell_command) { 323 // There should only be one argument that is the shell command itself 324 // to be used as is 325 if (argv[0] && !argv[1]) 326 shell_command.Printf("%s", argv[0]); 327 else 328 return false; 329 } else { 330 for (size_t i = 0; argv[i] != nullptr; ++i) { 331 const char *arg = 332 Args::GetShellSafeArgument(m_shell, argv[i], safe_arg); 333 shell_command.Printf(" %s", arg); 334 } 335 } 336 shell_arguments.AppendArgument(shell_command.GetString()); 337 m_executable = m_shell; 338 m_arguments = shell_arguments; 339 return true; 340 } else { 341 error.SetErrorString("invalid shell path"); 342 } 343 } else { 344 error.SetErrorString("not launching in shell"); 345 } 346 return false; 347 } 348