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 | |
| 30 | #if !defined (__GNU__) && !defined (_WIN32) |
| 31 | // Does not exist under GNU/HURD or Windows |
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__) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 42 | #include <spawn.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 43 | #include <sys/wait.h> |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 44 | #include <sys/syscall.h> |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 45 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 46 | |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 47 | #if defined (__FreeBSD__) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 48 | #include <pthread_np.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 51 | // C++ includes |
| 52 | #include <limits> |
| 53 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 54 | #include "lldb/Host/Host.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 55 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 56 | #include "lldb/Core/ArchSpec.h" |
| 57 | #include "lldb/Core/ConstString.h" |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 58 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 59 | #include "lldb/Core/Error.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 60 | #include "lldb/Core/Log.h" |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 61 | #include "lldb/Core/Module.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 62 | #include "lldb/Core/StreamString.h" |
Jim Ingham | c075ecd | 2012-05-04 19:24:49 +0000 | [diff] [blame] | 63 | #include "lldb/Core/ThreadSafeSTLMap.h" |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 64 | #include "lldb/Host/Config.h" |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 65 | #include "lldb/Host/Endian.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 66 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 67 | #include "lldb/Host/FileSystem.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 68 | #include "lldb/Host/Mutex.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 69 | #include "lldb/Target/FileAction.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 70 | #include "lldb/Target/Process.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 71 | #include "lldb/Target/ProcessLaunchInfo.h" |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 72 | #include "lldb/Target/TargetList.h" |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 73 | #include "lldb/Utility/CleanUp.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 74 | |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 75 | #include "llvm/ADT/STLExtras.h" |
Daniel Malea | fa7425d | 2013-08-06 21:40:08 +0000 | [diff] [blame] | 76 | #include "llvm/ADT/SmallString.h" |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 77 | #include "llvm/Support/Host.h" |
Zachary Turner | 9e757b7 | 2014-07-28 16:45:05 +0000 | [diff] [blame] | 78 | #include "llvm/Support/Path.h" |
Daniel Malea | fa7425d | 2013-08-06 21:40:08 +0000 | [diff] [blame] | 79 | #include "llvm/Support/raw_ostream.h" |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 80 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 81 | #if defined (__APPLE__) |
| 82 | #ifndef _POSIX_SPAWN_DISABLE_ASLR |
| 83 | #define _POSIX_SPAWN_DISABLE_ASLR 0x0100 |
| 84 | #endif |
| 85 | |
| 86 | extern "C" |
| 87 | { |
| 88 | int __pthread_chdir(const char *path); |
| 89 | int __pthread_fchdir (int fildes); |
| 90 | } |
| 91 | |
| 92 | #endif |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 93 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 94 | using namespace lldb; |
| 95 | using namespace lldb_private; |
| 96 | |
Todd Fiala | 17096d7 | 2014-07-16 19:03:16 +0000 | [diff] [blame] | 97 | // Define maximum thread name length |
| 98 | #if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__NetBSD__) |
| 99 | uint32_t const Host::MAX_THREAD_NAME_LENGTH = 16; |
| 100 | #else |
| 101 | uint32_t const Host::MAX_THREAD_NAME_LENGTH = std::numeric_limits<uint32_t>::max (); |
| 102 | #endif |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 103 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 104 | #if !defined (__APPLE__) && !defined (_WIN32) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 105 | struct MonitorInfo |
| 106 | { |
| 107 | lldb::pid_t pid; // The process ID to monitor |
| 108 | Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals |
| 109 | void *callback_baton; // The callback baton for the callback function |
| 110 | bool monitor_signals; // If true, call the callback when "pid" gets signaled. |
| 111 | }; |
| 112 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 113 | static thread_result_t |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 114 | MonitorChildProcessThreadFunction (void *arg); |
| 115 | |
| 116 | lldb::thread_t |
| 117 | Host::StartMonitoringChildProcess |
| 118 | ( |
| 119 | Host::MonitorChildProcessCallback callback, |
| 120 | void *callback_baton, |
| 121 | lldb::pid_t pid, |
| 122 | bool monitor_signals |
| 123 | ) |
| 124 | { |
| 125 | lldb::thread_t thread = LLDB_INVALID_HOST_THREAD; |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 126 | MonitorInfo * info_ptr = new MonitorInfo(); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 127 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 128 | info_ptr->pid = pid; |
| 129 | info_ptr->callback = callback; |
| 130 | info_ptr->callback_baton = callback_baton; |
| 131 | info_ptr->monitor_signals = monitor_signals; |
| 132 | |
| 133 | char thread_name[256]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 134 | ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid); |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 135 | thread = ThreadCreate (thread_name, |
| 136 | MonitorChildProcessThreadFunction, |
| 137 | info_ptr, |
| 138 | NULL); |
| 139 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 140 | return thread; |
| 141 | } |
| 142 | |
| 143 | //------------------------------------------------------------------ |
| 144 | // Scoped class that will disable thread canceling when it is |
| 145 | // constructed, and exception safely restore the previous value it |
| 146 | // when it goes out of scope. |
| 147 | //------------------------------------------------------------------ |
| 148 | class ScopedPThreadCancelDisabler |
| 149 | { |
| 150 | public: |
| 151 | ScopedPThreadCancelDisabler() |
| 152 | { |
| 153 | // Disable the ability for this thread to be cancelled |
| 154 | int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state); |
| 155 | if (err != 0) |
| 156 | m_old_state = -1; |
| 157 | |
| 158 | } |
| 159 | |
| 160 | ~ScopedPThreadCancelDisabler() |
| 161 | { |
| 162 | // Restore the ability for this thread to be cancelled to what it |
| 163 | // previously was. |
| 164 | if (m_old_state != -1) |
| 165 | ::pthread_setcancelstate (m_old_state, 0); |
| 166 | } |
| 167 | private: |
| 168 | int m_old_state; // Save the old cancelability state. |
| 169 | }; |
| 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 |
| 203 | ::pthread_testcancel (); |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 204 | // Get signals from all children with same process group of pid |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 205 | const ::pid_t wait_pid = ::waitpid (pid, &status, options); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 206 | ::pthread_testcancel (); |
| 207 | |
| 208 | if (wait_pid == -1) |
| 209 | { |
| 210 | if (errno == EINTR) |
| 211 | continue; |
| 212 | else |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 213 | { |
| 214 | if (log) |
| 215 | 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] | 216 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 217 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 218 | } |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 219 | else if (wait_pid > 0) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 220 | { |
| 221 | bool exited = false; |
| 222 | int signal = 0; |
| 223 | int exit_status = 0; |
| 224 | const char *status_cstr = NULL; |
| 225 | if (WIFSTOPPED(status)) |
| 226 | { |
| 227 | signal = WSTOPSIG(status); |
| 228 | status_cstr = "STOPPED"; |
| 229 | } |
| 230 | else if (WIFEXITED(status)) |
| 231 | { |
| 232 | exit_status = WEXITSTATUS(status); |
| 233 | status_cstr = "EXITED"; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 234 | exited = true; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 235 | } |
| 236 | else if (WIFSIGNALED(status)) |
| 237 | { |
| 238 | signal = WTERMSIG(status); |
| 239 | status_cstr = "SIGNALED"; |
Andrew Kaylor | 7d2abdf | 2013-09-04 16:06:04 +0000 | [diff] [blame] | 240 | if (wait_pid == abs(pid)) { |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 241 | exited = true; |
| 242 | exit_status = -1; |
| 243 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 244 | } |
| 245 | else |
| 246 | { |
Johnny Chen | 4480530 | 2011-07-19 19:48:13 +0000 | [diff] [blame] | 247 | status_cstr = "(\?\?\?)"; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // Scope for pthread_cancel_disabler |
| 251 | { |
| 252 | ScopedPThreadCancelDisabler pthread_cancel_disabler; |
| 253 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 254 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 255 | if (log) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 256 | 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] | 257 | function, |
| 258 | wait_pid, |
| 259 | options, |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 260 | pid, |
| 261 | status, |
| 262 | status_cstr, |
| 263 | signal, |
| 264 | exit_status); |
| 265 | |
| 266 | if (exited || (signal != 0 && monitor_signals)) |
| 267 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 268 | bool callback_return = false; |
| 269 | if (callback) |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 270 | callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 271 | |
| 272 | // If our process exited, then this thread should exit |
Andrew Kaylor | 7d2abdf | 2013-09-04 16:06:04 +0000 | [diff] [blame] | 273 | if (exited && wait_pid == abs(pid)) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 274 | { |
| 275 | if (log) |
| 276 | 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] | 277 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 278 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 279 | // If the callback returns true, it means this process should |
| 280 | // exit |
| 281 | if (callback_return) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 282 | { |
| 283 | if (log) |
| 284 | 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] | 285 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 286 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 292 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 293 | if (log) |
| 294 | log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg); |
| 295 | |
| 296 | return NULL; |
| 297 | } |
| 298 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 299 | #endif // #if !defined (__APPLE__) && !defined (_WIN32) |
| 300 | |
| 301 | #if !defined (__APPLE__) |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 302 | |
| 303 | void |
| 304 | Host::SystemLog (SystemLogType type, const char *format, va_list args) |
| 305 | { |
| 306 | vfprintf (stderr, format, args); |
| 307 | } |
| 308 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 309 | #endif |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 310 | |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 311 | void |
| 312 | Host::SystemLog (SystemLogType type, const char *format, ...) |
| 313 | { |
| 314 | va_list args; |
| 315 | va_start (args, format); |
| 316 | SystemLog (type, format, args); |
| 317 | va_end (args); |
| 318 | } |
| 319 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 320 | lldb::pid_t |
| 321 | Host::GetCurrentProcessID() |
| 322 | { |
| 323 | return ::getpid(); |
| 324 | } |
| 325 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 326 | #ifndef _WIN32 |
| 327 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 328 | lldb::tid_t |
| 329 | Host::GetCurrentThreadID() |
| 330 | { |
| 331 | #if defined (__APPLE__) |
Jean-Daniel Dupas | 3cfa8e2 | 2013-12-06 09:35:53 +0000 | [diff] [blame] | 332 | // Calling "mach_thread_self()" bumps the reference count on the thread |
Greg Clayton | 813ddfc | 2012-09-18 18:19:49 +0000 | [diff] [blame] | 333 | // port, so we need to deallocate it. mach_task_self() doesn't bump the ref |
| 334 | // count. |
| 335 | thread_port_t thread_self = mach_thread_self(); |
| 336 | mach_port_deallocate(mach_task_self(), thread_self); |
| 337 | return thread_self; |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 338 | #elif defined(__FreeBSD__) |
| 339 | return lldb::tid_t(pthread_getthreadid_np()); |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 340 | #elif defined(__linux__) |
| 341 | return lldb::tid_t(syscall(SYS_gettid)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 342 | #else |
| 343 | return lldb::tid_t(pthread_self()); |
| 344 | #endif |
| 345 | } |
| 346 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 347 | lldb::thread_t |
| 348 | Host::GetCurrentThread () |
| 349 | { |
| 350 | return lldb::thread_t(pthread_self()); |
| 351 | } |
| 352 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 353 | const char * |
| 354 | Host::GetSignalAsCString (int signo) |
| 355 | { |
| 356 | switch (signo) |
| 357 | { |
| 358 | case SIGHUP: return "SIGHUP"; // 1 hangup |
| 359 | case SIGINT: return "SIGINT"; // 2 interrupt |
| 360 | case SIGQUIT: return "SIGQUIT"; // 3 quit |
| 361 | case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught) |
| 362 | case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught) |
| 363 | case SIGABRT: return "SIGABRT"; // 6 abort() |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 364 | #if defined(SIGPOLL) |
Sylvestre Ledru | a4cc7de | 2013-12-13 09:51:39 +0000 | [diff] [blame] | 365 | #if !defined(SIGIO) || (SIGPOLL != SIGIO) |
| 366 | // Under some GNU/Linux, SIGPOLL and SIGIO are the same. Causing the build to |
| 367 | // fail with 'multiple define cases with same value' |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 368 | case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported) |
Benjamin Kramer | 44030f1 | 2011-11-04 16:06:40 +0000 | [diff] [blame] | 369 | #endif |
Sylvestre Ledru | a4cc7de | 2013-12-13 09:51:39 +0000 | [diff] [blame] | 370 | #endif |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 371 | #if defined(SIGEMT) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 372 | case SIGEMT: return "SIGEMT"; // 7 EMT instruction |
Benjamin Kramer | 44030f1 | 2011-11-04 16:06:40 +0000 | [diff] [blame] | 373 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 374 | case SIGFPE: return "SIGFPE"; // 8 floating point exception |
| 375 | case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored) |
| 376 | case SIGBUS: return "SIGBUS"; // 10 bus error |
| 377 | case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation |
| 378 | case SIGSYS: return "SIGSYS"; // 12 bad argument to system call |
| 379 | case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it |
| 380 | case SIGALRM: return "SIGALRM"; // 14 alarm clock |
| 381 | case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill |
| 382 | case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel |
| 383 | case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty |
| 384 | case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty |
| 385 | case SIGCONT: return "SIGCONT"; // 19 continue a stopped process |
| 386 | case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit |
| 387 | case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read |
| 388 | 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] | 389 | #if defined(SIGIO) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 390 | case SIGIO: return "SIGIO"; // 23 input/output possible signal |
| 391 | #endif |
| 392 | case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit |
| 393 | case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit |
| 394 | case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm |
| 395 | case SIGPROF: return "SIGPROF"; // 27 profiling time alarm |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 396 | #if defined(SIGWINCH) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 397 | case SIGWINCH: return "SIGWINCH"; // 28 window size changes |
Jean-Daniel Dupas | 13698b2 | 2013-12-12 18:12:16 +0000 | [diff] [blame] | 398 | #endif |
| 399 | #if defined(SIGINFO) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 400 | case SIGINFO: return "SIGINFO"; // 29 information request |
| 401 | #endif |
| 402 | case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1 |
| 403 | case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2 |
| 404 | default: |
| 405 | break; |
| 406 | } |
| 407 | return NULL; |
| 408 | } |
| 409 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 410 | #endif |
| 411 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 412 | void |
| 413 | Host::WillTerminate () |
| 414 | { |
| 415 | } |
| 416 | |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 417 | #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] | 418 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 419 | void |
| 420 | Host::ThreadCreated (const char *thread_name) |
| 421 | { |
| 422 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 423 | |
Peter Collingbourne | 2ced913 | 2011-08-05 00:35:43 +0000 | [diff] [blame] | 424 | void |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 425 | Host::Backtrace (Stream &strm, uint32_t max_frames) |
| 426 | { |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 427 | // 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] | 428 | } |
| 429 | |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 430 | size_t |
| 431 | Host::GetEnvironment (StringList &env) |
| 432 | { |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 433 | // 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] | 434 | return 0; |
| 435 | } |
| 436 | |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 437 | #endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 438 | |
| 439 | struct HostThreadCreateInfo |
| 440 | { |
| 441 | std::string thread_name; |
| 442 | thread_func_t thread_fptr; |
| 443 | thread_arg_t thread_arg; |
| 444 | |
| 445 | HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) : |
| 446 | thread_name (name ? name : ""), |
| 447 | thread_fptr (fptr), |
| 448 | thread_arg (arg) |
| 449 | { |
| 450 | } |
| 451 | }; |
| 452 | |
| 453 | static thread_result_t |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 454 | #ifdef _WIN32 |
| 455 | __stdcall |
| 456 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 457 | ThreadCreateTrampoline (thread_arg_t arg) |
| 458 | { |
| 459 | HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg; |
| 460 | Host::ThreadCreated (info->thread_name.c_str()); |
| 461 | thread_func_t thread_fptr = info->thread_fptr; |
| 462 | thread_arg_t thread_arg = info->thread_arg; |
| 463 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 464 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 465 | if (log) |
| 466 | log->Printf("thread created"); |
| 467 | |
| 468 | delete info; |
| 469 | return thread_fptr (thread_arg); |
| 470 | } |
| 471 | |
| 472 | lldb::thread_t |
| 473 | Host::ThreadCreate |
| 474 | ( |
| 475 | const char *thread_name, |
| 476 | thread_func_t thread_fptr, |
| 477 | thread_arg_t thread_arg, |
| 478 | Error *error |
| 479 | ) |
| 480 | { |
| 481 | lldb::thread_t thread = LLDB_INVALID_HOST_THREAD; |
| 482 | |
| 483 | // Host::ThreadCreateTrampoline will delete this pointer for us. |
| 484 | HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg); |
| 485 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 486 | #ifdef _WIN32 |
| 487 | thread = ::_beginthreadex(0, 0, ThreadCreateTrampoline, info_ptr, 0, NULL); |
| 488 | int err = thread <= 0 ? GetLastError() : 0; |
| 489 | #else |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 490 | int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 491 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 492 | if (err == 0) |
| 493 | { |
| 494 | if (error) |
| 495 | error->Clear(); |
| 496 | return thread; |
| 497 | } |
| 498 | |
| 499 | if (error) |
| 500 | error->SetError (err, eErrorTypePOSIX); |
| 501 | |
| 502 | return LLDB_INVALID_HOST_THREAD; |
| 503 | } |
| 504 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 505 | #ifndef _WIN32 |
| 506 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 507 | bool |
| 508 | Host::ThreadCancel (lldb::thread_t thread, Error *error) |
| 509 | { |
| 510 | int err = ::pthread_cancel (thread); |
| 511 | if (error) |
| 512 | error->SetError(err, eErrorTypePOSIX); |
| 513 | return err == 0; |
| 514 | } |
| 515 | |
| 516 | bool |
| 517 | Host::ThreadDetach (lldb::thread_t thread, Error *error) |
| 518 | { |
| 519 | int err = ::pthread_detach (thread); |
| 520 | if (error) |
| 521 | error->SetError(err, eErrorTypePOSIX); |
| 522 | return err == 0; |
| 523 | } |
| 524 | |
| 525 | bool |
| 526 | Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error) |
| 527 | { |
| 528 | int err = ::pthread_join (thread, thread_result_ptr); |
| 529 | if (error) |
| 530 | error->SetError(err, eErrorTypePOSIX); |
| 531 | return err == 0; |
| 532 | } |
| 533 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 534 | lldb::thread_key_t |
| 535 | Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback) |
| 536 | { |
| 537 | pthread_key_t key; |
| 538 | ::pthread_key_create (&key, callback); |
| 539 | return key; |
| 540 | } |
| 541 | |
| 542 | void* |
| 543 | Host::ThreadLocalStorageGet(lldb::thread_key_t key) |
| 544 | { |
| 545 | return ::pthread_getspecific (key); |
| 546 | } |
| 547 | |
| 548 | void |
| 549 | Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value) |
| 550 | { |
| 551 | ::pthread_setspecific (key, value); |
| 552 | } |
| 553 | |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 554 | bool |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 555 | Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name) |
| 556 | { |
Greg Clayton | 8571963 | 2013-02-27 22:51:58 +0000 | [diff] [blame] | 557 | #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5 |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 558 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 559 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
| 560 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 561 | pid = curr_pid; |
| 562 | |
| 563 | if (tid == LLDB_INVALID_THREAD_ID) |
| 564 | tid = curr_tid; |
| 565 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 566 | // Set the pthread name if possible |
| 567 | if (pid == curr_pid && tid == curr_tid) |
| 568 | { |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 569 | if (::pthread_setname_np (name) == 0) |
| 570 | return true; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 571 | } |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 572 | return false; |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 573 | #elif defined (__FreeBSD__) |
| 574 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 575 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
| 576 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 577 | pid = curr_pid; |
| 578 | |
| 579 | if (tid == LLDB_INVALID_THREAD_ID) |
| 580 | tid = curr_tid; |
| 581 | |
| 582 | // Set the pthread name if possible |
| 583 | if (pid == curr_pid && tid == curr_tid) |
| 584 | { |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 585 | ::pthread_set_name_np (::pthread_self(), name); |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 586 | return true; |
| 587 | } |
| 588 | return false; |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 589 | #elif defined (__linux__) || defined (__GLIBC__) |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 590 | void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np"); |
| 591 | if (fn) |
| 592 | { |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 593 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 594 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 595 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 596 | pid = curr_pid; |
| 597 | |
| 598 | if (tid == LLDB_INVALID_THREAD_ID) |
| 599 | tid = curr_tid; |
| 600 | |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 601 | if (pid == curr_pid && tid == curr_tid) |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 602 | { |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 603 | int (*pthread_setname_np_func)(pthread_t thread, const char *name); |
| 604 | *reinterpret_cast<void **> (&pthread_setname_np_func) = fn; |
| 605 | |
| 606 | if (pthread_setname_np_func (::pthread_self(), name) == 0) |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 607 | return true; |
| 608 | } |
| 609 | } |
| 610 | return false; |
Jim Ingham | 5c42d8a | 2013-05-15 18:27:08 +0000 | [diff] [blame] | 611 | #else |
| 612 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 613 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 616 | bool |
| 617 | Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, |
| 618 | const char *thread_name, size_t len) |
| 619 | { |
Enrico Granata | 67530b6 | 2014-02-12 03:37:33 +0000 | [diff] [blame] | 620 | std::unique_ptr<char[]> namebuf(new char[len+1]); |
| 621 | |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 622 | // Thread names are coming in like '<lldb.comm.debugger.edit>' and |
| 623 | // '<lldb.comm.debugger.editline>'. So just chopping the end of the string |
| 624 | // off leads to a lot of similar named threads. Go through the thread name |
| 625 | // and search for the last dot and use that. |
| 626 | const char *lastdot = ::strrchr (thread_name, '.'); |
| 627 | |
| 628 | if (lastdot && lastdot != thread_name) |
| 629 | thread_name = lastdot + 1; |
Enrico Granata | 67530b6 | 2014-02-12 03:37:33 +0000 | [diff] [blame] | 630 | ::strncpy (namebuf.get(), thread_name, len); |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 631 | namebuf[len] = 0; |
| 632 | |
Enrico Granata | 67530b6 | 2014-02-12 03:37:33 +0000 | [diff] [blame] | 633 | int namebuflen = strlen(namebuf.get()); |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 634 | if (namebuflen > 0) |
| 635 | { |
| 636 | if (namebuf[namebuflen - 1] == '(' || namebuf[namebuflen - 1] == '>') |
| 637 | { |
| 638 | // Trim off trailing '(' and '>' characters for a bit more cleanup. |
| 639 | namebuflen--; |
| 640 | namebuf[namebuflen] = 0; |
| 641 | } |
Enrico Granata | 67530b6 | 2014-02-12 03:37:33 +0000 | [diff] [blame] | 642 | return Host::SetThreadName (pid, tid, namebuf.get()); |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 643 | } |
| 644 | return false; |
| 645 | } |
| 646 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 647 | #endif |
| 648 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 649 | FileSpec |
Zachary Turner | 9e757b7 | 2014-07-28 16:45:05 +0000 | [diff] [blame] | 650 | Host::GetUserProfileFileSpec () |
| 651 | { |
| 652 | static FileSpec g_profile_filespec; |
| 653 | if (!g_profile_filespec) |
| 654 | { |
| 655 | llvm::SmallString<64> path; |
| 656 | llvm::sys::path::home_directory(path); |
| 657 | return FileSpec(path.c_str(), false); |
| 658 | } |
| 659 | return g_profile_filespec; |
| 660 | } |
| 661 | |
| 662 | FileSpec |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 663 | Host::GetProgramFileSpec () |
| 664 | { |
| 665 | static FileSpec g_program_filespec; |
| 666 | if (!g_program_filespec) |
| 667 | { |
| 668 | #if defined (__APPLE__) |
| 669 | char program_fullpath[PATH_MAX]; |
| 670 | // If DST is NULL, then return the number of bytes needed. |
| 671 | uint32_t len = sizeof(program_fullpath); |
| 672 | int err = _NSGetExecutablePath (program_fullpath, &len); |
| 673 | if (err == 0) |
Greg Clayton | b332639 | 2011-01-13 01:23:43 +0000 | [diff] [blame] | 674 | g_program_filespec.SetFile (program_fullpath, false); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 675 | else if (err == -1) |
| 676 | { |
| 677 | char *large_program_fullpath = (char *)::malloc (len + 1); |
| 678 | |
| 679 | err = _NSGetExecutablePath (large_program_fullpath, &len); |
| 680 | if (err == 0) |
Greg Clayton | b332639 | 2011-01-13 01:23:43 +0000 | [diff] [blame] | 681 | g_program_filespec.SetFile (large_program_fullpath, false); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 682 | |
| 683 | ::free (large_program_fullpath); |
| 684 | } |
| 685 | #elif defined (__linux__) |
| 686 | char exe_path[PATH_MAX]; |
Stephen Wilson | e5b94a9 | 2011-01-12 04:21:21 +0000 | [diff] [blame] | 687 | ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); |
| 688 | if (len > 0) { |
| 689 | exe_path[len] = 0; |
Greg Clayton | b332639 | 2011-01-13 01:23:43 +0000 | [diff] [blame] | 690 | g_program_filespec.SetFile(exe_path, false); |
Stephen Wilson | e5b94a9 | 2011-01-12 04:21:21 +0000 | [diff] [blame] | 691 | } |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 692 | #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 693 | int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() }; |
| 694 | size_t exe_path_size; |
| 695 | if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0) |
| 696 | { |
Greg Clayton | 87ff1ac | 2011-01-13 01:27:55 +0000 | [diff] [blame] | 697 | char *exe_path = new char[exe_path_size]; |
| 698 | if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0) |
| 699 | g_program_filespec.SetFile(exe_path, false); |
| 700 | delete[] exe_path; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 701 | } |
Zachary Turner | 9e757b7 | 2014-07-28 16:45:05 +0000 | [diff] [blame] | 702 | #elif defined(_WIN32) |
| 703 | std::vector<char> buffer(PATH_MAX); |
| 704 | ::GetModuleFileName(NULL, &buffer[0], buffer.size()); |
Zachary Turner | d8a5273 | 2014-07-29 05:39:21 +0000 | [diff] [blame] | 705 | g_program_filespec.SetFile(&buffer[0], false); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 706 | #endif |
| 707 | } |
| 708 | return g_program_filespec; |
| 709 | } |
| 710 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 711 | #if !defined (__APPLE__) // see Host.mm |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 712 | |
| 713 | bool |
| 714 | Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle) |
| 715 | { |
| 716 | bundle.Clear(); |
| 717 | return false; |
| 718 | } |
| 719 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 720 | bool |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 721 | Host::ResolveExecutableInBundle (FileSpec &file) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 722 | { |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 723 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 724 | } |
| 725 | #endif |
| 726 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 727 | #ifndef _WIN32 |
| 728 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 729 | // Opaque info that tracks a dynamic library that was loaded |
| 730 | struct DynamicLibraryInfo |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 731 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 732 | DynamicLibraryInfo (const FileSpec &fs, int o, void *h) : |
| 733 | file_spec (fs), |
| 734 | open_options (o), |
| 735 | handle (h) |
| 736 | { |
| 737 | } |
| 738 | |
| 739 | const FileSpec file_spec; |
| 740 | uint32_t open_options; |
| 741 | void * handle; |
| 742 | }; |
| 743 | |
| 744 | void * |
| 745 | Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error) |
| 746 | { |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 747 | char path[PATH_MAX]; |
| 748 | if (file_spec.GetPath(path, sizeof(path))) |
| 749 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 750 | int mode = 0; |
| 751 | |
| 752 | if (options & eDynamicLibraryOpenOptionLazy) |
| 753 | mode |= RTLD_LAZY; |
Greg Clayton | f939945 | 2011-02-08 05:24:57 +0000 | [diff] [blame] | 754 | else |
| 755 | mode |= RTLD_NOW; |
| 756 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 757 | |
| 758 | if (options & eDynamicLibraryOpenOptionLocal) |
| 759 | mode |= RTLD_LOCAL; |
| 760 | else |
| 761 | mode |= RTLD_GLOBAL; |
| 762 | |
| 763 | #ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED |
| 764 | if (options & eDynamicLibraryOpenOptionLimitGetSymbol) |
| 765 | mode |= RTLD_FIRST; |
Greg Clayton | 75852f5 | 2011-02-07 17:43:47 +0000 | [diff] [blame] | 766 | #endif |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 767 | |
| 768 | void * opaque = ::dlopen (path, mode); |
| 769 | |
| 770 | if (opaque) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 771 | { |
| 772 | error.Clear(); |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 773 | return new DynamicLibraryInfo (file_spec, options, opaque); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 774 | } |
| 775 | else |
| 776 | { |
| 777 | error.SetErrorString(::dlerror()); |
| 778 | } |
| 779 | } |
| 780 | else |
| 781 | { |
| 782 | error.SetErrorString("failed to extract path"); |
| 783 | } |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 784 | return NULL; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | Error |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 788 | Host::DynamicLibraryClose (void *opaque) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 789 | { |
| 790 | Error error; |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 791 | if (opaque == NULL) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 792 | { |
| 793 | error.SetErrorString ("invalid dynamic library handle"); |
| 794 | } |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 795 | else |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 796 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 797 | DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque; |
| 798 | if (::dlclose (dylib_info->handle) != 0) |
| 799 | { |
| 800 | error.SetErrorString(::dlerror()); |
| 801 | } |
| 802 | |
| 803 | dylib_info->open_options = 0; |
| 804 | dylib_info->handle = 0; |
| 805 | delete dylib_info; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 806 | } |
| 807 | return error; |
| 808 | } |
| 809 | |
| 810 | void * |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 811 | Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 812 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 813 | if (opaque == NULL) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 814 | { |
| 815 | error.SetErrorString ("invalid dynamic library handle"); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 816 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 817 | else |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 818 | { |
| 819 | DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque; |
| 820 | |
| 821 | void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name); |
| 822 | if (symbol_addr) |
| 823 | { |
| 824 | #ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED |
| 825 | // This host doesn't support limiting searches to this shared library |
| 826 | // so we need to verify that the match came from this shared library |
| 827 | // if it was requested in the Host::DynamicLibraryOpen() function. |
Greg Clayton | f939945 | 2011-02-08 05:24:57 +0000 | [diff] [blame] | 828 | if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol) |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 829 | { |
| 830 | FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr)); |
| 831 | if (match_dylib_spec != dylib_info->file_spec) |
| 832 | { |
| 833 | char dylib_path[PATH_MAX]; |
| 834 | if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path))) |
| 835 | error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path); |
| 836 | else |
| 837 | error.SetErrorString ("symbol not found"); |
| 838 | return NULL; |
| 839 | } |
| 840 | } |
| 841 | #endif |
| 842 | error.Clear(); |
| 843 | return symbol_addr; |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | error.SetErrorString(::dlerror()); |
| 848 | } |
| 849 | } |
| 850 | return NULL; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 851 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 852 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 853 | FileSpec |
| 854 | Host::GetModuleFileSpecForHostAddress (const void *host_addr) |
| 855 | { |
| 856 | FileSpec module_filespec; |
| 857 | Dl_info info; |
| 858 | if (::dladdr (host_addr, &info)) |
| 859 | { |
| 860 | if (info.dli_fname) |
| 861 | module_filespec.SetFile(info.dli_fname, true); |
| 862 | } |
| 863 | return module_filespec; |
| 864 | } |
| 865 | |
| 866 | #endif |
| 867 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 868 | |
| 869 | static void CleanupProcessSpecificLLDBTempDir () |
| 870 | { |
| 871 | // Get the process specific LLDB temporary directory and delete it. |
| 872 | FileSpec tmpdir_file_spec; |
| 873 | if (Host::GetLLDBPath (ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) |
| 874 | { |
| 875 | // Remove the LLDB temporary directory if we have one. Set "recurse" to |
| 876 | // true to all files that were created for the LLDB process can be cleaned up. |
| 877 | const bool recurse = true; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 878 | FileSystem::DeleteDirectory(tmpdir_file_spec.GetDirectory().GetCString(), recurse); |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 882 | bool |
| 883 | Host::GetLLDBPath (PathType path_type, FileSpec &file_spec) |
| 884 | { |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 885 | // To get paths related to LLDB we get the path to the executable that |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 886 | // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB", |
| 887 | // on linux this is assumed to be the "lldb" main executable. If LLDB on |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 888 | // linux is actually in a shared library (liblldb.so) then this function will |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 889 | // need to be modified to "do the right thing". |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 890 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_HOST); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 891 | |
| 892 | switch (path_type) |
| 893 | { |
| 894 | case ePathTypeLLDBShlibDir: |
| 895 | { |
| 896 | static ConstString g_lldb_so_dir; |
| 897 | if (!g_lldb_so_dir) |
| 898 | { |
David Majnemer | 8490da1 | 2014-07-22 22:00:42 +0000 | [diff] [blame] | 899 | FileSpec lldb_file_spec(Host::GetModuleFileSpecForHostAddress( |
| 900 | reinterpret_cast<void *>(reinterpret_cast<intptr_t>(Host::GetLLDBPath)))); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 901 | g_lldb_so_dir = lldb_file_spec.GetDirectory(); |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 902 | if (log) |
| 903 | log->Printf("Host::GetLLDBPath(ePathTypeLLDBShlibDir) => '%s'", g_lldb_so_dir.GetCString()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 904 | } |
| 905 | file_spec.GetDirectory() = g_lldb_so_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 906 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 907 | } |
| 908 | break; |
| 909 | |
| 910 | case ePathTypeSupportExecutableDir: |
| 911 | { |
| 912 | static ConstString g_lldb_support_exe_dir; |
| 913 | if (!g_lldb_support_exe_dir) |
| 914 | { |
| 915 | FileSpec lldb_file_spec; |
| 916 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 917 | { |
| 918 | char raw_path[PATH_MAX]; |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 919 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 920 | |
| 921 | #if defined (__APPLE__) |
| 922 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 923 | if (framework_pos) |
| 924 | { |
| 925 | framework_pos += strlen("LLDB.framework"); |
Todd Fiala | 013434e | 2014-07-09 01:29:05 +0000 | [diff] [blame] | 926 | #if defined (__arm__) || defined (__arm64__) || defined (__aarch64__) |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 927 | // Shallow bundle |
| 928 | *framework_pos = '\0'; |
| 929 | #else |
| 930 | // Normal bundle |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 931 | ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path)); |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 932 | #endif |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 933 | } |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 934 | #elif defined (__linux__) || defined (__FreeBSD__) || defined (__NetBSD__) |
| 935 | // Linux/*BSD will attempt to replace a */lib with */bin as the base directory for |
| 936 | // helper exe programs. This will fail if the /lib and /bin directories are rooted in entirely |
| 937 | // different trees. |
| 938 | if (log) |
| 939 | log->Printf ("Host::%s() attempting to derive the bin path (ePathTypeSupportExecutableDir) from this path: %s", __FUNCTION__, raw_path); |
| 940 | char *lib_pos = ::strstr (raw_path, "/lib"); |
| 941 | if (lib_pos != nullptr) |
| 942 | { |
| 943 | // First terminate the raw path at the start of lib. |
| 944 | *lib_pos = '\0'; |
| 945 | |
| 946 | // Now write in bin in place of lib. |
| 947 | ::strncpy (lib_pos, "/bin", PATH_MAX - (lib_pos - raw_path)); |
| 948 | |
| 949 | if (log) |
| 950 | log->Printf ("Host::%s() derived the bin path as: %s", __FUNCTION__, raw_path); |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | if (log) |
| 955 | log->Printf ("Host::%s() failed to find /lib/liblldb within the shared lib path, bailing on bin path construction", __FUNCTION__); |
| 956 | } |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 957 | #endif // #if defined (__APPLE__) |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 958 | llvm::SmallString<64> resolved_path(raw_path); |
| 959 | FileSpec::Resolve (resolved_path); |
| 960 | g_lldb_support_exe_dir.SetCString(resolved_path.c_str()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 961 | } |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 962 | if (log) |
| 963 | log->Printf("Host::GetLLDBPath(ePathTypeSupportExecutableDir) => '%s'", g_lldb_support_exe_dir.GetCString()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 964 | } |
| 965 | file_spec.GetDirectory() = g_lldb_support_exe_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 966 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 967 | } |
| 968 | break; |
| 969 | |
| 970 | case ePathTypeHeaderDir: |
| 971 | { |
| 972 | static ConstString g_lldb_headers_dir; |
| 973 | if (!g_lldb_headers_dir) |
| 974 | { |
| 975 | #if defined (__APPLE__) |
| 976 | FileSpec lldb_file_spec; |
| 977 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 978 | { |
| 979 | char raw_path[PATH_MAX]; |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 980 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 981 | |
| 982 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 983 | if (framework_pos) |
| 984 | { |
| 985 | framework_pos += strlen("LLDB.framework"); |
| 986 | ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path)); |
| 987 | } |
Jim Ingham | e1d263e | 2014-08-07 22:37:43 +0000 | [diff] [blame] | 988 | llvm::SmallString<64> resolved_path(raw_path); |
| 989 | FileSpec::Resolve (resolved_path); |
| 990 | g_lldb_headers_dir.SetCString(resolved_path.c_str()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 991 | } |
| 992 | #else |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 993 | // TODO: Anyone know how we can determine this for linux? Other systems?? |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 994 | g_lldb_headers_dir.SetCString ("/opt/local/include/lldb"); |
| 995 | #endif |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 996 | if (log) |
| 997 | log->Printf("Host::GetLLDBPath(ePathTypeHeaderDir) => '%s'", g_lldb_headers_dir.GetCString()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 998 | } |
| 999 | file_spec.GetDirectory() = g_lldb_headers_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1000 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1001 | } |
| 1002 | break; |
| 1003 | |
Joerg Sonnenberger | c759876 | 2013-09-25 17:52:18 +0000 | [diff] [blame] | 1004 | #ifdef LLDB_DISABLE_PYTHON |
| 1005 | case ePathTypePythonDir: |
| 1006 | return false; |
| 1007 | #else |
| 1008 | case ePathTypePythonDir: |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1009 | { |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1010 | static ConstString g_lldb_python_dir; |
| 1011 | if (!g_lldb_python_dir) |
| 1012 | { |
| 1013 | FileSpec lldb_file_spec; |
| 1014 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 1015 | { |
| 1016 | char raw_path[PATH_MAX]; |
Zachary Turner | c9bf0c7 | 2014-07-18 20:36:08 +0000 | [diff] [blame] | 1017 | #if defined(_WIN32) |
| 1018 | lldb_file_spec.AppendPathComponent("../lib/site-packages"); |
| 1019 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1020 | #else |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1021 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1022 | |
| 1023 | #if defined (__APPLE__) |
| 1024 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 1025 | if (framework_pos) |
| 1026 | { |
| 1027 | framework_pos += strlen("LLDB.framework"); |
| 1028 | ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path)); |
Todd Fiala | 5000e28 | 2014-01-18 08:05:32 +0000 | [diff] [blame] | 1029 | } |
| 1030 | else |
| 1031 | { |
| 1032 | #endif |
| 1033 | llvm::SmallString<256> python_version_dir; |
| 1034 | llvm::raw_svector_ostream os(python_version_dir); |
| 1035 | os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "/site-packages"; |
| 1036 | os.flush(); |
| 1037 | |
| 1038 | // We may get our string truncated. Should we protect |
| 1039 | // this with an assert? |
| 1040 | |
| 1041 | ::strncat(raw_path, python_version_dir.c_str(), |
| 1042 | sizeof(raw_path) - strlen(raw_path) - 1); |
Zachary Turner | c9bf0c7 | 2014-07-18 20:36:08 +0000 | [diff] [blame] | 1043 | #endif |
Todd Fiala | 5000e28 | 2014-01-18 08:05:32 +0000 | [diff] [blame] | 1044 | #if defined (__APPLE__) |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1045 | } |
| 1046 | #endif |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 1047 | llvm::SmallString<64> resolved_path(raw_path); |
| 1048 | FileSpec::Resolve (resolved_path); |
| 1049 | g_lldb_python_dir.SetCString(resolved_path.c_str()); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1050 | } |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1051 | |
| 1052 | if (log) |
| 1053 | log->Printf("Host::GetLLDBPath(ePathTypePythonDir) => '%s'", g_lldb_python_dir.GetCString()); |
| 1054 | |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1055 | } |
| 1056 | file_spec.GetDirectory() = g_lldb_python_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1057 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1058 | } |
| 1059 | break; |
Ed Maste | a85d364 | 2013-07-02 19:30:52 +0000 | [diff] [blame] | 1060 | #endif |
| 1061 | |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1062 | case ePathTypeLLDBSystemPlugins: // System plug-ins directory |
| 1063 | { |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1064 | #if defined (__APPLE__) || defined(__linux__) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1065 | static ConstString g_lldb_system_plugin_dir; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1066 | static bool g_lldb_system_plugin_dir_located = false; |
| 1067 | if (!g_lldb_system_plugin_dir_located) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1068 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1069 | g_lldb_system_plugin_dir_located = true; |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1070 | #if defined (__APPLE__) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1071 | FileSpec lldb_file_spec; |
| 1072 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 1073 | { |
| 1074 | char raw_path[PATH_MAX]; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1075 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1076 | |
| 1077 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 1078 | if (framework_pos) |
| 1079 | { |
| 1080 | framework_pos += strlen("LLDB.framework"); |
| 1081 | ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path)); |
Jim Ingham | e1d263e | 2014-08-07 22:37:43 +0000 | [diff] [blame] | 1082 | llvm::SmallString<64> resolved_path(raw_path); |
| 1083 | FileSpec::Resolve (resolved_path); |
| 1084 | g_lldb_system_plugin_dir.SetCString(resolved_path.c_str()); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1085 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1086 | return false; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1087 | } |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1088 | #elif defined (__linux__) |
| 1089 | FileSpec lldb_file_spec("/usr/lib/lldb", true); |
| 1090 | if (lldb_file_spec.Exists()) |
| 1091 | { |
| 1092 | g_lldb_system_plugin_dir.SetCString(lldb_file_spec.GetPath().c_str()); |
| 1093 | } |
| 1094 | #endif // __APPLE__ || __linux__ |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (log) |
| 1097 | log->Printf("Host::GetLLDBPath(ePathTypeLLDBSystemPlugins) => '%s'", g_lldb_system_plugin_dir.GetCString()); |
| 1098 | |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1099 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1100 | |
| 1101 | if (g_lldb_system_plugin_dir) |
| 1102 | { |
| 1103 | file_spec.GetDirectory() = g_lldb_system_plugin_dir; |
| 1104 | return true; |
| 1105 | } |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1106 | #else |
| 1107 | // TODO: where would system LLDB plug-ins be located on other systems? |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1108 | return false; |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1109 | #endif |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1110 | } |
| 1111 | break; |
| 1112 | |
| 1113 | case ePathTypeLLDBUserPlugins: // User plug-ins directory |
| 1114 | { |
| 1115 | #if defined (__APPLE__) |
| 1116 | static ConstString g_lldb_user_plugin_dir; |
| 1117 | if (!g_lldb_user_plugin_dir) |
| 1118 | { |
Jim Ingham | e1d263e | 2014-08-07 22:37:43 +0000 | [diff] [blame] | 1119 | llvm::SmallString<64> user_plugin_path("~/Library/Application Support/LLDB/PlugIns"); |
| 1120 | FileSpec::Resolve (user_plugin_path); |
| 1121 | if (user_plugin_path.size()) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1122 | { |
Jim Ingham | e1d263e | 2014-08-07 22:37:43 +0000 | [diff] [blame] | 1123 | g_lldb_user_plugin_dir.SetCString(user_plugin_path.c_str()); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | file_spec.GetDirectory() = g_lldb_user_plugin_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1127 | return (bool)file_spec.GetDirectory(); |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1128 | #elif defined (__linux__) |
| 1129 | static ConstString g_lldb_user_plugin_dir; |
| 1130 | if (!g_lldb_user_plugin_dir) |
| 1131 | { |
| 1132 | // XDG Base Directory Specification |
| 1133 | // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
| 1134 | // If XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb. |
| 1135 | FileSpec lldb_file_spec; |
| 1136 | const char *xdg_data_home = getenv("XDG_DATA_HOME"); |
| 1137 | if (xdg_data_home && xdg_data_home[0]) |
| 1138 | { |
| 1139 | std::string user_plugin_dir (xdg_data_home); |
| 1140 | user_plugin_dir += "/lldb"; |
| 1141 | lldb_file_spec.SetFile (user_plugin_dir.c_str(), true); |
| 1142 | } |
| 1143 | else |
| 1144 | { |
| 1145 | const char *home_dir = getenv("HOME"); |
| 1146 | if (home_dir && home_dir[0]) |
| 1147 | { |
| 1148 | std::string user_plugin_dir (home_dir); |
| 1149 | user_plugin_dir += "/.local/share/lldb"; |
| 1150 | lldb_file_spec.SetFile (user_plugin_dir.c_str(), true); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | if (lldb_file_spec.Exists()) |
| 1155 | g_lldb_user_plugin_dir.SetCString(lldb_file_spec.GetPath().c_str()); |
Greg Clayton | 3bd19fb | 2013-11-22 18:48:24 +0000 | [diff] [blame] | 1156 | if (log) |
| 1157 | log->Printf("Host::GetLLDBPath(ePathTypeLLDBUserPlugins) => '%s'", g_lldb_user_plugin_dir.GetCString()); |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1158 | } |
| 1159 | file_spec.GetDirectory() = g_lldb_user_plugin_dir; |
Sean Callanan | ddd7a2a | 2013-10-03 22:27:29 +0000 | [diff] [blame] | 1160 | return (bool)file_spec.GetDirectory(); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1161 | #endif |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1162 | // TODO: where would user LLDB plug-ins be located on other systems? |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1163 | return false; |
| 1164 | } |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1165 | |
| 1166 | case ePathTypeLLDBTempSystemDir: |
| 1167 | { |
| 1168 | static ConstString g_lldb_tmp_dir; |
| 1169 | if (!g_lldb_tmp_dir) |
| 1170 | { |
| 1171 | const char *tmpdir_cstr = getenv("TMPDIR"); |
| 1172 | if (tmpdir_cstr == NULL) |
| 1173 | { |
| 1174 | tmpdir_cstr = getenv("TMP"); |
| 1175 | if (tmpdir_cstr == NULL) |
| 1176 | tmpdir_cstr = getenv("TEMP"); |
| 1177 | } |
| 1178 | if (tmpdir_cstr) |
| 1179 | { |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 1180 | StreamString pid_tmpdir; |
| 1181 | pid_tmpdir.Printf("%s/lldb", tmpdir_cstr); |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1182 | if (FileSystem::MakeDirectory(pid_tmpdir.GetString().c_str(), eFilePermissionsDirectoryDefault) |
| 1183 | .Success()) |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 1184 | { |
| 1185 | pid_tmpdir.Printf("/%" PRIu64, Host::GetCurrentProcessID()); |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1186 | if (FileSystem::MakeDirectory(pid_tmpdir.GetString().c_str(), eFilePermissionsDirectoryDefault) |
| 1187 | .Success()) |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 1188 | { |
| 1189 | // Make an atexit handler to clean up the process specify LLDB temp dir |
| 1190 | // and all of its contents. |
| 1191 | ::atexit (CleanupProcessSpecificLLDBTempDir); |
| 1192 | g_lldb_tmp_dir.SetCString(pid_tmpdir.GetString().c_str()); |
| 1193 | if (log) |
| 1194 | log->Printf("Host::GetLLDBPath(ePathTypeLLDBTempSystemDir) => '%s'", g_lldb_tmp_dir.GetCString()); |
| 1195 | |
| 1196 | } |
| 1197 | } |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | file_spec.GetDirectory() = g_lldb_tmp_dir; |
| 1201 | return (bool)file_spec.GetDirectory(); |
| 1202 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1208 | #ifndef _WIN32 |
| 1209 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1210 | const char * |
| 1211 | Host::GetUserName (uint32_t uid, std::string &user_name) |
| 1212 | { |
| 1213 | struct passwd user_info; |
| 1214 | struct passwd *user_info_ptr = &user_info; |
| 1215 | char user_buffer[PATH_MAX]; |
| 1216 | size_t user_buffer_size = sizeof(user_buffer); |
| 1217 | if (::getpwuid_r (uid, |
| 1218 | &user_info, |
| 1219 | user_buffer, |
| 1220 | user_buffer_size, |
| 1221 | &user_info_ptr) == 0) |
| 1222 | { |
| 1223 | if (user_info_ptr) |
| 1224 | { |
| 1225 | user_name.assign (user_info_ptr->pw_name); |
| 1226 | return user_name.c_str(); |
| 1227 | } |
| 1228 | } |
| 1229 | user_name.clear(); |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | |
| 1233 | const char * |
| 1234 | Host::GetGroupName (uint32_t gid, std::string &group_name) |
| 1235 | { |
| 1236 | char group_buffer[PATH_MAX]; |
| 1237 | size_t group_buffer_size = sizeof(group_buffer); |
| 1238 | struct group group_info; |
| 1239 | struct group *group_info_ptr = &group_info; |
| 1240 | // Try the threadsafe version first |
| 1241 | if (::getgrgid_r (gid, |
| 1242 | &group_info, |
| 1243 | group_buffer, |
| 1244 | group_buffer_size, |
| 1245 | &group_info_ptr) == 0) |
| 1246 | { |
| 1247 | if (group_info_ptr) |
| 1248 | { |
| 1249 | group_name.assign (group_info_ptr->gr_name); |
| 1250 | return group_name.c_str(); |
| 1251 | } |
| 1252 | } |
| 1253 | else |
| 1254 | { |
| 1255 | // The threadsafe version isn't currently working |
| 1256 | // for me on darwin, but the non-threadsafe version |
| 1257 | // is, so I am calling it below. |
| 1258 | group_info_ptr = ::getgrgid (gid); |
| 1259 | if (group_info_ptr) |
| 1260 | { |
| 1261 | group_name.assign (group_info_ptr->gr_name); |
| 1262 | return group_name.c_str(); |
| 1263 | } |
| 1264 | } |
| 1265 | group_name.clear(); |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 1269 | uint32_t |
| 1270 | Host::GetUserID () |
| 1271 | { |
| 1272 | return getuid(); |
| 1273 | } |
| 1274 | |
| 1275 | uint32_t |
| 1276 | Host::GetGroupID () |
| 1277 | { |
| 1278 | return getgid(); |
| 1279 | } |
| 1280 | |
| 1281 | uint32_t |
| 1282 | Host::GetEffectiveUserID () |
| 1283 | { |
| 1284 | return geteuid(); |
| 1285 | } |
| 1286 | |
| 1287 | uint32_t |
| 1288 | Host::GetEffectiveGroupID () |
| 1289 | { |
| 1290 | return getegid(); |
| 1291 | } |
| 1292 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1293 | #endif |
| 1294 | |
Zachary Turner | 310035a | 2014-07-08 04:52:15 +0000 | [diff] [blame] | 1295 | #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) \ |
| 1296 | && !defined(__linux__) && !defined(_WIN32) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1297 | uint32_t |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1298 | Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1299 | { |
| 1300 | process_infos.Clear(); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1301 | return process_infos.GetSize(); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1304 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1305 | Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1306 | { |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1307 | process_info.Clear(); |
| 1308 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1309 | } |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 1310 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1311 | |
Matt Kopec | 085d6ce | 2013-05-31 22:00:07 +0000 | [diff] [blame] | 1312 | #if !defined(__linux__) |
| 1313 | bool |
| 1314 | Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach) |
| 1315 | { |
| 1316 | return false; |
| 1317 | } |
| 1318 | #endif |
| 1319 | |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 1320 | lldb::TargetSP |
| 1321 | Host::GetDummyTarget (lldb_private::Debugger &debugger) |
| 1322 | { |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1323 | static TargetSP g_dummy_target_sp; |
Filipe Cabecinhas | b018345 | 2012-05-17 15:48:02 +0000 | [diff] [blame] | 1324 | |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1325 | // FIXME: Maybe the dummy target should be per-Debugger |
| 1326 | if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid()) |
| 1327 | { |
| 1328 | ArchSpec arch(Target::GetDefaultArchitecture()); |
| 1329 | if (!arch.IsValid()) |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame^] | 1330 | arch = HostInfo::GetArchitecture(); |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1331 | Error err = debugger.GetTargetList().CreateTarget(debugger, |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 1332 | NULL, |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1333 | arch.GetTriple().getTriple().c_str(), |
| 1334 | false, |
| 1335 | NULL, |
| 1336 | g_dummy_target_sp); |
| 1337 | } |
Filipe Cabecinhas | b018345 | 2012-05-17 15:48:02 +0000 | [diff] [blame] | 1338 | |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1339 | return g_dummy_target_sp; |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1342 | struct ShellInfo |
| 1343 | { |
| 1344 | ShellInfo () : |
| 1345 | process_reaped (false), |
| 1346 | can_delete (false), |
| 1347 | pid (LLDB_INVALID_PROCESS_ID), |
| 1348 | signo(-1), |
| 1349 | status(-1) |
| 1350 | { |
| 1351 | } |
| 1352 | |
| 1353 | lldb_private::Predicate<bool> process_reaped; |
| 1354 | lldb_private::Predicate<bool> can_delete; |
| 1355 | lldb::pid_t pid; |
| 1356 | int signo; |
| 1357 | int status; |
| 1358 | }; |
| 1359 | |
| 1360 | static bool |
| 1361 | MonitorShellCommand (void *callback_baton, |
| 1362 | lldb::pid_t pid, |
| 1363 | bool exited, // True if the process did exit |
| 1364 | int signo, // Zero for no signal |
| 1365 | int status) // Exit value of process if signal is zero |
| 1366 | { |
| 1367 | ShellInfo *shell_info = (ShellInfo *)callback_baton; |
| 1368 | shell_info->pid = pid; |
| 1369 | shell_info->signo = signo; |
| 1370 | shell_info->status = status; |
| 1371 | // Let the thread running Host::RunShellCommand() know that the process |
| 1372 | // exited and that ShellInfo has been filled in by broadcasting to it |
| 1373 | shell_info->process_reaped.SetValue(1, eBroadcastAlways); |
| 1374 | // Now wait for a handshake back from that thread running Host::RunShellCommand |
| 1375 | // so we know that we can delete shell_info_ptr |
| 1376 | shell_info->can_delete.WaitForValueEqualTo(true); |
| 1377 | // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete... |
| 1378 | usleep(1000); |
| 1379 | // Now delete the shell info that was passed into this function |
| 1380 | delete shell_info; |
| 1381 | return true; |
| 1382 | } |
| 1383 | |
| 1384 | Error |
| 1385 | Host::RunShellCommand (const char *command, |
| 1386 | const char *working_dir, |
| 1387 | int *status_ptr, |
| 1388 | int *signo_ptr, |
| 1389 | std::string *command_output_ptr, |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1390 | uint32_t timeout_sec, |
| 1391 | const char *shell) |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1392 | { |
| 1393 | Error error; |
| 1394 | ProcessLaunchInfo launch_info; |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1395 | if (shell && shell[0]) |
| 1396 | { |
| 1397 | // Run the command in a shell |
| 1398 | launch_info.SetShell(shell); |
| 1399 | launch_info.GetArguments().AppendArgument(command); |
| 1400 | const bool localhost = true; |
| 1401 | const bool will_debug = false; |
| 1402 | const bool first_arg_is_full_shell_command = true; |
| 1403 | launch_info.ConvertArgumentsForLaunchingInShell (error, |
| 1404 | localhost, |
| 1405 | will_debug, |
Jim Ingham | df0ae22 | 2013-09-10 02:09:47 +0000 | [diff] [blame] | 1406 | first_arg_is_full_shell_command, |
| 1407 | 0); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1408 | } |
| 1409 | else |
| 1410 | { |
| 1411 | // No shell, just run it |
| 1412 | Args args (command); |
| 1413 | const bool first_arg_is_executable = true; |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 1414 | launch_info.SetArguments(args, first_arg_is_executable); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1415 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1416 | |
| 1417 | if (working_dir) |
| 1418 | launch_info.SetWorkingDirectory(working_dir); |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1419 | char output_file_path_buffer[PATH_MAX]; |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1420 | const char *output_file_path = NULL; |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1421 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1422 | if (command_output_ptr) |
| 1423 | { |
| 1424 | // Create a temporary file to get the stdout/stderr and redirect the |
| 1425 | // output of the command into this file. We will later read this file |
| 1426 | // if all goes well and fill the data into "command_output_ptr" |
Greg Clayton | c6931fc | 2013-12-04 18:53:50 +0000 | [diff] [blame] | 1427 | FileSpec tmpdir_file_spec; |
| 1428 | if (Host::GetLLDBPath (ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) |
| 1429 | { |
| 1430 | tmpdir_file_spec.GetFilename().SetCString("lldb-shell-output.XXXXXX"); |
| 1431 | strncpy(output_file_path_buffer, tmpdir_file_spec.GetPath().c_str(), sizeof(output_file_path_buffer)); |
| 1432 | } |
| 1433 | else |
| 1434 | { |
| 1435 | strncpy(output_file_path_buffer, "/tmp/lldb-shell-output.XXXXXX", sizeof(output_file_path_buffer)); |
| 1436 | } |
| 1437 | |
| 1438 | output_file_path = ::mktemp(output_file_path_buffer); |
| 1439 | } |
| 1440 | |
| 1441 | launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false); |
| 1442 | if (output_file_path) |
| 1443 | { |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1444 | launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1445 | launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1446 | } |
| 1447 | else |
| 1448 | { |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1449 | launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 1450 | launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true); |
| 1451 | } |
| 1452 | |
| 1453 | // The process monitor callback will delete the 'shell_info_ptr' below... |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 1454 | std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo()); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1455 | |
| 1456 | const bool monitor_signals = false; |
| 1457 | launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals); |
| 1458 | |
| 1459 | error = LaunchProcess (launch_info); |
| 1460 | const lldb::pid_t pid = launch_info.GetProcessID(); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1461 | |
| 1462 | if (error.Success() && pid == LLDB_INVALID_PROCESS_ID) |
| 1463 | error.SetErrorString("failed to get process ID"); |
| 1464 | |
| 1465 | if (error.Success()) |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1466 | { |
| 1467 | // The process successfully launched, so we can defer ownership of |
| 1468 | // "shell_info" to the MonitorShellCommand callback function that will |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 1469 | // 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] | 1470 | // doesn't need to delete the ShellInfo anymore. |
| 1471 | ShellInfo *shell_info = shell_info_ap.release(); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1472 | TimeValue *timeout_ptr = nullptr; |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1473 | TimeValue timeout_time(TimeValue::Now()); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1474 | if (timeout_sec > 0) { |
| 1475 | timeout_time.OffsetWithSeconds(timeout_sec); |
| 1476 | timeout_ptr = &timeout_time; |
| 1477 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1478 | bool timed_out = false; |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1479 | shell_info->process_reaped.WaitForValueEqualTo(true, timeout_ptr, &timed_out); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1480 | if (timed_out) |
| 1481 | { |
| 1482 | error.SetErrorString("timed out waiting for shell command to complete"); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1483 | |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 1484 | // Kill the process since it didn't complete within the timeout specified |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1485 | Kill (pid, SIGKILL); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1486 | // Wait for the monitor callback to get the message |
| 1487 | timeout_time = TimeValue::Now(); |
| 1488 | timeout_time.OffsetWithSeconds(1); |
| 1489 | timed_out = false; |
| 1490 | shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out); |
| 1491 | } |
| 1492 | else |
| 1493 | { |
| 1494 | if (status_ptr) |
| 1495 | *status_ptr = shell_info->status; |
| 1496 | |
| 1497 | if (signo_ptr) |
| 1498 | *signo_ptr = shell_info->signo; |
| 1499 | |
| 1500 | if (command_output_ptr) |
| 1501 | { |
| 1502 | command_output_ptr->clear(); |
| 1503 | FileSpec file_spec(output_file_path, File::eOpenOptionRead); |
| 1504 | uint64_t file_size = file_spec.GetByteSize(); |
| 1505 | if (file_size > 0) |
| 1506 | { |
| 1507 | if (file_size > command_output_ptr->max_size()) |
| 1508 | { |
| 1509 | error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string"); |
| 1510 | } |
| 1511 | else |
| 1512 | { |
| 1513 | command_output_ptr->resize(file_size); |
| 1514 | file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error); |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | shell_info->can_delete.SetValue(true, eBroadcastAlways); |
| 1520 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1521 | |
| 1522 | if (output_file_path) |
| 1523 | ::unlink (output_file_path); |
| 1524 | // Handshake with the monitor thread, or just let it know in advance that |
| 1525 | // it can delete "shell_info" in case we timed out and were not able to kill |
| 1526 | // the process... |
| 1527 | return error; |
| 1528 | } |
| 1529 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1530 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1531 | // LaunchProcessPosixSpawn for Apple, Linux, FreeBSD and other GLIBC |
| 1532 | // systems |
| 1533 | |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 1534 | #if defined (__APPLE__) || defined (__linux__) || defined (__FreeBSD__) || defined (__GLIBC__) || defined(__NetBSD__) |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1535 | |
| 1536 | // this method needs to be visible to macosx/Host.cpp and |
| 1537 | // common/Host.cpp. |
| 1538 | |
| 1539 | short |
| 1540 | Host::GetPosixspawnFlags (ProcessLaunchInfo &launch_info) |
| 1541 | { |
| 1542 | short flags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK; |
| 1543 | |
| 1544 | #if defined (__APPLE__) |
| 1545 | if (launch_info.GetFlags().Test (eLaunchFlagExec)) |
| 1546 | flags |= POSIX_SPAWN_SETEXEC; // Darwin specific posix_spawn flag |
| 1547 | |
| 1548 | if (launch_info.GetFlags().Test (eLaunchFlagDebug)) |
| 1549 | flags |= POSIX_SPAWN_START_SUSPENDED; // Darwin specific posix_spawn flag |
| 1550 | |
| 1551 | if (launch_info.GetFlags().Test (eLaunchFlagDisableASLR)) |
| 1552 | flags |= _POSIX_SPAWN_DISABLE_ASLR; // Darwin specific posix_spawn flag |
| 1553 | |
| 1554 | if (launch_info.GetLaunchInSeparateProcessGroup()) |
| 1555 | flags |= POSIX_SPAWN_SETPGROUP; |
| 1556 | |
| 1557 | #ifdef POSIX_SPAWN_CLOEXEC_DEFAULT |
| 1558 | #if defined (__APPLE__) && (defined (__x86_64__) || defined (__i386__)) |
| 1559 | static LazyBool g_use_close_on_exec_flag = eLazyBoolCalculate; |
| 1560 | if (g_use_close_on_exec_flag == eLazyBoolCalculate) |
| 1561 | { |
| 1562 | g_use_close_on_exec_flag = eLazyBoolNo; |
| 1563 | |
| 1564 | uint32_t major, minor, update; |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 1565 | if (HostInfo::GetOSVersion(major, minor, update)) |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1566 | { |
| 1567 | // Kernel panic if we use the POSIX_SPAWN_CLOEXEC_DEFAULT on 10.7 or earlier |
| 1568 | if (major > 10 || (major == 10 && minor > 7)) |
| 1569 | { |
| 1570 | // Only enable for 10.8 and later OS versions |
| 1571 | g_use_close_on_exec_flag = eLazyBoolYes; |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | #else |
| 1576 | static LazyBool g_use_close_on_exec_flag = eLazyBoolYes; |
| 1577 | #endif |
| 1578 | // Close all files exception those with file actions if this is supported. |
| 1579 | if (g_use_close_on_exec_flag == eLazyBoolYes) |
| 1580 | flags |= POSIX_SPAWN_CLOEXEC_DEFAULT; |
| 1581 | #endif |
| 1582 | #endif // #if defined (__APPLE__) |
| 1583 | return flags; |
| 1584 | } |
| 1585 | |
| 1586 | Error |
| 1587 | Host::LaunchProcessPosixSpawn (const char *exe_path, ProcessLaunchInfo &launch_info, ::pid_t &pid) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1588 | { |
| 1589 | Error error; |
| 1590 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_PROCESS)); |
| 1591 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1592 | posix_spawnattr_t attr; |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1593 | error.SetError( ::posix_spawnattr_init (&attr), eErrorTypePOSIX); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1594 | |
| 1595 | if (error.Fail() || log) |
| 1596 | error.PutToLog(log, "::posix_spawnattr_init ( &attr )"); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1597 | if (error.Fail()) |
| 1598 | return error; |
| 1599 | |
| 1600 | // Make a quick class that will cleanup the posix spawn attributes in case |
| 1601 | // we return in the middle of this function. |
| 1602 | lldb_utility::CleanUp <posix_spawnattr_t *, int> posix_spawnattr_cleanup(&attr, posix_spawnattr_destroy); |
| 1603 | |
| 1604 | sigset_t no_signals; |
| 1605 | sigset_t all_signals; |
| 1606 | sigemptyset (&no_signals); |
| 1607 | sigfillset (&all_signals); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1608 | ::posix_spawnattr_setsigmask(&attr, &no_signals); |
Todd Fiala | 571768c | 2014-01-23 17:07:54 +0000 | [diff] [blame] | 1609 | #if defined (__linux__) || defined (__FreeBSD__) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1610 | ::posix_spawnattr_setsigdefault(&attr, &no_signals); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1611 | #else |
| 1612 | ::posix_spawnattr_setsigdefault(&attr, &all_signals); |
| 1613 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1614 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1615 | short flags = GetPosixspawnFlags(launch_info); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1616 | |
| 1617 | error.SetError( ::posix_spawnattr_setflags (&attr, flags), eErrorTypePOSIX); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1618 | if (error.Fail() || log) |
| 1619 | error.PutToLog(log, "::posix_spawnattr_setflags ( &attr, flags=0x%8.8x )", flags); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1620 | if (error.Fail()) |
| 1621 | return error; |
| 1622 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1623 | // posix_spawnattr_setbinpref_np appears to be an Apple extension per: |
| 1624 | // http://www.unix.com/man-page/OSX/3/posix_spawnattr_setbinpref_np/ |
| 1625 | #if defined (__APPLE__) && !defined (__arm__) |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Don't set the binpref if a shell was provided. After all, that's only going to affect what version of the shell |
| 1628 | // is launched, not what fork of the binary is launched. We insert "arch --arch <ARCH> as part of the shell invocation |
| 1629 | // to do that job on OSX. |
| 1630 | |
| 1631 | if (launch_info.GetShell() == nullptr) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1632 | { |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 1633 | // We don't need to do this for ARM, and we really shouldn't now that we |
| 1634 | // have multiple CPU subtypes and no posix_spawnattr call that allows us |
| 1635 | // to set which CPU subtype to launch... |
| 1636 | const ArchSpec &arch_spec = launch_info.GetArchitecture(); |
| 1637 | cpu_type_t cpu = arch_spec.GetMachOCPUType(); |
| 1638 | cpu_type_t sub = arch_spec.GetMachOCPUSubType(); |
| 1639 | if (cpu != 0 && |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame] | 1640 | cpu != static_cast<cpu_type_t>(UINT32_MAX) && |
| 1641 | cpu != static_cast<cpu_type_t>(LLDB_INVALID_CPUTYPE) && |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 1642 | !(cpu == 0x01000007 && sub == 8)) // If haswell is specified, don't try to set the CPU type or we will fail |
| 1643 | { |
| 1644 | size_t ocount = 0; |
| 1645 | error.SetError( ::posix_spawnattr_setbinpref_np (&attr, 1, &cpu, &ocount), eErrorTypePOSIX); |
| 1646 | if (error.Fail() || log) |
| 1647 | 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] | 1648 | |
Jim Ingham | 90331d5 | 2014-02-21 01:25:21 +0000 | [diff] [blame] | 1649 | if (error.Fail() || ocount != 1) |
| 1650 | return error; |
| 1651 | } |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1654 | #endif |
| 1655 | |
| 1656 | const char *tmp_argv[2]; |
| 1657 | char * const *argv = (char * const*)launch_info.GetArguments().GetConstArgumentVector(); |
| 1658 | char * const *envp = (char * const*)launch_info.GetEnvironmentEntries().GetConstArgumentVector(); |
| 1659 | if (argv == NULL) |
| 1660 | { |
| 1661 | // posix_spawn gets very unhappy if it doesn't have at least the program |
| 1662 | // name in argv[0]. One of the side affects I have noticed is the environment |
| 1663 | // variables don't make it into the child process if "argv == NULL"!!! |
| 1664 | tmp_argv[0] = exe_path; |
| 1665 | tmp_argv[1] = NULL; |
| 1666 | argv = (char * const*)tmp_argv; |
| 1667 | } |
| 1668 | |
| 1669 | #if !defined (__APPLE__) |
| 1670 | // manage the working directory |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1671 | char current_dir[PATH_MAX]; |
| 1672 | current_dir[0] = '\0'; |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1673 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1674 | |
| 1675 | const char *working_dir = launch_info.GetWorkingDirectory(); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1676 | if (working_dir) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1677 | { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1678 | #if defined (__APPLE__) |
| 1679 | // Set the working directory on this thread only |
| 1680 | if (__pthread_chdir (working_dir) < 0) { |
| 1681 | if (errno == ENOENT) { |
| 1682 | error.SetErrorStringWithFormat("No such file or directory: %s", working_dir); |
| 1683 | } else if (errno == ENOTDIR) { |
| 1684 | error.SetErrorStringWithFormat("Path doesn't name a directory: %s", working_dir); |
| 1685 | } else { |
| 1686 | error.SetErrorStringWithFormat("An unknown error occurred when changing directory for process execution."); |
| 1687 | } |
| 1688 | return error; |
| 1689 | } |
| 1690 | #else |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1691 | if (::getcwd(current_dir, sizeof(current_dir)) == NULL) |
| 1692 | { |
| 1693 | error.SetError(errno, eErrorTypePOSIX); |
| 1694 | error.LogIfError(log, "unable to save the current directory"); |
| 1695 | return error; |
| 1696 | } |
| 1697 | |
| 1698 | if (::chdir(working_dir) == -1) |
| 1699 | { |
| 1700 | error.SetError(errno, eErrorTypePOSIX); |
| 1701 | error.LogIfError(log, "unable to change working directory to %s", working_dir); |
| 1702 | return error; |
| 1703 | } |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1704 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1707 | const size_t num_file_actions = launch_info.GetNumFileActions (); |
| 1708 | if (num_file_actions > 0) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1709 | { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1710 | posix_spawn_file_actions_t file_actions; |
| 1711 | error.SetError( ::posix_spawn_file_actions_init (&file_actions), eErrorTypePOSIX); |
| 1712 | if (error.Fail() || log) |
| 1713 | error.PutToLog(log, "::posix_spawn_file_actions_init ( &file_actions )"); |
| 1714 | if (error.Fail()) |
| 1715 | return error; |
| 1716 | |
| 1717 | // Make a quick class that will cleanup the posix spawn attributes in case |
| 1718 | // we return in the middle of this function. |
| 1719 | lldb_utility::CleanUp <posix_spawn_file_actions_t *, int> posix_spawn_file_actions_cleanup (&file_actions, posix_spawn_file_actions_destroy); |
| 1720 | |
| 1721 | for (size_t i=0; i<num_file_actions; ++i) |
| 1722 | { |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1723 | const FileAction *launch_file_action = launch_info.GetFileActionAtIndex(i); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1724 | if (launch_file_action) |
| 1725 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1726 | if (!AddPosixSpawnFileAction(&file_actions, launch_file_action, log, error)) |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1727 | return error; |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | error.SetError (::posix_spawnp (&pid, |
| 1732 | exe_path, |
| 1733 | &file_actions, |
| 1734 | &attr, |
| 1735 | argv, |
| 1736 | envp), |
| 1737 | eErrorTypePOSIX); |
| 1738 | |
| 1739 | if (error.Fail() || log) |
| 1740 | { |
| 1741 | 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] | 1742 | pid, exe_path, static_cast<void*>(&file_actions), |
| 1743 | static_cast<void*>(&attr), |
| 1744 | reinterpret_cast<const void*>(argv), |
| 1745 | reinterpret_cast<const void*>(envp)); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1746 | if (log) |
| 1747 | { |
| 1748 | for (int ii=0; argv[ii]; ++ii) |
| 1749 | log->Printf("argv[%i] = '%s'", ii, argv[ii]); |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | } |
| 1754 | else |
| 1755 | { |
| 1756 | error.SetError (::posix_spawnp (&pid, |
| 1757 | exe_path, |
| 1758 | NULL, |
| 1759 | &attr, |
| 1760 | argv, |
| 1761 | envp), |
| 1762 | eErrorTypePOSIX); |
| 1763 | |
| 1764 | if (error.Fail() || log) |
| 1765 | { |
| 1766 | 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] | 1767 | pid, exe_path, static_cast<void*>(&attr), |
| 1768 | reinterpret_cast<const void*>(argv), |
| 1769 | reinterpret_cast<const void*>(envp)); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1770 | if (log) |
| 1771 | { |
| 1772 | for (int ii=0; argv[ii]; ++ii) |
| 1773 | log->Printf("argv[%i] = '%s'", ii, argv[ii]); |
| 1774 | } |
| 1775 | } |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1778 | if (working_dir) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1779 | { |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1780 | #if defined (__APPLE__) |
| 1781 | // No more thread specific current working directory |
| 1782 | __pthread_fchdir (-1); |
| 1783 | #else |
| 1784 | if (::chdir(current_dir) == -1 && error.Success()) |
| 1785 | { |
| 1786 | error.SetError(errno, eErrorTypePOSIX); |
| 1787 | error.LogIfError(log, "unable to change current directory back to %s", |
| 1788 | current_dir); |
| 1789 | } |
| 1790 | #endif |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | return error; |
| 1794 | } |
| 1795 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1796 | bool |
| 1797 | Host::AddPosixSpawnFileAction(void *_file_actions, const FileAction *info, Log *log, Error &error) |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1798 | { |
| 1799 | if (info == NULL) |
| 1800 | return false; |
| 1801 | |
| 1802 | posix_spawn_file_actions_t *file_actions = reinterpret_cast<posix_spawn_file_actions_t *>(_file_actions); |
| 1803 | |
| 1804 | switch (info->GetAction()) |
| 1805 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1806 | case FileAction::eFileActionNone: |
| 1807 | error.Clear(); |
| 1808 | break; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1809 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1810 | case FileAction::eFileActionClose: |
| 1811 | if (info->GetFD() == -1) |
| 1812 | error.SetErrorString("invalid fd for posix_spawn_file_actions_addclose(...)"); |
| 1813 | else |
| 1814 | { |
| 1815 | error.SetError(::posix_spawn_file_actions_addclose(file_actions, info->GetFD()), eErrorTypePOSIX); |
| 1816 | if (log && (error.Fail() || log)) |
| 1817 | error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)", |
| 1818 | static_cast<void *>(file_actions), info->GetFD()); |
| 1819 | } |
| 1820 | break; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1821 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1822 | case FileAction::eFileActionDuplicate: |
| 1823 | if (info->GetFD() == -1) |
| 1824 | error.SetErrorString("invalid fd for posix_spawn_file_actions_adddup2(...)"); |
| 1825 | else if (info->GetActionArgument() == -1) |
| 1826 | error.SetErrorString("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)"); |
| 1827 | else |
| 1828 | { |
| 1829 | error.SetError( |
| 1830 | ::posix_spawn_file_actions_adddup2(file_actions, info->GetFD(), info->GetActionArgument()), |
| 1831 | eErrorTypePOSIX); |
| 1832 | if (log && (error.Fail() || log)) |
| 1833 | error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)", |
| 1834 | static_cast<void *>(file_actions), info->GetFD(), info->GetActionArgument()); |
| 1835 | } |
| 1836 | break; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1837 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1838 | case FileAction::eFileActionOpen: |
| 1839 | if (info->GetFD() == -1) |
| 1840 | error.SetErrorString("invalid fd in posix_spawn_file_actions_addopen(...)"); |
| 1841 | else |
| 1842 | { |
| 1843 | int oflag = info->GetActionArgument(); |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1844 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1845 | mode_t mode = 0; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1846 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1847 | if (oflag & O_CREAT) |
| 1848 | mode = 0640; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1849 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1850 | error.SetError( |
| 1851 | ::posix_spawn_file_actions_addopen(file_actions, info->GetFD(), info->GetPath(), oflag, mode), |
| 1852 | eErrorTypePOSIX); |
| 1853 | if (error.Fail() || log) |
| 1854 | error.PutToLog(log, |
| 1855 | "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)", |
| 1856 | static_cast<void *>(file_actions), info->GetFD(), info->GetPath(), oflag, mode); |
| 1857 | } |
| 1858 | break; |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1859 | } |
| 1860 | return error.Success(); |
| 1861 | } |
| 1862 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1863 | #endif // LaunchProcedssPosixSpawn: Apple, Linux, FreeBSD and other GLIBC systems |
| 1864 | |
| 1865 | |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 1866 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__GLIBC__) || defined(__NetBSD__) |
| 1867 | // The functions below implement process launching via posix_spawn() for Linux, |
| 1868 | // FreeBSD and NetBSD. |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1869 | |
| 1870 | Error |
| 1871 | Host::LaunchProcess (ProcessLaunchInfo &launch_info) |
| 1872 | { |
| 1873 | Error error; |
| 1874 | char exe_path[PATH_MAX]; |
| 1875 | |
| 1876 | PlatformSP host_platform_sp (Platform::GetDefaultPlatform ()); |
| 1877 | |
| 1878 | const ArchSpec &arch_spec = launch_info.GetArchitecture(); |
| 1879 | |
| 1880 | FileSpec exe_spec(launch_info.GetExecutableFile()); |
| 1881 | |
| 1882 | FileSpec::FileType file_type = exe_spec.GetFileType(); |
| 1883 | if (file_type != FileSpec::eFileTypeRegular) |
| 1884 | { |
| 1885 | lldb::ModuleSP exe_module_sp; |
| 1886 | error = host_platform_sp->ResolveExecutable (exe_spec, |
| 1887 | arch_spec, |
| 1888 | exe_module_sp, |
| 1889 | NULL); |
| 1890 | |
| 1891 | if (error.Fail()) |
| 1892 | return error; |
| 1893 | |
| 1894 | if (exe_module_sp) |
| 1895 | exe_spec = exe_module_sp->GetFileSpec(); |
| 1896 | } |
| 1897 | |
| 1898 | if (exe_spec.Exists()) |
| 1899 | { |
| 1900 | exe_spec.GetPath (exe_path, sizeof(exe_path)); |
| 1901 | } |
| 1902 | else |
| 1903 | { |
| 1904 | launch_info.GetExecutableFile().GetPath (exe_path, sizeof(exe_path)); |
| 1905 | error.SetErrorStringWithFormat ("executable doesn't exist: '%s'", exe_path); |
| 1906 | return error; |
| 1907 | } |
| 1908 | |
| 1909 | assert(!launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY)); |
| 1910 | |
| 1911 | ::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 1912 | |
| 1913 | error = LaunchProcessPosixSpawn(exe_path, launch_info, pid); |
| 1914 | |
| 1915 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 1916 | { |
| 1917 | // If all went well, then set the process ID into the launch info |
| 1918 | launch_info.SetProcessID(pid); |
| 1919 | |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1920 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1921 | |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1922 | // Make sure we reap any processes we spawn or we will have zombies. |
| 1923 | if (!launch_info.MonitorProcess()) |
| 1924 | { |
| 1925 | const bool monitor_signals = false; |
| 1926 | StartMonitoringChildProcess (Process::SetProcessExitStatus, |
| 1927 | NULL, |
| 1928 | pid, |
| 1929 | monitor_signals); |
Todd Fiala | 7674712 | 2014-01-23 00:52:28 +0000 | [diff] [blame] | 1930 | if (log) |
| 1931 | log->PutCString ("monitored child process with default Process::SetProcessExitStatus."); |
| 1932 | } |
| 1933 | else |
| 1934 | { |
| 1935 | if (log) |
| 1936 | log->PutCString ("monitored child process with user-specified process monitor."); |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | else |
| 1940 | { |
| 1941 | // Invalid process ID, something didn't go well |
| 1942 | if (error.Success()) |
| 1943 | error.SetErrorString ("process launch failed for unknown reasons"); |
| 1944 | } |
| 1945 | return error; |
| 1946 | } |
| 1947 | |
Joerg Sonnenberger | a53b359 | 2014-05-02 19:09:40 +0000 | [diff] [blame] | 1948 | #endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) |
Daniel Malea | 4244cbd | 2013-08-27 20:58:59 +0000 | [diff] [blame] | 1949 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1950 | #ifndef _WIN32 |
| 1951 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1952 | void |
| 1953 | Host::Kill(lldb::pid_t pid, int signo) |
| 1954 | { |
| 1955 | ::kill(pid, signo); |
| 1956 | } |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 1957 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 1958 | #endif |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1959 | |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 1960 | #if !defined (__APPLE__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1961 | bool |
Greg Clayton | 3b14763 | 2010-12-18 01:54:34 +0000 | [diff] [blame] | 1962 | Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1963 | { |
| 1964 | return false; |
| 1965 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1966 | |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 1967 | void |
| 1968 | Host::SetCrashDescriptionWithFormat (const char *format, ...) |
| 1969 | { |
| 1970 | } |
| 1971 | |
| 1972 | void |
| 1973 | Host::SetCrashDescription (const char *description) |
| 1974 | { |
| 1975 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1976 | |
| 1977 | lldb::pid_t |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1978 | Host::LaunchApplication (const FileSpec &app_file_spec) |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1979 | { |
| 1980 | return LLDB_INVALID_PROCESS_ID; |
| 1981 | } |
| 1982 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1983 | #endif |