1 //===-- Host.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 //===----------------------------------------------------------------------===//
14 #include <sys/types.h>
24 #if defined(__APPLE__)
25 #include <mach-o/dyld.h>
26 #include <mach/mach_init.h>
27 #include <mach/mach_port.h>
30 #if defined(__linux__) || defined(__FreeBSD__) || \
31 defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__NetBSD__)
32 #if !defined(__ANDROID__)
35 #include <sys/syscall.h>
39 #if defined(__FreeBSD__)
40 #include <pthread_np.h>
45 // Other libraries and framework includes
48 #include "lldb/Core/ArchSpec.h"
49 #include "lldb/Core/Error.h"
50 #include "lldb/Core/Log.h"
51 #include "lldb/Host/FileSpec.h"
52 #include "lldb/Host/FileSystem.h"
53 #include "lldb/Host/Host.h"
54 #include "lldb/Host/HostInfo.h"
55 #include "lldb/Host/HostProcess.h"
56 #include "lldb/Host/MonitoringProcessLauncher.h"
57 #include "lldb/Host/Predicate.h"
58 #include "lldb/Host/ProcessLauncher.h"
59 #include "lldb/Host/ThreadLauncher.h"
60 #include "lldb/Target/FileAction.h"
61 #include "lldb/Target/ProcessLaunchInfo.h"
62 #include "lldb/Target/UnixSignals.h"
63 #include "lldb/Utility/CleanUp.h"
64 #include "lldb/lldb-private-forward.h"
65 #include "llvm/ADT/SmallString.h"
66 #include "llvm/Support/FileSystem.h"
69 #include "lldb/Host/windows/ProcessLauncherWindows.h"
70 #elif defined(__linux__)
71 #include "lldb/Host/linux/ProcessLauncherLinux.h"
73 #include "lldb/Host/posix/ProcessLauncherPosix.h"
76 #if defined(__APPLE__)
77 #ifndef _POSIX_SPAWN_DISABLE_ASLR
78 #define _POSIX_SPAWN_DISABLE_ASLR 0x0100
82 int __pthread_chdir(const char *path);
83 int __pthread_fchdir(int fildes);
89 using namespace lldb_private;
91 #if !defined(__APPLE__) && !defined(_WIN32)
93 lldb::pid_t pid; // The process ID to monitor
94 Host::MonitorChildProcessCallback
95 callback; // The callback function to call when "pid" exits or signals
96 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
99 static thread_result_t MonitorChildProcessThreadFunction(void *arg);
101 HostThread Host::StartMonitoringChildProcess(
102 const Host::MonitorChildProcessCallback &callback, lldb::pid_t pid,
103 bool monitor_signals) {
104 MonitorInfo *info_ptr = new MonitorInfo();
107 info_ptr->callback = callback;
108 info_ptr->monitor_signals = monitor_signals;
110 char thread_name[256];
111 ::snprintf(thread_name, sizeof(thread_name),
112 "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
113 return ThreadLauncher::LaunchThread(
114 thread_name, MonitorChildProcessThreadFunction, info_ptr, NULL);
118 //------------------------------------------------------------------
119 // Scoped class that will disable thread canceling when it is
120 // constructed, and exception safely restore the previous value it
121 // when it goes out of scope.
122 //------------------------------------------------------------------
123 class ScopedPThreadCancelDisabler {
125 ScopedPThreadCancelDisabler() {
126 // Disable the ability for this thread to be cancelled
127 int err = ::pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &m_old_state);
132 ~ScopedPThreadCancelDisabler() {
133 // Restore the ability for this thread to be cancelled to what it
135 if (m_old_state != -1)
136 ::pthread_setcancelstate(m_old_state, 0);
140 int m_old_state; // Save the old cancelability state.
145 #if defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
146 static __thread volatile sig_atomic_t g_usr1_called;
148 static thread_local volatile sig_atomic_t g_usr1_called;
151 static void SigUsr1Handler(int) { g_usr1_called = 1; }
154 static bool CheckForMonitorCancellation() {
161 ::pthread_testcancel();
166 static thread_result_t MonitorChildProcessThreadFunction(void *arg) {
167 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
168 const char *function = __FUNCTION__;
170 log->Printf("%s (arg = %p) thread starting...", function, arg);
172 MonitorInfo *info = (MonitorInfo *)arg;
174 const Host::MonitorChildProcessCallback callback = info->callback;
175 const bool monitor_signals = info->monitor_signals;
177 assert(info->pid <= UINT32_MAX);
178 const ::pid_t pid = monitor_signals ? -1 * getpgid(info->pid) : info->pid;
183 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
186 const int options = __WALL;
189 // This signal is only used to interrupt the thread from waitpid
190 struct sigaction sigUsr1Action;
191 memset(&sigUsr1Action, 0, sizeof(sigUsr1Action));
192 sigUsr1Action.sa_handler = SigUsr1Handler;
193 ::sigaction(SIGUSR1, &sigUsr1Action, nullptr);
197 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS);
199 log->Printf("%s ::waitpid (pid = %" PRIi32 ", &status, options = %i)...",
200 function, pid, options);
202 if (CheckForMonitorCancellation())
205 // Get signals from all children with same process group of pid
206 const ::pid_t wait_pid = ::waitpid(pid, &status, options);
208 if (CheckForMonitorCancellation())
211 if (wait_pid == -1) {
217 "%s (arg = %p) thread exiting because waitpid failed (%s)...",
218 __FUNCTION__, arg, strerror(errno));
221 } else if (wait_pid > 0) {
225 const char *status_cstr = NULL;
226 if (WIFSTOPPED(status)) {
227 signal = WSTOPSIG(status);
228 status_cstr = "STOPPED";
229 } else if (WIFEXITED(status)) {
230 exit_status = WEXITSTATUS(status);
231 status_cstr = "EXITED";
233 } else if (WIFSIGNALED(status)) {
234 signal = WTERMSIG(status);
235 status_cstr = "SIGNALED";
236 if (wait_pid == abs(pid)) {
241 status_cstr = "(\?\?\?)";
244 // Scope for pthread_cancel_disabler
247 ScopedPThreadCancelDisabler pthread_cancel_disabler;
250 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS);
252 log->Printf("%s ::waitpid (pid = %" PRIi32
253 ", &status, options = %i) => pid = %" PRIi32
254 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i",
255 function, pid, options, wait_pid, status, status_cstr,
256 signal, exit_status);
258 if (exited || (signal != 0 && monitor_signals)) {
259 bool callback_return = false;
261 callback_return = callback(wait_pid, exited, signal, exit_status);
263 // If our process exited, then this thread should exit
264 if (exited && wait_pid == abs(pid)) {
266 log->Printf("%s (arg = %p) thread exiting because pid received "
271 // If the callback returns true, it means this process should
273 if (callback_return) {
275 log->Printf("%s (arg = %p) thread exiting because callback "
285 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS);
287 log->Printf("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
292 #endif // #if !defined (__APPLE__) && !defined (_WIN32)
294 #if !defined(__APPLE__)
296 void Host::SystemLog(SystemLogType type, const char *format, va_list args) {
297 vfprintf(stderr, format, args);
302 void Host::SystemLog(SystemLogType type, const char *format, ...) {
304 va_start(args, format);
305 SystemLog(type, format, args);
309 lldb::pid_t Host::GetCurrentProcessID() { return ::getpid(); }
313 lldb::tid_t Host::GetCurrentThreadID() {
314 #if defined(__APPLE__)
315 // Calling "mach_thread_self()" bumps the reference count on the thread
316 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
318 thread_port_t thread_self = mach_thread_self();
319 mach_port_deallocate(mach_task_self(), thread_self);
321 #elif defined(__FreeBSD__)
322 return lldb::tid_t(pthread_getthreadid_np());
323 #elif defined(__ANDROID__)
324 return lldb::tid_t(gettid());
325 #elif defined(__linux__)
326 return lldb::tid_t(syscall(SYS_gettid));
328 return lldb::tid_t(pthread_self());
332 lldb::thread_t Host::GetCurrentThread() {
333 return lldb::thread_t(pthread_self());
336 const char *Host::GetSignalAsCString(int signo) {
339 return "SIGHUP"; // 1 hangup
341 return "SIGINT"; // 2 interrupt
343 return "SIGQUIT"; // 3 quit
345 return "SIGILL"; // 4 illegal instruction (not reset when caught)
347 return "SIGTRAP"; // 5 trace trap (not reset when caught)
349 return "SIGABRT"; // 6 abort()
351 #if !defined(SIGIO) || (SIGPOLL != SIGIO)
352 // Under some GNU/Linux, SIGPOLL and SIGIO are the same. Causing the build to
353 // fail with 'multiple define cases with same value'
355 return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
360 return "SIGEMT"; // 7 EMT instruction
363 return "SIGFPE"; // 8 floating point exception
365 return "SIGKILL"; // 9 kill (cannot be caught or ignored)
367 return "SIGBUS"; // 10 bus error
369 return "SIGSEGV"; // 11 segmentation violation
371 return "SIGSYS"; // 12 bad argument to system call
373 return "SIGPIPE"; // 13 write on a pipe with no one to read it
375 return "SIGALRM"; // 14 alarm clock
377 return "SIGTERM"; // 15 software termination signal from kill
379 return "SIGURG"; // 16 urgent condition on IO channel
381 return "SIGSTOP"; // 17 sendable stop signal not from tty
383 return "SIGTSTP"; // 18 stop signal from tty
385 return "SIGCONT"; // 19 continue a stopped process
387 return "SIGCHLD"; // 20 to parent on child stop or exit
389 return "SIGTTIN"; // 21 to readers pgrp upon background tty read
391 return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local<OSTOP)
394 return "SIGIO"; // 23 input/output possible signal
397 return "SIGXCPU"; // 24 exceeded CPU time limit
399 return "SIGXFSZ"; // 25 exceeded file size limit
401 return "SIGVTALRM"; // 26 virtual time alarm
403 return "SIGPROF"; // 27 profiling time alarm
404 #if defined(SIGWINCH)
406 return "SIGWINCH"; // 28 window size changes
410 return "SIGINFO"; // 29 information request
413 return "SIGUSR1"; // 30 user defined signal 1
415 return "SIGUSR2"; // 31 user defined signal 2
427 Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback) {
429 ::pthread_key_create(&key, callback);
433 void *Host::ThreadLocalStorageGet(lldb::thread_key_t key) {
434 return ::pthread_getspecific(key);
437 void Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value) {
438 ::pthread_setspecific(key, value);
443 #if !defined(__APPLE__) // see Host.mm
445 bool Host::GetBundleDirectory(const FileSpec &file, FileSpec &bundle) {
450 bool Host::ResolveExecutableInBundle(FileSpec &file) { return false; }
455 FileSpec Host::GetModuleFileSpecForHostAddress(const void *host_addr) {
456 FileSpec module_filespec;
457 #if !defined(__ANDROID__)
459 if (::dladdr(host_addr, &info)) {
461 module_filespec.SetFile(info.dli_fname, true);
464 return module_filespec;
469 #if !defined(__linux__)
470 bool Host::FindProcessThreads(const lldb::pid_t pid, TidMap &tids_to_attach) {
477 : process_reaped(false), pid(LLDB_INVALID_PROCESS_ID), signo(-1),
480 lldb_private::Predicate<bool> process_reaped;
487 MonitorShellCommand(std::shared_ptr<ShellInfo> shell_info, lldb::pid_t pid,
488 bool exited, // True if the process did exit
489 int signo, // Zero for no signal
490 int status) // Exit value of process if signal is zero
492 shell_info->pid = pid;
493 shell_info->signo = signo;
494 shell_info->status = status;
495 // Let the thread running Host::RunShellCommand() know that the process
496 // exited and that ShellInfo has been filled in by broadcasting to it
497 shell_info->process_reaped.SetValue(true, eBroadcastAlways);
501 Error Host::RunShellCommand(const char *command, const FileSpec &working_dir,
502 int *status_ptr, int *signo_ptr,
503 std::string *command_output_ptr,
504 uint32_t timeout_sec, bool run_in_default_shell) {
505 return RunShellCommand(Args(command), working_dir, status_ptr, signo_ptr,
506 command_output_ptr, timeout_sec, run_in_default_shell);
509 Error Host::RunShellCommand(const Args &args, const FileSpec &working_dir,
510 int *status_ptr, int *signo_ptr,
511 std::string *command_output_ptr,
512 uint32_t timeout_sec, bool run_in_default_shell) {
514 ProcessLaunchInfo launch_info;
515 launch_info.SetArchitecture(HostInfo::GetArchitecture());
516 if (run_in_default_shell) {
517 // Run the command in a shell
518 launch_info.SetShell(HostInfo::GetDefaultShell());
519 launch_info.GetArguments().AppendArguments(args);
520 const bool localhost = true;
521 const bool will_debug = false;
522 const bool first_arg_is_full_shell_command = false;
523 launch_info.ConvertArgumentsForLaunchingInShell(
524 error, localhost, will_debug, first_arg_is_full_shell_command, 0);
526 // No shell, just run it
527 const bool first_arg_is_executable = true;
528 launch_info.SetArguments(args, first_arg_is_executable);
532 launch_info.SetWorkingDirectory(working_dir);
533 llvm::SmallString<PATH_MAX> output_file_path;
535 if (command_output_ptr) {
536 // Create a temporary file to get the stdout/stderr and redirect the
537 // output of the command into this file. We will later read this file
538 // if all goes well and fill the data into "command_output_ptr"
539 FileSpec tmpdir_file_spec;
540 if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) {
541 tmpdir_file_spec.AppendPathComponent("lldb-shell-output.%%%%%%");
542 llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath(),
545 llvm::sys::fs::createTemporaryFile("lldb-shell-output.%%%%%%", "",
550 FileSpec output_file_spec{output_file_path.c_str(), false};
552 launch_info.AppendSuppressFileAction(STDIN_FILENO, true, false);
553 if (output_file_spec) {
554 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_spec, false,
556 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
558 launch_info.AppendSuppressFileAction(STDOUT_FILENO, false, true);
559 launch_info.AppendSuppressFileAction(STDERR_FILENO, false, true);
562 std::shared_ptr<ShellInfo> shell_info_sp(new ShellInfo());
563 const bool monitor_signals = false;
564 launch_info.SetMonitorProcessCallback(
565 std::bind(MonitorShellCommand, shell_info_sp, std::placeholders::_1,
566 std::placeholders::_2, std::placeholders::_3,
567 std::placeholders::_4),
570 error = LaunchProcess(launch_info);
571 const lldb::pid_t pid = launch_info.GetProcessID();
573 if (error.Success() && pid == LLDB_INVALID_PROCESS_ID)
574 error.SetErrorString("failed to get process ID");
576 if (error.Success()) {
577 bool timed_out = false;
578 shell_info_sp->process_reaped.WaitForValueEqualTo(
579 true, std::chrono::seconds(timeout_sec), &timed_out);
581 error.SetErrorString("timed out waiting for shell command to complete");
583 // Kill the process since it didn't complete within the timeout specified
585 // Wait for the monitor callback to get the message
587 shell_info_sp->process_reaped.WaitForValueEqualTo(
588 true, std::chrono::seconds(1), &timed_out);
591 *status_ptr = shell_info_sp->status;
594 *signo_ptr = shell_info_sp->signo;
596 if (command_output_ptr) {
597 command_output_ptr->clear();
598 uint64_t file_size = output_file_spec.GetByteSize();
600 if (file_size > command_output_ptr->max_size()) {
601 error.SetErrorStringWithFormat(
602 "shell command output is too large to fit into a std::string");
604 std::vector<char> command_output(file_size);
605 output_file_spec.ReadFileContents(0, command_output.data(),
608 command_output_ptr->assign(command_output.data(), file_size);
615 if (FileSystem::GetFileExists(output_file_spec))
616 FileSystem::Unlink(output_file_spec);
620 // LaunchProcessPosixSpawn for Apple, Linux, FreeBSD and other GLIBC
623 #if defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || \
624 defined(__GLIBC__) || defined(__NetBSD__)
625 #if !defined(__ANDROID__)
626 // this method needs to be visible to macosx/Host.cpp and
629 short Host::GetPosixspawnFlags(const ProcessLaunchInfo &launch_info) {
630 short flags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
632 #if defined(__APPLE__)
633 if (launch_info.GetFlags().Test(eLaunchFlagExec))
634 flags |= POSIX_SPAWN_SETEXEC; // Darwin specific posix_spawn flag
636 if (launch_info.GetFlags().Test(eLaunchFlagDebug))
637 flags |= POSIX_SPAWN_START_SUSPENDED; // Darwin specific posix_spawn flag
639 if (launch_info.GetFlags().Test(eLaunchFlagDisableASLR))
640 flags |= _POSIX_SPAWN_DISABLE_ASLR; // Darwin specific posix_spawn flag
642 if (launch_info.GetLaunchInSeparateProcessGroup())
643 flags |= POSIX_SPAWN_SETPGROUP;
645 #ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
646 #if defined(__APPLE__) && (defined(__x86_64__) || defined(__i386__))
647 static LazyBool g_use_close_on_exec_flag = eLazyBoolCalculate;
648 if (g_use_close_on_exec_flag == eLazyBoolCalculate) {
649 g_use_close_on_exec_flag = eLazyBoolNo;
651 uint32_t major, minor, update;
652 if (HostInfo::GetOSVersion(major, minor, update)) {
653 // Kernel panic if we use the POSIX_SPAWN_CLOEXEC_DEFAULT on 10.7 or
655 if (major > 10 || (major == 10 && minor > 7)) {
656 // Only enable for 10.8 and later OS versions
657 g_use_close_on_exec_flag = eLazyBoolYes;
662 static LazyBool g_use_close_on_exec_flag = eLazyBoolYes;
664 // Close all files exception those with file actions if this is supported.
665 if (g_use_close_on_exec_flag == eLazyBoolYes)
666 flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;
668 #endif // #if defined (__APPLE__)
672 Error Host::LaunchProcessPosixSpawn(const char *exe_path,
673 const ProcessLaunchInfo &launch_info,
676 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST |
677 LIBLLDB_LOG_PROCESS));
679 posix_spawnattr_t attr;
680 error.SetError(::posix_spawnattr_init(&attr), eErrorTypePOSIX);
682 if (error.Fail() || log)
683 error.PutToLog(log, "::posix_spawnattr_init ( &attr )");
687 // Make a quick class that will cleanup the posix spawn attributes in case
688 // we return in the middle of this function.
689 lldb_utility::CleanUp<posix_spawnattr_t *, int> posix_spawnattr_cleanup(
690 &attr, posix_spawnattr_destroy);
693 sigset_t all_signals;
694 sigemptyset(&no_signals);
695 sigfillset(&all_signals);
696 ::posix_spawnattr_setsigmask(&attr, &no_signals);
697 #if defined(__linux__) || defined(__FreeBSD__)
698 ::posix_spawnattr_setsigdefault(&attr, &no_signals);
700 ::posix_spawnattr_setsigdefault(&attr, &all_signals);
703 short flags = GetPosixspawnFlags(launch_info);
705 error.SetError(::posix_spawnattr_setflags(&attr, flags), eErrorTypePOSIX);
706 if (error.Fail() || log)
707 error.PutToLog(log, "::posix_spawnattr_setflags ( &attr, flags=0x%8.8x )",
712 // posix_spawnattr_setbinpref_np appears to be an Apple extension per:
713 // http://www.unix.com/man-page/OSX/3/posix_spawnattr_setbinpref_np/
714 #if defined(__APPLE__) && !defined(__arm__)
716 // Don't set the binpref if a shell was provided. After all, that's only
717 // going to affect what version of the shell
718 // is launched, not what fork of the binary is launched. We insert "arch
719 // --arch <ARCH> as part of the shell invocation
720 // to do that job on OSX.
722 if (launch_info.GetShell() == nullptr) {
723 // We don't need to do this for ARM, and we really shouldn't now that we
724 // have multiple CPU subtypes and no posix_spawnattr call that allows us
725 // to set which CPU subtype to launch...
726 const ArchSpec &arch_spec = launch_info.GetArchitecture();
727 cpu_type_t cpu = arch_spec.GetMachOCPUType();
728 cpu_type_t sub = arch_spec.GetMachOCPUSubType();
729 if (cpu != 0 && cpu != static_cast<cpu_type_t>(UINT32_MAX) &&
730 cpu != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE) &&
731 !(cpu == 0x01000007 && sub == 8)) // If haswell is specified, don't try
732 // to set the CPU type or we will fail
735 error.SetError(::posix_spawnattr_setbinpref_np(&attr, 1, &cpu, &ocount),
737 if (error.Fail() || log)
738 error.PutToLog(log, "::posix_spawnattr_setbinpref_np ( &attr, 1, "
739 "cpu_type = 0x%8.8x, count => %llu )",
740 cpu, (uint64_t)ocount);
742 if (error.Fail() || ocount != 1)
749 const char *tmp_argv[2];
750 char *const *argv = const_cast<char *const *>(
751 launch_info.GetArguments().GetConstArgumentVector());
752 char *const *envp = const_cast<char *const *>(
753 launch_info.GetEnvironmentEntries().GetConstArgumentVector());
755 // posix_spawn gets very unhappy if it doesn't have at least the program
756 // name in argv[0]. One of the side affects I have noticed is the
758 // variables don't make it into the child process if "argv == NULL"!!!
759 tmp_argv[0] = exe_path;
761 argv = const_cast<char *const *>(tmp_argv);
764 #if !defined(__APPLE__)
765 // manage the working directory
766 char current_dir[PATH_MAX];
767 current_dir[0] = '\0';
770 FileSpec working_dir{launch_info.GetWorkingDirectory()};
772 #if defined(__APPLE__)
773 // Set the working directory on this thread only
774 if (__pthread_chdir(working_dir.GetCString()) < 0) {
775 if (errno == ENOENT) {
776 error.SetErrorStringWithFormat("No such file or directory: %s",
777 working_dir.GetCString());
778 } else if (errno == ENOTDIR) {
779 error.SetErrorStringWithFormat("Path doesn't name a directory: %s",
780 working_dir.GetCString());
782 error.SetErrorStringWithFormat("An unknown error occurred when "
783 "changing directory for process "
789 if (::getcwd(current_dir, sizeof(current_dir)) == NULL) {
790 error.SetError(errno, eErrorTypePOSIX);
791 error.LogIfError(log, "unable to save the current directory");
795 if (::chdir(working_dir.GetCString()) == -1) {
796 error.SetError(errno, eErrorTypePOSIX);
797 error.LogIfError(log, "unable to change working directory to %s",
798 working_dir.GetCString());
804 ::pid_t result_pid = LLDB_INVALID_PROCESS_ID;
805 const size_t num_file_actions = launch_info.GetNumFileActions();
806 if (num_file_actions > 0) {
807 posix_spawn_file_actions_t file_actions;
808 error.SetError(::posix_spawn_file_actions_init(&file_actions),
810 if (error.Fail() || log)
811 error.PutToLog(log, "::posix_spawn_file_actions_init ( &file_actions )");
815 // Make a quick class that will cleanup the posix spawn attributes in case
816 // we return in the middle of this function.
817 lldb_utility::CleanUp<posix_spawn_file_actions_t *, int>
818 posix_spawn_file_actions_cleanup(&file_actions,
819 posix_spawn_file_actions_destroy);
821 for (size_t i = 0; i < num_file_actions; ++i) {
822 const FileAction *launch_file_action =
823 launch_info.GetFileActionAtIndex(i);
824 if (launch_file_action) {
825 if (!AddPosixSpawnFileAction(&file_actions, launch_file_action, log,
832 ::posix_spawnp(&result_pid, exe_path, &file_actions, &attr, argv, envp),
835 if (error.Fail() || log) {
837 log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, "
838 "attr = %p, argv = %p, envp = %p )",
839 result_pid, exe_path, static_cast<void *>(&file_actions),
840 static_cast<void *>(&attr), reinterpret_cast<const void *>(argv),
841 reinterpret_cast<const void *>(envp));
843 for (int ii = 0; argv[ii]; ++ii)
844 log->Printf("argv[%i] = '%s'", ii, argv[ii]);
850 ::posix_spawnp(&result_pid, exe_path, NULL, &attr, argv, envp),
853 if (error.Fail() || log) {
854 error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', "
855 "file_actions = NULL, attr = %p, argv = %p, envp = "
857 result_pid, exe_path, static_cast<void *>(&attr),
858 reinterpret_cast<const void *>(argv),
859 reinterpret_cast<const void *>(envp));
861 for (int ii = 0; argv[ii]; ++ii)
862 log->Printf("argv[%i] = '%s'", ii, argv[ii]);
869 #if defined(__APPLE__)
870 // No more thread specific current working directory
871 __pthread_fchdir(-1);
873 if (::chdir(current_dir) == -1 && error.Success()) {
874 error.SetError(errno, eErrorTypePOSIX);
875 error.LogIfError(log, "unable to change current directory back to %s",
884 bool Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info,
885 Log *log, Error &error) {
889 posix_spawn_file_actions_t *file_actions =
890 reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions);
892 switch (info->GetAction()) {
893 case FileAction::eFileActionNone:
897 case FileAction::eFileActionClose:
898 if (info->GetFD() == -1)
899 error.SetErrorString(
900 "invalid fd for posix_spawn_file_actions_addclose(...)");
903 ::posix_spawn_file_actions_addclose(file_actions, info->GetFD()),
905 if (log && (error.Fail() || log))
907 "posix_spawn_file_actions_addclose (action=%p, fd=%i)",
908 static_cast<void *>(file_actions), info->GetFD());
912 case FileAction::eFileActionDuplicate:
913 if (info->GetFD() == -1)
914 error.SetErrorString(
915 "invalid fd for posix_spawn_file_actions_adddup2(...)");
916 else if (info->GetActionArgument() == -1)
917 error.SetErrorString(
918 "invalid duplicate fd for posix_spawn_file_actions_adddup2(...)");
921 ::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(),
922 info->GetActionArgument()),
924 if (log && (error.Fail() || log))
927 "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)",
928 static_cast<void *>(file_actions), info->GetFD(),
929 info->GetActionArgument());
933 case FileAction::eFileActionOpen:
934 if (info->GetFD() == -1)
935 error.SetErrorString(
936 "invalid fd in posix_spawn_file_actions_addopen(...)");
938 int oflag = info->GetActionArgument();
945 error.SetError(::posix_spawn_file_actions_addopen(
946 file_actions, info->GetFD(),
947 info->GetPath().str().c_str(), oflag, mode),
949 if (error.Fail() || log)
950 error.PutToLog(log, "posix_spawn_file_actions_addopen (action=%p, "
951 "fd=%i, path='%s', oflag=%i, mode=%i)",
952 static_cast<void *>(file_actions), info->GetFD(),
953 info->GetPath().str().c_str(), oflag, mode);
957 return error.Success();
959 #endif // !defined(__ANDROID__)
960 #endif // defined (__APPLE__) || defined (__linux__) || defined (__FreeBSD__) ||
961 // defined (__GLIBC__) || defined(__NetBSD__)
963 #if defined(__linux__) || defined(__FreeBSD__) || defined(__GLIBC__) || \
964 defined(__NetBSD__) || defined(_WIN32)
965 // The functions below implement process launching via posix_spawn() for Linux,
966 // FreeBSD and NetBSD.
968 Error Host::LaunchProcess(ProcessLaunchInfo &launch_info) {
969 std::unique_ptr<ProcessLauncher> delegate_launcher;
971 delegate_launcher.reset(new ProcessLauncherWindows());
972 #elif defined(__linux__)
973 delegate_launcher.reset(new ProcessLauncherLinux());
975 delegate_launcher.reset(new ProcessLauncherPosix());
977 MonitoringProcessLauncher launcher(std::move(delegate_launcher));
980 HostProcess process = launcher.LaunchProcess(launch_info, error);
982 // TODO(zturner): It would be better if the entire HostProcess were returned
983 // instead of writing
984 // it into this structure.
985 launch_info.SetProcessID(process.GetProcessId());
989 #endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
992 void Host::Kill(lldb::pid_t pid, int signo) { ::kill(pid, signo); }
996 #if !defined(__APPLE__)
997 bool Host::OpenFileInExternalEditor(const FileSpec &file_spec,
1004 const UnixSignalsSP &Host::GetUnixSignals() {
1005 static const auto s_unix_signals_sp =
1006 UnixSignals::Create(HostInfo::GetArchitecture());
1007 return s_unix_signals_sp;