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