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