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/Target/ProcessLaunchInfo.h"
15 #include "lldb/Target/FileAction.h"
16 #include "lldb/Target/Target.h"
23 using namespace lldb_private;
25 //----------------------------------------------------------------------------
26 // ProcessLaunchInfo member functions
27 //----------------------------------------------------------------------------
29 ProcessLaunchInfo::ProcessLaunchInfo () :
35 m_pty (new lldb_utility::PseudoTerminal),
37 m_monitor_callback (NULL),
38 m_monitor_callback_baton (NULL),
39 m_monitor_signals (false),
41 m_hijack_listener_sp ()
45 ProcessLaunchInfo::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) :
53 m_flags(launch_flags),
55 m_pty(new lldb_utility::PseudoTerminal),
57 m_monitor_callback(NULL),
58 m_monitor_callback_baton(NULL),
59 m_monitor_signals(false),
61 m_hijack_listener_sp()
65 FileAction file_action;
66 const bool read = true;
67 const bool write = false;
68 if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write))
69 AppendFileAction (file_action);
73 FileAction file_action;
74 const bool read = false;
75 const bool write = true;
76 if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write))
77 AppendFileAction (file_action);
81 FileAction file_action;
82 const bool read = false;
83 const bool write = true;
84 if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write))
85 AppendFileAction (file_action);
87 if (working_directory)
88 SetWorkingDirectory(working_directory);
92 ProcessLaunchInfo::AppendCloseFileAction (int fd)
94 FileAction file_action;
95 if (file_action.Close (fd))
97 AppendFileAction (file_action);
104 ProcessLaunchInfo::AppendDuplicateFileAction (int fd, int dup_fd)
106 FileAction file_action;
107 if (file_action.Duplicate (fd, dup_fd))
109 AppendFileAction (file_action);
116 ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec,
117 bool read, bool write)
119 FileAction file_action;
120 if (file_action.Open(fd, file_spec, read, write))
122 AppendFileAction (file_action);
129 ProcessLaunchInfo::AppendSuppressFileAction (int fd, bool read, bool write)
131 FileAction file_action;
132 if (file_action.Open(fd, FileSpec{"/dev/null", false}, read, write))
134 AppendFileAction (file_action);
141 ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const
143 if (idx < m_file_actions.size())
144 return &m_file_actions[idx];
149 ProcessLaunchInfo::GetFileActionForFD(int fd) const
151 for (size_t idx=0, count=m_file_actions.size(); idx < count; ++idx)
153 if (m_file_actions[idx].GetFD () == fd)
154 return &m_file_actions[idx];
160 ProcessLaunchInfo::GetWorkingDirectory() const
162 return m_working_dir;
166 ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir)
168 m_working_dir = working_dir;
172 ProcessLaunchInfo::GetProcessPluginName () const
174 if (m_plugin_name.empty())
176 return m_plugin_name.c_str();
180 ProcessLaunchInfo::SetProcessPluginName (const char *plugin)
182 if (plugin && plugin[0])
183 m_plugin_name.assign (plugin);
185 m_plugin_name.clear();
189 ProcessLaunchInfo::GetShell () const
195 ProcessLaunchInfo::SetShell (const FileSpec &shell)
200 m_shell.ResolveExecutableLocation();
201 m_flags.Set (lldb::eLaunchFlagLaunchInShell);
204 m_flags.Clear (lldb::eLaunchFlagLaunchInShell);
208 ProcessLaunchInfo::SetLaunchInSeparateProcessGroup (bool separate)
211 m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
213 m_flags.Clear (lldb::eLaunchFlagLaunchInSeparateProcessGroup);
217 ProcessLaunchInfo::SetShellExpandArguments (bool expand)
220 m_flags.Set(lldb::eLaunchFlagShellExpandArguments);
222 m_flags.Clear(lldb::eLaunchFlagShellExpandArguments);
226 ProcessLaunchInfo::Clear ()
228 ProcessInfo::Clear();
229 m_working_dir.Clear();
230 m_plugin_name.clear();
233 m_file_actions.clear();
235 m_listener_sp.reset();
236 m_hijack_listener_sp.reset();
240 ProcessLaunchInfo::SetMonitorProcessCallback (Host::MonitorChildProcessCallback callback,
242 bool monitor_signals)
244 m_monitor_callback = callback;
245 m_monitor_callback_baton = baton;
246 m_monitor_signals = monitor_signals;
250 ProcessLaunchInfo::MonitorProcess () const
252 if (m_monitor_callback && ProcessIDIsValid())
254 Host::StartMonitoringChildProcess (m_monitor_callback,
255 m_monitor_callback_baton,
264 ProcessLaunchInfo::SetDetachOnError (bool enable)
267 m_flags.Set(lldb::eLaunchFlagDetachOnError);
269 m_flags.Clear(lldb::eLaunchFlagDetachOnError);
273 ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty)
275 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
277 // 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"
279 if (GetFileActionForFD(STDIN_FILENO) == NULL ||
280 GetFileActionForFD(STDOUT_FILENO) == NULL ||
281 GetFileActionForFD(STDERR_FILENO) == NULL)
284 log->Printf ("ProcessLaunchInfo::%s at least one of stdin/stdout/stderr was not set, evaluating default handling",
287 if (m_flags.Test(eLaunchFlagDisableSTDIO))
290 log->Printf ("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding suppression action for stdin, stdout and stderr",
292 AppendSuppressFileAction (STDIN_FILENO , true, false);
293 AppendSuppressFileAction (STDOUT_FILENO, false, true);
294 AppendSuppressFileAction (STDERR_FILENO, false, true);
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
302 FileSpec in_file_spec;
303 FileSpec out_file_spec;
304 FileSpec err_file_spec;
307 // 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)
310 in_file_spec = target->GetStandardInputPath();
311 if (GetFileActionForFD(STDOUT_FILENO) == NULL)
312 out_file_spec = target->GetStandardOutputPath();
313 if (GetFileActionForFD(STDERR_FILENO) == NULL)
314 err_file_spec = target->GetStandardErrorPath();
318 log->Printf ("ProcessLaunchInfo::%s target stdin='%s', target stdout='%s', stderr='%s'",
320 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>");
326 AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
328 log->Printf ("ProcessLaunchInfo::%s appended stdin open file action for %s",
329 __FUNCTION__, in_file_spec.GetCString());
334 AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
336 log->Printf ("ProcessLaunchInfo::%s appended stdout open file action for %s",
337 __FUNCTION__, out_file_spec.GetCString());
342 AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
344 log->Printf ("ProcessLaunchInfo::%s appended stderr open file action for %s",
345 __FUNCTION__, err_file_spec.GetCString());
348 if (default_to_use_pty && (!in_file_spec || !out_file_spec || !err_file_spec))
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",
354 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;
361 if (m_pty->OpenFirstAvailableMaster(open_flags, NULL, 0))
363 const FileSpec slave_file_spec{m_pty->GetSlaveName(NULL, 0), false};
365 // Only use the slave tty if we don't have anything specified for
366 // input and don't have an action for stdin
367 if (!in_file_spec && GetFileActionForFD(STDIN_FILENO) == NULL)
369 AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false);
372 // Only use the slave tty if we don't have anything specified for
373 // output and don't have an action for stdout
374 if (!out_file_spec && GetFileActionForFD(STDOUT_FILENO) == NULL)
376 AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true);
379 // Only use the slave tty if we don't have anything specified for
380 // error and don't have an action for stderr
381 if (!err_file_spec && GetFileActionForFD(STDERR_FILENO) == NULL)
383 AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true);
393 ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error,
396 bool first_arg_is_full_shell_command,
401 if (GetFlags().Test (eLaunchFlagLaunchInShell))
405 std::string shell_executable = m_shell.GetPath();
407 const char **argv = GetArguments().GetConstArgumentVector ();
408 if (argv == NULL || argv[0] == NULL)
410 Args shell_arguments;
411 std::string safe_arg;
412 shell_arguments.AppendArgument (shell_executable.c_str());
413 const llvm::Triple &triple = GetArchitecture().GetTriple();
414 if (triple.getOS() == llvm::Triple::Win32 && !triple.isWindowsCygwinEnvironment())
415 shell_arguments.AppendArgument("/C");
417 shell_arguments.AppendArgument("-c");
419 StreamString shell_command;
422 // Add a modified PATH environment variable in case argv[0]
423 // is a relative path.
424 const char *argv0 = argv[0];
425 FileSpec arg_spec(argv0, false);
426 if (arg_spec.IsRelative())
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")
430 FileSpec working_dir = GetWorkingDirectory();
431 // 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();
437 new_path += working_dir.GetPath();
441 char current_working_dir[PATH_MAX];
442 const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir));
446 const char *curr_path = getenv("PATH");
449 if (new_path.size() > empty_path_len)
451 new_path += curr_path;
454 shell_command.PutCString(new_path.c_str());
457 if (triple.getOS() != llvm::Triple::Win32 || triple.isWindowsCygwinEnvironment())
458 shell_command.PutCString("exec");
460 // Only Apple supports /usr/bin/arch being able to specify the architecture
461 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
465 shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
466 // Set the resume count to 2:
468 // 2 - stop in /usr/bin/arch
469 // 3 - then we will stop in our program
470 SetResumeCount(num_resumes + 1);
474 // Set the resume count to 1:
476 // 2 - then we will stop in our program
477 SetResumeCount(num_resumes);
481 if (first_arg_is_full_shell_command)
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]);
491 for (size_t i=0; argv[i] != NULL; ++i)
493 const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
494 shell_command.Printf(" %s", arg);
497 shell_arguments.AppendArgument (shell_command.GetString().c_str());
498 m_executable = m_shell;
499 m_arguments = shell_arguments;
504 error.SetErrorString ("invalid shell path");
509 error.SetErrorString ("not launching in shell");
515 ProcessLaunchInfo::GetListenerForProcess (Debugger &debugger)
518 return *m_listener_sp;
520 return debugger.GetListener();