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