1 //===-- ProcessLaunchInfo.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 <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/LLDBLog.h" 17 #include "lldb/Utility/Log.h" 18 #include "lldb/Utility/StreamString.h" 19 20 #include "llvm/Support/ConvertUTF.h" 21 #include "llvm/Support/FileSystem.h" 22 23 #if !defined(_WIN32) 24 #include <climits> 25 #endif 26 27 using namespace lldb; 28 using namespace lldb_private; 29 30 // ProcessLaunchInfo member functions 31 32 ProcessLaunchInfo::ProcessLaunchInfo() 33 : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(0), 34 m_file_actions(), m_pty(new PseudoTerminal), m_monitor_callback(nullptr), 35 m_listener_sp(), m_hijack_listener_sp(), m_scripted_process_class_name(), 36 m_scripted_process_dictionary_sp() {} 37 38 ProcessLaunchInfo::ProcessLaunchInfo(const FileSpec &stdin_file_spec, 39 const FileSpec &stdout_file_spec, 40 const FileSpec &stderr_file_spec, 41 const FileSpec &working_directory, 42 uint32_t launch_flags) 43 : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(launch_flags), 44 m_file_actions(), m_pty(new PseudoTerminal), m_resume_count(0), 45 m_monitor_callback(nullptr), m_monitor_callback_baton(nullptr), 46 m_monitor_signals(false), m_listener_sp(), m_hijack_listener_sp(), 47 m_scripted_process_class_name(), m_scripted_process_dictionary_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 = std::string(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 m_scripted_process_class_name.clear(); 177 m_scripted_process_dictionary_sp.reset(); 178 } 179 180 void ProcessLaunchInfo::SetMonitorProcessCallback( 181 const Host::MonitorChildProcessCallback &callback, bool monitor_signals) { 182 m_monitor_callback = callback; 183 m_monitor_signals = monitor_signals; 184 } 185 186 bool ProcessLaunchInfo::NoOpMonitorCallback(lldb::pid_t pid, bool exited, int signal, int status) { 187 Log *log = GetLog(LLDBLog::Process); 188 LLDB_LOG(log, "pid = {0}, exited = {1}, signal = {2}, status = {3}", pid, 189 exited, signal, status); 190 return true; 191 } 192 193 bool ProcessLaunchInfo::MonitorProcess() const { 194 if (m_monitor_callback && ProcessIDIsValid()) { 195 llvm::Expected<HostThread> maybe_thread = 196 Host::StartMonitoringChildProcess(m_monitor_callback, GetProcessID(), 197 m_monitor_signals); 198 if (!maybe_thread) 199 LLDB_LOG(GetLog(LLDBLog::Host), "failed to launch host thread: {}", 200 llvm::toString(maybe_thread.takeError())); 201 return true; 202 } 203 return false; 204 } 205 206 void ProcessLaunchInfo::SetDetachOnError(bool enable) { 207 if (enable) 208 m_flags.Set(lldb::eLaunchFlagDetachOnError); 209 else 210 m_flags.Clear(lldb::eLaunchFlagDetachOnError); 211 } 212 213 llvm::Error ProcessLaunchInfo::SetUpPtyRedirection() { 214 Log *log = GetLog(LLDBLog::Process); 215 216 bool stdin_free = GetFileActionForFD(STDIN_FILENO) == nullptr; 217 bool stdout_free = GetFileActionForFD(STDOUT_FILENO) == nullptr; 218 bool stderr_free = GetFileActionForFD(STDERR_FILENO) == nullptr; 219 bool any_free = stdin_free || stdout_free || stderr_free; 220 if (!any_free) 221 return llvm::Error::success(); 222 223 LLDB_LOG(log, "Generating a pty to use for stdin/out/err"); 224 225 int open_flags = O_RDWR | O_NOCTTY; 226 #if !defined(_WIN32) 227 // We really shouldn't be specifying platform specific flags that are 228 // intended for a system call in generic code. But this will have to 229 // do for now. 230 open_flags |= O_CLOEXEC; 231 #endif 232 if (llvm::Error Err = m_pty->OpenFirstAvailablePrimary(open_flags)) 233 return Err; 234 235 const FileSpec secondary_file_spec(m_pty->GetSecondaryName()); 236 237 if (stdin_free) 238 AppendOpenFileAction(STDIN_FILENO, secondary_file_spec, true, false); 239 240 if (stdout_free) 241 AppendOpenFileAction(STDOUT_FILENO, secondary_file_spec, false, true); 242 243 if (stderr_free) 244 AppendOpenFileAction(STDERR_FILENO, secondary_file_spec, false, true); 245 return llvm::Error::success(); 246 } 247 248 bool ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell( 249 Status &error, bool will_debug, bool first_arg_is_full_shell_command, 250 uint32_t num_resumes) { 251 error.Clear(); 252 253 if (GetFlags().Test(eLaunchFlagLaunchInShell)) { 254 if (m_shell) { 255 std::string shell_executable = m_shell.GetPath(); 256 257 const char **argv = GetArguments().GetConstArgumentVector(); 258 if (argv == nullptr || argv[0] == nullptr) 259 return false; 260 Args shell_arguments; 261 shell_arguments.AppendArgument(shell_executable); 262 const llvm::Triple &triple = GetArchitecture().GetTriple(); 263 if (triple.getOS() == llvm::Triple::Win32 && 264 !triple.isWindowsCygwinEnvironment()) 265 shell_arguments.AppendArgument(llvm::StringRef("/C")); 266 else 267 shell_arguments.AppendArgument(llvm::StringRef("-c")); 268 269 StreamString shell_command; 270 if (will_debug) { 271 // Add a modified PATH environment variable in case argv[0] is a 272 // relative path. 273 const char *argv0 = argv[0]; 274 FileSpec arg_spec(argv0); 275 if (arg_spec.IsRelative()) { 276 // We have a relative path to our executable which may not work if we 277 // just try to run "a.out" (without it being converted to "./a.out") 278 FileSpec working_dir = GetWorkingDirectory(); 279 // Be sure to put quotes around PATH's value in case any paths have 280 // spaces... 281 std::string new_path("PATH=\""); 282 const size_t empty_path_len = new_path.size(); 283 284 if (working_dir) { 285 new_path += working_dir.GetPath(); 286 } else { 287 llvm::SmallString<64> cwd; 288 if (! llvm::sys::fs::current_path(cwd)) 289 new_path += cwd; 290 } 291 std::string curr_path; 292 if (HostInfo::GetEnvironmentVar("PATH", curr_path)) { 293 if (new_path.size() > empty_path_len) 294 new_path += ':'; 295 new_path += curr_path; 296 } 297 new_path += "\" "; 298 shell_command.PutCString(new_path); 299 } 300 301 if (triple.getOS() != llvm::Triple::Win32 || 302 triple.isWindowsCygwinEnvironment()) 303 shell_command.PutCString("exec"); 304 305 // Only Apple supports /usr/bin/arch being able to specify the 306 // architecture 307 if (GetArchitecture().IsValid() && // Valid architecture 308 GetArchitecture().GetTriple().getVendor() == 309 llvm::Triple::Apple && // Apple only 310 GetArchitecture().GetCore() != 311 ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h 312 { 313 shell_command.Printf(" /usr/bin/arch -arch %s", 314 GetArchitecture().GetArchitectureName()); 315 // Set the resume count to 2: 316 // 1 - stop in shell 317 // 2 - stop in /usr/bin/arch 318 // 3 - then we will stop in our program 319 SetResumeCount(num_resumes + 1); 320 } else { 321 // Set the resume count to 1: 322 // 1 - stop in shell 323 // 2 - then we will stop in our program 324 SetResumeCount(num_resumes); 325 } 326 } 327 328 if (first_arg_is_full_shell_command) { 329 // There should only be one argument that is the shell command itself 330 // to be used as is 331 if (argv[0] && !argv[1]) 332 shell_command.Printf("%s", argv[0]); 333 else 334 return false; 335 } else { 336 for (size_t i = 0; argv[i] != nullptr; ++i) { 337 std::string safe_arg = Args::GetShellSafeArgument(m_shell, argv[i]); 338 // Add a space to separate this arg from the previous one. 339 shell_command.PutCString(" "); 340 shell_command.PutCString(safe_arg); 341 } 342 } 343 shell_arguments.AppendArgument(shell_command.GetString()); 344 m_executable = m_shell; 345 m_arguments = shell_arguments; 346 return true; 347 } else { 348 error.SetErrorString("invalid shell path"); 349 } 350 } else { 351 error.SetErrorString("not launching in shell"); 352 } 353 return false; 354 } 355