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