Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1 | //===-- Host.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 | |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 10 | // C includes |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 11 | #include <errno.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 12 | #include <limits.h> |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 14 | #include <sys/types.h> |
Zachary Turner | cb37ddd | 2014-10-15 17:27:11 +0000 | [diff] [blame] | 15 | #ifndef _WIN32 |
Deepak Panickal | b36da43 | 2014-01-13 14:55:15 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 17 | #include <dlfcn.h> |
| 18 | #include <grp.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 19 | #include <netdb.h> |
| 20 | #include <pwd.h> |
Jason Molenda | 4a4b5dc | 2013-11-21 02:45:55 +0000 | [diff] [blame] | 21 | #include <sys/stat.h> |
Sylvestre Ledru | 785ee47 | 2013-09-05 15:39:09 +0000 | [diff] [blame] | 22 | #endif |
| 23 | |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 24 | #if defined (__APPLE__) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 25 | #include <mach/mach_port.h> |
Charles Davis | 510938e | 2013-08-27 05:04:57 +0000 | [diff] [blame] | 26 | #include <mach/mach_init.h> |
| 27 | #include <mach-o/dyld.h> |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 28 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 29 | |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 30 | #if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) || defined(__NetBSD__) |
Shawn Best | 8da0bf3 | 2014-11-08 01:41:49 +0000 | [diff] [blame] | 31 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 32 | #include <spawn.h> |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 33 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 34 | #include <sys/wait.h> |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 35 | #include <sys/syscall.h> |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 36 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 37 | |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 38 | #if defined (__FreeBSD__) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 39 | #include <pthread_np.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 42 | // C++ includes |
| 43 | #include <limits> |
| 44 | |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 45 | #include "lldb/Host/FileSystem.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 46 | #include "lldb/Host/Host.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 47 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 48 | #include "lldb/Core/ArchSpec.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 49 | #include "lldb/Core/Error.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 50 | #include "lldb/Core/Log.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 51 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 52 | #include "lldb/Host/HostProcess.h" |
| 53 | #include "lldb/Host/MonitoringProcessLauncher.h" |
Vince Harron | 76861ea | 2015-01-16 06:47:43 +0000 | [diff] [blame] | 54 | #include "lldb/Host/Predicate.h" |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 55 | #include "lldb/Host/ProcessLauncher.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 56 | #include "lldb/Host/ThreadLauncher.h" |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 57 | #include "lldb/lldb-private-forward.h" |
Vince Harron | 76861ea | 2015-01-16 06:47:43 +0000 | [diff] [blame] | 58 | #include "llvm/Support/FileSystem.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 59 | #include "lldb/Target/FileAction.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 60 | #include "lldb/Target/ProcessLaunchInfo.h" |
Vince Harron | 76861ea | 2015-01-16 06:47:43 +0000 | [diff] [blame] | 61 | #include "lldb/Target/UnixSignals.h" |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 62 | #include "lldb/Utility/CleanUp.h" |
Vince Harron | 76861ea | 2015-01-16 06:47:43 +0000 | [diff] [blame] | 63 | #include "llvm/ADT/SmallString.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 64 | |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 65 | #if defined(_WIN32) |
| 66 | #include "lldb/Host/windows/ProcessLauncherWindows.h" |
Tamas Berghammer | 00e305d | 2015-02-12 18:13:44 +0000 | [diff] [blame] | 67 | #elif defined(__ANDROID__) || defined(__ANDROID_NDK__) |
| 68 | #include "lldb/Host/android/ProcessLauncherAndroid.h" |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 69 | #else |
| 70 | #include "lldb/Host/posix/ProcessLauncherPosix.h" |
| 71 | #endif |
| 72 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 73 | #if defined (__APPLE__) |
| 74 | #ifndef _POSIX_SPAWN_DISABLE_ASLR |
| 75 | #define _POSIX_SPAWN_DISABLE_ASLR 0x0100 |
| 76 | #endif |
| 77 | |
| 78 | extern "C" |
| 79 | { |
| 80 | int __pthread_chdir(const char *path); |
| 81 | int __pthread_fchdir (int fildes); |
| 82 | } |
| 83 | |
| 84 | #endif |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 85 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 86 | using namespace lldb; |
| 87 | using namespace lldb_private; |
| 88 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 89 | #if !defined (__APPLE__) && !defined (_WIN32) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 90 | struct MonitorInfo |
| 91 | { |
| 92 | lldb::pid_t pid; // The process ID to monitor |
| 93 | Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals |
| 94 | void *callback_baton; // The callback baton for the callback function |
| 95 | bool monitor_signals; // If true, call the callback when "pid" gets signaled. |
| 96 | }; |
| 97 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 98 | static thread_result_t |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 99 | MonitorChildProcessThreadFunction (void *arg); |
| 100 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 101 | HostThread |
| 102 | Host::StartMonitoringChildProcess(Host::MonitorChildProcessCallback callback, void *callback_baton, lldb::pid_t pid, bool monitor_signals) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 103 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 104 | MonitorInfo * info_ptr = new MonitorInfo(); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 105 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 106 | info_ptr->pid = pid; |
| 107 | info_ptr->callback = callback; |
| 108 | info_ptr->callback_baton = callback_baton; |
| 109 | info_ptr->monitor_signals = monitor_signals; |
| 110 | |
| 111 | char thread_name[256]; |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 112 | ::snprintf(thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid); |
| 113 | return ThreadLauncher::LaunchThread(thread_name, MonitorChildProcessThreadFunction, info_ptr, NULL); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 116 | #ifndef __linux__ |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 117 | //------------------------------------------------------------------ |
| 118 | // Scoped class that will disable thread canceling when it is |
| 119 | // constructed, and exception safely restore the previous value it |
| 120 | // when it goes out of scope. |
| 121 | //------------------------------------------------------------------ |
| 122 | class ScopedPThreadCancelDisabler |
| 123 | { |
| 124 | public: |
| 125 | ScopedPThreadCancelDisabler() |
| 126 | { |
| 127 | // Disable the ability for this thread to be cancelled |
| 128 | int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state); |
| 129 | if (err != 0) |
| 130 | m_old_state = -1; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ~ScopedPThreadCancelDisabler() |
| 134 | { |
| 135 | // Restore the ability for this thread to be cancelled to what it |
| 136 | // previously was. |
| 137 | if (m_old_state != -1) |
| 138 | ::pthread_setcancelstate (m_old_state, 0); |
| 139 | } |
| 140 | private: |
| 141 | int m_old_state; // Save the old cancelability state. |
| 142 | }; |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 143 | #endif // __linux__ |
| 144 | |
| 145 | #ifdef __linux__ |
Omair Javaid | 2040548 | 2015-08-09 19:04:41 +0000 | [diff] [blame] | 146 | #if defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)) |
| 147 | static __thread volatile sig_atomic_t g_usr1_called; |
| 148 | #else |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 149 | static thread_local volatile sig_atomic_t g_usr1_called; |
Omair Javaid | 2040548 | 2015-08-09 19:04:41 +0000 | [diff] [blame] | 150 | #endif |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 151 | |
| 152 | static void |
| 153 | SigUsr1Handler (int) |
| 154 | { |
| 155 | g_usr1_called = 1; |
| 156 | } |
| 157 | #endif // __linux__ |
| 158 | |
| 159 | static bool |
| 160 | CheckForMonitorCancellation() |
| 161 | { |
| 162 | #ifdef __linux__ |
| 163 | if (g_usr1_called) |
| 164 | { |
| 165 | g_usr1_called = 0; |
| 166 | return true; |
| 167 | } |
| 168 | #else |
| 169 | ::pthread_testcancel (); |
| 170 | #endif |
| 171 | return false; |
| 172 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 173 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 174 | static thread_result_t |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 175 | MonitorChildProcessThreadFunction (void *arg) |
| 176 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 177 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 178 | const char *function = __FUNCTION__; |
| 179 | if (log) |
| 180 | log->Printf ("%s (arg = %p) thread starting...", function, arg); |
| 181 | |
| 182 | MonitorInfo *info = (MonitorInfo *)arg; |
| 183 | |
| 184 | const Host::MonitorChildProcessCallback callback = info->callback; |
| 185 | void * const callback_baton = info->callback_baton; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 186 | const bool monitor_signals = info->monitor_signals; |
| 187 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 188 | assert (info->pid <= UINT32_MAX); |
Andrew MacPherson | 82aae0d | 2014-04-02 06:57:45 +0000 | [diff] [blame] | 189 | const ::pid_t pid = monitor_signals ? -1 * getpgid(info->pid) : info->pid; |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 190 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 191 | delete info; |
| 192 | |
| 193 | int status = -1; |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 194 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
Ashok Thirumurthi | 0f3b9b8 | 2013-05-01 20:38:19 +0000 | [diff] [blame] | 195 | #define __WALL 0 |
| 196 | #endif |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 197 | const int options = __WALL; |
| 198 | |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 199 | #ifdef __linux__ |
| 200 | // This signal is only used to interrupt the thread from waitpid |
| 201 | struct sigaction sigUsr1Action; |
| 202 | memset(&sigUsr1Action, 0, sizeof(sigUsr1Action)); |
| 203 | sigUsr1Action.sa_handler = SigUsr1Handler; |
| 204 | ::sigaction(SIGUSR1, &sigUsr1Action, nullptr); |
| 205 | #endif // __linux__ |
| 206 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 207 | while (1) |
| 208 | { |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 209 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 210 | if (log) |
Vince Harron | b956a81 | 2015-01-23 22:48:28 +0000 | [diff] [blame] | 211 | log->Printf("%s ::waitpid (pid = %" PRIi32 ", &status, options = %i)...", function, pid, options); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 212 | |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 213 | if (CheckForMonitorCancellation ()) |
| 214 | break; |
| 215 | |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 216 | // Get signals from all children with same process group of pid |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 217 | const ::pid_t wait_pid = ::waitpid (pid, &status, options); |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 218 | |
| 219 | if (CheckForMonitorCancellation ()) |
| 220 | break; |
| 221 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 222 | if (wait_pid == -1) |
| 223 | { |
| 224 | if (errno == EINTR) |
| 225 | continue; |
| 226 | else |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 227 | { |
| 228 | if (log) |
| 229 | log->Printf ("%s (arg = %p) thread exiting because waitpid failed (%s)...", __FUNCTION__, arg, strerror(errno)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 230 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 231 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 232 | } |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 233 | else if (wait_pid > 0) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 234 | { |
| 235 | bool exited = false; |
| 236 | int signal = 0; |
| 237 | int exit_status = 0; |
| 238 | const char *status_cstr = NULL; |
| 239 | if (WIFSTOPPED(status)) |
| 240 | { |
| 241 | signal = WSTOPSIG(status); |
| 242 | status_cstr = "STOPPED"; |
| 243 | } |
| 244 | else if (WIFEXITED(status)) |
| 245 | { |
| 246 | exit_status = WEXITSTATUS(status); |
| 247 | status_cstr = "EXITED"; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 248 | exited = true; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 249 | } |
| 250 | else if (WIFSIGNALED(status)) |
| 251 | { |
| 252 | signal = WTERMSIG(status); |
| 253 | status_cstr = "SIGNALED"; |
Andrew Kaylor | 7d2abdf | 2013-09-04 16:06:04 +0000 | [diff] [blame] | 254 | if (wait_pid == abs(pid)) { |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 255 | exited = true; |
| 256 | exit_status = -1; |
| 257 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 258 | } |
| 259 | else |
| 260 | { |
Johnny Chen | 4480530 | 2011-07-19 19:48:13 +0000 | [diff] [blame] | 261 | status_cstr = "(\?\?\?)"; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // Scope for pthread_cancel_disabler |
| 265 | { |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 266 | #ifndef __linux__ |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 267 | ScopedPThreadCancelDisabler pthread_cancel_disabler; |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 268 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 269 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 270 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 271 | if (log) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 272 | log->Printf ("%s ::waitpid (pid = %" PRIi32 ", &status, options = %i) => pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 273 | function, |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 274 | pid, |
Vince Harron | b956a81 | 2015-01-23 22:48:28 +0000 | [diff] [blame] | 275 | options, |
| 276 | wait_pid, |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 277 | status, |
| 278 | status_cstr, |
| 279 | signal, |
| 280 | exit_status); |
| 281 | |
| 282 | if (exited || (signal != 0 && monitor_signals)) |
| 283 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 284 | bool callback_return = false; |
| 285 | if (callback) |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 286 | callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 287 | |
| 288 | // If our process exited, then this thread should exit |
Andrew Kaylor | 7d2abdf | 2013-09-04 16:06:04 +0000 | [diff] [blame] | 289 | if (exited && wait_pid == abs(pid)) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 290 | { |
| 291 | if (log) |
| 292 | log->Printf ("%s (arg = %p) thread exiting because pid received exit signal...", __FUNCTION__, arg); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 293 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 294 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 295 | // If the callback returns true, it means this process should |
| 296 | // exit |
| 297 | if (callback_return) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 298 | { |
| 299 | if (log) |
| 300 | log->Printf ("%s (arg = %p) thread exiting because callback returned true...", __FUNCTION__, arg); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 301 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 302 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 308 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 309 | if (log) |
| 310 | log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg); |
| 311 | |
| 312 | return NULL; |
| 313 | } |
| 314 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 315 | #endif // #if !defined (__APPLE__) && !defined (_WIN32) |
| 316 | |
| 317 | #if !defined (__APPLE__) |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 318 | |
| 319 | void |
| 320 | Host::SystemLog (SystemLogType type, const char *format, va_list args) |
| 321 | { |
| 322 | vfprintf (stderr, format, args); |
| 323 | } |
| 324 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 325 | #endif |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 326 | |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 327 | void |
| 328 | Host::SystemLog (SystemLogType type, const char *format, ...) |
| 329 | { |
| 330 | va_list args; |
| 331 | va_start (args, format); |
| 332 | SystemLog (type, format, args); |
| 333 | va_end (args); |
| 334 | } |
| 335 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 336 | lldb::pid_t |
| 337 | Host::GetCurrentProcessID() |
| 338 | { |
| 339 | return ::getpid(); |
| 340 | } |
| 341 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 342 | #ifndef _WIN32 |
| 343 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 344 | lldb::tid_t |
| 345 | Host::GetCurrentThreadID() |
| 346 | { |
| 347 | #if defined (__APPLE__) |
Jean-Daniel Dupas | 3cfa8e2 | 2013-12-06 09:35:53 +0000 | [diff] [blame] | 348 | // Calling "mach_thread_self()" bumps the reference count on the thread |
Greg Clayton | 813ddfc | 2012-09-18 18:19:49 +0000 | [diff] [blame] | 349 | // port, so we need to deallocate it. mach_task_self() doesn't bump the ref |
| 350 | // count. |
| 351 | thread_port_t thread_self = mach_thread_self(); |
| 352 | mach_port_deallocate(mach_task_self(), thread_self); |
| 353 | return thread_self; |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 354 | #elif defined(__FreeBSD__) |
| 355 | return lldb::tid_t(pthread_getthreadid_np()); |
Shawn Best | 8da0bf3 | 2014-11-08 01:41:49 +0000 | [diff] [blame] | 356 | #elif defined(__ANDROID_NDK__) |
| 357 | return lldb::tid_t(gettid()); |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 358 | #elif defined(__linux__) |
| 359 | return lldb::tid_t(syscall(SYS_gettid)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 360 | #else |
| 361 | return lldb::tid_t(pthread_self()); |
| 362 | #endif |
| 363 | } |
| 364 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 365 | lldb::thread_t |
| 366 | Host::GetCurrentThread () |
| 367 | { |
| 368 | return lldb::thread_t(pthread_self()); |
| 369 | } |
| 370 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 371 | const char * |
| 372 | Host::GetSignalAsCString (int signo) |
| 373 | { |
| 374 | switch (signo) |
| 375 | { |
| 376 | case SIGHUP: return "SIGHUP"; // 1 hangup |
| 377 | case SIGINT: return "SIGINT"; // 2 interrupt |
| 378 | case SIGQUIT: return "SIGQUIT"; // 3 quit |
| 379 | case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught) |
| 380 | case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught) |
| 381 | case SIGABRT: return "SIGABRT"; // 6 abort() |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 382 | #if defined(SIGPOLL) |
Sylvestre Ledru | a4cc7de | 2013-12-13 09:51:39 +0000 | [diff] [blame] | 383 | #if !defined(SIGIO) || (SIGPOLL != SIGIO) |
| 384 | // Under some GNU/Linux, SIGPOLL and SIGIO are the same. Causing the build to |
| 385 | // fail with 'multiple define cases with same value' |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 386 | case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported) |
Benjamin Kramer | 44030f1 | 2011-11-04 16:06:40 +0000 | [diff] [blame] | 387 | #endif |
Sylvestre Ledru | a4cc7de | 2013-12-13 09:51:39 +0000 | [diff] [blame] | 388 | #endif |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 389 | #if defined(SIGEMT) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 390 | case SIGEMT: return "SIGEMT"; // 7 EMT instruction |
Benjamin Kramer | 44030f1 | 2011-11-04 16:06:40 +0000 | [diff] [blame] | 391 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 392 | case SIGFPE: return "SIGFPE"; // 8 floating point exception |
| 393 | case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored) |
| 394 | case SIGBUS: return "SIGBUS"; // 10 bus error |
| 395 | case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation |
| 396 | case SIGSYS: return "SIGSYS"; // 12 bad argument to system call |
| 397 | case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it |
| 398 | case SIGALRM: return "SIGALRM"; // 14 alarm clock |
| 399 | case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill |
| 400 | case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel |
| 401 | case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty |
| 402 | case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty |
| 403 | case SIGCONT: return "SIGCONT"; // 19 continue a stopped process |
| 404 | case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit |
| 405 | case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read |
| 406 | case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local<OSTOP) |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 407 | #if defined(SIGIO) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 408 | case SIGIO: return "SIGIO"; // 23 input/output possible signal |
| 409 | #endif |
| 410 | case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit |
| 411 | case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit |
| 412 | case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm |
| 413 | case SIGPROF: return "SIGPROF"; // 27 profiling time alarm |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 414 | #if defined(SIGWINCH) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 415 | case SIGWINCH: return "SIGWINCH"; // 28 window size changes |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 416 | #endif |
| 417 | #if defined(SIGINFO) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 418 | case SIGINFO: return "SIGINFO"; // 29 information request |
| 419 | #endif |
| 420 | case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1 |
| 421 | case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2 |
| 422 | default: |
| 423 | break; |
| 424 | } |
| 425 | return NULL; |
| 426 | } |
| 427 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 428 | #endif |
| 429 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 430 | #ifndef _WIN32 |
| 431 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 432 | lldb::thread_key_t |
| 433 | Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback) |
| 434 | { |
| 435 | pthread_key_t key; |
| 436 | ::pthread_key_create (&key, callback); |
| 437 | return key; |
| 438 | } |
| 439 | |
| 440 | void* |
| 441 | Host::ThreadLocalStorageGet(lldb::thread_key_t key) |
| 442 | { |
| 443 | return ::pthread_getspecific (key); |
| 444 | } |
| 445 | |
| 446 | void |
| 447 | Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value) |
| 448 | { |
| 449 | ::pthread_setspecific (key, value); |
| 450 | } |
| 451 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 452 | #endif |
| 453 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 454 | #if !defined (__APPLE__) // see Host.mm |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 455 | |
| 456 | bool |
| 457 | Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle) |
| 458 | { |
| 459 | bundle.Clear(); |
| 460 | return false; |
| 461 | } |
| 462 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 463 | bool |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 464 | Host::ResolveExecutableInBundle (FileSpec &file) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 465 | { |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 466 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 467 | } |
| 468 | #endif |
| 469 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 470 | #ifndef _WIN32 |
| 471 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 472 | FileSpec |
| 473 | Host::GetModuleFileSpecForHostAddress (const void *host_addr) |
| 474 | { |
| 475 | FileSpec module_filespec; |
Shawn Best | 8da0bf3 | 2014-11-08 01:41:49 +0000 | [diff] [blame] | 476 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 477 | Dl_info info; |
| 478 | if (::dladdr (host_addr, &info)) |
| 479 | { |
| 480 | if (info.dli_fname) |
| 481 | module_filespec.SetFile(info.dli_fname, true); |
| 482 | } |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 483 | #endif |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 484 | return module_filespec; |
| 485 | } |
| 486 | |
| 487 | #endif |
| 488 | |
Matt Kopec | 085d6ce | 2013-05-31 22:00:07 +0000 | [diff] [blame] | 489 | #if !defined(__linux__) |
| 490 | bool |
| 491 | Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach) |
| 492 | { |
| 493 | return false; |
| 494 | } |
| 495 | #endif |
| 496 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 497 | struct ShellInfo |
| 498 | { |
| 499 | ShellInfo () : |
| 500 | process_reaped (false), |
| 501 | can_delete (false), |
| 502 | pid (LLDB_INVALID_PROCESS_ID), |
| 503 | signo(-1), |
| 504 | status(-1) |
| 505 | { |
| 506 | } |
| 507 | |
| 508 | lldb_private::Predicate<bool> process_reaped; |
| 509 | lldb_private::Predicate<bool> can_delete; |
| 510 | lldb::pid_t pid; |
| 511 | int signo; |
| 512 | int status; |
| 513 | }; |
| 514 | |
| 515 | static bool |
| 516 | MonitorShellCommand (void *callback_baton, |
| 517 | lldb::pid_t pid, |
| 518 | bool exited, // True if the process did exit |
| 519 | int signo, // Zero for no signal |
| 520 | int status) // Exit value of process if signal is zero |
| 521 | { |
| 522 | ShellInfo *shell_info = (ShellInfo *)callback_baton; |
| 523 | shell_info->pid = pid; |
| 524 | shell_info->signo = signo; |
| 525 | shell_info->status = status; |
| 526 | // Let the thread running Host::RunShellCommand() know that the process |
| 527 | // exited and that ShellInfo has been filled in by broadcasting to it |
| 528 | shell_info->process_reaped.SetValue(1, eBroadcastAlways); |
| 529 | // Now wait for a handshake back from that thread running Host::RunShellCommand |
| 530 | // so we know that we can delete shell_info_ptr |
| 531 | shell_info->can_delete.WaitForValueEqualTo(true); |
| 532 | // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete... |
| 533 | usleep(1000); |
| 534 | // Now delete the shell info that was passed into this function |
| 535 | delete shell_info; |
| 536 | return true; |
| 537 | } |
| 538 | |
| 539 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 540 | Host::RunShellCommand(const char *command, |
| 541 | const FileSpec &working_dir, |
| 542 | int *status_ptr, |
| 543 | int *signo_ptr, |
| 544 | std::string *command_output_ptr, |
| 545 | uint32_t timeout_sec, |
| 546 | bool run_in_default_shell) |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 547 | { |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 548 | return RunShellCommand(Args(command), working_dir, status_ptr, signo_ptr, command_output_ptr, timeout_sec, run_in_default_shell); |
| 549 | } |
| 550 | |
| 551 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 552 | Host::RunShellCommand(const Args &args, |
| 553 | const FileSpec &working_dir, |
| 554 | int *status_ptr, |
| 555 | int *signo_ptr, |
| 556 | std::string *command_output_ptr, |
| 557 | uint32_t timeout_sec, |
| 558 | bool run_in_default_shell) |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 559 | { |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 560 | Error error; |
| 561 | ProcessLaunchInfo launch_info; |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 562 | launch_info.SetArchitecture(HostInfo::GetArchitecture()); |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 563 | if (run_in_default_shell) |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 564 | { |
| 565 | // Run the command in a shell |
Zachary Turner | 10687b0 | 2014-10-20 17:46:43 +0000 | [diff] [blame] | 566 | launch_info.SetShell(HostInfo::GetDefaultShell()); |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 567 | launch_info.GetArguments().AppendArguments(args); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 568 | const bool localhost = true; |
| 569 | const bool will_debug = false; |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 570 | const bool first_arg_is_full_shell_command = false; |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 571 | launch_info.ConvertArgumentsForLaunchingInShell (error, |
| 572 | localhost, |
| 573 | will_debug, |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 574 | first_arg_is_full_shell_command, |
| 575 | 0); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 576 | } |
| 577 | else |
| 578 | { |
| 579 | // No shell, just run it |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 580 | const bool first_arg_is_executable = true; |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 581 | launch_info.SetArguments(args, first_arg_is_executable); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 582 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 583 | |
| 584 | if (working_dir) |
| 585 | launch_info.SetWorkingDirectory(working_dir); |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 586 | llvm::SmallString<PATH_MAX> output_file_path; |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 587 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 588 | if (command_output_ptr) |
| 589 | { |
| 590 | // Create a temporary file to get the stdout/stderr and redirect the |
| 591 | // output of the command into this file. We will later read this file |
| 592 | // if all goes well and fill the data into "command_output_ptr" |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 593 | FileSpec tmpdir_file_spec; |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 594 | if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 595 | { |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 596 | tmpdir_file_spec.AppendPathComponent("lldb-shell-output.%%%%%%"); |
| 597 | llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath().c_str(), output_file_path); |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 598 | } |
| 599 | else |
| 600 | { |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 601 | llvm::sys::fs::createTemporaryFile("lldb-shell-output.%%%%%%", "", output_file_path); |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 602 | } |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 603 | } |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 604 | |
| 605 | FileSpec output_file_spec{output_file_path.c_str(), false}; |
| 606 | |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 607 | launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 608 | if (output_file_spec) |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 609 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 610 | launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_spec, false, true); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 611 | launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 612 | } |
| 613 | else |
| 614 | { |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 615 | launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 616 | launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true); |
| 617 | } |
| 618 | |
| 619 | // The process monitor callback will delete the 'shell_info_ptr' below... |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 620 | std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo()); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 621 | |
| 622 | const bool monitor_signals = false; |
| 623 | launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals); |
| 624 | |
| 625 | error = LaunchProcess (launch_info); |
| 626 | const lldb::pid_t pid = launch_info.GetProcessID(); |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 627 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 628 | if (error.Success() && pid == LLDB_INVALID_PROCESS_ID) |
| 629 | error.SetErrorString("failed to get process ID"); |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 630 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 631 | if (error.Success()) |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 632 | { |
| 633 | // The process successfully launched, so we can defer ownership of |
| 634 | // "shell_info" to the MonitorShellCommand callback function that will |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 635 | // get called when the process dies. We release the unique pointer as it |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 636 | // doesn't need to delete the ShellInfo anymore. |
| 637 | ShellInfo *shell_info = shell_info_ap.release(); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 638 | TimeValue *timeout_ptr = nullptr; |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 639 | TimeValue timeout_time(TimeValue::Now()); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 640 | if (timeout_sec > 0) { |
| 641 | timeout_time.OffsetWithSeconds(timeout_sec); |
| 642 | timeout_ptr = &timeout_time; |
| 643 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 644 | bool timed_out = false; |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 645 | shell_info->process_reaped.WaitForValueEqualTo(true, timeout_ptr, &timed_out); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 646 | if (timed_out) |
| 647 | { |
| 648 | error.SetErrorString("timed out waiting for shell command to complete"); |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 649 | |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 650 | // Kill the process since it didn't complete within the timeout specified |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 651 | Kill (pid, SIGKILL); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 652 | // Wait for the monitor callback to get the message |
| 653 | timeout_time = TimeValue::Now(); |
| 654 | timeout_time.OffsetWithSeconds(1); |
| 655 | timed_out = false; |
| 656 | shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out); |
| 657 | } |
| 658 | else |
| 659 | { |
| 660 | if (status_ptr) |
| 661 | *status_ptr = shell_info->status; |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 662 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 663 | if (signo_ptr) |
| 664 | *signo_ptr = shell_info->signo; |
Enrico Granata | 66eda73 | 2015-04-17 01:50:11 +0000 | [diff] [blame] | 665 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 666 | if (command_output_ptr) |
| 667 | { |
| 668 | command_output_ptr->clear(); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 669 | uint64_t file_size = output_file_spec.GetByteSize(); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 670 | if (file_size > 0) |
| 671 | { |
| 672 | if (file_size > command_output_ptr->max_size()) |
| 673 | { |
| 674 | error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string"); |
| 675 | } |
| 676 | else |
| 677 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 678 | std::vector<char> command_output(file_size); |
| 679 | output_file_spec.ReadFileContents(0, command_output.data(), file_size, &error); |
| 680 | if (error.Success()) |
| 681 | command_output_ptr->assign(command_output.data(), file_size); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | shell_info->can_delete.SetValue(true, eBroadcastAlways); |
| 687 | } |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 688 | |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 689 | if (FileSystem::GetFileExists(output_file_spec)) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 690 | FileSystem::Unlink(output_file_spec); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 691 | // Handshake with the monitor thread, or just let it know in advance that |
| 692 | // it can delete "shell_info" in case we timed out and were not able to kill |
| 693 | // the process... |
| 694 | return error; |
| 695 | } |
| 696 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 697 | // LaunchProcessPosixSpawn for Apple, Linux, FreeBSD and other GLIBC |
| 698 | // systems |
| 699 | |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 700 | #if defined (__APPLE__) || defined (__linux__) || defined (__FreeBSD__) || defined (__GLIBC__) || defined(__NetBSD__) |
Tamas Berghammer | 00e305d | 2015-02-12 18:13:44 +0000 | [diff] [blame] | 701 | #if !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 702 | // this method needs to be visible to macosx/Host.cpp and |
| 703 | // common/Host.cpp. |
| 704 | |
| 705 | short |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 706 | Host::GetPosixspawnFlags(const ProcessLaunchInfo &launch_info) |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 707 | { |
| 708 | short flags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK; |
| 709 | |
| 710 | #if defined (__APPLE__) |
| 711 | if (launch_info.GetFlags().Test (eLaunchFlagExec)) |
| 712 | flags |= POSIX_SPAWN_SETEXEC; // Darwin specific posix_spawn flag |
| 713 | |
| 714 | if (launch_info.GetFlags().Test (eLaunchFlagDebug)) |
| 715 | flags |= POSIX_SPAWN_START_SUSPENDED; // Darwin specific posix_spawn flag |
| 716 | |
| 717 | if (launch_info.GetFlags().Test (eLaunchFlagDisableASLR)) |
| 718 | flags |= _POSIX_SPAWN_DISABLE_ASLR; // Darwin specific posix_spawn flag |
| 719 | |
| 720 | if (launch_info.GetLaunchInSeparateProcessGroup()) |
| 721 | flags |= POSIX_SPAWN_SETPGROUP; |
| 722 | |
| 723 | #ifdef POSIX_SPAWN_CLOEXEC_DEFAULT |
| 724 | #if defined (__APPLE__) && (defined (__x86_64__) || defined (__i386__)) |
| 725 | static LazyBool g_use_close_on_exec_flag = eLazyBoolCalculate; |
| 726 | if (g_use_close_on_exec_flag == eLazyBoolCalculate) |
| 727 | { |
| 728 | g_use_close_on_exec_flag = eLazyBoolNo; |
| 729 | |
| 730 | uint32_t major, minor, update; |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 731 | if (HostInfo::GetOSVersion(major, minor, update)) |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 732 | { |
| 733 | // Kernel panic if we use the POSIX_SPAWN_CLOEXEC_DEFAULT on 10.7 or earlier |
| 734 | if (major > 10 || (major == 10 && minor > 7)) |
| 735 | { |
| 736 | // Only enable for 10.8 and later OS versions |
| 737 | g_use_close_on_exec_flag = eLazyBoolYes; |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | #else |
| 742 | static LazyBool g_use_close_on_exec_flag = eLazyBoolYes; |
| 743 | #endif |
| 744 | // Close all files exception those with file actions if this is supported. |
| 745 | if (g_use_close_on_exec_flag == eLazyBoolYes) |
| 746 | flags |= POSIX_SPAWN_CLOEXEC_DEFAULT; |
| 747 | #endif |
| 748 | #endif // #if defined (__APPLE__) |
| 749 | return flags; |
| 750 | } |
| 751 | |
| 752 | Error |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 753 | Host::LaunchProcessPosixSpawn(const char *exe_path, const ProcessLaunchInfo &launch_info, lldb::pid_t &pid) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 754 | { |
| 755 | Error error; |
| 756 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_PROCESS)); |
| 757 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 758 | posix_spawnattr_t attr; |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 759 | error.SetError( ::posix_spawnattr_init (&attr), eErrorTypePOSIX); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 760 | |
| 761 | if (error.Fail() || log) |
| 762 | error.PutToLog(log, "::posix_spawnattr_init ( &attr )"); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 763 | if (error.Fail()) |
| 764 | return error; |
| 765 | |
| 766 | // Make a quick class that will cleanup the posix spawn attributes in case |
| 767 | // we return in the middle of this function. |
| 768 | lldb_utility::CleanUp <posix_spawnattr_t *, int> posix_spawnattr_cleanup(&attr, posix_spawnattr_destroy); |
| 769 | |
| 770 | sigset_t no_signals; |
| 771 | sigset_t all_signals; |
| 772 | sigemptyset (&no_signals); |
| 773 | sigfillset (&all_signals); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 774 | ::posix_spawnattr_setsigmask(&attr, &no_signals); |
Todd Fiala | 571768c | 2014-01-23 17:07:54 +0000 | [diff] [blame] | 775 | #if defined (__linux__) || defined (__FreeBSD__) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 776 | ::posix_spawnattr_setsigdefault(&attr, &no_signals); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 777 | #else |
| 778 | ::posix_spawnattr_setsigdefault(&attr, &all_signals); |
| 779 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 780 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 781 | short flags = GetPosixspawnFlags(launch_info); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 782 | |
| 783 | error.SetError( ::posix_spawnattr_setflags (&attr, flags), eErrorTypePOSIX); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 784 | if (error.Fail() || log) |
| 785 | error.PutToLog(log, "::posix_spawnattr_setflags ( &attr, flags=0x%8.8x )", flags); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 786 | if (error.Fail()) |
| 787 | return error; |
| 788 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 789 | // posix_spawnattr_setbinpref_np appears to be an Apple extension per: |
| 790 | // http://www.unix.com/man-page/OSX/3/posix_spawnattr_setbinpref_np/ |
| 791 | #if defined (__APPLE__) && !defined (__arm__) |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 792 | |
| 793 | // Don't set the binpref if a shell was provided. After all, that's only going to affect what version of the shell |
| 794 | // is launched, not what fork of the binary is launched. We insert "arch --arch <ARCH> as part of the shell invocation |
| 795 | // to do that job on OSX. |
| 796 | |
| 797 | if (launch_info.GetShell() == nullptr) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 798 | { |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 799 | // We don't need to do this for ARM, and we really shouldn't now that we |
| 800 | // have multiple CPU subtypes and no posix_spawnattr call that allows us |
| 801 | // to set which CPU subtype to launch... |
| 802 | const ArchSpec &arch_spec = launch_info.GetArchitecture(); |
| 803 | cpu_type_t cpu = arch_spec.GetMachOCPUType(); |
| 804 | cpu_type_t sub = arch_spec.GetMachOCPUSubType(); |
| 805 | if (cpu != 0 && |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame] | 806 | cpu != static_cast<cpu_type_t>(UINT32_MAX) && |
| 807 | cpu != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE) && |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 808 | !(cpu == 0x01000007 && sub == 8)) // If haswell is specified, don't try to set the CPU type or we will fail |
| 809 | { |
| 810 | size_t ocount = 0; |
| 811 | error.SetError( ::posix_spawnattr_setbinpref_np (&attr, 1, &cpu, &ocount), eErrorTypePOSIX); |
| 812 | if (error.Fail() || log) |
| 813 | error.PutToLog(log, "::posix_spawnattr_setbinpref_np ( &attr, 1, cpu_type = 0x%8.8x, count => %llu )", cpu, (uint64_t)ocount); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 814 | |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 815 | if (error.Fail() || ocount != 1) |
| 816 | return error; |
| 817 | } |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 820 | #endif |
| 821 | |
| 822 | const char *tmp_argv[2]; |
Saleem Abdulrasool | ba507b0 | 2015-10-18 19:34:38 +0000 | [diff] [blame^] | 823 | char * const *argv = const_cast<char * const*>(launch_info.GetArguments().GetConstArgumentVector()); |
| 824 | char * const *envp = const_cast<char * const*>(launch_info.GetEnvironmentEntries().GetConstArgumentVector()); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 825 | if (argv == NULL) |
| 826 | { |
| 827 | // posix_spawn gets very unhappy if it doesn't have at least the program |
| 828 | // name in argv[0]. One of the side affects I have noticed is the environment |
| 829 | // variables don't make it into the child process if "argv == NULL"!!! |
| 830 | tmp_argv[0] = exe_path; |
| 831 | tmp_argv[1] = NULL; |
Saleem Abdulrasool | ba507b0 | 2015-10-18 19:34:38 +0000 | [diff] [blame^] | 832 | argv = const_cast<char * const*>(tmp_argv); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | #if !defined (__APPLE__) |
| 836 | // manage the working directory |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 837 | char current_dir[PATH_MAX]; |
| 838 | current_dir[0] = '\0'; |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 839 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 840 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 841 | FileSpec working_dir{launch_info.GetWorkingDirectory()}; |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 842 | if (working_dir) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 843 | { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 844 | #if defined (__APPLE__) |
| 845 | // Set the working directory on this thread only |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 846 | if (__pthread_chdir(working_dir.GetCString()) < 0) { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 847 | if (errno == ENOENT) { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 848 | error.SetErrorStringWithFormat("No such file or directory: %s", |
| 849 | working_dir.GetCString()); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 850 | } else if (errno == ENOTDIR) { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 851 | error.SetErrorStringWithFormat("Path doesn't name a directory: %s", |
| 852 | working_dir.GetCString()); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 853 | } else { |
| 854 | error.SetErrorStringWithFormat("An unknown error occurred when changing directory for process execution."); |
| 855 | } |
| 856 | return error; |
| 857 | } |
| 858 | #else |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 859 | if (::getcwd(current_dir, sizeof(current_dir)) == NULL) |
| 860 | { |
| 861 | error.SetError(errno, eErrorTypePOSIX); |
| 862 | error.LogIfError(log, "unable to save the current directory"); |
| 863 | return error; |
| 864 | } |
| 865 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 866 | if (::chdir(working_dir.GetCString()) == -1) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 867 | { |
| 868 | error.SetError(errno, eErrorTypePOSIX); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 869 | error.LogIfError(log, "unable to change working directory to %s", |
| 870 | working_dir.GetCString()); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 871 | return error; |
| 872 | } |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 873 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 876 | ::pid_t result_pid = LLDB_INVALID_PROCESS_ID; |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 877 | const size_t num_file_actions = launch_info.GetNumFileActions (); |
| 878 | if (num_file_actions > 0) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 879 | { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 880 | posix_spawn_file_actions_t file_actions; |
| 881 | error.SetError( ::posix_spawn_file_actions_init (&file_actions), eErrorTypePOSIX); |
| 882 | if (error.Fail() || log) |
| 883 | error.PutToLog(log, "::posix_spawn_file_actions_init ( &file_actions )"); |
| 884 | if (error.Fail()) |
| 885 | return error; |
| 886 | |
| 887 | // Make a quick class that will cleanup the posix spawn attributes in case |
| 888 | // we return in the middle of this function. |
| 889 | lldb_utility::CleanUp <posix_spawn_file_actions_t *, int> posix_spawn_file_actions_cleanup (&file_actions, posix_spawn_file_actions_destroy); |
| 890 | |
| 891 | for (size_t i=0; i<num_file_actions; ++i) |
| 892 | { |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 893 | const FileAction *launch_file_action = launch_info.GetFileActionAtIndex(i); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 894 | if (launch_file_action) |
| 895 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 896 | if (!AddPosixSpawnFileAction(&file_actions, launch_file_action, log, error)) |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 897 | return error; |
| 898 | } |
| 899 | } |
| 900 | |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 901 | error.SetError(::posix_spawnp(&result_pid, exe_path, &file_actions, &attr, argv, envp), eErrorTypePOSIX); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 902 | |
| 903 | if (error.Fail() || log) |
| 904 | { |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 905 | error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = %p, attr = %p, argv = %p, envp = %p )", result_pid, |
| 906 | exe_path, static_cast<void *>(&file_actions), static_cast<void *>(&attr), reinterpret_cast<const void *>(argv), |
| 907 | reinterpret_cast<const void *>(envp)); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 908 | if (log) |
| 909 | { |
| 910 | for (int ii=0; argv[ii]; ++ii) |
| 911 | log->Printf("argv[%i] = '%s'", ii, argv[ii]); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | } |
| 916 | else |
| 917 | { |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 918 | error.SetError(::posix_spawnp(&result_pid, exe_path, NULL, &attr, argv, envp), eErrorTypePOSIX); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 919 | |
| 920 | if (error.Fail() || log) |
| 921 | { |
| 922 | error.PutToLog(log, "::posix_spawnp ( pid => %i, path = '%s', file_actions = NULL, attr = %p, argv = %p, envp = %p )", |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 923 | result_pid, exe_path, static_cast<void *>(&attr), reinterpret_cast<const void *>(argv), |
| 924 | reinterpret_cast<const void *>(envp)); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 925 | if (log) |
| 926 | { |
| 927 | for (int ii=0; argv[ii]; ++ii) |
| 928 | log->Printf("argv[%i] = '%s'", ii, argv[ii]); |
| 929 | } |
| 930 | } |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 931 | } |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 932 | pid = result_pid; |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 933 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 934 | if (working_dir) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 935 | { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 936 | #if defined (__APPLE__) |
| 937 | // No more thread specific current working directory |
| 938 | __pthread_fchdir (-1); |
| 939 | #else |
| 940 | if (::chdir(current_dir) == -1 && error.Success()) |
| 941 | { |
| 942 | error.SetError(errno, eErrorTypePOSIX); |
| 943 | error.LogIfError(log, "unable to change current directory back to %s", |
| 944 | current_dir); |
| 945 | } |
| 946 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | return error; |
| 950 | } |
| 951 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 952 | bool |
| 953 | Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info, Log *log, Error &error) |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 954 | { |
| 955 | if (info == NULL) |
| 956 | return false; |
| 957 | |
| 958 | posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions); |
| 959 | |
| 960 | switch (info->GetAction()) |
| 961 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 962 | case FileAction::eFileActionNone: |
| 963 | error.Clear(); |
| 964 | break; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 965 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 966 | case FileAction::eFileActionClose: |
| 967 | if (info->GetFD() == -1) |
| 968 | error.SetErrorString("invalid fd for posix_spawn_file_actions_addclose(...)"); |
| 969 | else |
| 970 | { |
| 971 | error.SetError(::posix_spawn_file_actions_addclose(file_actions, info->GetFD()), eErrorTypePOSIX); |
| 972 | if (log && (error.Fail() || log)) |
| 973 | error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)", |
| 974 | static_cast<void *>(file_actions), info->GetFD()); |
| 975 | } |
| 976 | break; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 977 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 978 | case FileAction::eFileActionDuplicate: |
| 979 | if (info->GetFD() == -1) |
| 980 | error.SetErrorString("invalid fd for posix_spawn_file_actions_adddup2(...)"); |
| 981 | else if (info->GetActionArgument() == -1) |
| 982 | error.SetErrorString("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)"); |
| 983 | else |
| 984 | { |
| 985 | error.SetError( |
| 986 | ::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(), info->GetActionArgument()), |
| 987 | eErrorTypePOSIX); |
| 988 | if (log && (error.Fail() || log)) |
| 989 | error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)", |
| 990 | static_cast<void *>(file_actions), info->GetFD(), info->GetActionArgument()); |
| 991 | } |
| 992 | break; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 993 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 994 | case FileAction::eFileActionOpen: |
| 995 | if (info->GetFD() == -1) |
| 996 | error.SetErrorString("invalid fd in posix_spawn_file_actions_addopen(...)"); |
| 997 | else |
| 998 | { |
| 999 | int oflag = info->GetActionArgument(); |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1000 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1001 | mode_t mode = 0; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1002 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1003 | if (oflag & O_CREAT) |
| 1004 | mode = 0640; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1005 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1006 | error.SetError( |
| 1007 | ::posix_spawn_file_actions_addopen(file_actions, info->GetFD(), info->GetPath(), oflag, mode), |
| 1008 | eErrorTypePOSIX); |
| 1009 | if (error.Fail() || log) |
| 1010 | error.PutToLog(log, |
| 1011 | "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)", |
| 1012 | static_cast<void *>(file_actions), info->GetFD(), info->GetPath(), oflag, mode); |
| 1013 | } |
| 1014 | break; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1015 | } |
| 1016 | return error.Success(); |
| 1017 | } |
Tamas Berghammer | 00e305d | 2015-02-12 18:13:44 +0000 | [diff] [blame] | 1018 | #endif // !defined(__ANDROID__) && !defined(__ANDROID_NDK__) |
| 1019 | #endif // defined (__APPLE__) || defined (__linux__) || defined (__FreeBSD__) || defined (__GLIBC__) || defined(__NetBSD__) |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1020 | |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 1021 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__GLIBC__) || defined(__NetBSD__) || defined(_WIN32) |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 1022 | // The functions below implement process launching via posix_spawn() for Linux, |
| 1023 | // FreeBSD and NetBSD. |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1024 | |
| 1025 | Error |
| 1026 | Host::LaunchProcess (ProcessLaunchInfo &launch_info) |
| 1027 | { |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 1028 | std::unique_ptr<ProcessLauncher> delegate_launcher; |
| 1029 | #if defined(_WIN32) |
| 1030 | delegate_launcher.reset(new ProcessLauncherWindows()); |
Tamas Berghammer | 00e305d | 2015-02-12 18:13:44 +0000 | [diff] [blame] | 1031 | #elif defined(__ANDROID__) || defined(__ANDROID_NDK__) |
| 1032 | delegate_launcher.reset(new ProcessLauncherAndroid()); |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 1033 | #else |
| 1034 | delegate_launcher.reset(new ProcessLauncherPosix()); |
| 1035 | #endif |
| 1036 | MonitoringProcessLauncher launcher(std::move(delegate_launcher)); |
| 1037 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1038 | Error error; |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 1039 | HostProcess process = launcher.LaunchProcess(launch_info, error); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1040 | |
Zachary Turner | 172d37d | 2014-10-14 21:55:08 +0000 | [diff] [blame] | 1041 | // TODO(zturner): It would be better if the entire HostProcess were returned instead of writing |
| 1042 | // it into this structure. |
| 1043 | launch_info.SetProcessID(process.GetProcessId()); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1044 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1045 | return error; |
| 1046 | } |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 1047 | #endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1048 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1049 | #ifndef _WIN32 |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1050 | void |
| 1051 | Host::Kill(lldb::pid_t pid, int signo) |
| 1052 | { |
| 1053 | ::kill(pid, signo); |
| 1054 | } |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 1055 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1056 | #endif |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1057 | |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 1058 | #if !defined (__APPLE__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1059 | bool |
Greg Clayton | 3b14763 | 2010-12-18 01:54:34 +0000 | [diff] [blame] | 1060 | Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1061 | { |
| 1062 | return false; |
| 1063 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1064 | |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 1065 | void |
| 1066 | Host::SetCrashDescriptionWithFormat (const char *format, ...) |
| 1067 | { |
| 1068 | } |
| 1069 | |
| 1070 | void |
| 1071 | Host::SetCrashDescription (const char *description) |
| 1072 | { |
| 1073 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1074 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1075 | #endif |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 1076 | |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 1077 | const UnixSignalsSP & |
| 1078 | Host::GetUnixSignals() |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 1079 | { |
Chaoren Lin | 98d0a4b | 2015-07-14 01:09:28 +0000 | [diff] [blame] | 1080 | static const auto s_unix_signals_sp = UnixSignals::Create(HostInfo::GetArchitecture()); |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 1081 | return s_unix_signals_sp; |
| 1082 | } |