180814287SRaphael Isemann //===-- ProcessLaunchInfo.cpp ---------------------------------------------===//
2eef758e9SPavel Labath //
3eef758e9SPavel Labath // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4eef758e9SPavel Labath // See https://llvm.org/LICENSE.txt for license information.
5eef758e9SPavel Labath // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eef758e9SPavel Labath //
7eef758e9SPavel Labath //===----------------------------------------------------------------------===//
8eef758e9SPavel Labath
9eef758e9SPavel Labath #include <climits>
10eef758e9SPavel Labath
11eef758e9SPavel Labath #include "lldb/Host/Config.h"
12eef758e9SPavel Labath #include "lldb/Host/FileAction.h"
13eef758e9SPavel Labath #include "lldb/Host/FileSystem.h"
14eef758e9SPavel Labath #include "lldb/Host/HostInfo.h"
15eef758e9SPavel Labath #include "lldb/Host/ProcessLaunchInfo.h"
16c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
17eef758e9SPavel Labath #include "lldb/Utility/Log.h"
18eef758e9SPavel Labath #include "lldb/Utility/StreamString.h"
19eef758e9SPavel Labath
20eef758e9SPavel Labath #include "llvm/Support/ConvertUTF.h"
21eef758e9SPavel Labath #include "llvm/Support/FileSystem.h"
22eef758e9SPavel Labath
23eef758e9SPavel Labath #if !defined(_WIN32)
2476e47d48SRaphael Isemann #include <climits>
25eef758e9SPavel Labath #endif
26eef758e9SPavel Labath
27eef758e9SPavel Labath using namespace lldb;
28eef758e9SPavel Labath using namespace lldb_private;
29eef758e9SPavel Labath
30eef758e9SPavel Labath // ProcessLaunchInfo member functions
31eef758e9SPavel Labath
ProcessLaunchInfo()32eef758e9SPavel Labath ProcessLaunchInfo::ProcessLaunchInfo()
33eef758e9SPavel Labath : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(0),
349494c510SJonas Devlieghere m_file_actions(), m_pty(new PseudoTerminal), m_monitor_callback(nullptr),
359494c510SJonas Devlieghere m_listener_sp(), m_hijack_listener_sp(), m_scripted_process_class_name(),
369494c510SJonas Devlieghere m_scripted_process_dictionary_sp() {}
37eef758e9SPavel Labath
ProcessLaunchInfo(const FileSpec & stdin_file_spec,const FileSpec & stdout_file_spec,const FileSpec & stderr_file_spec,const FileSpec & working_directory,uint32_t launch_flags)38eef758e9SPavel Labath ProcessLaunchInfo::ProcessLaunchInfo(const FileSpec &stdin_file_spec,
39eef758e9SPavel Labath const FileSpec &stdout_file_spec,
40eef758e9SPavel Labath const FileSpec &stderr_file_spec,
41eef758e9SPavel Labath const FileSpec &working_directory,
42eef758e9SPavel Labath uint32_t launch_flags)
43eef758e9SPavel Labath : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(launch_flags),
44*28c878aeSShafik Yaghmour m_file_actions(), m_pty(new PseudoTerminal),
453e0ad115SMed Ismail Bennani m_scripted_process_class_name(), m_scripted_process_dictionary_sp() {
46eef758e9SPavel Labath if (stdin_file_spec) {
47eef758e9SPavel Labath FileAction file_action;
48eef758e9SPavel Labath const bool read = true;
49eef758e9SPavel Labath const bool write = false;
50eef758e9SPavel Labath if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write))
51eef758e9SPavel Labath AppendFileAction(file_action);
52eef758e9SPavel Labath }
53eef758e9SPavel Labath if (stdout_file_spec) {
54eef758e9SPavel Labath FileAction file_action;
55eef758e9SPavel Labath const bool read = false;
56eef758e9SPavel Labath const bool write = true;
57eef758e9SPavel Labath if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write))
58eef758e9SPavel Labath AppendFileAction(file_action);
59eef758e9SPavel Labath }
60eef758e9SPavel Labath if (stderr_file_spec) {
61eef758e9SPavel Labath FileAction file_action;
62eef758e9SPavel Labath const bool read = false;
63eef758e9SPavel Labath const bool write = true;
64eef758e9SPavel Labath if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write))
65eef758e9SPavel Labath AppendFileAction(file_action);
66eef758e9SPavel Labath }
67eef758e9SPavel Labath if (working_directory)
68eef758e9SPavel Labath SetWorkingDirectory(working_directory);
69eef758e9SPavel Labath }
70eef758e9SPavel Labath
AppendCloseFileAction(int fd)71eef758e9SPavel Labath bool ProcessLaunchInfo::AppendCloseFileAction(int fd) {
72eef758e9SPavel Labath FileAction file_action;
73eef758e9SPavel Labath if (file_action.Close(fd)) {
74eef758e9SPavel Labath AppendFileAction(file_action);
75eef758e9SPavel Labath return true;
76eef758e9SPavel Labath }
77eef758e9SPavel Labath return false;
78eef758e9SPavel Labath }
79eef758e9SPavel Labath
AppendDuplicateFileAction(int fd,int dup_fd)80eef758e9SPavel Labath bool ProcessLaunchInfo::AppendDuplicateFileAction(int fd, int dup_fd) {
81eef758e9SPavel Labath FileAction file_action;
82eef758e9SPavel Labath if (file_action.Duplicate(fd, dup_fd)) {
83eef758e9SPavel Labath AppendFileAction(file_action);
84eef758e9SPavel Labath return true;
85eef758e9SPavel Labath }
86eef758e9SPavel Labath return false;
87eef758e9SPavel Labath }
88eef758e9SPavel Labath
AppendOpenFileAction(int fd,const FileSpec & file_spec,bool read,bool write)89eef758e9SPavel Labath bool ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec,
90eef758e9SPavel Labath bool read, bool write) {
91eef758e9SPavel Labath FileAction file_action;
92eef758e9SPavel Labath if (file_action.Open(fd, file_spec, read, write)) {
93eef758e9SPavel Labath AppendFileAction(file_action);
94eef758e9SPavel Labath return true;
95eef758e9SPavel Labath }
96eef758e9SPavel Labath return false;
97eef758e9SPavel Labath }
98eef758e9SPavel Labath
AppendSuppressFileAction(int fd,bool read,bool write)99eef758e9SPavel Labath bool ProcessLaunchInfo::AppendSuppressFileAction(int fd, bool read,
100eef758e9SPavel Labath bool write) {
101eef758e9SPavel Labath FileAction file_action;
102eef758e9SPavel Labath if (file_action.Open(fd, FileSpec(FileSystem::DEV_NULL), read, write)) {
103eef758e9SPavel Labath AppendFileAction(file_action);
104eef758e9SPavel Labath return true;
105eef758e9SPavel Labath }
106eef758e9SPavel Labath return false;
107eef758e9SPavel Labath }
108eef758e9SPavel Labath
GetFileActionAtIndex(size_t idx) const109eef758e9SPavel Labath const FileAction *ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const {
110eef758e9SPavel Labath if (idx < m_file_actions.size())
111eef758e9SPavel Labath return &m_file_actions[idx];
112eef758e9SPavel Labath return nullptr;
113eef758e9SPavel Labath }
114eef758e9SPavel Labath
GetFileActionForFD(int fd) const115eef758e9SPavel Labath const FileAction *ProcessLaunchInfo::GetFileActionForFD(int fd) const {
116eef758e9SPavel Labath for (size_t idx = 0, count = m_file_actions.size(); idx < count; ++idx) {
117eef758e9SPavel Labath if (m_file_actions[idx].GetFD() == fd)
118eef758e9SPavel Labath return &m_file_actions[idx];
119eef758e9SPavel Labath }
120eef758e9SPavel Labath return nullptr;
121eef758e9SPavel Labath }
122eef758e9SPavel Labath
GetWorkingDirectory() const123eef758e9SPavel Labath const FileSpec &ProcessLaunchInfo::GetWorkingDirectory() const {
124eef758e9SPavel Labath return m_working_dir;
125eef758e9SPavel Labath }
126eef758e9SPavel Labath
SetWorkingDirectory(const FileSpec & working_dir)127eef758e9SPavel Labath void ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir) {
128eef758e9SPavel Labath m_working_dir = working_dir;
129eef758e9SPavel Labath }
130eef758e9SPavel Labath
GetProcessPluginName() const131eef758e9SPavel Labath const char *ProcessLaunchInfo::GetProcessPluginName() const {
132eef758e9SPavel Labath return (m_plugin_name.empty() ? nullptr : m_plugin_name.c_str());
133eef758e9SPavel Labath }
134eef758e9SPavel Labath
SetProcessPluginName(llvm::StringRef plugin)135eef758e9SPavel Labath void ProcessLaunchInfo::SetProcessPluginName(llvm::StringRef plugin) {
136adcd0268SBenjamin Kramer m_plugin_name = std::string(plugin);
137eef758e9SPavel Labath }
138eef758e9SPavel Labath
GetShell() const139eef758e9SPavel Labath const FileSpec &ProcessLaunchInfo::GetShell() const { return m_shell; }
140eef758e9SPavel Labath
SetShell(const FileSpec & shell)141eef758e9SPavel Labath void ProcessLaunchInfo::SetShell(const FileSpec &shell) {
142eef758e9SPavel Labath m_shell = shell;
143eef758e9SPavel Labath if (m_shell) {
144eef758e9SPavel Labath FileSystem::Instance().ResolveExecutableLocation(m_shell);
145eef758e9SPavel Labath m_flags.Set(lldb::eLaunchFlagLaunchInShell);
146eef758e9SPavel Labath } else
147eef758e9SPavel Labath m_flags.Clear(lldb::eLaunchFlagLaunchInShell);
148eef758e9SPavel Labath }
149eef758e9SPavel Labath
SetLaunchInSeparateProcessGroup(bool separate)150eef758e9SPavel Labath void ProcessLaunchInfo::SetLaunchInSeparateProcessGroup(bool separate) {
151eef758e9SPavel Labath if (separate)
152eef758e9SPavel Labath m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
153eef758e9SPavel Labath else
154eef758e9SPavel Labath m_flags.Clear(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
155eef758e9SPavel Labath }
156eef758e9SPavel Labath
SetShellExpandArguments(bool expand)157eef758e9SPavel Labath void ProcessLaunchInfo::SetShellExpandArguments(bool expand) {
158eef758e9SPavel Labath if (expand)
159eef758e9SPavel Labath m_flags.Set(lldb::eLaunchFlagShellExpandArguments);
160eef758e9SPavel Labath else
161eef758e9SPavel Labath m_flags.Clear(lldb::eLaunchFlagShellExpandArguments);
162eef758e9SPavel Labath }
163eef758e9SPavel Labath
Clear()164eef758e9SPavel Labath void ProcessLaunchInfo::Clear() {
165eef758e9SPavel Labath ProcessInfo::Clear();
166eef758e9SPavel Labath m_working_dir.Clear();
167eef758e9SPavel Labath m_plugin_name.clear();
168eef758e9SPavel Labath m_shell.Clear();
169eef758e9SPavel Labath m_flags.Clear();
170eef758e9SPavel Labath m_file_actions.clear();
171eef758e9SPavel Labath m_resume_count = 0;
172eef758e9SPavel Labath m_listener_sp.reset();
173eef758e9SPavel Labath m_hijack_listener_sp.reset();
1743e0ad115SMed Ismail Bennani m_scripted_process_class_name.clear();
1753e0ad115SMed Ismail Bennani m_scripted_process_dictionary_sp.reset();
176eef758e9SPavel Labath }
177eef758e9SPavel Labath
NoOpMonitorCallback(lldb::pid_t pid,int signal,int status)17812c9c4a8SPavel Labath void ProcessLaunchInfo::NoOpMonitorCallback(lldb::pid_t pid, int signal,
17912c9c4a8SPavel Labath int status) {
180a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Process);
18112c9c4a8SPavel Labath LLDB_LOG(log, "pid = {0}, signal = {1}, status = {2}", pid, signal, status);
182eef758e9SPavel Labath }
183eef758e9SPavel Labath
MonitorProcess() const184eef758e9SPavel Labath bool ProcessLaunchInfo::MonitorProcess() const {
185eef758e9SPavel Labath if (m_monitor_callback && ProcessIDIsValid()) {
18623d10f7aSFangrui Song llvm::Expected<HostThread> maybe_thread =
18712c9c4a8SPavel Labath Host::StartMonitoringChildProcess(m_monitor_callback, GetProcessID());
18823d10f7aSFangrui Song if (!maybe_thread)
189a007a6d8SPavel Labath LLDB_LOG(GetLog(LLDBLog::Host), "failed to launch host thread: {}",
19023d10f7aSFangrui Song llvm::toString(maybe_thread.takeError()));
191eef758e9SPavel Labath return true;
192eef758e9SPavel Labath }
193eef758e9SPavel Labath return false;
194eef758e9SPavel Labath }
195eef758e9SPavel Labath
SetDetachOnError(bool enable)196eef758e9SPavel Labath void ProcessLaunchInfo::SetDetachOnError(bool enable) {
197eef758e9SPavel Labath if (enable)
198eef758e9SPavel Labath m_flags.Set(lldb::eLaunchFlagDetachOnError);
199eef758e9SPavel Labath else
200eef758e9SPavel Labath m_flags.Clear(lldb::eLaunchFlagDetachOnError);
201eef758e9SPavel Labath }
202eef758e9SPavel Labath
SetUpPtyRedirection()203eef758e9SPavel Labath llvm::Error ProcessLaunchInfo::SetUpPtyRedirection() {
204a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Process);
2055c4cb323SPavel Labath
2065c4cb323SPavel Labath bool stdin_free = GetFileActionForFD(STDIN_FILENO) == nullptr;
2075c4cb323SPavel Labath bool stdout_free = GetFileActionForFD(STDOUT_FILENO) == nullptr;
2085c4cb323SPavel Labath bool stderr_free = GetFileActionForFD(STDERR_FILENO) == nullptr;
2095c4cb323SPavel Labath bool any_free = stdin_free || stdout_free || stderr_free;
2105c4cb323SPavel Labath if (!any_free)
2115c4cb323SPavel Labath return llvm::Error::success();
2125c4cb323SPavel Labath
213eef758e9SPavel Labath LLDB_LOG(log, "Generating a pty to use for stdin/out/err");
214eef758e9SPavel Labath
215eef758e9SPavel Labath int open_flags = O_RDWR | O_NOCTTY;
216eef758e9SPavel Labath #if !defined(_WIN32)
217eef758e9SPavel Labath // We really shouldn't be specifying platform specific flags that are
218eef758e9SPavel Labath // intended for a system call in generic code. But this will have to
219eef758e9SPavel Labath // do for now.
220eef758e9SPavel Labath open_flags |= O_CLOEXEC;
221eef758e9SPavel Labath #endif
2226bb123b8SPavel Labath if (llvm::Error Err = m_pty->OpenFirstAvailablePrimary(open_flags))
2236bb123b8SPavel Labath return Err;
2246bb123b8SPavel Labath
2253dfb9498SPavel Labath const FileSpec secondary_file_spec(m_pty->GetSecondaryName());
226eef758e9SPavel Labath
2275c4cb323SPavel Labath if (stdin_free)
22864ec505dSJonas Devlieghere AppendOpenFileAction(STDIN_FILENO, secondary_file_spec, true, false);
229eef758e9SPavel Labath
2305c4cb323SPavel Labath if (stdout_free)
23164ec505dSJonas Devlieghere AppendOpenFileAction(STDOUT_FILENO, secondary_file_spec, false, true);
232eef758e9SPavel Labath
2335c4cb323SPavel Labath if (stderr_free)
23464ec505dSJonas Devlieghere AppendOpenFileAction(STDERR_FILENO, secondary_file_spec, false, true);
235eef758e9SPavel Labath return llvm::Error::success();
236eef758e9SPavel Labath }
237eef758e9SPavel Labath
ConvertArgumentsForLaunchingInShell(Status & error,bool will_debug,bool first_arg_is_full_shell_command,uint32_t num_resumes)238eef758e9SPavel Labath bool ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell(
23945c3fc97SRaphael Isemann Status &error, bool will_debug, bool first_arg_is_full_shell_command,
24045c3fc97SRaphael Isemann uint32_t num_resumes) {
241eef758e9SPavel Labath error.Clear();
242eef758e9SPavel Labath
243eef758e9SPavel Labath if (GetFlags().Test(eLaunchFlagLaunchInShell)) {
244eef758e9SPavel Labath if (m_shell) {
245eef758e9SPavel Labath std::string shell_executable = m_shell.GetPath();
246eef758e9SPavel Labath
247eef758e9SPavel Labath const char **argv = GetArguments().GetConstArgumentVector();
248eef758e9SPavel Labath if (argv == nullptr || argv[0] == nullptr)
249eef758e9SPavel Labath return false;
250eef758e9SPavel Labath Args shell_arguments;
251eef758e9SPavel Labath shell_arguments.AppendArgument(shell_executable);
252eef758e9SPavel Labath const llvm::Triple &triple = GetArchitecture().GetTriple();
253eef758e9SPavel Labath if (triple.getOS() == llvm::Triple::Win32 &&
254eef758e9SPavel Labath !triple.isWindowsCygwinEnvironment())
255eef758e9SPavel Labath shell_arguments.AppendArgument(llvm::StringRef("/C"));
256eef758e9SPavel Labath else
257eef758e9SPavel Labath shell_arguments.AppendArgument(llvm::StringRef("-c"));
258eef758e9SPavel Labath
259eef758e9SPavel Labath StreamString shell_command;
260eef758e9SPavel Labath if (will_debug) {
261eef758e9SPavel Labath // Add a modified PATH environment variable in case argv[0] is a
262eef758e9SPavel Labath // relative path.
263eef758e9SPavel Labath const char *argv0 = argv[0];
264eef758e9SPavel Labath FileSpec arg_spec(argv0);
265eef758e9SPavel Labath if (arg_spec.IsRelative()) {
266eef758e9SPavel Labath // We have a relative path to our executable which may not work if we
267eef758e9SPavel Labath // just try to run "a.out" (without it being converted to "./a.out")
268eef758e9SPavel Labath FileSpec working_dir = GetWorkingDirectory();
269eef758e9SPavel Labath // Be sure to put quotes around PATH's value in case any paths have
270eef758e9SPavel Labath // spaces...
271eef758e9SPavel Labath std::string new_path("PATH=\"");
272eef758e9SPavel Labath const size_t empty_path_len = new_path.size();
273eef758e9SPavel Labath
274eef758e9SPavel Labath if (working_dir) {
275eef758e9SPavel Labath new_path += working_dir.GetPath();
276eef758e9SPavel Labath } else {
277eef758e9SPavel Labath llvm::SmallString<64> cwd;
278eef758e9SPavel Labath if (! llvm::sys::fs::current_path(cwd))
279eef758e9SPavel Labath new_path += cwd;
280eef758e9SPavel Labath }
281eef758e9SPavel Labath std::string curr_path;
282eef758e9SPavel Labath if (HostInfo::GetEnvironmentVar("PATH", curr_path)) {
283eef758e9SPavel Labath if (new_path.size() > empty_path_len)
284eef758e9SPavel Labath new_path += ':';
285eef758e9SPavel Labath new_path += curr_path;
286eef758e9SPavel Labath }
287eef758e9SPavel Labath new_path += "\" ";
288eef758e9SPavel Labath shell_command.PutCString(new_path);
289eef758e9SPavel Labath }
290eef758e9SPavel Labath
291eef758e9SPavel Labath if (triple.getOS() != llvm::Triple::Win32 ||
292eef758e9SPavel Labath triple.isWindowsCygwinEnvironment())
293eef758e9SPavel Labath shell_command.PutCString("exec");
294eef758e9SPavel Labath
295eef758e9SPavel Labath // Only Apple supports /usr/bin/arch being able to specify the
296eef758e9SPavel Labath // architecture
297eef758e9SPavel Labath if (GetArchitecture().IsValid() && // Valid architecture
298eef758e9SPavel Labath GetArchitecture().GetTriple().getVendor() ==
299eef758e9SPavel Labath llvm::Triple::Apple && // Apple only
300eef758e9SPavel Labath GetArchitecture().GetCore() !=
301eef758e9SPavel Labath ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h
302eef758e9SPavel Labath {
303eef758e9SPavel Labath shell_command.Printf(" /usr/bin/arch -arch %s",
304eef758e9SPavel Labath GetArchitecture().GetArchitectureName());
305eef758e9SPavel Labath // Set the resume count to 2:
306eef758e9SPavel Labath // 1 - stop in shell
307eef758e9SPavel Labath // 2 - stop in /usr/bin/arch
308eef758e9SPavel Labath // 3 - then we will stop in our program
309eef758e9SPavel Labath SetResumeCount(num_resumes + 1);
310eef758e9SPavel Labath } else {
311eef758e9SPavel Labath // Set the resume count to 1:
312eef758e9SPavel Labath // 1 - stop in shell
313eef758e9SPavel Labath // 2 - then we will stop in our program
314eef758e9SPavel Labath SetResumeCount(num_resumes);
315eef758e9SPavel Labath }
316eef758e9SPavel Labath }
317eef758e9SPavel Labath
318eef758e9SPavel Labath if (first_arg_is_full_shell_command) {
319eef758e9SPavel Labath // There should only be one argument that is the shell command itself
320eef758e9SPavel Labath // to be used as is
321eef758e9SPavel Labath if (argv[0] && !argv[1])
322eef758e9SPavel Labath shell_command.Printf("%s", argv[0]);
323eef758e9SPavel Labath else
324eef758e9SPavel Labath return false;
325eef758e9SPavel Labath } else {
326eef758e9SPavel Labath for (size_t i = 0; argv[i] != nullptr; ++i) {
327bb1d702eSRaphael Isemann std::string safe_arg = Args::GetShellSafeArgument(m_shell, argv[i]);
328bb1d702eSRaphael Isemann // Add a space to separate this arg from the previous one.
329bb1d702eSRaphael Isemann shell_command.PutCString(" ");
330bb1d702eSRaphael Isemann shell_command.PutCString(safe_arg);
331eef758e9SPavel Labath }
332eef758e9SPavel Labath }
333eef758e9SPavel Labath shell_arguments.AppendArgument(shell_command.GetString());
334eef758e9SPavel Labath m_executable = m_shell;
335eef758e9SPavel Labath m_arguments = shell_arguments;
336eef758e9SPavel Labath return true;
337eef758e9SPavel Labath } else {
338eef758e9SPavel Labath error.SetErrorString("invalid shell path");
339eef758e9SPavel Labath }
340eef758e9SPavel Labath } else {
341eef758e9SPavel Labath error.SetErrorString("not launching in shell");
342eef758e9SPavel Labath }
343eef758e9SPavel Labath return false;
344eef758e9SPavel Labath }
345