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