blob: bd2e1bc4c4a10f8a7854c891636849a27c1f2a85 [file] [log] [blame]
Todd Fiala6d6b55d2014-06-30 00:30:53 +00001//===-- 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 Fiala2850b1b2014-06-30 23:51:35 +000010#include "lldb/Host/Config.h"
11
Greg Clayton8012cad2014-11-17 19:39:20 +000012#include "lldb/Core/Debugger.h"
Todd Fiala75f47c32014-10-11 21:42:09 +000013#include "lldb/Core/Log.h"
Zachary Turner696b5282014-08-14 16:01:25 +000014#include "lldb/Target/ProcessLaunchInfo.h"
15#include "lldb/Target/FileAction.h"
Todd Fiala6d6b55d2014-06-30 00:30:53 +000016#include "lldb/Target/Target.h"
17
Zachary Turner90aff472015-03-03 23:36:51 +000018#if !defined(_WIN32)
19#include <limits.h>
20#endif
21
Todd Fiala6d6b55d2014-06-30 00:30:53 +000022using namespace lldb;
23using namespace lldb_private;
24
25//----------------------------------------------------------------------------
Todd Fiala6d6b55d2014-06-30 00:30:53 +000026// ProcessLaunchInfo member functions
27//----------------------------------------------------------------------------
28
29ProcessLaunchInfo::ProcessLaunchInfo () :
30 ProcessInfo(),
31 m_working_dir (),
32 m_plugin_name (),
Todd Fiala6d6b55d2014-06-30 00:30:53 +000033 m_flags (0),
34 m_file_actions (),
Zachary Turner44e442b2014-09-12 22:38:39 +000035 m_pty (new lldb_utility::PseudoTerminal),
Todd Fiala6d6b55d2014-06-30 00:30:53 +000036 m_resume_count (0),
37 m_monitor_callback (NULL),
38 m_monitor_callback_baton (NULL),
39 m_monitor_signals (false),
Greg Clayton8012cad2014-11-17 19:39:20 +000040 m_listener_sp (),
Todd Fiala6d6b55d2014-06-30 00:30:53 +000041 m_hijack_listener_sp ()
42{
43}
44
Chaoren Lind3173f32015-05-29 19:52:29 +000045ProcessLaunchInfo::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 Turnercf3f3682014-09-12 23:10:33 +000050 ProcessInfo(),
51 m_working_dir(),
52 m_plugin_name(),
Zachary Turnercf3f3682014-09-12 23:10:33 +000053 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 Clayton8012cad2014-11-17 19:39:20 +000060 m_listener_sp (),
Zachary Turnercf3f3682014-09-12 23:10:33 +000061 m_hijack_listener_sp()
Todd Fiala6d6b55d2014-06-30 00:30:53 +000062{
Chaoren Lind3173f32015-05-29 19:52:29 +000063 if (stdin_file_spec)
Todd Fiala6d6b55d2014-06-30 00:30:53 +000064 {
Zachary Turner696b5282014-08-14 16:01:25 +000065 FileAction file_action;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000066 const bool read = true;
67 const bool write = false;
Chaoren Lind3173f32015-05-29 19:52:29 +000068 if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +000069 AppendFileAction (file_action);
70 }
Chaoren Lind3173f32015-05-29 19:52:29 +000071 if (stdout_file_spec)
Todd Fiala6d6b55d2014-06-30 00:30:53 +000072 {
Zachary Turner696b5282014-08-14 16:01:25 +000073 FileAction file_action;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000074 const bool read = false;
75 const bool write = true;
Chaoren Lind3173f32015-05-29 19:52:29 +000076 if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +000077 AppendFileAction (file_action);
78 }
Chaoren Lind3173f32015-05-29 19:52:29 +000079 if (stderr_file_spec)
Todd Fiala6d6b55d2014-06-30 00:30:53 +000080 {
Zachary Turner696b5282014-08-14 16:01:25 +000081 FileAction file_action;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000082 const bool read = false;
83 const bool write = true;
Chaoren Lind3173f32015-05-29 19:52:29 +000084 if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +000085 AppendFileAction (file_action);
86 }
87 if (working_directory)
88 SetWorkingDirectory(working_directory);
89}
90
91bool
92ProcessLaunchInfo::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
103bool
104ProcessLaunchInfo::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
115bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000116ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec,
117 bool read, bool write)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000118{
119 FileAction file_action;
Chaoren Lind3173f32015-05-29 19:52:29 +0000120 if (file_action.Open(fd, file_spec, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000121 {
122 AppendFileAction (file_action);
123 return true;
124 }
125 return false;
126}
127
128bool
129ProcessLaunchInfo::AppendSuppressFileAction (int fd, bool read, bool write)
130{
131 FileAction file_action;
Chaoren Lind3173f32015-05-29 19:52:29 +0000132 if (file_action.Open(fd, FileSpec{"/dev/null", false}, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000133 {
134 AppendFileAction (file_action);
135 return true;
136 }
137 return false;
138}
139
Zachary Turner696b5282014-08-14 16:01:25 +0000140const FileAction *
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000141ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000142{
143 if (idx < m_file_actions.size())
144 return &m_file_actions[idx];
145 return NULL;
146}
147
Zachary Turner696b5282014-08-14 16:01:25 +0000148const FileAction *
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000149ProcessLaunchInfo::GetFileActionForFD(int fd) const
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000150{
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 Lind3173f32015-05-29 19:52:29 +0000159const FileSpec &
160ProcessLaunchInfo::GetWorkingDirectory() const
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000161{
Chaoren Lind3173f32015-05-29 19:52:29 +0000162 return m_working_dir;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000163}
164
165void
Chaoren Lind3173f32015-05-29 19:52:29 +0000166ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000167{
Chaoren Lind3173f32015-05-29 19:52:29 +0000168 m_working_dir = working_dir;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000169}
170
171const char *
172ProcessLaunchInfo::GetProcessPluginName () const
173{
174 if (m_plugin_name.empty())
175 return NULL;
176 return m_plugin_name.c_str();
177}
178
179void
180ProcessLaunchInfo::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 Turner10687b02014-10-20 17:46:43 +0000188const FileSpec &
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000189ProcessLaunchInfo::GetShell () const
190{
Zachary Turner10687b02014-10-20 17:46:43 +0000191 return m_shell;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000192}
193
194void
Zachary Turner10687b02014-10-20 17:46:43 +0000195ProcessLaunchInfo::SetShell (const FileSpec &shell)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000196{
Zachary Turner10687b02014-10-20 17:46:43 +0000197 m_shell = shell;
198 if (m_shell)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000199 {
Zachary Turner10687b02014-10-20 17:46:43 +0000200 m_shell.ResolveExecutableLocation();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000201 m_flags.Set (lldb::eLaunchFlagLaunchInShell);
202 }
203 else
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000204 m_flags.Clear (lldb::eLaunchFlagLaunchInShell);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000205}
206
207void
208ProcessLaunchInfo::SetLaunchInSeparateProcessGroup (bool separate)
209{
210 if (separate)
211 m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
212 else
213 m_flags.Clear (lldb::eLaunchFlagLaunchInSeparateProcessGroup);
Enrico Granatad7a83a92015-02-10 03:06:24 +0000214}
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000215
Enrico Granatad7a83a92015-02-10 03:06:24 +0000216void
Enrico Granatab38ef8c2015-02-20 22:20:30 +0000217ProcessLaunchInfo::SetShellExpandArguments (bool expand)
Enrico Granatad7a83a92015-02-10 03:06:24 +0000218{
Enrico Granatab38ef8c2015-02-20 22:20:30 +0000219 if (expand)
220 m_flags.Set(lldb::eLaunchFlagShellExpandArguments);
Enrico Granatad7a83a92015-02-10 03:06:24 +0000221 else
Enrico Granatab38ef8c2015-02-20 22:20:30 +0000222 m_flags.Clear(lldb::eLaunchFlagShellExpandArguments);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000223}
224
225void
226ProcessLaunchInfo::Clear ()
227{
228 ProcessInfo::Clear();
Chaoren Lind3173f32015-05-29 19:52:29 +0000229 m_working_dir.Clear();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000230 m_plugin_name.clear();
Zachary Turner10687b02014-10-20 17:46:43 +0000231 m_shell.Clear();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000232 m_flags.Clear();
233 m_file_actions.clear();
234 m_resume_count = 0;
Greg Clayton8012cad2014-11-17 19:39:20 +0000235 m_listener_sp.reset();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000236 m_hijack_listener_sp.reset();
237}
238
239void
240ProcessLaunchInfo::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
249bool
250ProcessLaunchInfo::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
263void
264ProcessLaunchInfo::SetDetachOnError (bool enable)
265{
266 if (enable)
267 m_flags.Set(lldb::eLaunchFlagDetachOnError);
268 else
269 m_flags.Clear(lldb::eLaunchFlagDetachOnError);
270}
271
272void
273ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty)
274{
Todd Fiala75f47c32014-10-11 21:42:09 +0000275 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
276
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000277 // 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 Claytonb8e9b8b2014-10-14 20:18:05 +0000279 if (GetFileActionForFD(STDIN_FILENO) == NULL ||
280 GetFileActionForFD(STDOUT_FILENO) == NULL ||
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000281 GetFileActionForFD(STDERR_FILENO) == NULL)
282 {
Todd Fiala75f47c32014-10-11 21:42:09 +0000283 if (log)
284 log->Printf ("ProcessLaunchInfo::%s at least one of stdin/stdout/stderr was not set, evaluating default handling",
285 __FUNCTION__);
286
Greg Claytonbf91f712015-07-10 18:04:46 +0000287 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 Fiala6d6b55d2014-06-30 00:30:53 +0000294 if (m_flags.Test(eLaunchFlagDisableSTDIO))
295 {
Todd Fiala75f47c32014-10-11 21:42:09 +0000296 if (log)
297 log->Printf ("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding suppression action for stdin, stdout and stderr",
298 __FUNCTION__);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000299 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 Lind3173f32015-05-29 19:52:29 +0000309 FileSpec in_file_spec;
310 FileSpec out_file_spec;
311 FileSpec err_file_spec;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000312 if (target)
313 {
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000314 // 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 Lind3173f32015-05-29 19:52:29 +0000317 in_file_spec = target->GetStandardInputPath();
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000318 if (GetFileActionForFD(STDOUT_FILENO) == NULL)
Chaoren Lind3173f32015-05-29 19:52:29 +0000319 out_file_spec = target->GetStandardOutputPath();
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000320 if (GetFileActionForFD(STDERR_FILENO) == NULL)
Chaoren Lind3173f32015-05-29 19:52:29 +0000321 err_file_spec = target->GetStandardErrorPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000322 }
323
Todd Fiala75f47c32014-10-11 21:42:09 +0000324 if (log)
325 log->Printf ("ProcessLaunchInfo::%s target stdin='%s', target stdout='%s', stderr='%s'",
326 __FUNCTION__,
Chaoren Lind3173f32015-05-29 19:52:29 +0000327 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 Fiala75f47c32014-10-11 21:42:09 +0000330
Chaoren Lind3173f32015-05-29 19:52:29 +0000331 if (in_file_spec)
Todd Fiala75f47c32014-10-11 21:42:09 +0000332 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000333 AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
Todd Fiala75f47c32014-10-11 21:42:09 +0000334 if (log)
335 log->Printf ("ProcessLaunchInfo::%s appended stdin open file action for %s",
Chaoren Lind3173f32015-05-29 19:52:29 +0000336 __FUNCTION__, in_file_spec.GetCString());
Todd Fiala75f47c32014-10-11 21:42:09 +0000337 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000338
Chaoren Lind3173f32015-05-29 19:52:29 +0000339 if (out_file_spec)
Todd Fiala75f47c32014-10-11 21:42:09 +0000340 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000341 AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
Todd Fiala75f47c32014-10-11 21:42:09 +0000342 if (log)
343 log->Printf ("ProcessLaunchInfo::%s appended stdout open file action for %s",
Chaoren Lind3173f32015-05-29 19:52:29 +0000344 __FUNCTION__, out_file_spec.GetCString());
Todd Fiala75f47c32014-10-11 21:42:09 +0000345 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000346
Chaoren Lind3173f32015-05-29 19:52:29 +0000347 if (err_file_spec)
Todd Fiala75f47c32014-10-11 21:42:09 +0000348 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000349 AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
Todd Fiala75f47c32014-10-11 21:42:09 +0000350 if (log)
351 log->Printf ("ProcessLaunchInfo::%s appended stderr open file action for %s",
Chaoren Lind3173f32015-05-29 19:52:29 +0000352 __FUNCTION__, err_file_spec.GetCString());
Todd Fiala75f47c32014-10-11 21:42:09 +0000353 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000354
Chaoren Lind3173f32015-05-29 19:52:29 +0000355 if (default_to_use_pty && (!in_file_spec || !out_file_spec || !err_file_spec))
Todd Fiala75f47c32014-10-11 21:42:09 +0000356 {
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 Turner362a8132015-02-04 19:11:48 +0000361 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 Fiala75f47c32014-10-11 21:42:09 +0000369 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000370 const FileSpec slave_file_spec{m_pty->GetSlaveName(NULL, 0), false};
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000371
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000372 // Only use the slave tty if we don't have anything specified for
373 // input and don't have an action for stdin
Chaoren Lind3173f32015-05-29 19:52:29 +0000374 if (!in_file_spec && GetFileActionForFD(STDIN_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000375 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000376 AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000377 }
378
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000379 // Only use the slave tty if we don't have anything specified for
380 // output and don't have an action for stdout
Chaoren Lind3173f32015-05-29 19:52:29 +0000381 if (!out_file_spec && GetFileActionForFD(STDOUT_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000382 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000383 AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000384 }
385
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000386 // Only use the slave tty if we don't have anything specified for
387 // error and don't have an action for stderr
Chaoren Lind3173f32015-05-29 19:52:29 +0000388 if (!err_file_spec && GetFileActionForFD(STDERR_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000389 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000390 AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000391 }
392 }
393 }
394 }
395 }
396}
397
398
399bool
400ProcessLaunchInfo::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 Turner10687b02014-10-20 17:46:43 +0000410 if (m_shell)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000411 {
Zachary Turner10687b02014-10-20 17:46:43 +0000412 std::string shell_executable = m_shell.GetPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000413
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 Turner10687b02014-10-20 17:46:43 +0000419 shell_arguments.AppendArgument (shell_executable.c_str());
Zachary Turner270e99a2014-12-08 21:36:42 +0000420 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 Fiala6d6b55d2014-06-30 00:30:53 +0000426 StreamString shell_command;
427 if (will_debug)
428 {
429 // Add a modified PATH environment variable in case argv[0]
Zachary Turner270e99a2014-12-08 21:36:42 +0000430 // is a relative path.
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000431 const char *argv0 = argv[0];
Zachary Turner270e99a2014-12-08 21:36:42 +0000432 FileSpec arg_spec(argv0, false);
Chaoren Lin372e9062015-06-09 17:54:27 +0000433 if (arg_spec.IsRelative())
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000434 {
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 Lind3173f32015-05-29 19:52:29 +0000437 FileSpec working_dir = GetWorkingDirectory();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000438 // 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 Lind3173f32015-05-29 19:52:29 +0000442 if (working_dir)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000443 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000444 new_path += working_dir.GetPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000445 }
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 Turner270e99a2014-12-08 21:36:42 +0000464 if (triple.getOS() != llvm::Triple::Win32 || triple.isWindowsCygwinEnvironment())
465 shell_command.PutCString("exec");
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000466
467 // Only Apple supports /usr/bin/arch being able to specify the architecture
Greg Claytonbc766682014-08-12 21:38:59 +0000468 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 Fiala6d6b55d2014-06-30 00:30:53 +0000471 {
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 Turner10687b02014-10-20 17:46:43 +0000505 m_executable = m_shell;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000506 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 Clayton8012cad2014-11-17 19:39:20 +0000520
521Listener &
522ProcessLaunchInfo::GetListenerForProcess (Debugger &debugger)
523{
524 if (m_listener_sp)
525 return *m_listener_sp;
526 else
527 return debugger.GetListener();
528}