Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 1 | //===-- ProcessLaunchInfo.cpp -----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 10 | #include <climits> |
Todd Fiala | 2850b1b | 2014-06-30 23:51:35 +0000 | [diff] [blame] | 11 | |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Debugger.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 13 | #include "lldb/Host/Config.h" |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 14 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 15 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 16 | #include "lldb/Target/FileAction.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 17 | #include "lldb/Target/ProcessLaunchInfo.h" |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 18 | #include "lldb/Target/Target.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Log.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 20 | #include "lldb/Utility/StreamString.h" |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 21 | |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ConvertUTF.h" |
Pavel Labath | 1d5855b | 2017-01-23 15:56:45 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 24 | |
| 25 | #if !defined(_WIN32) |
| 26 | #include <limits.h> |
| 27 | #endif |
| 28 | |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 29 | using namespace lldb; |
| 30 | using namespace lldb_private; |
| 31 | |
| 32 | //---------------------------------------------------------------------------- |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 33 | // ProcessLaunchInfo member functions |
| 34 | //---------------------------------------------------------------------------- |
| 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | ProcessLaunchInfo::ProcessLaunchInfo() |
| 37 | : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(0), |
Pavel Labath | 07d6f88 | 2017-12-11 10:09:14 +0000 | [diff] [blame] | 38 | m_file_actions(), m_pty(new PseudoTerminal), m_resume_count(0), |
| 39 | m_monitor_callback(nullptr), m_monitor_callback_baton(nullptr), |
| 40 | m_monitor_signals(false), m_listener_sp(), m_hijack_listener_sp() {} |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 41 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 42 | ProcessLaunchInfo::ProcessLaunchInfo(const FileSpec &stdin_file_spec, |
| 43 | const FileSpec &stdout_file_spec, |
| 44 | const FileSpec &stderr_file_spec, |
| 45 | const FileSpec &working_directory, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | uint32_t launch_flags) |
| 47 | : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(launch_flags), |
Pavel Labath | 07d6f88 | 2017-12-11 10:09:14 +0000 | [diff] [blame] | 48 | m_file_actions(), m_pty(new PseudoTerminal), m_resume_count(0), |
| 49 | m_monitor_callback(nullptr), m_monitor_callback_baton(nullptr), |
| 50 | m_monitor_signals(false), m_listener_sp(), m_hijack_listener_sp() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | if (stdin_file_spec) { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 52 | FileAction file_action; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | const bool read = true; |
| 54 | const bool write = false; |
| 55 | if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write)) |
| 56 | AppendFileAction(file_action); |
| 57 | } |
| 58 | if (stdout_file_spec) { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 59 | FileAction file_action; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | const bool read = false; |
| 61 | const bool write = true; |
| 62 | if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write)) |
| 63 | AppendFileAction(file_action); |
| 64 | } |
| 65 | if (stderr_file_spec) { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 66 | FileAction file_action; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | const bool read = false; |
| 68 | const bool write = true; |
| 69 | if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write)) |
| 70 | AppendFileAction(file_action); |
| 71 | } |
| 72 | if (working_directory) |
| 73 | SetWorkingDirectory(working_directory); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | bool ProcessLaunchInfo::AppendCloseFileAction(int fd) { |
| 77 | FileAction file_action; |
| 78 | if (file_action.Close(fd)) { |
| 79 | AppendFileAction(file_action); |
| 80 | return true; |
| 81 | } |
| 82 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | bool ProcessLaunchInfo::AppendDuplicateFileAction(int fd, int dup_fd) { |
| 86 | FileAction file_action; |
| 87 | if (file_action.Duplicate(fd, dup_fd)) { |
| 88 | AppendFileAction(file_action); |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | bool ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec, |
| 95 | bool read, bool write) { |
| 96 | FileAction file_action; |
| 97 | if (file_action.Open(fd, file_spec, read, write)) { |
| 98 | AppendFileAction(file_action); |
| 99 | return true; |
| 100 | } |
| 101 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | bool ProcessLaunchInfo::AppendSuppressFileAction(int fd, bool read, |
| 105 | bool write) { |
| 106 | FileAction file_action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 107 | if (file_action.Open(fd, FileSpec(FileSystem::DEV_NULL), read, write)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | AppendFileAction(file_action); |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | const FileAction *ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const { |
| 115 | if (idx < m_file_actions.size()) |
| 116 | return &m_file_actions[idx]; |
| 117 | return nullptr; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | const FileAction *ProcessLaunchInfo::GetFileActionForFD(int fd) const { |
| 121 | for (size_t idx = 0, count = m_file_actions.size(); idx < count; ++idx) { |
| 122 | if (m_file_actions[idx].GetFD() == fd) |
| 123 | return &m_file_actions[idx]; |
| 124 | } |
| 125 | return nullptr; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | const FileSpec &ProcessLaunchInfo::GetWorkingDirectory() const { |
| 129 | return m_working_dir; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | void ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir) { |
| 133 | m_working_dir = working_dir; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | const char *ProcessLaunchInfo::GetProcessPluginName() const { |
| 137 | return (m_plugin_name.empty() ? nullptr : m_plugin_name.c_str()); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 140 | void ProcessLaunchInfo::SetProcessPluginName(llvm::StringRef plugin) { |
| 141 | m_plugin_name = plugin; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | const FileSpec &ProcessLaunchInfo::GetShell() const { return m_shell; } |
| 145 | |
| 146 | void ProcessLaunchInfo::SetShell(const FileSpec &shell) { |
| 147 | m_shell = shell; |
| 148 | if (m_shell) { |
Jonas Devlieghere | 2c22c80 | 2018-11-01 17:09:22 +0000 | [diff] [blame] | 149 | FileSystem::Instance().ResolveExecutableLocation(m_shell); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | m_flags.Set(lldb::eLaunchFlagLaunchInShell); |
| 151 | } else |
| 152 | m_flags.Clear(lldb::eLaunchFlagLaunchInShell); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | void ProcessLaunchInfo::SetLaunchInSeparateProcessGroup(bool separate) { |
| 156 | if (separate) |
| 157 | m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup); |
| 158 | else |
| 159 | m_flags.Clear(lldb::eLaunchFlagLaunchInSeparateProcessGroup); |
| 160 | } |
| 161 | |
| 162 | void ProcessLaunchInfo::SetShellExpandArguments(bool expand) { |
| 163 | if (expand) |
| 164 | m_flags.Set(lldb::eLaunchFlagShellExpandArguments); |
| 165 | else |
| 166 | m_flags.Clear(lldb::eLaunchFlagShellExpandArguments); |
| 167 | } |
| 168 | |
| 169 | void ProcessLaunchInfo::Clear() { |
| 170 | ProcessInfo::Clear(); |
| 171 | m_working_dir.Clear(); |
| 172 | m_plugin_name.clear(); |
| 173 | m_shell.Clear(); |
| 174 | m_flags.Clear(); |
| 175 | m_file_actions.clear(); |
| 176 | m_resume_count = 0; |
| 177 | m_listener_sp.reset(); |
| 178 | m_hijack_listener_sp.reset(); |
| 179 | } |
| 180 | |
| 181 | void ProcessLaunchInfo::SetMonitorProcessCallback( |
| 182 | const Host::MonitorChildProcessCallback &callback, bool monitor_signals) { |
| 183 | m_monitor_callback = callback; |
| 184 | m_monitor_signals = monitor_signals; |
| 185 | } |
| 186 | |
Pavel Labath | 245dd2e | 2018-05-15 13:42:26 +0000 | [diff] [blame] | 187 | bool ProcessLaunchInfo::NoOpMonitorCallback(lldb::pid_t pid, bool exited, int signal, int status) { |
| 188 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS); |
| 189 | LLDB_LOG(log, "pid = {0}, exited = {1}, signal = {2}, status = {3}", pid, |
| 190 | exited, signal, status); |
| 191 | return true; |
| 192 | } |
| 193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | bool ProcessLaunchInfo::MonitorProcess() const { |
| 195 | if (m_monitor_callback && ProcessIDIsValid()) { |
| 196 | Host::StartMonitoringChildProcess(m_monitor_callback, GetProcessID(), |
| 197 | m_monitor_signals); |
| 198 | return true; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | void ProcessLaunchInfo::SetDetachOnError(bool enable) { |
| 204 | if (enable) |
| 205 | m_flags.Set(lldb::eLaunchFlagDetachOnError); |
| 206 | else |
| 207 | m_flags.Clear(lldb::eLaunchFlagDetachOnError); |
| 208 | } |
| 209 | |
| 210 | void ProcessLaunchInfo::FinalizeFileActions(Target *target, |
| 211 | bool default_to_use_pty) { |
| 212 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 213 | |
| 214 | // If nothing for stdin or stdout or stderr was specified, then check the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 215 | // process for any default settings that were set with "settings set" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | if (GetFileActionForFD(STDIN_FILENO) == nullptr || |
| 217 | GetFileActionForFD(STDOUT_FILENO) == nullptr || |
| 218 | GetFileActionForFD(STDERR_FILENO) == nullptr) { |
| 219 | if (log) |
| 220 | log->Printf("ProcessLaunchInfo::%s at least one of stdin/stdout/stderr " |
| 221 | "was not set, evaluating default handling", |
| 222 | __FUNCTION__); |
| 223 | |
| 224 | if (m_flags.Test(eLaunchFlagLaunchInTTY)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 225 | // Do nothing, if we are launching in a remote terminal no file actions |
| 226 | // should be done at all. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | return; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 228 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 230 | if (m_flags.Test(eLaunchFlagDisableSTDIO)) { |
| 231 | if (log) |
| 232 | log->Printf("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding " |
| 233 | "suppression action for stdin, stdout and stderr", |
| 234 | __FUNCTION__); |
| 235 | AppendSuppressFileAction(STDIN_FILENO, true, false); |
| 236 | AppendSuppressFileAction(STDOUT_FILENO, false, true); |
| 237 | AppendSuppressFileAction(STDERR_FILENO, false, true); |
| 238 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 239 | // Check for any values that might have gotten set with any of: (lldb) |
| 240 | // settings set target.input-path (lldb) settings set target.output-path |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | // (lldb) settings set target.error-path |
| 242 | FileSpec in_file_spec; |
| 243 | FileSpec out_file_spec; |
| 244 | FileSpec err_file_spec; |
| 245 | if (target) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 246 | // Only override with the target settings if we don't already have an |
| 247 | // action for in, out or error |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | if (GetFileActionForFD(STDIN_FILENO) == nullptr) |
| 249 | in_file_spec = target->GetStandardInputPath(); |
| 250 | if (GetFileActionForFD(STDOUT_FILENO) == nullptr) |
| 251 | out_file_spec = target->GetStandardOutputPath(); |
| 252 | if (GetFileActionForFD(STDERR_FILENO) == nullptr) |
| 253 | err_file_spec = target->GetStandardErrorPath(); |
| 254 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 255 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | if (log) |
| 257 | log->Printf("ProcessLaunchInfo::%s target stdin='%s', target " |
| 258 | "stdout='%s', stderr='%s'", |
| 259 | __FUNCTION__, |
| 260 | in_file_spec ? in_file_spec.GetCString() : "<null>", |
| 261 | out_file_spec ? out_file_spec.GetCString() : "<null>", |
| 262 | err_file_spec ? err_file_spec.GetCString() : "<null>"); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 263 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | if (in_file_spec) { |
| 265 | AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 266 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | log->Printf( |
| 268 | "ProcessLaunchInfo::%s appended stdin open file action for %s", |
| 269 | __FUNCTION__, in_file_spec.GetCString()); |
| 270 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 271 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | if (out_file_spec) { |
| 273 | AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true); |
| 274 | if (log) |
| 275 | log->Printf( |
| 276 | "ProcessLaunchInfo::%s appended stdout open file action for %s", |
| 277 | __FUNCTION__, out_file_spec.GetCString()); |
| 278 | } |
Greg Clayton | bf91f71 | 2015-07-10 18:04:46 +0000 | [diff] [blame] | 279 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 280 | if (err_file_spec) { |
| 281 | AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true); |
| 282 | if (log) |
| 283 | log->Printf( |
| 284 | "ProcessLaunchInfo::%s appended stderr open file action for %s", |
| 285 | __FUNCTION__, err_file_spec.GetCString()); |
| 286 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 287 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | if (default_to_use_pty && |
| 289 | (!in_file_spec || !out_file_spec || !err_file_spec)) { |
| 290 | if (log) |
| 291 | log->Printf("ProcessLaunchInfo::%s default_to_use_pty is set, and at " |
| 292 | "least one stdin/stderr/stdout is unset, so generating a " |
| 293 | "pty to use for it", |
| 294 | __FUNCTION__); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 295 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 296 | int open_flags = O_RDWR | O_NOCTTY; |
Hafiz Abid Qadeer | f6ee79c | 2016-12-15 15:00:41 +0000 | [diff] [blame] | 297 | #if !defined(_WIN32) |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 298 | // We really shouldn't be specifying platform specific flags that are |
| 299 | // intended for a system call in generic code. But this will have to |
| 300 | // do for now. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 301 | open_flags |= O_CLOEXEC; |
Zachary Turner | 362a813 | 2015-02-04 19:11:48 +0000 | [diff] [blame] | 302 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 303 | if (m_pty->OpenFirstAvailableMaster(open_flags, nullptr, 0)) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 304 | const FileSpec slave_file_spec(m_pty->GetSlaveName(nullptr, 0)); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 305 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 306 | // Only use the slave tty if we don't have anything specified for |
| 307 | // input and don't have an action for stdin |
| 308 | if (!in_file_spec && GetFileActionForFD(STDIN_FILENO) == nullptr) { |
| 309 | AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false); |
| 310 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 311 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 312 | // Only use the slave tty if we don't have anything specified for |
| 313 | // output and don't have an action for stdout |
| 314 | if (!out_file_spec && GetFileActionForFD(STDOUT_FILENO) == nullptr) { |
| 315 | AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true); |
| 316 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 317 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 318 | // Only use the slave tty if we don't have anything specified for |
| 319 | // error and don't have an action for stderr |
| 320 | if (!err_file_spec && GetFileActionForFD(STDERR_FILENO) == nullptr) { |
| 321 | AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true); |
| 322 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 323 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 324 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 325 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 326 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | bool ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell( |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 330 | Status &error, bool localhost, bool will_debug, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 331 | bool first_arg_is_full_shell_command, int32_t num_resumes) { |
| 332 | error.Clear(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 333 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 334 | if (GetFlags().Test(eLaunchFlagLaunchInShell)) { |
| 335 | if (m_shell) { |
| 336 | std::string shell_executable = m_shell.GetPath(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 337 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 338 | const char **argv = GetArguments().GetConstArgumentVector(); |
| 339 | if (argv == nullptr || argv[0] == nullptr) |
| 340 | return false; |
| 341 | Args shell_arguments; |
| 342 | std::string safe_arg; |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 343 | shell_arguments.AppendArgument(shell_executable); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | const llvm::Triple &triple = GetArchitecture().GetTriple(); |
| 345 | if (triple.getOS() == llvm::Triple::Win32 && |
| 346 | !triple.isWindowsCygwinEnvironment()) |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 347 | shell_arguments.AppendArgument(llvm::StringRef("/C")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | else |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 349 | shell_arguments.AppendArgument(llvm::StringRef("-c")); |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 350 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 351 | StreamString shell_command; |
| 352 | if (will_debug) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 353 | // Add a modified PATH environment variable in case argv[0] is a |
| 354 | // relative path. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 355 | const char *argv0 = argv[0]; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 356 | FileSpec arg_spec(argv0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | if (arg_spec.IsRelative()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 358 | // We have a relative path to our executable which may not work if we |
| 359 | // just try to run "a.out" (without it being converted to "./a.out") |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 360 | FileSpec working_dir = GetWorkingDirectory(); |
| 361 | // Be sure to put quotes around PATH's value in case any paths have |
| 362 | // spaces... |
| 363 | std::string new_path("PATH=\""); |
| 364 | const size_t empty_path_len = new_path.size(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | if (working_dir) { |
| 367 | new_path += working_dir.GetPath(); |
| 368 | } else { |
Pavel Labath | 1d5855b | 2017-01-23 15:56:45 +0000 | [diff] [blame] | 369 | llvm::SmallString<64> cwd; |
| 370 | if (! llvm::sys::fs::current_path(cwd)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 371 | new_path += cwd; |
| 372 | } |
| 373 | std::string curr_path; |
| 374 | if (HostInfo::GetEnvironmentVar("PATH", curr_path)) { |
| 375 | if (new_path.size() > empty_path_len) |
| 376 | new_path += ':'; |
| 377 | new_path += curr_path; |
| 378 | } |
| 379 | new_path += "\" "; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 380 | shell_command.PutCString(new_path); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 381 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 382 | |
| 383 | if (triple.getOS() != llvm::Triple::Win32 || |
| 384 | triple.isWindowsCygwinEnvironment()) |
| 385 | shell_command.PutCString("exec"); |
| 386 | |
| 387 | // Only Apple supports /usr/bin/arch being able to specify the |
| 388 | // architecture |
| 389 | if (GetArchitecture().IsValid() && // Valid architecture |
| 390 | GetArchitecture().GetTriple().getVendor() == |
| 391 | llvm::Triple::Apple && // Apple only |
| 392 | GetArchitecture().GetCore() != |
| 393 | ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h |
| 394 | { |
| 395 | shell_command.Printf(" /usr/bin/arch -arch %s", |
| 396 | GetArchitecture().GetArchitectureName()); |
| 397 | // Set the resume count to 2: |
| 398 | // 1 - stop in shell |
| 399 | // 2 - stop in /usr/bin/arch |
| 400 | // 3 - then we will stop in our program |
| 401 | SetResumeCount(num_resumes + 1); |
| 402 | } else { |
| 403 | // Set the resume count to 1: |
| 404 | // 1 - stop in shell |
| 405 | // 2 - then we will stop in our program |
| 406 | SetResumeCount(num_resumes); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (first_arg_is_full_shell_command) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 411 | // There should only be one argument that is the shell command itself |
| 412 | // to be used as is |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 413 | if (argv[0] && !argv[1]) |
| 414 | shell_command.Printf("%s", argv[0]); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 415 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 416 | return false; |
| 417 | } else { |
| 418 | for (size_t i = 0; argv[i] != nullptr; ++i) { |
| 419 | const char *arg = |
| 420 | Args::GetShellSafeArgument(m_shell, argv[i], safe_arg); |
| 421 | shell_command.Printf(" %s", arg); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 422 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | } |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 424 | shell_arguments.AppendArgument(shell_command.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | m_executable = m_shell; |
| 426 | m_arguments = shell_arguments; |
| 427 | return true; |
| 428 | } else { |
| 429 | error.SetErrorString("invalid shell path"); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 430 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 431 | } else { |
| 432 | error.SetErrorString("not launching in shell"); |
| 433 | } |
| 434 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 435 | } |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 436 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 437 | ListenerSP ProcessLaunchInfo::GetListenerForProcess(Debugger &debugger) { |
| 438 | if (m_listener_sp) |
| 439 | return m_listener_sp; |
| 440 | else |
| 441 | return debugger.GetListener(); |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 442 | } |