blob: ac1eba04f0cb83a119352e0a94f8ee4d4efb4f5d [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
Eugene Zelenko9394d7722016-02-18 00:10:17 +000010#include <climits>
Todd Fiala2850b1b2014-06-30 23:51:35 +000011
Zachary Turner190fadc2016-03-22 17:58:09 +000012#include "lldb/Host/Config.h"
Zachary Turner4eff2d32015-10-14 21:37:36 +000013#include "lldb/Host/FileSystem.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000014#include "lldb/Host/HostInfo.h"
Zachary Turner696b5282014-08-14 16:01:25 +000015#include "lldb/Target/FileAction.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000016#include "lldb/Target/ProcessLaunchInfo.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000017#include "lldb/Utility/Log.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000018#include "lldb/Utility/StreamString.h"
Todd Fiala6d6b55d2014-06-30 00:30:53 +000019
Zachary Turner190fadc2016-03-22 17:58:09 +000020#include "llvm/Support/ConvertUTF.h"
Pavel Labath1d5855b2017-01-23 15:56:45 +000021#include "llvm/Support/FileSystem.h"
Zachary Turner190fadc2016-03-22 17:58:09 +000022
23#if !defined(_WIN32)
24#include <limits.h>
25#endif
26
Todd Fiala6d6b55d2014-06-30 00:30:53 +000027using namespace lldb;
28using namespace lldb_private;
29
30//----------------------------------------------------------------------------
Todd Fiala6d6b55d2014-06-30 00:30:53 +000031// ProcessLaunchInfo member functions
32//----------------------------------------------------------------------------
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034ProcessLaunchInfo::ProcessLaunchInfo()
35 : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(0),
Pavel Labath07d6f882017-12-11 10:09:14 +000036 m_file_actions(), m_pty(new PseudoTerminal), m_resume_count(0),
37 m_monitor_callback(nullptr), m_monitor_callback_baton(nullptr),
38 m_monitor_signals(false), m_listener_sp(), m_hijack_listener_sp() {}
Todd Fiala6d6b55d2014-06-30 00:30:53 +000039
Chaoren Lind3173f32015-05-29 19:52:29 +000040ProcessLaunchInfo::ProcessLaunchInfo(const FileSpec &stdin_file_spec,
41 const FileSpec &stdout_file_spec,
42 const FileSpec &stderr_file_spec,
43 const FileSpec &working_directory,
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 uint32_t launch_flags)
45 : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(launch_flags),
Pavel Labath07d6f882017-12-11 10:09:14 +000046 m_file_actions(), m_pty(new PseudoTerminal), m_resume_count(0),
47 m_monitor_callback(nullptr), m_monitor_callback_baton(nullptr),
48 m_monitor_signals(false), m_listener_sp(), m_hijack_listener_sp() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 if (stdin_file_spec) {
Todd Fiala6d6b55d2014-06-30 00:30:53 +000050 FileAction file_action;
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 const bool read = true;
52 const bool write = false;
53 if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write))
54 AppendFileAction(file_action);
55 }
56 if (stdout_file_spec) {
Todd Fiala6d6b55d2014-06-30 00:30:53 +000057 FileAction file_action;
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 const bool read = false;
59 const bool write = true;
60 if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write))
61 AppendFileAction(file_action);
62 }
63 if (stderr_file_spec) {
Todd Fiala6d6b55d2014-06-30 00:30:53 +000064 FileAction file_action;
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 const bool read = false;
66 const bool write = true;
67 if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write))
68 AppendFileAction(file_action);
69 }
70 if (working_directory)
71 SetWorkingDirectory(working_directory);
Todd Fiala6d6b55d2014-06-30 00:30:53 +000072}
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074bool ProcessLaunchInfo::AppendCloseFileAction(int fd) {
75 FileAction file_action;
76 if (file_action.Close(fd)) {
77 AppendFileAction(file_action);
78 return true;
79 }
80 return false;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000081}
82
Kate Stoneb9c1b512016-09-06 20:57:50 +000083bool ProcessLaunchInfo::AppendDuplicateFileAction(int fd, int dup_fd) {
84 FileAction file_action;
85 if (file_action.Duplicate(fd, dup_fd)) {
86 AppendFileAction(file_action);
87 return true;
88 }
89 return false;
Todd Fiala6d6b55d2014-06-30 00:30:53 +000090}
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092bool ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec,
93 bool read, bool write) {
94 FileAction file_action;
95 if (file_action.Open(fd, file_spec, read, write)) {
96 AppendFileAction(file_action);
97 return true;
98 }
99 return false;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000100}
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102bool ProcessLaunchInfo::AppendSuppressFileAction(int fd, bool read,
103 bool write) {
104 FileAction file_action;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000105 if (file_action.Open(fd, FileSpec(FileSystem::DEV_NULL), read, write)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 AppendFileAction(file_action);
107 return true;
108 }
109 return false;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112const FileAction *ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const {
113 if (idx < m_file_actions.size())
114 return &m_file_actions[idx];
115 return nullptr;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000116}
117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118const FileAction *ProcessLaunchInfo::GetFileActionForFD(int fd) const {
119 for (size_t idx = 0, count = m_file_actions.size(); idx < count; ++idx) {
120 if (m_file_actions[idx].GetFD() == fd)
121 return &m_file_actions[idx];
122 }
123 return nullptr;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000124}
125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126const FileSpec &ProcessLaunchInfo::GetWorkingDirectory() const {
127 return m_working_dir;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000128}
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130void ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir) {
131 m_working_dir = working_dir;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134const char *ProcessLaunchInfo::GetProcessPluginName() const {
135 return (m_plugin_name.empty() ? nullptr : m_plugin_name.c_str());
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000136}
137
Zachary Turnerfe114832016-11-12 16:56:47 +0000138void ProcessLaunchInfo::SetProcessPluginName(llvm::StringRef plugin) {
139 m_plugin_name = plugin;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142const FileSpec &ProcessLaunchInfo::GetShell() const { return m_shell; }
143
144void ProcessLaunchInfo::SetShell(const FileSpec &shell) {
145 m_shell = shell;
146 if (m_shell) {
Jonas Devlieghere2c22c802018-11-01 17:09:22 +0000147 FileSystem::Instance().ResolveExecutableLocation(m_shell);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 m_flags.Set(lldb::eLaunchFlagLaunchInShell);
149 } else
150 m_flags.Clear(lldb::eLaunchFlagLaunchInShell);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000151}
152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153void ProcessLaunchInfo::SetLaunchInSeparateProcessGroup(bool separate) {
154 if (separate)
155 m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
156 else
157 m_flags.Clear(lldb::eLaunchFlagLaunchInSeparateProcessGroup);
158}
159
160void ProcessLaunchInfo::SetShellExpandArguments(bool expand) {
161 if (expand)
162 m_flags.Set(lldb::eLaunchFlagShellExpandArguments);
163 else
164 m_flags.Clear(lldb::eLaunchFlagShellExpandArguments);
165}
166
167void ProcessLaunchInfo::Clear() {
168 ProcessInfo::Clear();
169 m_working_dir.Clear();
170 m_plugin_name.clear();
171 m_shell.Clear();
172 m_flags.Clear();
173 m_file_actions.clear();
174 m_resume_count = 0;
175 m_listener_sp.reset();
176 m_hijack_listener_sp.reset();
177}
178
179void ProcessLaunchInfo::SetMonitorProcessCallback(
180 const Host::MonitorChildProcessCallback &callback, bool monitor_signals) {
181 m_monitor_callback = callback;
182 m_monitor_signals = monitor_signals;
183}
184
Pavel Labath245dd2e2018-05-15 13:42:26 +0000185bool 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192bool ProcessLaunchInfo::MonitorProcess() const {
193 if (m_monitor_callback && ProcessIDIsValid()) {
194 Host::StartMonitoringChildProcess(m_monitor_callback, GetProcessID(),
195 m_monitor_signals);
196 return true;
197 }
198 return false;
199}
200
201void ProcessLaunchInfo::SetDetachOnError(bool enable) {
202 if (enable)
203 m_flags.Set(lldb::eLaunchFlagDetachOnError);
204 else
205 m_flags.Clear(lldb::eLaunchFlagDetachOnError);
206}
207
Pavel Labath8e55dde2019-01-08 11:55:19 +0000208llvm::Error ProcessLaunchInfo::SetUpPtyRedirection() {
209 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS);
210 LLDB_LOG(log, "Generating a pty to use for stdin/out/err");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211
Pavel Labath8e55dde2019-01-08 11:55:19 +0000212 int open_flags = O_RDWR | O_NOCTTY;
Hafiz Abid Qadeerf6ee79c2016-12-15 15:00:41 +0000213#if !defined(_WIN32)
Pavel Labath8e55dde2019-01-08 11:55:19 +0000214 // We really shouldn't be specifying platform specific flags that are
215 // intended for a system call in generic code. But this will have to
216 // do for now.
217 open_flags |= O_CLOEXEC;
Zachary Turner362a8132015-02-04 19:11:48 +0000218#endif
Pavel Labath8e55dde2019-01-08 11:55:19 +0000219 if (!m_pty->OpenFirstAvailableMaster(open_flags, nullptr, 0)) {
220 return llvm::createStringError(llvm::inconvertibleErrorCode(),
221 "PTY::OpenFirstAvailableMaster failed");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 }
Pavel Labath8e55dde2019-01-08 11:55:19 +0000223 const FileSpec slave_file_spec(m_pty->GetSlaveName(nullptr, 0));
224
225 // Only use the slave tty if we don't have anything specified for
226 // input and don't have an action for stdin
227 if (GetFileActionForFD(STDIN_FILENO) == nullptr)
228 AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false);
229
230 // Only use the slave tty if we don't have anything specified for
231 // output and don't have an action for stdout
232 if (GetFileActionForFD(STDOUT_FILENO) == nullptr)
233 AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true);
234
235 // Only use the slave tty if we don't have anything specified for
236 // error and don't have an action for stderr
237 if (GetFileActionForFD(STDERR_FILENO) == nullptr)
238 AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true);
239 return llvm::Error::success();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000240}
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242bool ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell(
Zachary Turner97206d52017-05-12 04:51:55 +0000243 Status &error, bool localhost, bool will_debug,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 bool first_arg_is_full_shell_command, int32_t num_resumes) {
245 error.Clear();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 if (GetFlags().Test(eLaunchFlagLaunchInShell)) {
248 if (m_shell) {
249 std::string shell_executable = m_shell.GetPath();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 const char **argv = GetArguments().GetConstArgumentVector();
252 if (argv == nullptr || argv[0] == nullptr)
253 return false;
254 Args shell_arguments;
255 std::string safe_arg;
Zachary Turnerecbb0bb2016-09-19 17:54:06 +0000256 shell_arguments.AppendArgument(shell_executable);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 const llvm::Triple &triple = GetArchitecture().GetTriple();
258 if (triple.getOS() == llvm::Triple::Win32 &&
259 !triple.isWindowsCygwinEnvironment())
Zachary Turnerecbb0bb2016-09-19 17:54:06 +0000260 shell_arguments.AppendArgument(llvm::StringRef("/C"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 else
Zachary Turnerecbb0bb2016-09-19 17:54:06 +0000262 shell_arguments.AppendArgument(llvm::StringRef("-c"));
Zachary Turner270e99a2014-12-08 21:36:42 +0000263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 StreamString shell_command;
265 if (will_debug) {
Adrian Prantl05097242018-04-30 16:49:04 +0000266 // Add a modified PATH environment variable in case argv[0] is a
267 // relative path.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 const char *argv0 = argv[0];
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000269 FileSpec arg_spec(argv0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 if (arg_spec.IsRelative()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000271 // We have a relative path to our executable which may not work if we
272 // just try to run "a.out" (without it being converted to "./a.out")
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 FileSpec working_dir = GetWorkingDirectory();
274 // Be sure to put quotes around PATH's value in case any paths have
275 // spaces...
276 std::string new_path("PATH=\"");
277 const size_t empty_path_len = new_path.size();
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 if (working_dir) {
280 new_path += working_dir.GetPath();
281 } else {
Pavel Labath1d5855b2017-01-23 15:56:45 +0000282 llvm::SmallString<64> cwd;
283 if (! llvm::sys::fs::current_path(cwd))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 new_path += cwd;
285 }
286 std::string curr_path;
287 if (HostInfo::GetEnvironmentVar("PATH", curr_path)) {
288 if (new_path.size() > empty_path_len)
289 new_path += ':';
290 new_path += curr_path;
291 }
292 new_path += "\" ";
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000293 shell_command.PutCString(new_path);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000294 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295
296 if (triple.getOS() != llvm::Triple::Win32 ||
297 triple.isWindowsCygwinEnvironment())
298 shell_command.PutCString("exec");
299
300 // Only Apple supports /usr/bin/arch being able to specify the
301 // architecture
302 if (GetArchitecture().IsValid() && // Valid architecture
303 GetArchitecture().GetTriple().getVendor() ==
304 llvm::Triple::Apple && // Apple only
305 GetArchitecture().GetCore() !=
306 ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h
307 {
308 shell_command.Printf(" /usr/bin/arch -arch %s",
309 GetArchitecture().GetArchitectureName());
310 // Set the resume count to 2:
311 // 1 - stop in shell
312 // 2 - stop in /usr/bin/arch
313 // 3 - then we will stop in our program
314 SetResumeCount(num_resumes + 1);
315 } else {
316 // Set the resume count to 1:
317 // 1 - stop in shell
318 // 2 - then we will stop in our program
319 SetResumeCount(num_resumes);
320 }
321 }
322
323 if (first_arg_is_full_shell_command) {
Adrian Prantl05097242018-04-30 16:49:04 +0000324 // There should only be one argument that is the shell command itself
325 // to be used as is
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 if (argv[0] && !argv[1])
327 shell_command.Printf("%s", argv[0]);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000328 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 return false;
330 } else {
331 for (size_t i = 0; argv[i] != nullptr; ++i) {
332 const char *arg =
333 Args::GetShellSafeArgument(m_shell, argv[i], safe_arg);
334 shell_command.Printf(" %s", arg);
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000335 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 }
Zachary Turnerecbb0bb2016-09-19 17:54:06 +0000337 shell_arguments.AppendArgument(shell_command.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 m_executable = m_shell;
339 m_arguments = shell_arguments;
340 return true;
341 } else {
342 error.SetErrorString("invalid shell path");
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000343 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 } else {
345 error.SetErrorString("not launching in shell");
346 }
347 return false;
Todd Fiala6d6b55d2014-06-30 00:30:53 +0000348}