blob: 119502203820ce91b646ba164450f7973a45cd1c [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
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000045ProcessLaunchInfo::ProcessLaunchInfo(const char *stdin_path, const char *stdout_path, const char *stderr_path,
Zachary Turnercf3f3682014-09-12 23:10:33 +000046 const char *working_directory, uint32_t launch_flags) :
47 ProcessInfo(),
48 m_working_dir(),
49 m_plugin_name(),
Zachary Turnercf3f3682014-09-12 23:10:33 +000050 m_flags(launch_flags),
51 m_file_actions(),
52 m_pty(new lldb_utility::PseudoTerminal),
53 m_resume_count(0),
54 m_monitor_callback(NULL),
55 m_monitor_callback_baton(NULL),
56 m_monitor_signals(false),
Greg Clayton8012cad2014-11-17 19:39:20 +000057 m_listener_sp (),
Zachary Turnercf3f3682014-09-12 23:10:33 +000058 m_hijack_listener_sp()
Todd Fiala6d6b55d2014-06-30 00:30:53 +000059{
60 if (stdin_path)
61 {
Zachary Turner696b5282014-08-14 16:01:25 +000062 FileAction file_action;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000063 const bool read = true;
64 const bool write = false;
65 if (file_action.Open(STDIN_FILENO, stdin_path, read, write))
66 AppendFileAction (file_action);
67 }
68 if (stdout_path)
69 {
Zachary Turner696b5282014-08-14 16:01:25 +000070 FileAction file_action;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000071 const bool read = false;
72 const bool write = true;
73 if (file_action.Open(STDOUT_FILENO, stdout_path, read, write))
74 AppendFileAction (file_action);
75 }
76 if (stderr_path)
77 {
Zachary Turner696b5282014-08-14 16:01:25 +000078 FileAction file_action;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000079 const bool read = false;
80 const bool write = true;
81 if (file_action.Open(STDERR_FILENO, stderr_path, read, write))
82 AppendFileAction (file_action);
83 }
84 if (working_directory)
85 SetWorkingDirectory(working_directory);
86}
87
88bool
89ProcessLaunchInfo::AppendCloseFileAction (int fd)
90{
91 FileAction file_action;
92 if (file_action.Close (fd))
93 {
94 AppendFileAction (file_action);
95 return true;
96 }
97 return false;
98}
99
100bool
101ProcessLaunchInfo::AppendDuplicateFileAction (int fd, int dup_fd)
102{
103 FileAction file_action;
104 if (file_action.Duplicate (fd, dup_fd))
105 {
106 AppendFileAction (file_action);
107 return true;
108 }
109 return false;
110}
111
112bool
113ProcessLaunchInfo::AppendOpenFileAction (int fd, const char *path, bool read, bool write)
114{
115 FileAction file_action;
116 if (file_action.Open (fd, path, read, write))
117 {
118 AppendFileAction (file_action);
119 return true;
120 }
121 return false;
122}
123
124bool
125ProcessLaunchInfo::AppendSuppressFileAction (int fd, bool read, bool write)
126{
127 FileAction file_action;
128 if (file_action.Open (fd, "/dev/null", read, write))
129 {
130 AppendFileAction (file_action);
131 return true;
132 }
133 return false;
134}
135
Zachary Turner696b5282014-08-14 16:01:25 +0000136const FileAction *
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000137ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000138{
139 if (idx < m_file_actions.size())
140 return &m_file_actions[idx];
141 return NULL;
142}
143
Zachary Turner696b5282014-08-14 16:01:25 +0000144const FileAction *
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000145ProcessLaunchInfo::GetFileActionForFD(int fd) const
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000146{
147 for (size_t idx=0, count=m_file_actions.size(); idx < count; ++idx)
148 {
149 if (m_file_actions[idx].GetFD () == fd)
150 return &m_file_actions[idx];
151 }
152 return NULL;
153}
154
155const char *
156ProcessLaunchInfo::GetWorkingDirectory () const
157{
158 if (m_working_dir.empty())
159 return NULL;
160 return m_working_dir.c_str();
161}
162
163void
164ProcessLaunchInfo::SetWorkingDirectory (const char *working_dir)
165{
166 if (working_dir && working_dir[0])
167 m_working_dir.assign (working_dir);
168 else
169 m_working_dir.clear();
170}
171
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();
230 m_working_dir.clear();
231 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
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000288 if (m_flags.Test(eLaunchFlagDisableSTDIO))
289 {
Todd Fiala75f47c32014-10-11 21:42:09 +0000290 if (log)
291 log->Printf ("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding suppression action for stdin, stdout and stderr",
292 __FUNCTION__);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000293 AppendSuppressFileAction (STDIN_FILENO , true, false);
294 AppendSuppressFileAction (STDOUT_FILENO, false, true);
295 AppendSuppressFileAction (STDERR_FILENO, false, true);
296 }
297 else
298 {
299 // Check for any values that might have gotten set with any of:
300 // (lldb) settings set target.input-path
301 // (lldb) settings set target.output-path
302 // (lldb) settings set target.error-path
303 FileSpec in_path;
304 FileSpec out_path;
305 FileSpec err_path;
306 if (target)
307 {
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000308 // Only override with the target settings if we don't already have
309 // an action for in, out or error
310 if (GetFileActionForFD(STDIN_FILENO) == NULL)
311 in_path = target->GetStandardInputPath();
312 if (GetFileActionForFD(STDOUT_FILENO) == NULL)
313 out_path = target->GetStandardOutputPath();
314 if (GetFileActionForFD(STDERR_FILENO) == NULL)
315 err_path = target->GetStandardErrorPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000316 }
317
Todd Fiala75f47c32014-10-11 21:42:09 +0000318 if (log)
319 log->Printf ("ProcessLaunchInfo::%s target stdin='%s', target stdout='%s', stderr='%s'",
320 __FUNCTION__,
321 in_path ? in_path.GetPath().c_str () : "<null>",
322 out_path ? out_path.GetPath().c_str () : "<null>",
323 err_path ? err_path.GetPath().c_str () : "<null>");
324
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000325 char path[PATH_MAX];
326 if (in_path && in_path.GetPath(path, sizeof(path)))
Todd Fiala75f47c32014-10-11 21:42:09 +0000327 {
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000328 AppendOpenFileAction(STDIN_FILENO, path, true, false);
Todd Fiala75f47c32014-10-11 21:42:09 +0000329 if (log)
330 log->Printf ("ProcessLaunchInfo::%s appended stdin open file action for %s",
331 __FUNCTION__,
332 in_path.GetPath().c_str ());
333 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000334
335 if (out_path && out_path.GetPath(path, sizeof(path)))
Todd Fiala75f47c32014-10-11 21:42:09 +0000336 {
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000337 AppendOpenFileAction(STDOUT_FILENO, path, false, true);
Todd Fiala75f47c32014-10-11 21:42:09 +0000338 if (log)
339 log->Printf ("ProcessLaunchInfo::%s appended stdout open file action for %s",
340 __FUNCTION__,
341 out_path.GetPath().c_str ());
342 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000343
344 if (err_path && err_path.GetPath(path, sizeof(path)))
Todd Fiala75f47c32014-10-11 21:42:09 +0000345 {
346 if (log)
347 log->Printf ("ProcessLaunchInfo::%s appended stderr open file action for %s",
348 __FUNCTION__,
349 err_path.GetPath().c_str ());
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000350 AppendOpenFileAction(STDERR_FILENO, path, false, true);
Todd Fiala75f47c32014-10-11 21:42:09 +0000351 }
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000352
Todd Fiala75f47c32014-10-11 21:42:09 +0000353 if (default_to_use_pty && (!in_path || !out_path || !err_path))
354 {
355 if (log)
356 log->Printf ("ProcessLaunchInfo::%s default_to_use_pty is set, and at least one stdin/stderr/stdout is unset, so generating a pty to use for it",
357 __FUNCTION__);
358
Zachary Turner362a8132015-02-04 19:11:48 +0000359 int open_flags = O_RDWR | O_NOCTTY;
360#if !defined(_MSC_VER)
361 // We really shouldn't be specifying platform specific flags
362 // that are intended for a system call in generic code. But
363 // this will have to do for now.
364 open_flags |= O_CLOEXEC;
365#endif
366 if (m_pty->OpenFirstAvailableMaster(open_flags, NULL, 0))
Todd Fiala75f47c32014-10-11 21:42:09 +0000367 {
Zachary Turner44e442b2014-09-12 22:38:39 +0000368 const char *slave_path = m_pty->GetSlaveName(NULL, 0);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000369
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000370 // Only use the slave tty if we don't have anything specified for
371 // input and don't have an action for stdin
372 if (!in_path && GetFileActionForFD(STDIN_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000373 {
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000374 AppendOpenFileAction(STDIN_FILENO, slave_path, true, false);
375 }
376
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000377 // Only use the slave tty if we don't have anything specified for
378 // output and don't have an action for stdout
379 if (!out_path && GetFileActionForFD(STDOUT_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000380 {
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000381 AppendOpenFileAction(STDOUT_FILENO, slave_path, false, true);
382 }
383
Greg Claytonb8e9b8b2014-10-14 20:18:05 +0000384 // Only use the slave tty if we don't have anything specified for
385 // error and don't have an action for stderr
386 if (!err_path && GetFileActionForFD(STDERR_FILENO) == NULL)
Todd Fiala75f47c32014-10-11 21:42:09 +0000387 {
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000388 AppendOpenFileAction(STDERR_FILENO, slave_path, false, true);
389 }
390 }
391 }
392 }
393 }
394}
395
396
397bool
398ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error,
399 bool localhost,
400 bool will_debug,
401 bool first_arg_is_full_shell_command,
402 int32_t num_resumes)
403{
404 error.Clear();
405
406 if (GetFlags().Test (eLaunchFlagLaunchInShell))
407 {
Zachary Turner10687b02014-10-20 17:46:43 +0000408 if (m_shell)
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000409 {
Zachary Turner10687b02014-10-20 17:46:43 +0000410 std::string shell_executable = m_shell.GetPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000411
412 const char **argv = GetArguments().GetConstArgumentVector ();
413 if (argv == NULL || argv[0] == NULL)
414 return false;
415 Args shell_arguments;
416 std::string safe_arg;
Zachary Turner10687b02014-10-20 17:46:43 +0000417 shell_arguments.AppendArgument (shell_executable.c_str());
Zachary Turner270e99a2014-12-08 21:36:42 +0000418 const llvm::Triple &triple = GetArchitecture().GetTriple();
419 if (triple.getOS() == llvm::Triple::Win32 && !triple.isWindowsCygwinEnvironment())
420 shell_arguments.AppendArgument("/C");
421 else
422 shell_arguments.AppendArgument("-c");
423
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000424 StreamString shell_command;
425 if (will_debug)
426 {
427 // Add a modified PATH environment variable in case argv[0]
Zachary Turner270e99a2014-12-08 21:36:42 +0000428 // is a relative path.
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000429 const char *argv0 = argv[0];
Zachary Turner270e99a2014-12-08 21:36:42 +0000430 FileSpec arg_spec(argv0, false);
431 if (arg_spec.IsRelativeToCurrentWorkingDirectory())
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000432 {
433 // We have a relative path to our executable which may not work if
434 // we just try to run "a.out" (without it being converted to "./a.out")
435 const char *working_dir = GetWorkingDirectory();
436 // Be sure to put quotes around PATH's value in case any paths have spaces...
437 std::string new_path("PATH=\"");
438 const size_t empty_path_len = new_path.size();
439
440 if (working_dir && working_dir[0])
441 {
442 new_path += working_dir;
443 }
444 else
445 {
446 char current_working_dir[PATH_MAX];
447 const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir));
448 if (cwd && cwd[0])
449 new_path += cwd;
450 }
451 const char *curr_path = getenv("PATH");
452 if (curr_path)
453 {
454 if (new_path.size() > empty_path_len)
455 new_path += ':';
456 new_path += curr_path;
457 }
458 new_path += "\" ";
459 shell_command.PutCString(new_path.c_str());
460 }
461
Zachary Turner270e99a2014-12-08 21:36:42 +0000462 if (triple.getOS() != llvm::Triple::Win32 || triple.isWindowsCygwinEnvironment())
463 shell_command.PutCString("exec");
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000464
465 // Only Apple supports /usr/bin/arch being able to specify the architecture
Greg Claytonbc766682014-08-12 21:38:59 +0000466 if (GetArchitecture().IsValid() && // Valid architecture
467 GetArchitecture().GetTriple().getVendor() == llvm::Triple::Apple && // Apple only
468 GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000469 {
470 shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
471 // Set the resume count to 2:
472 // 1 - stop in shell
473 // 2 - stop in /usr/bin/arch
474 // 3 - then we will stop in our program
475 SetResumeCount(num_resumes + 1);
476 }
477 else
478 {
479 // Set the resume count to 1:
480 // 1 - stop in shell
481 // 2 - then we will stop in our program
482 SetResumeCount(num_resumes);
483 }
484 }
485
486 if (first_arg_is_full_shell_command)
487 {
488 // There should only be one argument that is the shell command itself to be used as is
489 if (argv[0] && !argv[1])
490 shell_command.Printf("%s", argv[0]);
491 else
492 return false;
493 }
494 else
495 {
496 for (size_t i=0; argv[i] != NULL; ++i)
497 {
498 const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
499 shell_command.Printf(" %s", arg);
500 }
501 }
502 shell_arguments.AppendArgument (shell_command.GetString().c_str());
Zachary Turner10687b02014-10-20 17:46:43 +0000503 m_executable = m_shell;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000504 m_arguments = shell_arguments;
505 return true;
506 }
507 else
508 {
509 error.SetErrorString ("invalid shell path");
510 }
511 }
512 else
513 {
514 error.SetErrorString ("not launching in shell");
515 }
516 return false;
517}
Greg Clayton8012cad2014-11-17 19:39:20 +0000518
519Listener &
520ProcessLaunchInfo::GetListenerForProcess (Debugger &debugger)
521{
522 if (m_listener_sp)
523 return *m_listener_sp;
524 else
525 return debugger.GetListener();
526}