blob: ec1c3c6ee0b0f5400389c62c880d4691b2408901 [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
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000287 if (m_flags.Test(eLaunchFlagDisableSTDIO))
288 {
Todd Fiala75f47c32014-10-11 21:42:09 +0000289 if (log)
290 log->Printf ("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding suppression action for stdin, stdout and stderr",
291 __FUNCTION__);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000292 AppendSuppressFileAction (STDIN_FILENO , true, false);
293 AppendSuppressFileAction (STDOUT_FILENO, false, true);
294 AppendSuppressFileAction (STDERR_FILENO, false, true);
295 }
296 else
297 {
298 // Check for any values that might have gotten set with any of:
299 // (lldb) settings set target.input-path
300 // (lldb) settings set target.output-path
301 // (lldb) settings set target.error-path
Chaoren Lind3173f32015-05-29 19:52:29 +0000302 FileSpec in_file_spec;
303 FileSpec out_file_spec;
304 FileSpec err_file_spec;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000305 if (target)
306 {
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000307 // Only override with the target settings if we don't already have
308 // an action for in, out or error
309 if (GetFileActionForFD(STDIN_FILENO) == NULL)
Chaoren Lind3173f32015-05-29 19:52:29 +0000310 in_file_spec = target->GetStandardInputPath();
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000311 if (GetFileActionForFD(STDOUT_FILENO) == NULL)
Chaoren Lind3173f32015-05-29 19:52:29 +0000312 out_file_spec = target->GetStandardOutputPath();
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000313 if (GetFileActionForFD(STDERR_FILENO) == NULL)
Chaoren Lind3173f32015-05-29 19:52:29 +0000314 err_file_spec = target->GetStandardErrorPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000315 }
316
Todd Fiala75f47c32014-10-11 21:42:09 +0000317 if (log)
318 log->Printf ("ProcessLaunchInfo::%s target stdin='%s', target stdout='%s', stderr='%s'",
319 __FUNCTION__,
Chaoren Lind3173f32015-05-29 19:52:29 +0000320 in_file_spec ? in_file_spec.GetCString() : "<null>",
321 out_file_spec ? out_file_spec.GetCString() : "<null>",
322 err_file_spec ? err_file_spec.GetCString() : "<null>");
Todd Fiala75f47c32014-10-11 21:42:09 +0000323
Chaoren Lind3173f32015-05-29 19:52:29 +0000324 if (in_file_spec)
Todd Fiala75f47c32014-10-11 21:42:09 +0000325 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000326 AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
Todd Fiala75f47c32014-10-11 21:42:09 +0000327 if (log)
328 log->Printf ("ProcessLaunchInfo::%s appended stdin open file action for %s",
Chaoren Lind3173f32015-05-29 19:52:29 +0000329 __FUNCTION__, in_file_spec.GetCString());
Todd Fiala75f47c32014-10-11 21:42:09 +0000330 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000331
Chaoren Lind3173f32015-05-29 19:52:29 +0000332 if (out_file_spec)
Todd Fiala75f47c32014-10-11 21:42:09 +0000333 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000334 AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
Todd Fiala75f47c32014-10-11 21:42:09 +0000335 if (log)
336 log->Printf ("ProcessLaunchInfo::%s appended stdout open file action for %s",
Chaoren Lind3173f32015-05-29 19:52:29 +0000337 __FUNCTION__, out_file_spec.GetCString());
Todd Fiala75f47c32014-10-11 21:42:09 +0000338 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000339
Chaoren Lind3173f32015-05-29 19:52:29 +0000340 if (err_file_spec)
Todd Fiala75f47c32014-10-11 21:42:09 +0000341 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000342 AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
Todd Fiala75f47c32014-10-11 21:42:09 +0000343 if (log)
344 log->Printf ("ProcessLaunchInfo::%s appended stderr open file action for %s",
Chaoren Lind3173f32015-05-29 19:52:29 +0000345 __FUNCTION__, err_file_spec.GetCString());
Todd Fiala75f47c32014-10-11 21:42:09 +0000346 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000347
Chaoren Lind3173f32015-05-29 19:52:29 +0000348 if (default_to_use_pty && (!in_file_spec || !out_file_spec || !err_file_spec))
Todd Fiala75f47c32014-10-11 21:42:09 +0000349 {
350 if (log)
351 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",
352 __FUNCTION__);
353
Zachary Turner362a8132015-02-04 19:11:48 +0000354 int open_flags = O_RDWR | O_NOCTTY;
355#if !defined(_MSC_VER)
356 // We really shouldn't be specifying platform specific flags
357 // that are intended for a system call in generic code. But
358 // this will have to do for now.
359 open_flags |= O_CLOEXEC;
360#endif
361 if (m_pty->OpenFirstAvailableMaster(open_flags, NULL, 0))
Todd Fiala75f47c32014-10-11 21:42:09 +0000362 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000363 const FileSpec slave_file_spec{m_pty->GetSlaveName(NULL, 0), false};
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000364
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000365 // Only use the slave tty if we don't have anything specified for
366 // input and don't have an action for stdin
Chaoren Lind3173f32015-05-29 19:52:29 +0000367 if (!in_file_spec && GetFileActionForFD(STDIN_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000368 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000369 AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000370 }
371
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000372 // Only use the slave tty if we don't have anything specified for
373 // output and don't have an action for stdout
Chaoren Lind3173f32015-05-29 19:52:29 +0000374 if (!out_file_spec && GetFileActionForFD(STDOUT_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000375 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000376 AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true);
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 // error and don't have an action for stderr
Chaoren Lind3173f32015-05-29 19:52:29 +0000381 if (!err_file_spec && GetFileActionForFD(STDERR_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000382 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000383 AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000384 }
385 }
386 }
387 }
388 }
389}
390
391
392bool
393ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error,
394 bool localhost,
395 bool will_debug,
396 bool first_arg_is_full_shell_command,
397 int32_t num_resumes)
398{
399 error.Clear();
400
401 if (GetFlags().Test (eLaunchFlagLaunchInShell))
402 {
Zachary Turner10687b02014-10-20 17:46:43 +0000403 if (m_shell)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000404 {
Zachary Turner10687b02014-10-20 17:46:43 +0000405 std::string shell_executable = m_shell.GetPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000406
407 const char **argv = GetArguments().GetConstArgumentVector ();
408 if (argv == NULL || argv[0] == NULL)
409 return false;
410 Args shell_arguments;
411 std::string safe_arg;
Zachary Turner10687b02014-10-20 17:46:43 +0000412 shell_arguments.AppendArgument (shell_executable.c_str());
Zachary Turner270e99a2014-12-08 21:36:42 +0000413 const llvm::Triple &triple = GetArchitecture().GetTriple();
414 if (triple.getOS() == llvm::Triple::Win32 && !triple.isWindowsCygwinEnvironment())
415 shell_arguments.AppendArgument("/C");
416 else
417 shell_arguments.AppendArgument("-c");
418
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000419 StreamString shell_command;
420 if (will_debug)
421 {
422 // Add a modified PATH environment variable in case argv[0]
Zachary Turner270e99a2014-12-08 21:36:42 +0000423 // is a relative path.
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000424 const char *argv0 = argv[0];
Zachary Turner270e99a2014-12-08 21:36:42 +0000425 FileSpec arg_spec(argv0, false);
426 if (arg_spec.IsRelativeToCurrentWorkingDirectory())
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000427 {
428 // We have a relative path to our executable which may not work if
429 // we just try to run "a.out" (without it being converted to "./a.out")
Chaoren Lind3173f32015-05-29 19:52:29 +0000430 FileSpec working_dir = GetWorkingDirectory();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000431 // Be sure to put quotes around PATH's value in case any paths have spaces...
432 std::string new_path("PATH=\"");
433 const size_t empty_path_len = new_path.size();
434
Chaoren Lind3173f32015-05-29 19:52:29 +0000435 if (working_dir)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000436 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000437 new_path += working_dir.GetPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000438 }
439 else
440 {
441 char current_working_dir[PATH_MAX];
442 const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir));
443 if (cwd && cwd[0])
444 new_path += cwd;
445 }
446 const char *curr_path = getenv("PATH");
447 if (curr_path)
448 {
449 if (new_path.size() > empty_path_len)
450 new_path += ':';
451 new_path += curr_path;
452 }
453 new_path += "\" ";
454 shell_command.PutCString(new_path.c_str());
455 }
456
Zachary Turner270e99a2014-12-08 21:36:42 +0000457 if (triple.getOS() != llvm::Triple::Win32 || triple.isWindowsCygwinEnvironment())
458 shell_command.PutCString("exec");
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000459
460 // Only Apple supports /usr/bin/arch being able to specify the architecture
Greg Claytonbc766682014-08-12 21:38:59 +0000461 if (GetArchitecture().IsValid() && // Valid architecture
462 GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple && // Apple only
463 GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000464 {
465 shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
466 // Set the resume count to 2:
467 // 1 - stop in shell
468 // 2 - stop in /usr/bin/arch
469 // 3 - then we will stop in our program
470 SetResumeCount(num_resumes + 1);
471 }
472 else
473 {
474 // Set the resume count to 1:
475 // 1 - stop in shell
476 // 2 - then we will stop in our program
477 SetResumeCount(num_resumes);
478 }
479 }
480
481 if (first_arg_is_full_shell_command)
482 {
483 // There should only be one argument that is the shell command itself to be used as is
484 if (argv[0] && !argv[1])
485 shell_command.Printf("%s", argv[0]);
486 else
487 return false;
488 }
489 else
490 {
491 for (size_t i=0; argv[i] != NULL; ++i)
492 {
493 const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
494 shell_command.Printf(" %s", arg);
495 }
496 }
497 shell_arguments.AppendArgument (shell_command.GetString().c_str());
Zachary Turner10687b02014-10-20 17:46:43 +0000498 m_executable = m_shell;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000499 m_arguments = shell_arguments;
500 return true;
501 }
502 else
503 {
504 error.SetErrorString ("invalid shell path");
505 }
506 }
507 else
508 {
509 error.SetErrorString ("not launching in shell");
510 }
511 return false;
512}
Greg Clayton8012cad2014-11-17 19:39:20 +0000513
514Listener &
515ProcessLaunchInfo::GetListenerForProcess (Debugger &debugger)
516{
517 if (m_listener_sp)
518 return *m_listener_sp;
519 else
520 return debugger.GetListener();
521}