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