1 //===-- ProcessLaunchInfo.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/Config.h"
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Target/ProcessLaunchInfo.h"
16 #include "lldb/Target/FileAction.h"
17 #include "lldb/Target/Target.h"
24 using namespace lldb_private;
26 //----------------------------------------------------------------------------
27 // ProcessLaunchInfo member functions
28 //----------------------------------------------------------------------------
30 ProcessLaunchInfo::ProcessLaunchInfo () :
36 m_pty (new lldb_utility::PseudoTerminal),
38 m_monitor_callback (NULL),
39 m_monitor_callback_baton (NULL),
40 m_monitor_signals (false),
42 m_hijack_listener_sp ()
46 ProcessLaunchInfo::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) :
54 m_flags(launch_flags),
56 m_pty(new lldb_utility::PseudoTerminal),
58 m_monitor_callback(NULL),
59 m_monitor_callback_baton(NULL),
60 m_monitor_signals(false),
62 m_hijack_listener_sp()
66 FileAction file_action;
67 const bool read = true;
68 const bool write = false;
69 if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write))
70 AppendFileAction (file_action);
74 FileAction file_action;
75 const bool read = false;
76 const bool write = true;
77 if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write))
78 AppendFileAction (file_action);
82 FileAction file_action;
83 const bool read = false;
84 const bool write = true;
85 if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write))
86 AppendFileAction (file_action);
88 if (working_directory)
89 SetWorkingDirectory(working_directory);
93 ProcessLaunchInfo::AppendCloseFileAction (int fd)
95 FileAction file_action;
96 if (file_action.Close (fd))
98 AppendFileAction (file_action);
105 ProcessLaunchInfo::AppendDuplicateFileAction (int fd, int dup_fd)
107 FileAction file_action;
108 if (file_action.Duplicate (fd, dup_fd))
110 AppendFileAction (file_action);
117 ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec,
118 bool read, bool write)
120 FileAction file_action;
121 if (file_action.Open(fd, file_spec, read, write))
123 AppendFileAction (file_action);
130 ProcessLaunchInfo::AppendSuppressFileAction (int fd, bool read, bool write)
132 FileAction file_action;
133 if (file_action.Open(fd, FileSpec{FileSystem::DEV_NULL, false}, read, write))
135 AppendFileAction (file_action);
142 ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const
144 if (idx < m_file_actions.size())
145 return &m_file_actions[idx];
150 ProcessLaunchInfo::GetFileActionForFD(int fd) const
152 for (size_t idx=0, count=m_file_actions.size(); idx < count; ++idx)
154 if (m_file_actions[idx].GetFD () == fd)
155 return &m_file_actions[idx];
161 ProcessLaunchInfo::GetWorkingDirectory() const
163 return m_working_dir;
167 ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir)
169 m_working_dir = working_dir;
173 ProcessLaunchInfo::GetProcessPluginName () const
175 if (m_plugin_name.empty())
177 return m_plugin_name.c_str();
181 ProcessLaunchInfo::SetProcessPluginName (const char *plugin)
183 if (plugin && plugin[0])
184 m_plugin_name.assign (plugin);
186 m_plugin_name.clear();
190 ProcessLaunchInfo::GetShell () const
196 ProcessLaunchInfo::SetShell (const FileSpec &shell)
201 m_shell.ResolveExecutableLocation();
202 m_flags.Set (lldb::eLaunchFlagLaunchInShell);
205 m_flags.Clear (lldb::eLaunchFlagLaunchInShell);
209 ProcessLaunchInfo::SetLaunchInSeparateProcessGroup (bool separate)
212 m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
214 m_flags.Clear (lldb::eLaunchFlagLaunchInSeparateProcessGroup);
218 ProcessLaunchInfo::SetShellExpandArguments (bool expand)
221 m_flags.Set(lldb::eLaunchFlagShellExpandArguments);
223 m_flags.Clear(lldb::eLaunchFlagShellExpandArguments);
227 ProcessLaunchInfo::Clear ()
229 ProcessInfo::Clear();
230 m_working_dir.Clear();
231 m_plugin_name.clear();
234 m_file_actions.clear();
236 m_listener_sp.reset();
237 m_hijack_listener_sp.reset();
241 ProcessLaunchInfo::SetMonitorProcessCallback (Host::MonitorChildProcessCallback callback,
243 bool monitor_signals)
245 m_monitor_callback = callback;
246 m_monitor_callback_baton = baton;
247 m_monitor_signals = monitor_signals;
251 ProcessLaunchInfo::MonitorProcess () const
253 if (m_monitor_callback && ProcessIDIsValid())
255 Host::StartMonitoringChildProcess (m_monitor_callback,
256 m_monitor_callback_baton,
265 ProcessLaunchInfo::SetDetachOnError (bool enable)
268 m_flags.Set(lldb::eLaunchFlagDetachOnError);
270 m_flags.Clear(lldb::eLaunchFlagDetachOnError);
274 ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty)
276 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
278 // 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"
280 if (GetFileActionForFD(STDIN_FILENO) == NULL ||
281 GetFileActionForFD(STDOUT_FILENO) == NULL ||
282 GetFileActionForFD(STDERR_FILENO) == NULL)
285 log->Printf ("ProcessLaunchInfo::%s at least one of stdin/stdout/stderr was not set, evaluating default handling",
288 if (m_flags.Test(eLaunchFlagLaunchInTTY))
290 // Do nothing, if we are launching in a remote terminal
291 // no file actions should be done at all.
295 if (m_flags.Test(eLaunchFlagDisableSTDIO))
298 log->Printf ("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding suppression action for stdin, stdout and stderr",
300 AppendSuppressFileAction (STDIN_FILENO , true, false);
301 AppendSuppressFileAction (STDOUT_FILENO, false, true);
302 AppendSuppressFileAction (STDERR_FILENO, false, true);
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
310 FileSpec in_file_spec;
311 FileSpec out_file_spec;
312 FileSpec err_file_spec;
315 // 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)
318 in_file_spec = target->GetStandardInputPath();
319 if (GetFileActionForFD(STDOUT_FILENO) == NULL)
320 out_file_spec = target->GetStandardOutputPath();
321 if (GetFileActionForFD(STDERR_FILENO) == NULL)
322 err_file_spec = target->GetStandardErrorPath();
326 log->Printf ("ProcessLaunchInfo::%s target stdin='%s', target stdout='%s', stderr='%s'",
328 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>");
334 AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
336 log->Printf ("ProcessLaunchInfo::%s appended stdin open file action for %s",
337 __FUNCTION__, in_file_spec.GetCString());
342 AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
344 log->Printf ("ProcessLaunchInfo::%s appended stdout open file action for %s",
345 __FUNCTION__, out_file_spec.GetCString());
350 AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
352 log->Printf ("ProcessLaunchInfo::%s appended stderr open file action for %s",
353 __FUNCTION__, err_file_spec.GetCString());
356 if (default_to_use_pty && (!in_file_spec || !out_file_spec || !err_file_spec))
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",
362 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;
369 if (m_pty->OpenFirstAvailableMaster(open_flags, NULL, 0))
371 const FileSpec slave_file_spec{m_pty->GetSlaveName(NULL, 0), false};
373 // Only use the slave tty if we don't have anything specified for
374 // input and don't have an action for stdin
375 if (!in_file_spec && GetFileActionForFD(STDIN_FILENO) == NULL)
377 AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false);
380 // Only use the slave tty if we don't have anything specified for
381 // output and don't have an action for stdout
382 if (!out_file_spec && GetFileActionForFD(STDOUT_FILENO) == NULL)
384 AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true);
387 // Only use the slave tty if we don't have anything specified for
388 // error and don't have an action for stderr
389 if (!err_file_spec && GetFileActionForFD(STDERR_FILENO) == NULL)
391 AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true);
401 ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error,
404 bool first_arg_is_full_shell_command,
409 if (GetFlags().Test (eLaunchFlagLaunchInShell))
413 std::string shell_executable = m_shell.GetPath();
415 const char **argv = GetArguments().GetConstArgumentVector ();
416 if (argv == NULL || argv[0] == NULL)
418 Args shell_arguments;
419 std::string safe_arg;
420 shell_arguments.AppendArgument (shell_executable.c_str());
421 const llvm::Triple &triple = GetArchitecture().GetTriple();
422 if (triple.getOS() == llvm::Triple::Win32 && !triple.isWindowsCygwinEnvironment())
423 shell_arguments.AppendArgument("/C");
425 shell_arguments.AppendArgument("-c");
427 StreamString shell_command;
430 // Add a modified PATH environment variable in case argv[0]
431 // is a relative path.
432 const char *argv0 = argv[0];
433 FileSpec arg_spec(argv0, false);
434 if (arg_spec.IsRelative())
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")
438 FileSpec working_dir = GetWorkingDirectory();
439 // 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();
445 new_path += working_dir.GetPath();
449 char current_working_dir[PATH_MAX];
450 const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir));
454 const char *curr_path = getenv("PATH");
457 if (new_path.size() > empty_path_len)
459 new_path += curr_path;
462 shell_command.PutCString(new_path.c_str());
465 if (triple.getOS() != llvm::Triple::Win32 || triple.isWindowsCygwinEnvironment())
466 shell_command.PutCString("exec");
468 // Only Apple supports /usr/bin/arch being able to specify the architecture
469 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
473 shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
474 // Set the resume count to 2:
476 // 2 - stop in /usr/bin/arch
477 // 3 - then we will stop in our program
478 SetResumeCount(num_resumes + 1);
482 // Set the resume count to 1:
484 // 2 - then we will stop in our program
485 SetResumeCount(num_resumes);
489 if (first_arg_is_full_shell_command)
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]);
499 for (size_t i=0; argv[i] != NULL; ++i)
501 const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
502 shell_command.Printf(" %s", arg);
505 shell_arguments.AppendArgument (shell_command.GetString().c_str());
506 m_executable = m_shell;
507 m_arguments = shell_arguments;
512 error.SetErrorString ("invalid shell path");
517 error.SetErrorString ("not launching in shell");
523 ProcessLaunchInfo::GetListenerForProcess (Debugger &debugger)
526 return *m_listener_sp;
528 return debugger.GetListener();