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