150
|
1 //===-- Host.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 // C includes
|
|
10 #include <errno.h>
|
|
11 #include <limits.h>
|
|
12 #include <stdlib.h>
|
|
13 #include <sys/types.h>
|
|
14 #ifndef _WIN32
|
|
15 #include <dlfcn.h>
|
|
16 #include <grp.h>
|
|
17 #include <netdb.h>
|
|
18 #include <pwd.h>
|
|
19 #include <sys/stat.h>
|
|
20 #include <unistd.h>
|
|
21 #endif
|
|
22
|
|
23 #if defined(__APPLE__)
|
|
24 #include <mach-o/dyld.h>
|
|
25 #include <mach/mach_init.h>
|
|
26 #include <mach/mach_port.h>
|
|
27 #endif
|
|
28
|
|
29 #if defined(__linux__) || defined(__FreeBSD__) || \
|
|
30 defined(__FreeBSD_kernel__) || defined(__APPLE__) || \
|
|
31 defined(__NetBSD__) || defined(__OpenBSD__)
|
|
32 #if !defined(__ANDROID__)
|
|
33 #include <spawn.h>
|
|
34 #endif
|
|
35 #include <sys/syscall.h>
|
|
36 #include <sys/wait.h>
|
|
37 #endif
|
|
38
|
|
39 #if defined(__FreeBSD__)
|
|
40 #include <pthread_np.h>
|
|
41 #endif
|
|
42
|
|
43 #if defined(__NetBSD__)
|
|
44 #include <lwp.h>
|
|
45 #endif
|
|
46
|
|
47 #include <csignal>
|
|
48
|
|
49 #include "lldb/Host/FileAction.h"
|
|
50 #include "lldb/Host/FileSystem.h"
|
|
51 #include "lldb/Host/Host.h"
|
|
52 #include "lldb/Host/HostInfo.h"
|
|
53 #include "lldb/Host/HostProcess.h"
|
|
54 #include "lldb/Host/MonitoringProcessLauncher.h"
|
|
55 #include "lldb/Host/ProcessLaunchInfo.h"
|
|
56 #include "lldb/Host/ProcessLauncher.h"
|
|
57 #include "lldb/Host/ThreadLauncher.h"
|
|
58 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
|
|
59 #include "lldb/Utility/DataBufferLLVM.h"
|
|
60 #include "lldb/Utility/FileSpec.h"
|
|
61 #include "lldb/Utility/Log.h"
|
|
62 #include "lldb/Utility/Predicate.h"
|
|
63 #include "lldb/Utility/Status.h"
|
|
64 #include "lldb/lldb-private-forward.h"
|
|
65 #include "llvm/ADT/SmallString.h"
|
|
66 #include "llvm/ADT/StringSwitch.h"
|
|
67 #include "llvm/Support/Errno.h"
|
|
68 #include "llvm/Support/FileSystem.h"
|
|
69
|
|
70 #if defined(_WIN32)
|
|
71 #include "lldb/Host/windows/ConnectionGenericFileWindows.h"
|
|
72 #include "lldb/Host/windows/ProcessLauncherWindows.h"
|
|
73 #else
|
|
74 #include "lldb/Host/posix/ProcessLauncherPosixFork.h"
|
|
75 #endif
|
|
76
|
|
77 #if defined(__APPLE__)
|
|
78 #ifndef _POSIX_SPAWN_DISABLE_ASLR
|
|
79 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
|
|
80 #endif
|
|
81
|
|
82 extern "C" {
|
|
83 int __pthread_chdir(const char *path);
|
|
84 int __pthread_fchdir(int fildes);
|
|
85 }
|
|
86
|
|
87 #endif
|
|
88
|
|
89 using namespace lldb;
|
|
90 using namespace lldb_private;
|
|
91
|
|
92 #if !defined(__APPLE__) && !defined(_WIN32)
|
|
93 struct MonitorInfo {
|
|
94 lldb::pid_t pid; // The process ID to monitor
|
|
95 Host::MonitorChildProcessCallback
|
|
96 callback; // The callback function to call when "pid" exits or signals
|
|
97 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
|
|
98 };
|
|
99
|
|
100 static thread_result_t MonitorChildProcessThreadFunction(void *arg);
|
|
101
|
|
102 llvm::Expected<HostThread> Host::StartMonitoringChildProcess(
|
|
103 const Host::MonitorChildProcessCallback &callback, lldb::pid_t pid,
|
|
104 bool monitor_signals) {
|
|
105 MonitorInfo *info_ptr = new MonitorInfo();
|
|
106
|
|
107 info_ptr->pid = pid;
|
|
108 info_ptr->callback = callback;
|
|
109 info_ptr->monitor_signals = monitor_signals;
|
|
110
|
|
111 char thread_name[256];
|
|
112 ::snprintf(thread_name, sizeof(thread_name),
|
|
113 "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
|
|
114 return ThreadLauncher::LaunchThread(
|
|
115 thread_name, MonitorChildProcessThreadFunction, info_ptr, 0);
|
|
116 }
|
|
117
|
|
118 #ifndef __linux__
|
|
119 // Scoped class that will disable thread canceling when it is constructed, and
|
|
120 // exception safely restore the previous value it when it goes out of scope.
|
|
121 class ScopedPThreadCancelDisabler {
|
|
122 public:
|
|
123 ScopedPThreadCancelDisabler() {
|
|
124 // Disable the ability for this thread to be cancelled
|
|
125 int err = ::pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &m_old_state);
|
|
126 if (err != 0)
|
|
127 m_old_state = -1;
|
|
128 }
|
|
129
|
|
130 ~ScopedPThreadCancelDisabler() {
|
|
131 // Restore the ability for this thread to be cancelled to what it
|
|
132 // previously was.
|
|
133 if (m_old_state != -1)
|
|
134 ::pthread_setcancelstate(m_old_state, 0);
|
|
135 }
|
|
136
|
|
137 private:
|
|
138 int m_old_state; // Save the old cancelability state.
|
|
139 };
|
|
140 #endif // __linux__
|
|
141
|
|
142 #ifdef __linux__
|
|
143 #if defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
|
|
144 static __thread volatile sig_atomic_t g_usr1_called;
|
|
145 #else
|
|
146 static thread_local volatile sig_atomic_t g_usr1_called;
|
|
147 #endif
|
|
148
|
|
149 static void SigUsr1Handler(int) { g_usr1_called = 1; }
|
|
150 #endif // __linux__
|
|
151
|
|
152 static bool CheckForMonitorCancellation() {
|
|
153 #ifdef __linux__
|
|
154 if (g_usr1_called) {
|
|
155 g_usr1_called = 0;
|
|
156 return true;
|
|
157 }
|
|
158 #else
|
|
159 ::pthread_testcancel();
|
|
160 #endif
|
|
161 return false;
|
|
162 }
|
|
163
|
|
164 static thread_result_t MonitorChildProcessThreadFunction(void *arg) {
|
|
165 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
|
|
166 const char *function = __FUNCTION__;
|
|
167 LLDB_LOGF(log, "%s (arg = %p) thread starting...", function, arg);
|
|
168
|
|
169 MonitorInfo *info = (MonitorInfo *)arg;
|
|
170
|
|
171 const Host::MonitorChildProcessCallback callback = info->callback;
|
|
172 const bool monitor_signals = info->monitor_signals;
|
|
173
|
|
174 assert(info->pid <= UINT32_MAX);
|
|
175 const ::pid_t pid = monitor_signals ? -1 * getpgid(info->pid) : info->pid;
|
|
176
|
|
177 delete info;
|
|
178
|
|
179 int status = -1;
|
|
180 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
|
|
181 #define __WALL 0
|
|
182 #endif
|
|
183 const int options = __WALL;
|
|
184
|
|
185 #ifdef __linux__
|
|
186 // This signal is only used to interrupt the thread from waitpid
|
|
187 struct sigaction sigUsr1Action;
|
|
188 memset(&sigUsr1Action, 0, sizeof(sigUsr1Action));
|
|
189 sigUsr1Action.sa_handler = SigUsr1Handler;
|
|
190 ::sigaction(SIGUSR1, &sigUsr1Action, nullptr);
|
|
191 #endif // __linux__
|
|
192
|
|
193 while (1) {
|
|
194 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS);
|
|
195 LLDB_LOGF(log, "%s ::waitpid (pid = %" PRIi32 ", &status, options = %i)...",
|
|
196 function, pid, options);
|
|
197
|
|
198 if (CheckForMonitorCancellation())
|
|
199 break;
|
|
200
|
|
201 // Get signals from all children with same process group of pid
|
|
202 const ::pid_t wait_pid = ::waitpid(pid, &status, options);
|
|
203
|
|
204 if (CheckForMonitorCancellation())
|
|
205 break;
|
|
206
|
|
207 if (wait_pid == -1) {
|
|
208 if (errno == EINTR)
|
|
209 continue;
|
|
210 else {
|
|
211 LLDB_LOG(log,
|
|
212 "arg = {0}, thread exiting because waitpid failed ({1})...",
|
|
213 arg, llvm::sys::StrError());
|
|
214 break;
|
|
215 }
|
|
216 } else if (wait_pid > 0) {
|
|
217 bool exited = false;
|
|
218 int signal = 0;
|
|
219 int exit_status = 0;
|
|
220 const char *status_cstr = nullptr;
|
|
221 if (WIFSTOPPED(status)) {
|
|
222 signal = WSTOPSIG(status);
|
|
223 status_cstr = "STOPPED";
|
|
224 } else if (WIFEXITED(status)) {
|
|
225 exit_status = WEXITSTATUS(status);
|
|
226 status_cstr = "EXITED";
|
|
227 exited = true;
|
|
228 } else if (WIFSIGNALED(status)) {
|
|
229 signal = WTERMSIG(status);
|
|
230 status_cstr = "SIGNALED";
|
|
231 if (wait_pid == abs(pid)) {
|
|
232 exited = true;
|
|
233 exit_status = -1;
|
|
234 }
|
|
235 } else {
|
|
236 status_cstr = "(\?\?\?)";
|
|
237 }
|
|
238
|
|
239 // Scope for pthread_cancel_disabler
|
|
240 {
|
|
241 #ifndef __linux__
|
|
242 ScopedPThreadCancelDisabler pthread_cancel_disabler;
|
|
243 #endif
|
|
244
|
|
245 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS);
|
|
246 LLDB_LOGF(log,
|
|
247 "%s ::waitpid (pid = %" PRIi32
|
|
248 ", &status, options = %i) => pid = %" PRIi32
|
|
249 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i",
|
|
250 function, pid, options, wait_pid, status, status_cstr, signal,
|
|
251 exit_status);
|
|
252
|
|
253 if (exited || (signal != 0 && monitor_signals)) {
|
|
254 bool callback_return = false;
|
|
255 if (callback)
|
|
256 callback_return = callback(wait_pid, exited, signal, exit_status);
|
|
257
|
|
258 // If our process exited, then this thread should exit
|
|
259 if (exited && wait_pid == abs(pid)) {
|
|
260 LLDB_LOGF(log,
|
|
261 "%s (arg = %p) thread exiting because pid received "
|
|
262 "exit signal...",
|
|
263 __FUNCTION__, arg);
|
|
264 break;
|
|
265 }
|
|
266 // If the callback returns true, it means this process should exit
|
|
267 if (callback_return) {
|
|
268 LLDB_LOGF(log,
|
|
269 "%s (arg = %p) thread exiting because callback "
|
|
270 "returned true...",
|
|
271 __FUNCTION__, arg);
|
|
272 break;
|
|
273 }
|
|
274 }
|
|
275 }
|
|
276 }
|
|
277 }
|
|
278
|
|
279 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS);
|
|
280 LLDB_LOGF(log, "%s (arg = %p) thread exiting...", __FUNCTION__, arg);
|
|
281
|
|
282 return nullptr;
|
|
283 }
|
|
284
|
|
285 #endif // #if !defined (__APPLE__) && !defined (_WIN32)
|
|
286
|
|
287 #if !defined(__APPLE__)
|
|
288
|
|
289 void Host::SystemLog(SystemLogType type, const char *format, va_list args) {
|
|
290 vfprintf(stderr, format, args);
|
|
291 }
|
|
292
|
|
293 #endif
|
|
294
|
|
295 void Host::SystemLog(SystemLogType type, const char *format, ...) {
|
|
296 {
|
|
297 va_list args;
|
|
298 va_start(args, format);
|
|
299 SystemLog(type, format, args);
|
|
300 va_end(args);
|
|
301 }
|
|
302
|
|
303 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST));
|
|
304 if (log && log->GetVerbose()) {
|
|
305 // Log to log channel. This allows testcases to grep for log output.
|
|
306 va_list args;
|
|
307 va_start(args, format);
|
|
308 log->VAPrintf(format, args);
|
|
309 va_end(args);
|
|
310 }
|
|
311 }
|
|
312
|
|
313 lldb::pid_t Host::GetCurrentProcessID() { return ::getpid(); }
|
|
314
|
|
315 #ifndef _WIN32
|
|
316
|
|
317 lldb::thread_t Host::GetCurrentThread() {
|
|
318 return lldb::thread_t(pthread_self());
|
|
319 }
|
|
320
|
|
321 const char *Host::GetSignalAsCString(int signo) {
|
|
322 switch (signo) {
|
|
323 case SIGHUP:
|
|
324 return "SIGHUP"; // 1 hangup
|
|
325 case SIGINT:
|
|
326 return "SIGINT"; // 2 interrupt
|
|
327 case SIGQUIT:
|
|
328 return "SIGQUIT"; // 3 quit
|
|
329 case SIGILL:
|
|
330 return "SIGILL"; // 4 illegal instruction (not reset when caught)
|
|
331 case SIGTRAP:
|
|
332 return "SIGTRAP"; // 5 trace trap (not reset when caught)
|
|
333 case SIGABRT:
|
|
334 return "SIGABRT"; // 6 abort()
|
|
335 #if defined(SIGPOLL)
|
|
336 #if !defined(SIGIO) || (SIGPOLL != SIGIO)
|
|
337 // Under some GNU/Linux, SIGPOLL and SIGIO are the same. Causing the build to
|
|
338 // fail with 'multiple define cases with same value'
|
|
339 case SIGPOLL:
|
|
340 return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
|
|
341 #endif
|
|
342 #endif
|
|
343 #if defined(SIGEMT)
|
|
344 case SIGEMT:
|
|
345 return "SIGEMT"; // 7 EMT instruction
|
|
346 #endif
|
|
347 case SIGFPE:
|
|
348 return "SIGFPE"; // 8 floating point exception
|
|
349 case SIGKILL:
|
|
350 return "SIGKILL"; // 9 kill (cannot be caught or ignored)
|
|
351 case SIGBUS:
|
|
352 return "SIGBUS"; // 10 bus error
|
|
353 case SIGSEGV:
|
|
354 return "SIGSEGV"; // 11 segmentation violation
|
|
355 case SIGSYS:
|
|
356 return "SIGSYS"; // 12 bad argument to system call
|
|
357 case SIGPIPE:
|
|
358 return "SIGPIPE"; // 13 write on a pipe with no one to read it
|
|
359 case SIGALRM:
|
|
360 return "SIGALRM"; // 14 alarm clock
|
|
361 case SIGTERM:
|
|
362 return "SIGTERM"; // 15 software termination signal from kill
|
|
363 case SIGURG:
|
|
364 return "SIGURG"; // 16 urgent condition on IO channel
|
|
365 case SIGSTOP:
|
|
366 return "SIGSTOP"; // 17 sendable stop signal not from tty
|
|
367 case SIGTSTP:
|
|
368 return "SIGTSTP"; // 18 stop signal from tty
|
|
369 case SIGCONT:
|
|
370 return "SIGCONT"; // 19 continue a stopped process
|
|
371 case SIGCHLD:
|
|
372 return "SIGCHLD"; // 20 to parent on child stop or exit
|
|
373 case SIGTTIN:
|
|
374 return "SIGTTIN"; // 21 to readers pgrp upon background tty read
|
|
375 case SIGTTOU:
|
|
376 return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local<OSTOP)
|
|
377 #if defined(SIGIO)
|
|
378 case SIGIO:
|
|
379 return "SIGIO"; // 23 input/output possible signal
|
|
380 #endif
|
|
381 case SIGXCPU:
|
|
382 return "SIGXCPU"; // 24 exceeded CPU time limit
|
|
383 case SIGXFSZ:
|
|
384 return "SIGXFSZ"; // 25 exceeded file size limit
|
|
385 case SIGVTALRM:
|
|
386 return "SIGVTALRM"; // 26 virtual time alarm
|
|
387 case SIGPROF:
|
|
388 return "SIGPROF"; // 27 profiling time alarm
|
|
389 #if defined(SIGWINCH)
|
|
390 case SIGWINCH:
|
|
391 return "SIGWINCH"; // 28 window size changes
|
|
392 #endif
|
|
393 #if defined(SIGINFO)
|
|
394 case SIGINFO:
|
|
395 return "SIGINFO"; // 29 information request
|
|
396 #endif
|
|
397 case SIGUSR1:
|
|
398 return "SIGUSR1"; // 30 user defined signal 1
|
|
399 case SIGUSR2:
|
|
400 return "SIGUSR2"; // 31 user defined signal 2
|
|
401 default:
|
|
402 break;
|
|
403 }
|
|
404 return nullptr;
|
|
405 }
|
|
406
|
|
407 #endif
|
|
408
|
|
409 #if !defined(__APPLE__) // see Host.mm
|
|
410
|
|
411 bool Host::GetBundleDirectory(const FileSpec &file, FileSpec &bundle) {
|
|
412 bundle.Clear();
|
|
413 return false;
|
|
414 }
|
|
415
|
|
416 bool Host::ResolveExecutableInBundle(FileSpec &file) { return false; }
|
|
417 #endif
|
|
418
|
|
419 #ifndef _WIN32
|
|
420
|
|
421 FileSpec Host::GetModuleFileSpecForHostAddress(const void *host_addr) {
|
|
422 FileSpec module_filespec;
|
|
423 #if !defined(__ANDROID__)
|
|
424 Dl_info info;
|
|
425 if (::dladdr(host_addr, &info)) {
|
|
426 if (info.dli_fname) {
|
|
427 module_filespec.SetFile(info.dli_fname, FileSpec::Style::native);
|
|
428 FileSystem::Instance().Resolve(module_filespec);
|
|
429 }
|
|
430 }
|
|
431 #endif
|
|
432 return module_filespec;
|
|
433 }
|
|
434
|
|
435 #endif
|
|
436
|
|
437 #if !defined(__linux__)
|
|
438 bool Host::FindProcessThreads(const lldb::pid_t pid, TidMap &tids_to_attach) {
|
|
439 return false;
|
|
440 }
|
|
441 #endif
|
|
442
|
|
443 struct ShellInfo {
|
|
444 ShellInfo()
|
|
445 : process_reaped(false), pid(LLDB_INVALID_PROCESS_ID), signo(-1),
|
|
446 status(-1) {}
|
|
447
|
|
448 lldb_private::Predicate<bool> process_reaped;
|
|
449 lldb::pid_t pid;
|
|
450 int signo;
|
|
451 int status;
|
|
452 };
|
|
453
|
|
454 static bool
|
|
455 MonitorShellCommand(std::shared_ptr<ShellInfo> shell_info, lldb::pid_t pid,
|
|
456 bool exited, // True if the process did exit
|
|
457 int signo, // Zero for no signal
|
|
458 int status) // Exit value of process if signal is zero
|
|
459 {
|
|
460 shell_info->pid = pid;
|
|
461 shell_info->signo = signo;
|
|
462 shell_info->status = status;
|
|
463 // Let the thread running Host::RunShellCommand() know that the process
|
|
464 // exited and that ShellInfo has been filled in by broadcasting to it
|
|
465 shell_info->process_reaped.SetValue(true, eBroadcastAlways);
|
|
466 return true;
|
|
467 }
|
|
468
|
|
469 Status Host::RunShellCommand(const char *command, const FileSpec &working_dir,
|
|
470 int *status_ptr, int *signo_ptr,
|
|
471 std::string *command_output_ptr,
|
|
472 const Timeout<std::micro> &timeout,
|
|
473 bool run_in_default_shell,
|
|
474 bool hide_stderr) {
|
|
475 return RunShellCommand(Args(command), working_dir, status_ptr, signo_ptr,
|
|
476 command_output_ptr, timeout, run_in_default_shell,
|
|
477 hide_stderr);
|
|
478 }
|
|
479
|
|
480 Status Host::RunShellCommand(const Args &args, const FileSpec &working_dir,
|
|
481 int *status_ptr, int *signo_ptr,
|
|
482 std::string *command_output_ptr,
|
|
483 const Timeout<std::micro> &timeout,
|
|
484 bool run_in_default_shell,
|
|
485 bool hide_stderr) {
|
|
486 Status error;
|
|
487 ProcessLaunchInfo launch_info;
|
|
488 launch_info.SetArchitecture(HostInfo::GetArchitecture());
|
|
489 if (run_in_default_shell) {
|
|
490 // Run the command in a shell
|
|
491 launch_info.SetShell(HostInfo::GetDefaultShell());
|
|
492 launch_info.GetArguments().AppendArguments(args);
|
|
493 const bool localhost = true;
|
|
494 const bool will_debug = false;
|
|
495 const bool first_arg_is_full_shell_command = false;
|
|
496 launch_info.ConvertArgumentsForLaunchingInShell(
|
|
497 error, localhost, will_debug, first_arg_is_full_shell_command, 0);
|
|
498 } else {
|
|
499 // No shell, just run it
|
|
500 const bool first_arg_is_executable = true;
|
|
501 launch_info.SetArguments(args, first_arg_is_executable);
|
|
502 }
|
|
503
|
|
504 if (working_dir)
|
|
505 launch_info.SetWorkingDirectory(working_dir);
|
|
506 llvm::SmallString<64> output_file_path;
|
|
507
|
|
508 if (command_output_ptr) {
|
|
509 // Create a temporary file to get the stdout/stderr and redirect the output
|
|
510 // of the command into this file. We will later read this file if all goes
|
|
511 // well and fill the data into "command_output_ptr"
|
|
512 if (FileSpec tmpdir_file_spec = HostInfo::GetProcessTempDir()) {
|
|
513 tmpdir_file_spec.AppendPathComponent("lldb-shell-output.%%%%%%");
|
|
514 llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath(),
|
|
515 output_file_path);
|
|
516 } else {
|
|
517 llvm::sys::fs::createTemporaryFile("lldb-shell-output.%%%%%%", "",
|
|
518 output_file_path);
|
|
519 }
|
|
520 }
|
|
521
|
|
522 FileSpec output_file_spec(output_file_path.c_str());
|
|
523 // Set up file descriptors.
|
|
524 launch_info.AppendSuppressFileAction(STDIN_FILENO, true, false);
|
|
525 if (output_file_spec)
|
|
526 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_spec, false,
|
|
527 true);
|
|
528 else
|
|
529 launch_info.AppendSuppressFileAction(STDOUT_FILENO, false, true);
|
|
530
|
|
531 if (output_file_spec && !hide_stderr)
|
|
532 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
|
|
533 else
|
|
534 launch_info.AppendSuppressFileAction(STDERR_FILENO, false, true);
|
|
535
|
|
536 std::shared_ptr<ShellInfo> shell_info_sp(new ShellInfo());
|
|
537 const bool monitor_signals = false;
|
|
538 launch_info.SetMonitorProcessCallback(
|
|
539 std::bind(MonitorShellCommand, shell_info_sp, std::placeholders::_1,
|
|
540 std::placeholders::_2, std::placeholders::_3,
|
|
541 std::placeholders::_4),
|
|
542 monitor_signals);
|
|
543
|
|
544 error = LaunchProcess(launch_info);
|
|
545 const lldb::pid_t pid = launch_info.GetProcessID();
|
|
546
|
|
547 if (error.Success() && pid == LLDB_INVALID_PROCESS_ID)
|
|
548 error.SetErrorString("failed to get process ID");
|
|
549
|
|
550 if (error.Success()) {
|
|
551 if (!shell_info_sp->process_reaped.WaitForValueEqualTo(true, timeout)) {
|
|
552 error.SetErrorString("timed out waiting for shell command to complete");
|
|
553
|
|
554 // Kill the process since it didn't complete within the timeout specified
|
|
555 Kill(pid, SIGKILL);
|
|
556 // Wait for the monitor callback to get the message
|
|
557 shell_info_sp->process_reaped.WaitForValueEqualTo(
|
|
558 true, std::chrono::seconds(1));
|
|
559 } else {
|
|
560 if (status_ptr)
|
|
561 *status_ptr = shell_info_sp->status;
|
|
562
|
|
563 if (signo_ptr)
|
|
564 *signo_ptr = shell_info_sp->signo;
|
|
565
|
|
566 if (command_output_ptr) {
|
|
567 command_output_ptr->clear();
|
|
568 uint64_t file_size =
|
|
569 FileSystem::Instance().GetByteSize(output_file_spec);
|
|
570 if (file_size > 0) {
|
|
571 if (file_size > command_output_ptr->max_size()) {
|
|
572 error.SetErrorStringWithFormat(
|
|
573 "shell command output is too large to fit into a std::string");
|
|
574 } else {
|
|
575 auto Buffer =
|
|
576 FileSystem::Instance().CreateDataBuffer(output_file_spec);
|
|
577 if (error.Success())
|
|
578 command_output_ptr->assign(Buffer->GetChars(),
|
|
579 Buffer->GetByteSize());
|
|
580 }
|
|
581 }
|
|
582 }
|
|
583 }
|
|
584 }
|
|
585
|
|
586 llvm::sys::fs::remove(output_file_spec.GetPath());
|
|
587 return error;
|
|
588 }
|
|
589
|
|
590 // The functions below implement process launching for non-Apple-based
|
|
591 // platforms
|
|
592 #if !defined(__APPLE__)
|
|
593 Status Host::LaunchProcess(ProcessLaunchInfo &launch_info) {
|
|
594 std::unique_ptr<ProcessLauncher> delegate_launcher;
|
|
595 #if defined(_WIN32)
|
|
596 delegate_launcher.reset(new ProcessLauncherWindows());
|
|
597 #else
|
|
598 delegate_launcher.reset(new ProcessLauncherPosixFork());
|
|
599 #endif
|
|
600 MonitoringProcessLauncher launcher(std::move(delegate_launcher));
|
|
601
|
|
602 Status error;
|
|
603 HostProcess process = launcher.LaunchProcess(launch_info, error);
|
|
604
|
|
605 // TODO(zturner): It would be better if the entire HostProcess were returned
|
|
606 // instead of writing it into this structure.
|
|
607 launch_info.SetProcessID(process.GetProcessId());
|
|
608
|
|
609 return error;
|
|
610 }
|
|
611 #endif // !defined(__APPLE__)
|
|
612
|
|
613 #ifndef _WIN32
|
|
614 void Host::Kill(lldb::pid_t pid, int signo) { ::kill(pid, signo); }
|
|
615
|
|
616 #endif
|
|
617
|
|
618 #if !defined(__APPLE__)
|
|
619 bool Host::OpenFileInExternalEditor(const FileSpec &file_spec,
|
|
620 uint32_t line_no) {
|
|
621 return false;
|
|
622 }
|
|
623
|
|
624 #endif
|
|
625
|
|
626 std::unique_ptr<Connection> Host::CreateDefaultConnection(llvm::StringRef url) {
|
|
627 #if defined(_WIN32)
|
|
628 if (url.startswith("file://"))
|
|
629 return std::unique_ptr<Connection>(new ConnectionGenericFile());
|
|
630 #endif
|
|
631 return std::unique_ptr<Connection>(new ConnectionFileDescriptor());
|
|
632 }
|
|
633
|
|
634 #if defined(LLVM_ON_UNIX)
|
|
635 WaitStatus WaitStatus::Decode(int wstatus) {
|
|
636 if (WIFEXITED(wstatus))
|
|
637 return {Exit, uint8_t(WEXITSTATUS(wstatus))};
|
|
638 else if (WIFSIGNALED(wstatus))
|
|
639 return {Signal, uint8_t(WTERMSIG(wstatus))};
|
|
640 else if (WIFSTOPPED(wstatus))
|
|
641 return {Stop, uint8_t(WSTOPSIG(wstatus))};
|
|
642 llvm_unreachable("Unknown wait status");
|
|
643 }
|
|
644 #endif
|
|
645
|
|
646 void llvm::format_provider<WaitStatus>::format(const WaitStatus &WS,
|
|
647 raw_ostream &OS,
|
|
648 StringRef Options) {
|
|
649 if (Options == "g") {
|
|
650 char type;
|
|
651 switch (WS.type) {
|
|
652 case WaitStatus::Exit:
|
|
653 type = 'W';
|
|
654 break;
|
|
655 case WaitStatus::Signal:
|
|
656 type = 'X';
|
|
657 break;
|
|
658 case WaitStatus::Stop:
|
|
659 type = 'S';
|
|
660 break;
|
|
661 }
|
|
662 OS << formatv("{0}{1:x-2}", type, WS.status);
|
|
663 return;
|
|
664 }
|
|
665
|
|
666 assert(Options.empty());
|
|
667 const char *desc;
|
|
668 switch(WS.type) {
|
|
669 case WaitStatus::Exit:
|
|
670 desc = "Exited with status";
|
|
671 break;
|
|
672 case WaitStatus::Signal:
|
|
673 desc = "Killed by signal";
|
|
674 break;
|
|
675 case WaitStatus::Stop:
|
|
676 desc = "Stopped by signal";
|
|
677 break;
|
|
678 }
|
|
679 OS << desc << " " << int(WS.status);
|
|
680 }
|