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 | |
Todd Fiala | 2850b1b | 2014-06-30 23:51:35 +0000 | [diff] [blame] | 10 | #include "lldb/Host/Config.h" |
| 11 | |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Debugger.h" |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Log.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 14 | #include "lldb/Target/ProcessLaunchInfo.h" |
| 15 | #include "lldb/Target/FileAction.h" |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 16 | #include "lldb/Target/Target.h" |
| 17 | |
Zachary Turner | 90aff47 | 2015-03-03 23:36:51 +0000 | [diff] [blame^] | 18 | #if !defined(_WIN32) |
| 19 | #include <limits.h> |
| 20 | #endif |
| 21 | |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
| 25 | //---------------------------------------------------------------------------- |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 26 | // ProcessLaunchInfo member functions |
| 27 | //---------------------------------------------------------------------------- |
| 28 | |
| 29 | ProcessLaunchInfo::ProcessLaunchInfo () : |
| 30 | ProcessInfo(), |
| 31 | m_working_dir (), |
| 32 | m_plugin_name (), |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 33 | m_flags (0), |
| 34 | m_file_actions (), |
Zachary Turner | 44e442b | 2014-09-12 22:38:39 +0000 | [diff] [blame] | 35 | m_pty (new lldb_utility::PseudoTerminal), |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 36 | m_resume_count (0), |
| 37 | m_monitor_callback (NULL), |
| 38 | m_monitor_callback_baton (NULL), |
| 39 | m_monitor_signals (false), |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 40 | m_listener_sp (), |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 41 | m_hijack_listener_sp () |
| 42 | { |
| 43 | } |
| 44 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 45 | ProcessLaunchInfo::ProcessLaunchInfo(const char *stdin_path, const char *stdout_path, const char *stderr_path, |
Zachary Turner | cf3f368 | 2014-09-12 23:10:33 +0000 | [diff] [blame] | 46 | const char *working_directory, uint32_t launch_flags) : |
| 47 | ProcessInfo(), |
| 48 | m_working_dir(), |
| 49 | m_plugin_name(), |
Zachary Turner | cf3f368 | 2014-09-12 23:10:33 +0000 | [diff] [blame] | 50 | m_flags(launch_flags), |
| 51 | m_file_actions(), |
| 52 | m_pty(new lldb_utility::PseudoTerminal), |
| 53 | m_resume_count(0), |
| 54 | m_monitor_callback(NULL), |
| 55 | m_monitor_callback_baton(NULL), |
| 56 | m_monitor_signals(false), |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 57 | m_listener_sp (), |
Zachary Turner | cf3f368 | 2014-09-12 23:10:33 +0000 | [diff] [blame] | 58 | m_hijack_listener_sp() |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 59 | { |
| 60 | if (stdin_path) |
| 61 | { |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 62 | FileAction file_action; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 63 | const bool read = true; |
| 64 | const bool write = false; |
| 65 | if (file_action.Open(STDIN_FILENO, stdin_path, read, write)) |
| 66 | AppendFileAction (file_action); |
| 67 | } |
| 68 | if (stdout_path) |
| 69 | { |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 70 | FileAction file_action; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 71 | const bool read = false; |
| 72 | const bool write = true; |
| 73 | if (file_action.Open(STDOUT_FILENO, stdout_path, read, write)) |
| 74 | AppendFileAction (file_action); |
| 75 | } |
| 76 | if (stderr_path) |
| 77 | { |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 78 | FileAction file_action; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 79 | const bool read = false; |
| 80 | const bool write = true; |
| 81 | if (file_action.Open(STDERR_FILENO, stderr_path, read, write)) |
| 82 | AppendFileAction (file_action); |
| 83 | } |
| 84 | if (working_directory) |
| 85 | SetWorkingDirectory(working_directory); |
| 86 | } |
| 87 | |
| 88 | bool |
| 89 | ProcessLaunchInfo::AppendCloseFileAction (int fd) |
| 90 | { |
| 91 | FileAction file_action; |
| 92 | if (file_action.Close (fd)) |
| 93 | { |
| 94 | AppendFileAction (file_action); |
| 95 | return true; |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | bool |
| 101 | ProcessLaunchInfo::AppendDuplicateFileAction (int fd, int dup_fd) |
| 102 | { |
| 103 | FileAction file_action; |
| 104 | if (file_action.Duplicate (fd, dup_fd)) |
| 105 | { |
| 106 | AppendFileAction (file_action); |
| 107 | return true; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | bool |
| 113 | ProcessLaunchInfo::AppendOpenFileAction (int fd, const char *path, bool read, bool write) |
| 114 | { |
| 115 | FileAction file_action; |
| 116 | if (file_action.Open (fd, path, read, write)) |
| 117 | { |
| 118 | AppendFileAction (file_action); |
| 119 | return true; |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | bool |
| 125 | ProcessLaunchInfo::AppendSuppressFileAction (int fd, bool read, bool write) |
| 126 | { |
| 127 | FileAction file_action; |
| 128 | if (file_action.Open (fd, "/dev/null", read, write)) |
| 129 | { |
| 130 | AppendFileAction (file_action); |
| 131 | return true; |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 136 | const FileAction * |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 137 | ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 138 | { |
| 139 | if (idx < m_file_actions.size()) |
| 140 | return &m_file_actions[idx]; |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 144 | const FileAction * |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 145 | ProcessLaunchInfo::GetFileActionForFD(int fd) const |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 146 | { |
| 147 | for (size_t idx=0, count=m_file_actions.size(); idx < count; ++idx) |
| 148 | { |
| 149 | if (m_file_actions[idx].GetFD () == fd) |
| 150 | return &m_file_actions[idx]; |
| 151 | } |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | const char * |
| 156 | ProcessLaunchInfo::GetWorkingDirectory () const |
| 157 | { |
| 158 | if (m_working_dir.empty()) |
| 159 | return NULL; |
| 160 | return m_working_dir.c_str(); |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | ProcessLaunchInfo::SetWorkingDirectory (const char *working_dir) |
| 165 | { |
| 166 | if (working_dir && working_dir[0]) |
| 167 | m_working_dir.assign (working_dir); |
| 168 | else |
| 169 | m_working_dir.clear(); |
| 170 | } |
| 171 | |
| 172 | const char * |
| 173 | ProcessLaunchInfo::GetProcessPluginName () const |
| 174 | { |
| 175 | if (m_plugin_name.empty()) |
| 176 | return NULL; |
| 177 | return m_plugin_name.c_str(); |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | ProcessLaunchInfo::SetProcessPluginName (const char *plugin) |
| 182 | { |
| 183 | if (plugin && plugin[0]) |
| 184 | m_plugin_name.assign (plugin); |
| 185 | else |
| 186 | m_plugin_name.clear(); |
| 187 | } |
| 188 | |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 189 | const FileSpec & |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 190 | ProcessLaunchInfo::GetShell () const |
| 191 | { |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 192 | return m_shell; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 196 | ProcessLaunchInfo::SetShell (const FileSpec &shell) |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 197 | { |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 198 | m_shell = shell; |
| 199 | if (m_shell) |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 200 | { |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 201 | m_shell.ResolveExecutableLocation(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 202 | m_flags.Set (lldb::eLaunchFlagLaunchInShell); |
| 203 | } |
| 204 | else |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 205 | m_flags.Clear (lldb::eLaunchFlagLaunchInShell); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void |
| 209 | ProcessLaunchInfo::SetLaunchInSeparateProcessGroup (bool separate) |
| 210 | { |
| 211 | if (separate) |
| 212 | m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup); |
| 213 | else |
| 214 | m_flags.Clear (lldb::eLaunchFlagLaunchInSeparateProcessGroup); |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 215 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 216 | |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 217 | void |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 218 | ProcessLaunchInfo::SetShellExpandArguments (bool expand) |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 219 | { |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 220 | if (expand) |
| 221 | m_flags.Set(lldb::eLaunchFlagShellExpandArguments); |
Enrico Granata | d7a83a9 | 2015-02-10 03:06:24 +0000 | [diff] [blame] | 222 | else |
Enrico Granata | b38ef8c | 2015-02-20 22:20:30 +0000 | [diff] [blame] | 223 | m_flags.Clear(lldb::eLaunchFlagShellExpandArguments); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void |
| 227 | ProcessLaunchInfo::Clear () |
| 228 | { |
| 229 | ProcessInfo::Clear(); |
| 230 | m_working_dir.clear(); |
| 231 | m_plugin_name.clear(); |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 232 | m_shell.Clear(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 233 | m_flags.Clear(); |
| 234 | m_file_actions.clear(); |
| 235 | m_resume_count = 0; |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 236 | m_listener_sp.reset(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 237 | m_hijack_listener_sp.reset(); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | ProcessLaunchInfo::SetMonitorProcessCallback (Host::MonitorChildProcessCallback callback, |
| 242 | void *baton, |
| 243 | bool monitor_signals) |
| 244 | { |
| 245 | m_monitor_callback = callback; |
| 246 | m_monitor_callback_baton = baton; |
| 247 | m_monitor_signals = monitor_signals; |
| 248 | } |
| 249 | |
| 250 | bool |
| 251 | ProcessLaunchInfo::MonitorProcess () const |
| 252 | { |
| 253 | if (m_monitor_callback && ProcessIDIsValid()) |
| 254 | { |
| 255 | Host::StartMonitoringChildProcess (m_monitor_callback, |
| 256 | m_monitor_callback_baton, |
| 257 | GetProcessID(), |
| 258 | m_monitor_signals); |
| 259 | return true; |
| 260 | } |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | ProcessLaunchInfo::SetDetachOnError (bool enable) |
| 266 | { |
| 267 | if (enable) |
| 268 | m_flags.Set(lldb::eLaunchFlagDetachOnError); |
| 269 | else |
| 270 | m_flags.Clear(lldb::eLaunchFlagDetachOnError); |
| 271 | } |
| 272 | |
| 273 | void |
| 274 | ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty) |
| 275 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 276 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 277 | |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 278 | // If nothing for stdin or stdout or stderr was specified, then check the process for any default |
| 279 | // settings that were set with "settings set" |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 280 | if (GetFileActionForFD(STDIN_FILENO) == NULL || |
| 281 | GetFileActionForFD(STDOUT_FILENO) == NULL || |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 282 | GetFileActionForFD(STDERR_FILENO) == NULL) |
| 283 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 284 | if (log) |
| 285 | log->Printf ("ProcessLaunchInfo::%s at least one of stdin/stdout/stderr was not set, evaluating default handling", |
| 286 | __FUNCTION__); |
| 287 | |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 288 | if (m_flags.Test(eLaunchFlagDisableSTDIO)) |
| 289 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 290 | if (log) |
| 291 | log->Printf ("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding suppression action for stdin, stdout and stderr", |
| 292 | __FUNCTION__); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 293 | AppendSuppressFileAction (STDIN_FILENO , true, false); |
| 294 | AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 295 | AppendSuppressFileAction (STDERR_FILENO, false, true); |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | // Check for any values that might have gotten set with any of: |
| 300 | // (lldb) settings set target.input-path |
| 301 | // (lldb) settings set target.output-path |
| 302 | // (lldb) settings set target.error-path |
| 303 | FileSpec in_path; |
| 304 | FileSpec out_path; |
| 305 | FileSpec err_path; |
| 306 | if (target) |
| 307 | { |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 308 | // Only override with the target settings if we don't already have |
| 309 | // an action for in, out or error |
| 310 | if (GetFileActionForFD(STDIN_FILENO) == NULL) |
| 311 | in_path = target->GetStandardInputPath(); |
| 312 | if (GetFileActionForFD(STDOUT_FILENO) == NULL) |
| 313 | out_path = target->GetStandardOutputPath(); |
| 314 | if (GetFileActionForFD(STDERR_FILENO) == NULL) |
| 315 | err_path = target->GetStandardErrorPath(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 318 | if (log) |
| 319 | log->Printf ("ProcessLaunchInfo::%s target stdin='%s', target stdout='%s', stderr='%s'", |
| 320 | __FUNCTION__, |
| 321 | in_path ? in_path.GetPath().c_str () : "<null>", |
| 322 | out_path ? out_path.GetPath().c_str () : "<null>", |
| 323 | err_path ? err_path.GetPath().c_str () : "<null>"); |
| 324 | |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 325 | char path[PATH_MAX]; |
| 326 | if (in_path && in_path.GetPath(path, sizeof(path))) |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 327 | { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 328 | AppendOpenFileAction(STDIN_FILENO, path, true, false); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 329 | if (log) |
| 330 | log->Printf ("ProcessLaunchInfo::%s appended stdin open file action for %s", |
| 331 | __FUNCTION__, |
| 332 | in_path.GetPath().c_str ()); |
| 333 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 334 | |
| 335 | if (out_path && out_path.GetPath(path, sizeof(path))) |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 336 | { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 337 | AppendOpenFileAction(STDOUT_FILENO, path, false, true); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 338 | if (log) |
| 339 | log->Printf ("ProcessLaunchInfo::%s appended stdout open file action for %s", |
| 340 | __FUNCTION__, |
| 341 | out_path.GetPath().c_str ()); |
| 342 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 343 | |
| 344 | if (err_path && err_path.GetPath(path, sizeof(path))) |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 345 | { |
| 346 | if (log) |
| 347 | log->Printf ("ProcessLaunchInfo::%s appended stderr open file action for %s", |
| 348 | __FUNCTION__, |
| 349 | err_path.GetPath().c_str ()); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 350 | AppendOpenFileAction(STDERR_FILENO, path, false, true); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 351 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 352 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 353 | if (default_to_use_pty && (!in_path || !out_path || !err_path)) |
| 354 | { |
| 355 | if (log) |
| 356 | log->Printf ("ProcessLaunchInfo::%s default_to_use_pty is set, and at least one stdin/stderr/stdout is unset, so generating a pty to use for it", |
| 357 | __FUNCTION__); |
| 358 | |
Zachary Turner | 362a813 | 2015-02-04 19:11:48 +0000 | [diff] [blame] | 359 | int open_flags = O_RDWR | O_NOCTTY; |
| 360 | #if !defined(_MSC_VER) |
| 361 | // We really shouldn't be specifying platform specific flags |
| 362 | // that are intended for a system call in generic code. But |
| 363 | // this will have to do for now. |
| 364 | open_flags |= O_CLOEXEC; |
| 365 | #endif |
| 366 | if (m_pty->OpenFirstAvailableMaster(open_flags, NULL, 0)) |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 367 | { |
Zachary Turner | 44e442b | 2014-09-12 22:38:39 +0000 | [diff] [blame] | 368 | const char *slave_path = m_pty->GetSlaveName(NULL, 0); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 369 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 370 | // Only use the slave tty if we don't have anything specified for |
| 371 | // input and don't have an action for stdin |
| 372 | if (!in_path && GetFileActionForFD(STDIN_FILENO) == NULL) |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 373 | { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 374 | AppendOpenFileAction(STDIN_FILENO, slave_path, true, false); |
| 375 | } |
| 376 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 377 | // Only use the slave tty if we don't have anything specified for |
| 378 | // output and don't have an action for stdout |
| 379 | if (!out_path && GetFileActionForFD(STDOUT_FILENO) == NULL) |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 380 | { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 381 | AppendOpenFileAction(STDOUT_FILENO, slave_path, false, true); |
| 382 | } |
| 383 | |
Greg Clayton | b8e9b8b | 2014-10-14 20:18:05 +0000 | [diff] [blame] | 384 | // Only use the slave tty if we don't have anything specified for |
| 385 | // error and don't have an action for stderr |
| 386 | if (!err_path && GetFileActionForFD(STDERR_FILENO) == NULL) |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 387 | { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 388 | AppendOpenFileAction(STDERR_FILENO, slave_path, false, true); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | |
| 397 | bool |
| 398 | ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error, |
| 399 | bool localhost, |
| 400 | bool will_debug, |
| 401 | bool first_arg_is_full_shell_command, |
| 402 | int32_t num_resumes) |
| 403 | { |
| 404 | error.Clear(); |
| 405 | |
| 406 | if (GetFlags().Test (eLaunchFlagLaunchInShell)) |
| 407 | { |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 408 | if (m_shell) |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 409 | { |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 410 | std::string shell_executable = m_shell.GetPath(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 411 | |
| 412 | const char **argv = GetArguments().GetConstArgumentVector (); |
| 413 | if (argv == NULL || argv[0] == NULL) |
| 414 | return false; |
| 415 | Args shell_arguments; |
| 416 | std::string safe_arg; |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 417 | shell_arguments.AppendArgument (shell_executable.c_str()); |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 418 | const llvm::Triple &triple = GetArchitecture().GetTriple(); |
| 419 | if (triple.getOS() == llvm::Triple::Win32 && !triple.isWindowsCygwinEnvironment()) |
| 420 | shell_arguments.AppendArgument("/C"); |
| 421 | else |
| 422 | shell_arguments.AppendArgument("-c"); |
| 423 | |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 424 | StreamString shell_command; |
| 425 | if (will_debug) |
| 426 | { |
| 427 | // Add a modified PATH environment variable in case argv[0] |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 428 | // is a relative path. |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 429 | const char *argv0 = argv[0]; |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 430 | FileSpec arg_spec(argv0, false); |
| 431 | if (arg_spec.IsRelativeToCurrentWorkingDirectory()) |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 432 | { |
| 433 | // We have a relative path to our executable which may not work if |
| 434 | // we just try to run "a.out" (without it being converted to "./a.out") |
| 435 | const char *working_dir = GetWorkingDirectory(); |
| 436 | // Be sure to put quotes around PATH's value in case any paths have spaces... |
| 437 | std::string new_path("PATH=\""); |
| 438 | const size_t empty_path_len = new_path.size(); |
| 439 | |
| 440 | if (working_dir && working_dir[0]) |
| 441 | { |
| 442 | new_path += working_dir; |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | char current_working_dir[PATH_MAX]; |
| 447 | const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir)); |
| 448 | if (cwd && cwd[0]) |
| 449 | new_path += cwd; |
| 450 | } |
| 451 | const char *curr_path = getenv("PATH"); |
| 452 | if (curr_path) |
| 453 | { |
| 454 | if (new_path.size() > empty_path_len) |
| 455 | new_path += ':'; |
| 456 | new_path += curr_path; |
| 457 | } |
| 458 | new_path += "\" "; |
| 459 | shell_command.PutCString(new_path.c_str()); |
| 460 | } |
| 461 | |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 462 | if (triple.getOS() != llvm::Triple::Win32 || triple.isWindowsCygwinEnvironment()) |
| 463 | shell_command.PutCString("exec"); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 464 | |
| 465 | // Only Apple supports /usr/bin/arch being able to specify the architecture |
Greg Clayton | bc76668 | 2014-08-12 21:38:59 +0000 | [diff] [blame] | 466 | if (GetArchitecture().IsValid() && // Valid architecture |
| 467 | GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple && // Apple only |
| 468 | GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 469 | { |
| 470 | shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName()); |
| 471 | // Set the resume count to 2: |
| 472 | // 1 - stop in shell |
| 473 | // 2 - stop in /usr/bin/arch |
| 474 | // 3 - then we will stop in our program |
| 475 | SetResumeCount(num_resumes + 1); |
| 476 | } |
| 477 | else |
| 478 | { |
| 479 | // Set the resume count to 1: |
| 480 | // 1 - stop in shell |
| 481 | // 2 - then we will stop in our program |
| 482 | SetResumeCount(num_resumes); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (first_arg_is_full_shell_command) |
| 487 | { |
| 488 | // There should only be one argument that is the shell command itself to be used as is |
| 489 | if (argv[0] && !argv[1]) |
| 490 | shell_command.Printf("%s", argv[0]); |
| 491 | else |
| 492 | return false; |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | for (size_t i=0; argv[i] != NULL; ++i) |
| 497 | { |
| 498 | const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg); |
| 499 | shell_command.Printf(" %s", arg); |
| 500 | } |
| 501 | } |
| 502 | shell_arguments.AppendArgument (shell_command.GetString().c_str()); |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 503 | m_executable = m_shell; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 504 | m_arguments = shell_arguments; |
| 505 | return true; |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | error.SetErrorString ("invalid shell path"); |
| 510 | } |
| 511 | } |
| 512 | else |
| 513 | { |
| 514 | error.SetErrorString ("not launching in shell"); |
| 515 | } |
| 516 | return false; |
| 517 | } |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 518 | |
| 519 | Listener & |
| 520 | ProcessLaunchInfo::GetListenerForProcess (Debugger &debugger) |
| 521 | { |
| 522 | if (m_listener_sp) |
| 523 | return *m_listener_sp; |
| 524 | else |
| 525 | return debugger.GetListener(); |
| 526 | } |