blob: fc17fe614c5cd4aca00511967ce37981e74dc969 [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 Turner4eff2d32015-10-14 21:37:36 +000014#include "lldb/Host/FileSystem.h"
Zachary Turner696b5282014-08-14 16:01:25 +000015#include "lldb/Target/ProcessLaunchInfo.h"
16#include "lldb/Target/FileAction.h"
Todd Fiala6d6b55d2014-06-30 00:30:53 +000017#include "lldb/Target/Target.h"
18
Zachary Turner90aff472015-03-03 23:36:51 +000019#if !defined(_WIN32)
20#include <limits.h>
21#endif
22
Todd Fiala6d6b55d2014-06-30 00:30:53 +000023using namespace lldb;
24using namespace lldb_private;
25
26//----------------------------------------------------------------------------
Todd Fiala6d6b55d2014-06-30 00:30:53 +000027// ProcessLaunchInfo member functions
28//----------------------------------------------------------------------------
29
30ProcessLaunchInfo::ProcessLaunchInfo () :
31 ProcessInfo(),
32 m_working_dir (),
33 m_plugin_name (),
Todd Fiala6d6b55d2014-06-30 00:30:53 +000034 m_flags (0),
35 m_file_actions (),
Zachary Turner44e442b2014-09-12 22:38:39 +000036 m_pty (new lldb_utility::PseudoTerminal),
Todd Fiala6d6b55d2014-06-30 00:30:53 +000037 m_resume_count (0),
38 m_monitor_callback (NULL),
39 m_monitor_callback_baton (NULL),
40 m_monitor_signals (false),
Greg Clayton8012cad2014-11-17 19:39:20 +000041 m_listener_sp (),
Todd Fiala6d6b55d2014-06-30 00:30:53 +000042 m_hijack_listener_sp ()
43{
44}
45
Chaoren Lind3173f32015-05-29 19:52:29 +000046ProcessLaunchInfo::ProcessLaunchInfo(const FileSpec &stdin_file_spec,
47 const FileSpec &stdout_file_spec,
48 const FileSpec &stderr_file_spec,
49 const FileSpec &working_directory,
50 uint32_t launch_flags) :
Zachary Turnercf3f3682014-09-12 23:10:33 +000051 ProcessInfo(),
52 m_working_dir(),
53 m_plugin_name(),
Zachary Turnercf3f3682014-09-12 23:10:33 +000054 m_flags(launch_flags),
55 m_file_actions(),
56 m_pty(new lldb_utility::PseudoTerminal),
57 m_resume_count(0),
58 m_monitor_callback(NULL),
59 m_monitor_callback_baton(NULL),
60 m_monitor_signals(false),
Greg Clayton8012cad2014-11-17 19:39:20 +000061 m_listener_sp (),
Zachary Turnercf3f3682014-09-12 23:10:33 +000062 m_hijack_listener_sp()
Todd Fiala6d6b55d2014-06-30 00:30:53 +000063{
Chaoren Lind3173f32015-05-29 19:52:29 +000064 if (stdin_file_spec)
Todd Fiala6d6b55d2014-06-30 00:30:53 +000065 {
Zachary Turner696b5282014-08-14 16:01:25 +000066 FileAction file_action;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000067 const bool read = true;
68 const bool write = false;
Chaoren Lind3173f32015-05-29 19:52:29 +000069 if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +000070 AppendFileAction (file_action);
71 }
Chaoren Lind3173f32015-05-29 19:52:29 +000072 if (stdout_file_spec)
Todd Fiala6d6b55d2014-06-30 00:30:53 +000073 {
Zachary Turner696b5282014-08-14 16:01:25 +000074 FileAction file_action;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000075 const bool read = false;
76 const bool write = true;
Chaoren Lind3173f32015-05-29 19:52:29 +000077 if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +000078 AppendFileAction (file_action);
79 }
Chaoren Lind3173f32015-05-29 19:52:29 +000080 if (stderr_file_spec)
Todd Fiala6d6b55d2014-06-30 00:30:53 +000081 {
Zachary Turner696b5282014-08-14 16:01:25 +000082 FileAction file_action;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000083 const bool read = false;
84 const bool write = true;
Chaoren Lind3173f32015-05-29 19:52:29 +000085 if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +000086 AppendFileAction (file_action);
87 }
88 if (working_directory)
89 SetWorkingDirectory(working_directory);
90}
91
92bool
93ProcessLaunchInfo::AppendCloseFileAction (int fd)
94{
95 FileAction file_action;
96 if (file_action.Close (fd))
97 {
98 AppendFileAction (file_action);
99 return true;
100 }
101 return false;
102}
103
104bool
105ProcessLaunchInfo::AppendDuplicateFileAction (int fd, int dup_fd)
106{
107 FileAction file_action;
108 if (file_action.Duplicate (fd, dup_fd))
109 {
110 AppendFileAction (file_action);
111 return true;
112 }
113 return false;
114}
115
116bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000117ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec,
118 bool read, bool write)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000119{
120 FileAction file_action;
Chaoren Lind3173f32015-05-29 19:52:29 +0000121 if (file_action.Open(fd, file_spec, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000122 {
123 AppendFileAction (file_action);
124 return true;
125 }
126 return false;
127}
128
129bool
130ProcessLaunchInfo::AppendSuppressFileAction (int fd, bool read, bool write)
131{
132 FileAction file_action;
Zachary Turner4eff2d32015-10-14 21:37:36 +0000133 if (file_action.Open(fd, FileSpec{FileSystem::DEV_NULL, false}, read, write))
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000134 {
135 AppendFileAction (file_action);
136 return true;
137 }
138 return false;
139}
140
Zachary Turner696b5282014-08-14 16:01:25 +0000141const FileAction *
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000142ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000143{
144 if (idx < m_file_actions.size())
145 return &m_file_actions[idx];
146 return NULL;
147}
148
Zachary Turner696b5282014-08-14 16:01:25 +0000149const FileAction *
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000150ProcessLaunchInfo::GetFileActionForFD(int fd) const
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000151{
152 for (size_t idx=0, count=m_file_actions.size(); idx < count; ++idx)
153 {
154 if (m_file_actions[idx].GetFD () == fd)
155 return &m_file_actions[idx];
156 }
157 return NULL;
158}
159
Chaoren Lind3173f32015-05-29 19:52:29 +0000160const FileSpec &
161ProcessLaunchInfo::GetWorkingDirectory() const
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000162{
Chaoren Lind3173f32015-05-29 19:52:29 +0000163 return m_working_dir;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000164}
165
166void
Chaoren Lind3173f32015-05-29 19:52:29 +0000167ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000168{
Chaoren Lind3173f32015-05-29 19:52:29 +0000169 m_working_dir = working_dir;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000170}
171
172const char *
173ProcessLaunchInfo::GetProcessPluginName () const
174{
175 if (m_plugin_name.empty())
176 return NULL;
177 return m_plugin_name.c_str();
178}
179
180void
181ProcessLaunchInfo::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 Turner10687b02014-10-20 17:46:43 +0000189const FileSpec &
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000190ProcessLaunchInfo::GetShell () const
191{
Zachary Turner10687b02014-10-20 17:46:43 +0000192 return m_shell;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000193}
194
195void
Zachary Turner10687b02014-10-20 17:46:43 +0000196ProcessLaunchInfo::SetShell (const FileSpec &shell)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000197{
Zachary Turner10687b02014-10-20 17:46:43 +0000198 m_shell = shell;
199 if (m_shell)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000200 {
Zachary Turner10687b02014-10-20 17:46:43 +0000201 m_shell.ResolveExecutableLocation();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000202 m_flags.Set (lldb::eLaunchFlagLaunchInShell);
203 }
204 else
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000205 m_flags.Clear (lldb::eLaunchFlagLaunchInShell);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000206}
207
208void
209ProcessLaunchInfo::SetLaunchInSeparateProcessGroup (bool separate)
210{
211 if (separate)
212 m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
213 else
214 m_flags.Clear (lldb::eLaunchFlagLaunchInSeparateProcessGroup);
Enrico Granatad7a83a92015-02-10 03:06:24 +0000215}
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000216
Enrico Granatad7a83a92015-02-10 03:06:24 +0000217void
Enrico Granatab38ef8c2015-02-20 22:20:30 +0000218ProcessLaunchInfo::SetShellExpandArguments (bool expand)
Enrico Granatad7a83a92015-02-10 03:06:24 +0000219{
Enrico Granatab38ef8c2015-02-20 22:20:30 +0000220 if (expand)
221 m_flags.Set(lldb::eLaunchFlagShellExpandArguments);
Enrico Granatad7a83a92015-02-10 03:06:24 +0000222 else
Enrico Granatab38ef8c2015-02-20 22:20:30 +0000223 m_flags.Clear(lldb::eLaunchFlagShellExpandArguments);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000224}
225
226void
227ProcessLaunchInfo::Clear ()
228{
229 ProcessInfo::Clear();
Chaoren Lind3173f32015-05-29 19:52:29 +0000230 m_working_dir.Clear();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000231 m_plugin_name.clear();
Zachary Turner10687b02014-10-20 17:46:43 +0000232 m_shell.Clear();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000233 m_flags.Clear();
234 m_file_actions.clear();
235 m_resume_count = 0;
Greg Clayton8012cad2014-11-17 19:39:20 +0000236 m_listener_sp.reset();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000237 m_hijack_listener_sp.reset();
238}
239
240void
241ProcessLaunchInfo::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
250bool
251ProcessLaunchInfo::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
264void
265ProcessLaunchInfo::SetDetachOnError (bool enable)
266{
267 if (enable)
268 m_flags.Set(lldb::eLaunchFlagDetachOnError);
269 else
270 m_flags.Clear(lldb::eLaunchFlagDetachOnError);
271}
272
273void
274ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty)
275{
Todd Fiala75f47c32014-10-11 21:42:09 +0000276 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
277
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000278 // 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 Claytonb8e9b8b2014-10-14 20:18:05 +0000280 if (GetFileActionForFD(STDIN_FILENO) == NULL ||
281 GetFileActionForFD(STDOUT_FILENO) == NULL ||
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000282 GetFileActionForFD(STDERR_FILENO) == NULL)
283 {
Todd Fiala75f47c32014-10-11 21:42:09 +0000284 if (log)
285 log->Printf ("ProcessLaunchInfo::%s at least one of stdin/stdout/stderr was not set, evaluating default handling",
286 __FUNCTION__);
287
Greg Claytonbf91f712015-07-10 18:04:46 +0000288 if (m_flags.Test(eLaunchFlagLaunchInTTY))
289 {
290 // Do nothing, if we are launching in a remote terminal
291 // no file actions should be done at all.
292 return;
293 }
294
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000295 if (m_flags.Test(eLaunchFlagDisableSTDIO))
296 {
Todd Fiala75f47c32014-10-11 21:42:09 +0000297 if (log)
298 log->Printf ("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding suppression action for stdin, stdout and stderr",
299 __FUNCTION__);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000300 AppendSuppressFileAction (STDIN_FILENO , true, false);
301 AppendSuppressFileAction (STDOUT_FILENO, false, true);
302 AppendSuppressFileAction (STDERR_FILENO, false, true);
303 }
304 else
305 {
306 // Check for any values that might have gotten set with any of:
307 // (lldb) settings set target.input-path
308 // (lldb) settings set target.output-path
309 // (lldb) settings set target.error-path
Chaoren Lind3173f32015-05-29 19:52:29 +0000310 FileSpec in_file_spec;
311 FileSpec out_file_spec;
312 FileSpec err_file_spec;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000313 if (target)
314 {
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000315 // Only override with the target settings if we don't already have
316 // an action for in, out or error
317 if (GetFileActionForFD(STDIN_FILENO) == NULL)
Chaoren Lind3173f32015-05-29 19:52:29 +0000318 in_file_spec = target->GetStandardInputPath();
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000319 if (GetFileActionForFD(STDOUT_FILENO) == NULL)
Chaoren Lind3173f32015-05-29 19:52:29 +0000320 out_file_spec = target->GetStandardOutputPath();
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000321 if (GetFileActionForFD(STDERR_FILENO) == NULL)
Chaoren Lind3173f32015-05-29 19:52:29 +0000322 err_file_spec = target->GetStandardErrorPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000323 }
324
Todd Fiala75f47c32014-10-11 21:42:09 +0000325 if (log)
326 log->Printf ("ProcessLaunchInfo::%s target stdin='%s', target stdout='%s', stderr='%s'",
327 __FUNCTION__,
Chaoren Lind3173f32015-05-29 19:52:29 +0000328 in_file_spec ? in_file_spec.GetCString() : "<null>",
329 out_file_spec ? out_file_spec.GetCString() : "<null>",
330 err_file_spec ? err_file_spec.GetCString() : "<null>");
Todd Fiala75f47c32014-10-11 21:42:09 +0000331
Chaoren Lind3173f32015-05-29 19:52:29 +0000332 if (in_file_spec)
Todd Fiala75f47c32014-10-11 21:42:09 +0000333 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000334 AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
Todd Fiala75f47c32014-10-11 21:42:09 +0000335 if (log)
336 log->Printf ("ProcessLaunchInfo::%s appended stdin open file action for %s",
Chaoren Lind3173f32015-05-29 19:52:29 +0000337 __FUNCTION__, in_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 (out_file_spec)
Todd Fiala75f47c32014-10-11 21:42:09 +0000341 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000342 AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
Todd Fiala75f47c32014-10-11 21:42:09 +0000343 if (log)
344 log->Printf ("ProcessLaunchInfo::%s appended stdout open file action for %s",
Chaoren Lind3173f32015-05-29 19:52:29 +0000345 __FUNCTION__, out_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 (err_file_spec)
Todd Fiala75f47c32014-10-11 21:42:09 +0000349 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000350 AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
Todd Fiala75f47c32014-10-11 21:42:09 +0000351 if (log)
352 log->Printf ("ProcessLaunchInfo::%s appended stderr open file action for %s",
Chaoren Lind3173f32015-05-29 19:52:29 +0000353 __FUNCTION__, err_file_spec.GetCString());
Todd Fiala75f47c32014-10-11 21:42:09 +0000354 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000355
Chaoren Lind3173f32015-05-29 19:52:29 +0000356 if (default_to_use_pty && (!in_file_spec || !out_file_spec || !err_file_spec))
Todd Fiala75f47c32014-10-11 21:42:09 +0000357 {
358 if (log)
359 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",
360 __FUNCTION__);
361
Zachary Turner362a8132015-02-04 19:11:48 +0000362 int open_flags = O_RDWR | O_NOCTTY;
363#if !defined(_MSC_VER)
364 // We really shouldn't be specifying platform specific flags
365 // that are intended for a system call in generic code. But
366 // this will have to do for now.
367 open_flags |= O_CLOEXEC;
368#endif
369 if (m_pty->OpenFirstAvailableMaster(open_flags, NULL, 0))
Todd Fiala75f47c32014-10-11 21:42:09 +0000370 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000371 const FileSpec slave_file_spec{m_pty->GetSlaveName(NULL, 0), false};
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000372
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000373 // Only use the slave tty if we don't have anything specified for
374 // input and don't have an action for stdin
Chaoren Lind3173f32015-05-29 19:52:29 +0000375 if (!in_file_spec && GetFileActionForFD(STDIN_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000376 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000377 AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000378 }
379
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000380 // Only use the slave tty if we don't have anything specified for
381 // output and don't have an action for stdout
Chaoren Lind3173f32015-05-29 19:52:29 +0000382 if (!out_file_spec && GetFileActionForFD(STDOUT_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000383 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000384 AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000385 }
386
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000387 // Only use the slave tty if we don't have anything specified for
388 // error and don't have an action for stderr
Chaoren Lind3173f32015-05-29 19:52:29 +0000389 if (!err_file_spec && GetFileActionForFD(STDERR_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000390 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000391 AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000392 }
393 }
394 }
395 }
396 }
397}
398
399
400bool
401ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error,
402 bool localhost,
403 bool will_debug,
404 bool first_arg_is_full_shell_command,
405 int32_t num_resumes)
406{
407 error.Clear();
408
409 if (GetFlags().Test (eLaunchFlagLaunchInShell))
410 {
Zachary Turner10687b02014-10-20 17:46:43 +0000411 if (m_shell)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000412 {
Zachary Turner10687b02014-10-20 17:46:43 +0000413 std::string shell_executable = m_shell.GetPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000414
415 const char **argv = GetArguments().GetConstArgumentVector ();
416 if (argv == NULL || argv[0] == NULL)
417 return false;
418 Args shell_arguments;
419 std::string safe_arg;
Zachary Turner10687b02014-10-20 17:46:43 +0000420 shell_arguments.AppendArgument (shell_executable.c_str());
Zachary Turner270e99a2014-12-08 21:36:42 +0000421 const llvm::Triple &triple = GetArchitecture().GetTriple();
422 if (triple.getOS() == llvm::Triple::Win32 && !triple.isWindowsCygwinEnvironment())
423 shell_arguments.AppendArgument("/C");
424 else
425 shell_arguments.AppendArgument("-c");
426
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000427 StreamString shell_command;
428 if (will_debug)
429 {
430 // Add a modified PATH environment variable in case argv[0]
Zachary Turner270e99a2014-12-08 21:36:42 +0000431 // is a relative path.
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000432 const char *argv0 = argv[0];
Zachary Turner270e99a2014-12-08 21:36:42 +0000433 FileSpec arg_spec(argv0, false);
Chaoren Lin372e9062015-06-09 17:54:27 +0000434 if (arg_spec.IsRelative())
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000435 {
436 // We have a relative path to our executable which may not work if
437 // we just try to run "a.out" (without it being converted to "./a.out")
Chaoren Lind3173f32015-05-29 19:52:29 +0000438 FileSpec working_dir = GetWorkingDirectory();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000439 // Be sure to put quotes around PATH's value in case any paths have spaces...
440 std::string new_path("PATH=\"");
441 const size_t empty_path_len = new_path.size();
442
Chaoren Lind3173f32015-05-29 19:52:29 +0000443 if (working_dir)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000444 {
Chaoren Lind3173f32015-05-29 19:52:29 +0000445 new_path += working_dir.GetPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000446 }
447 else
448 {
449 char current_working_dir[PATH_MAX];
450 const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir));
451 if (cwd && cwd[0])
452 new_path += cwd;
453 }
454 const char *curr_path = getenv("PATH");
455 if (curr_path)
456 {
457 if (new_path.size() > empty_path_len)
458 new_path += ':';
459 new_path += curr_path;
460 }
461 new_path += "\" ";
462 shell_command.PutCString(new_path.c_str());
463 }
464
Zachary Turner270e99a2014-12-08 21:36:42 +0000465 if (triple.getOS() != llvm::Triple::Win32 || triple.isWindowsCygwinEnvironment())
466 shell_command.PutCString("exec");
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000467
468 // Only Apple supports /usr/bin/arch being able to specify the architecture
Greg Claytonbc766682014-08-12 21:38:59 +0000469 if (GetArchitecture().IsValid() && // Valid architecture
470 GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple && // Apple only
471 GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000472 {
473 shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
474 // Set the resume count to 2:
475 // 1 - stop in shell
476 // 2 - stop in /usr/bin/arch
477 // 3 - then we will stop in our program
478 SetResumeCount(num_resumes + 1);
479 }
480 else
481 {
482 // Set the resume count to 1:
483 // 1 - stop in shell
484 // 2 - then we will stop in our program
485 SetResumeCount(num_resumes);
486 }
487 }
488
489 if (first_arg_is_full_shell_command)
490 {
491 // There should only be one argument that is the shell command itself to be used as is
492 if (argv[0] && !argv[1])
493 shell_command.Printf("%s", argv[0]);
494 else
495 return false;
496 }
497 else
498 {
499 for (size_t i=0; argv[i] != NULL; ++i)
500 {
501 const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
502 shell_command.Printf(" %s", arg);
503 }
504 }
505 shell_arguments.AppendArgument (shell_command.GetString().c_str());
Zachary Turner10687b02014-10-20 17:46:43 +0000506 m_executable = m_shell;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000507 m_arguments = shell_arguments;
508 return true;
509 }
510 else
511 {
512 error.SetErrorString ("invalid shell path");
513 }
514 }
515 else
516 {
517 error.SetErrorString ("not launching in shell");
518 }
519 return false;
520}
Greg Clayton8012cad2014-11-17 19:39:20 +0000521
522Listener &
523ProcessLaunchInfo::GetListenerForProcess (Debugger &debugger)
524{
525 if (m_listener_sp)
526 return *m_listener_sp;
527 else
528 return debugger.GetListener();
529}