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 |
| 13 | #include <dlfcn.h> |
| 14 | #include <errno.h> |
| 15 | #include <grp.h> |
| 16 | #include <limits.h> |
| 17 | #include <netdb.h> |
| 18 | #include <pwd.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 19 | #include <sys/types.h> |
Ed Maste | f2b0162 | 2013-06-28 12:35:08 +0000 | [diff] [blame] | 20 | #include <sys/sysctl.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
| 23 | #if defined (__APPLE__) |
| 24 | |
| 25 | #include <dispatch/dispatch.h> |
| 26 | #include <libproc.h> |
| 27 | #include <mach-o/dyld.h> |
| 28 | #include <mach/mach_port.h> |
| 29 | |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 30 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 31 | |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 32 | #if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 33 | #include <sys/wait.h> |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 34 | #include <sys/syscall.h> |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 35 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 36 | |
Ed Maste | 8b006c6 | 2013-06-25 18:58:11 +0000 | [diff] [blame] | 37 | #if defined (__FreeBSD__) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 38 | #include <pthread_np.h> |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 41 | #include "lldb/Host/Host.h" |
| 42 | #include "lldb/Core/ArchSpec.h" |
| 43 | #include "lldb/Core/ConstString.h" |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 44 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 45 | #include "lldb/Core/Error.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 46 | #include "lldb/Core/Log.h" |
| 47 | #include "lldb/Core/StreamString.h" |
Jim Ingham | c075ecd | 2012-05-04 19:24:49 +0000 | [diff] [blame] | 48 | #include "lldb/Core/ThreadSafeSTLMap.h" |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 49 | #include "lldb/Host/Config.h" |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 50 | #include "lldb/Host/Endian.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 51 | #include "lldb/Host/FileSpec.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 52 | #include "lldb/Host/Mutex.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 53 | #include "lldb/Target/Process.h" |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 54 | #include "lldb/Target/TargetList.h" |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 55 | |
Daniel Malea | fa7425d | 2013-08-06 21:40:08 +0000 | [diff] [blame^] | 56 | #include "llvm/ADT/SmallString.h" |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 57 | #include "llvm/Support/Host.h" |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 58 | #include "llvm/Support/MachO.h" |
Daniel Malea | fa7425d | 2013-08-06 21:40:08 +0000 | [diff] [blame^] | 59 | #include "llvm/Support/raw_ostream.h" |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 60 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 61 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 62 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 63 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 64 | |
| 65 | using namespace lldb; |
| 66 | using namespace lldb_private; |
| 67 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 68 | |
Greg Clayton | 1c4cd07 | 2011-11-17 19:41:57 +0000 | [diff] [blame] | 69 | #if !defined (__APPLE__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 70 | struct MonitorInfo |
| 71 | { |
| 72 | lldb::pid_t pid; // The process ID to monitor |
| 73 | Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals |
| 74 | void *callback_baton; // The callback baton for the callback function |
| 75 | bool monitor_signals; // If true, call the callback when "pid" gets signaled. |
| 76 | }; |
| 77 | |
| 78 | static void * |
| 79 | MonitorChildProcessThreadFunction (void *arg); |
| 80 | |
| 81 | lldb::thread_t |
| 82 | Host::StartMonitoringChildProcess |
| 83 | ( |
| 84 | Host::MonitorChildProcessCallback callback, |
| 85 | void *callback_baton, |
| 86 | lldb::pid_t pid, |
| 87 | bool monitor_signals |
| 88 | ) |
| 89 | { |
| 90 | lldb::thread_t thread = LLDB_INVALID_HOST_THREAD; |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 91 | MonitorInfo * info_ptr = new MonitorInfo(); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 92 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 93 | info_ptr->pid = pid; |
| 94 | info_ptr->callback = callback; |
| 95 | info_ptr->callback_baton = callback_baton; |
| 96 | info_ptr->monitor_signals = monitor_signals; |
| 97 | |
| 98 | char thread_name[256]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 99 | ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid); |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 100 | thread = ThreadCreate (thread_name, |
| 101 | MonitorChildProcessThreadFunction, |
| 102 | info_ptr, |
| 103 | NULL); |
| 104 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 105 | return thread; |
| 106 | } |
| 107 | |
| 108 | //------------------------------------------------------------------ |
| 109 | // Scoped class that will disable thread canceling when it is |
| 110 | // constructed, and exception safely restore the previous value it |
| 111 | // when it goes out of scope. |
| 112 | //------------------------------------------------------------------ |
| 113 | class ScopedPThreadCancelDisabler |
| 114 | { |
| 115 | public: |
| 116 | ScopedPThreadCancelDisabler() |
| 117 | { |
| 118 | // Disable the ability for this thread to be cancelled |
| 119 | int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state); |
| 120 | if (err != 0) |
| 121 | m_old_state = -1; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | ~ScopedPThreadCancelDisabler() |
| 126 | { |
| 127 | // Restore the ability for this thread to be cancelled to what it |
| 128 | // previously was. |
| 129 | if (m_old_state != -1) |
| 130 | ::pthread_setcancelstate (m_old_state, 0); |
| 131 | } |
| 132 | private: |
| 133 | int m_old_state; // Save the old cancelability state. |
| 134 | }; |
| 135 | |
| 136 | static void * |
| 137 | MonitorChildProcessThreadFunction (void *arg) |
| 138 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 139 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 140 | const char *function = __FUNCTION__; |
| 141 | if (log) |
| 142 | log->Printf ("%s (arg = %p) thread starting...", function, arg); |
| 143 | |
| 144 | MonitorInfo *info = (MonitorInfo *)arg; |
| 145 | |
| 146 | const Host::MonitorChildProcessCallback callback = info->callback; |
| 147 | void * const callback_baton = info->callback_baton; |
| 148 | const lldb::pid_t pid = info->pid; |
| 149 | const bool monitor_signals = info->monitor_signals; |
| 150 | |
| 151 | delete info; |
| 152 | |
| 153 | int status = -1; |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 154 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
Ashok Thirumurthi | 0f3b9b8 | 2013-05-01 20:38:19 +0000 | [diff] [blame] | 155 | #define __WALL 0 |
| 156 | #endif |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 157 | const int options = __WALL; |
| 158 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 159 | while (1) |
| 160 | { |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 161 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 162 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 163 | log->Printf("%s ::wait_pid (pid = %" PRIu64 ", &status, options = %i)...", function, pid, options); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 164 | |
| 165 | // Wait for all child processes |
| 166 | ::pthread_testcancel (); |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 167 | // Get signals from all children with same process group of pid |
| 168 | const lldb::pid_t wait_pid = ::waitpid (-1*pid, &status, options); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 169 | ::pthread_testcancel (); |
| 170 | |
| 171 | if (wait_pid == -1) |
| 172 | { |
| 173 | if (errno == EINTR) |
| 174 | continue; |
| 175 | else |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 176 | { |
| 177 | if (log) |
| 178 | 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] | 179 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 180 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 181 | } |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 182 | else if (wait_pid > 0) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 183 | { |
| 184 | bool exited = false; |
| 185 | int signal = 0; |
| 186 | int exit_status = 0; |
| 187 | const char *status_cstr = NULL; |
| 188 | if (WIFSTOPPED(status)) |
| 189 | { |
| 190 | signal = WSTOPSIG(status); |
| 191 | status_cstr = "STOPPED"; |
| 192 | } |
| 193 | else if (WIFEXITED(status)) |
| 194 | { |
| 195 | exit_status = WEXITSTATUS(status); |
| 196 | status_cstr = "EXITED"; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 197 | exited = true; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 198 | } |
| 199 | else if (WIFSIGNALED(status)) |
| 200 | { |
| 201 | signal = WTERMSIG(status); |
| 202 | status_cstr = "SIGNALED"; |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 203 | if (wait_pid == pid) { |
| 204 | exited = true; |
| 205 | exit_status = -1; |
| 206 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 207 | } |
| 208 | else |
| 209 | { |
Johnny Chen | 4480530 | 2011-07-19 19:48:13 +0000 | [diff] [blame] | 210 | status_cstr = "(\?\?\?)"; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Scope for pthread_cancel_disabler |
| 214 | { |
| 215 | ScopedPThreadCancelDisabler pthread_cancel_disabler; |
| 216 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 217 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 218 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 219 | log->Printf ("%s ::waitpid (pid = %" PRIu64 ", &status, options = %i) => pid = %" PRIu64 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 220 | function, |
| 221 | wait_pid, |
| 222 | options, |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 223 | pid, |
| 224 | status, |
| 225 | status_cstr, |
| 226 | signal, |
| 227 | exit_status); |
| 228 | |
| 229 | if (exited || (signal != 0 && monitor_signals)) |
| 230 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 231 | bool callback_return = false; |
| 232 | if (callback) |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 233 | callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 234 | |
| 235 | // If our process exited, then this thread should exit |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 236 | if (exited && wait_pid == pid) |
| 237 | { |
| 238 | if (log) |
| 239 | 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] | 240 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 241 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 242 | // If the callback returns true, it means this process should |
| 243 | // exit |
| 244 | if (callback_return) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 245 | { |
| 246 | if (log) |
| 247 | 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] | 248 | break; |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 249 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 255 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 256 | if (log) |
| 257 | log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg); |
| 258 | |
| 259 | return NULL; |
| 260 | } |
| 261 | |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 262 | |
| 263 | void |
| 264 | Host::SystemLog (SystemLogType type, const char *format, va_list args) |
| 265 | { |
| 266 | vfprintf (stderr, format, args); |
| 267 | } |
| 268 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 269 | #endif // #if !defined (__APPLE__) |
| 270 | |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 271 | void |
| 272 | Host::SystemLog (SystemLogType type, const char *format, ...) |
| 273 | { |
| 274 | va_list args; |
| 275 | va_start (args, format); |
| 276 | SystemLog (type, format, args); |
| 277 | va_end (args); |
| 278 | } |
| 279 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 280 | size_t |
| 281 | Host::GetPageSize() |
| 282 | { |
| 283 | return ::getpagesize(); |
| 284 | } |
| 285 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 286 | const ArchSpec & |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 287 | Host::GetArchitecture (SystemDefaultArchitecture arch_kind) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 288 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 289 | static bool g_supports_32 = false; |
| 290 | static bool g_supports_64 = false; |
| 291 | static ArchSpec g_host_arch_32; |
| 292 | static ArchSpec g_host_arch_64; |
| 293 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 294 | #if defined (__APPLE__) |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 295 | |
| 296 | // Apple is different in that it can support both 32 and 64 bit executables |
| 297 | // in the same operating system running concurrently. Here we detect the |
| 298 | // correct host architectures for both 32 and 64 bit including if 64 bit |
| 299 | // executables are supported on the system. |
| 300 | |
| 301 | if (g_supports_32 == false && g_supports_64 == false) |
| 302 | { |
| 303 | // All apple systems support 32 bit execution. |
| 304 | g_supports_32 = true; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 305 | uint32_t cputype, cpusubtype; |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 306 | uint32_t is_64_bit_capable = false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 307 | size_t len = sizeof(cputype); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 308 | ArchSpec host_arch; |
| 309 | // These will tell us about the kernel architecture, which even on a 64 |
| 310 | // bit machine can be 32 bit... |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 311 | if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0) |
| 312 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 313 | len = sizeof (cpusubtype); |
| 314 | if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0) |
| 315 | cpusubtype = CPU_TYPE_ANY; |
| 316 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 317 | len = sizeof (is_64_bit_capable); |
| 318 | if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0) |
| 319 | { |
| 320 | if (is_64_bit_capable) |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 321 | g_supports_64 = true; |
| 322 | } |
| 323 | |
| 324 | if (is_64_bit_capable) |
| 325 | { |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 326 | #if defined (__i386__) || defined (__x86_64__) |
| 327 | if (cpusubtype == CPU_SUBTYPE_486) |
| 328 | cpusubtype = CPU_SUBTYPE_I386_ALL; |
| 329 | #endif |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 330 | if (cputype & CPU_ARCH_ABI64) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 331 | { |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 332 | // We have a 64 bit kernel on a 64 bit system |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 333 | g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype); |
| 334 | g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 335 | } |
| 336 | else |
| 337 | { |
| 338 | // We have a 32 bit kernel on a 64 bit system |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 339 | g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 340 | cputype |= CPU_ARCH_ABI64; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 341 | g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 344 | else |
| 345 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 346 | g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 347 | g_host_arch_64.Clear(); |
| 348 | } |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 349 | } |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | #else // #if defined (__APPLE__) |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 353 | |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 354 | if (g_supports_32 == false && g_supports_64 == false) |
| 355 | { |
Peter Collingbourne | 1f6198d | 2011-11-05 01:35:31 +0000 | [diff] [blame] | 356 | llvm::Triple triple(llvm::sys::getDefaultTargetTriple()); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 357 | |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 358 | g_host_arch_32.Clear(); |
| 359 | g_host_arch_64.Clear(); |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 360 | |
Greg Clayton | b29e6c6 | 2012-10-11 17:38:58 +0000 | [diff] [blame] | 361 | // If the OS is Linux, "unknown" in the vendor slot isn't what we want |
| 362 | // for the default triple. It's probably an artifact of config.guess. |
| 363 | if (triple.getOS() == llvm::Triple::Linux && triple.getVendor() == llvm::Triple::UnknownVendor) |
| 364 | triple.setVendorName(""); |
| 365 | |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 366 | switch (triple.getArch()) |
| 367 | { |
| 368 | default: |
| 369 | g_host_arch_32.SetTriple(triple); |
| 370 | g_supports_32 = true; |
| 371 | break; |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 372 | |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 373 | case llvm::Triple::x86_64: |
Greg Clayton | 542e407 | 2012-09-07 17:49:29 +0000 | [diff] [blame] | 374 | g_host_arch_64.SetTriple(triple); |
| 375 | g_supports_64 = true; |
| 376 | g_host_arch_32.SetTriple(triple.get32BitArchVariant()); |
| 377 | g_supports_32 = true; |
| 378 | break; |
| 379 | |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 380 | case llvm::Triple::sparcv9: |
| 381 | case llvm::Triple::ppc64: |
Stephen Wilson | bd58871 | 2011-02-24 19:15:09 +0000 | [diff] [blame] | 382 | g_host_arch_64.SetTriple(triple); |
| 383 | g_supports_64 = true; |
| 384 | break; |
| 385 | } |
Greg Clayton | 4796c4f | 2011-02-17 02:05:38 +0000 | [diff] [blame] | 386 | |
| 387 | g_supports_32 = g_host_arch_32.IsValid(); |
| 388 | g_supports_64 = g_host_arch_64.IsValid(); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 389 | } |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 390 | |
| 391 | #endif // #else for #if defined (__APPLE__) |
| 392 | |
| 393 | if (arch_kind == eSystemDefaultArchitecture32) |
| 394 | return g_host_arch_32; |
| 395 | else if (arch_kind == eSystemDefaultArchitecture64) |
| 396 | return g_host_arch_64; |
| 397 | |
| 398 | if (g_supports_64) |
| 399 | return g_host_arch_64; |
| 400 | |
| 401 | return g_host_arch_32; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | const ConstString & |
| 405 | Host::GetVendorString() |
| 406 | { |
| 407 | static ConstString g_vendor; |
| 408 | if (!g_vendor) |
| 409 | { |
Greg Clayton | 950971f | 2012-05-12 00:01:21 +0000 | [diff] [blame] | 410 | const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture); |
| 411 | const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName(); |
| 412 | g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size()); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 413 | } |
| 414 | return g_vendor; |
| 415 | } |
| 416 | |
| 417 | const ConstString & |
| 418 | Host::GetOSString() |
| 419 | { |
| 420 | static ConstString g_os_string; |
| 421 | if (!g_os_string) |
| 422 | { |
Greg Clayton | 950971f | 2012-05-12 00:01:21 +0000 | [diff] [blame] | 423 | const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture); |
| 424 | const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName(); |
| 425 | g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size()); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 426 | } |
| 427 | return g_os_string; |
| 428 | } |
| 429 | |
| 430 | const ConstString & |
| 431 | Host::GetTargetTriple() |
| 432 | { |
| 433 | static ConstString g_host_triple; |
| 434 | if (!(g_host_triple)) |
| 435 | { |
Greg Clayton | 950971f | 2012-05-12 00:01:21 +0000 | [diff] [blame] | 436 | const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture); |
| 437 | g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str()); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 438 | } |
| 439 | return g_host_triple; |
| 440 | } |
| 441 | |
| 442 | lldb::pid_t |
| 443 | Host::GetCurrentProcessID() |
| 444 | { |
| 445 | return ::getpid(); |
| 446 | } |
| 447 | |
| 448 | lldb::tid_t |
| 449 | Host::GetCurrentThreadID() |
| 450 | { |
| 451 | #if defined (__APPLE__) |
Greg Clayton | 813ddfc | 2012-09-18 18:19:49 +0000 | [diff] [blame] | 452 | // Calling "mach_port_deallocate()" bumps the reference count on the thread |
| 453 | // port, so we need to deallocate it. mach_task_self() doesn't bump the ref |
| 454 | // count. |
| 455 | thread_port_t thread_self = mach_thread_self(); |
| 456 | mach_port_deallocate(mach_task_self(), thread_self); |
| 457 | return thread_self; |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 458 | #elif defined(__FreeBSD__) |
| 459 | return lldb::tid_t(pthread_getthreadid_np()); |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 460 | #elif defined(__linux__) |
| 461 | return lldb::tid_t(syscall(SYS_gettid)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 462 | #else |
| 463 | return lldb::tid_t(pthread_self()); |
| 464 | #endif |
| 465 | } |
| 466 | |
Jim Ingham | 372787f | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 467 | lldb::thread_t |
| 468 | Host::GetCurrentThread () |
| 469 | { |
| 470 | return lldb::thread_t(pthread_self()); |
| 471 | } |
| 472 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 473 | const char * |
| 474 | Host::GetSignalAsCString (int signo) |
| 475 | { |
| 476 | switch (signo) |
| 477 | { |
| 478 | case SIGHUP: return "SIGHUP"; // 1 hangup |
| 479 | case SIGINT: return "SIGINT"; // 2 interrupt |
| 480 | case SIGQUIT: return "SIGQUIT"; // 3 quit |
| 481 | case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught) |
| 482 | case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught) |
| 483 | case SIGABRT: return "SIGABRT"; // 6 abort() |
Greg Clayton | 0ddf6be | 2011-11-04 03:42:38 +0000 | [diff] [blame] | 484 | #if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE)) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 485 | case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported) |
Benjamin Kramer | 44030f1 | 2011-11-04 16:06:40 +0000 | [diff] [blame] | 486 | #endif |
| 487 | #if !defined(_POSIX_C_SOURCE) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 488 | case SIGEMT: return "SIGEMT"; // 7 EMT instruction |
Benjamin Kramer | 44030f1 | 2011-11-04 16:06:40 +0000 | [diff] [blame] | 489 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 490 | case SIGFPE: return "SIGFPE"; // 8 floating point exception |
| 491 | case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored) |
| 492 | case SIGBUS: return "SIGBUS"; // 10 bus error |
| 493 | case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation |
| 494 | case SIGSYS: return "SIGSYS"; // 12 bad argument to system call |
| 495 | case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it |
| 496 | case SIGALRM: return "SIGALRM"; // 14 alarm clock |
| 497 | case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill |
| 498 | case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel |
| 499 | case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty |
| 500 | case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty |
| 501 | case SIGCONT: return "SIGCONT"; // 19 continue a stopped process |
| 502 | case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit |
| 503 | case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read |
| 504 | case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local<OSTOP) |
| 505 | #if !defined(_POSIX_C_SOURCE) |
| 506 | case SIGIO: return "SIGIO"; // 23 input/output possible signal |
| 507 | #endif |
| 508 | case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit |
| 509 | case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit |
| 510 | case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm |
| 511 | case SIGPROF: return "SIGPROF"; // 27 profiling time alarm |
| 512 | #if !defined(_POSIX_C_SOURCE) |
| 513 | case SIGWINCH: return "SIGWINCH"; // 28 window size changes |
| 514 | case SIGINFO: return "SIGINFO"; // 29 information request |
| 515 | #endif |
| 516 | case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1 |
| 517 | case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2 |
| 518 | default: |
| 519 | break; |
| 520 | } |
| 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | void |
| 525 | Host::WillTerminate () |
| 526 | { |
| 527 | } |
| 528 | |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 529 | #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] | 530 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 531 | void |
| 532 | Host::ThreadCreated (const char *thread_name) |
| 533 | { |
| 534 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 535 | |
Peter Collingbourne | 2ced913 | 2011-08-05 00:35:43 +0000 | [diff] [blame] | 536 | void |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 537 | Host::Backtrace (Stream &strm, uint32_t max_frames) |
| 538 | { |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 539 | // 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] | 540 | } |
| 541 | |
Greg Clayton | 85851dd | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 542 | size_t |
| 543 | Host::GetEnvironment (StringList &env) |
| 544 | { |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 545 | // 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] | 546 | return 0; |
| 547 | } |
| 548 | |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 549 | #endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 550 | |
| 551 | struct HostThreadCreateInfo |
| 552 | { |
| 553 | std::string thread_name; |
| 554 | thread_func_t thread_fptr; |
| 555 | thread_arg_t thread_arg; |
| 556 | |
| 557 | HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) : |
| 558 | thread_name (name ? name : ""), |
| 559 | thread_fptr (fptr), |
| 560 | thread_arg (arg) |
| 561 | { |
| 562 | } |
| 563 | }; |
| 564 | |
| 565 | static thread_result_t |
| 566 | ThreadCreateTrampoline (thread_arg_t arg) |
| 567 | { |
| 568 | HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg; |
| 569 | Host::ThreadCreated (info->thread_name.c_str()); |
| 570 | thread_func_t thread_fptr = info->thread_fptr; |
| 571 | thread_arg_t thread_arg = info->thread_arg; |
| 572 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 573 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 574 | if (log) |
| 575 | log->Printf("thread created"); |
| 576 | |
| 577 | delete info; |
| 578 | return thread_fptr (thread_arg); |
| 579 | } |
| 580 | |
| 581 | lldb::thread_t |
| 582 | Host::ThreadCreate |
| 583 | ( |
| 584 | const char *thread_name, |
| 585 | thread_func_t thread_fptr, |
| 586 | thread_arg_t thread_arg, |
| 587 | Error *error |
| 588 | ) |
| 589 | { |
| 590 | lldb::thread_t thread = LLDB_INVALID_HOST_THREAD; |
| 591 | |
| 592 | // Host::ThreadCreateTrampoline will delete this pointer for us. |
| 593 | HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg); |
| 594 | |
| 595 | int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr); |
| 596 | if (err == 0) |
| 597 | { |
| 598 | if (error) |
| 599 | error->Clear(); |
| 600 | return thread; |
| 601 | } |
| 602 | |
| 603 | if (error) |
| 604 | error->SetError (err, eErrorTypePOSIX); |
| 605 | |
| 606 | return LLDB_INVALID_HOST_THREAD; |
| 607 | } |
| 608 | |
| 609 | bool |
| 610 | Host::ThreadCancel (lldb::thread_t thread, Error *error) |
| 611 | { |
| 612 | int err = ::pthread_cancel (thread); |
| 613 | if (error) |
| 614 | error->SetError(err, eErrorTypePOSIX); |
| 615 | return err == 0; |
| 616 | } |
| 617 | |
| 618 | bool |
| 619 | Host::ThreadDetach (lldb::thread_t thread, Error *error) |
| 620 | { |
| 621 | int err = ::pthread_detach (thread); |
| 622 | if (error) |
| 623 | error->SetError(err, eErrorTypePOSIX); |
| 624 | return err == 0; |
| 625 | } |
| 626 | |
| 627 | bool |
| 628 | Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error) |
| 629 | { |
| 630 | int err = ::pthread_join (thread, thread_result_ptr); |
| 631 | if (error) |
| 632 | error->SetError(err, eErrorTypePOSIX); |
| 633 | return err == 0; |
| 634 | } |
| 635 | |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 636 | bool |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 637 | Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name) |
| 638 | { |
Greg Clayton | 8571963 | 2013-02-27 22:51:58 +0000 | [diff] [blame] | 639 | #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] | 640 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 641 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
| 642 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 643 | pid = curr_pid; |
| 644 | |
| 645 | if (tid == LLDB_INVALID_THREAD_ID) |
| 646 | tid = curr_tid; |
| 647 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 648 | // Set the pthread name if possible |
| 649 | if (pid == curr_pid && tid == curr_tid) |
| 650 | { |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 651 | if (::pthread_setname_np (name) == 0) |
| 652 | return true; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 653 | } |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 654 | return false; |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 655 | #elif defined (__FreeBSD__) |
| 656 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 657 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
| 658 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 659 | pid = curr_pid; |
| 660 | |
| 661 | if (tid == LLDB_INVALID_THREAD_ID) |
| 662 | tid = curr_tid; |
| 663 | |
| 664 | // Set the pthread name if possible |
| 665 | if (pid == curr_pid && tid == curr_tid) |
| 666 | { |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 667 | ::pthread_set_name_np (::pthread_self(), name); |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 668 | return true; |
| 669 | } |
| 670 | return false; |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 671 | #elif defined (__linux__) || defined (__GLIBC__) |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 672 | void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np"); |
| 673 | if (fn) |
| 674 | { |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 675 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 676 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 677 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 678 | pid = curr_pid; |
| 679 | |
| 680 | if (tid == LLDB_INVALID_THREAD_ID) |
| 681 | tid = curr_tid; |
| 682 | |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 683 | if (pid == curr_pid && tid == curr_tid) |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 684 | { |
Michael Sartain | c205243 | 2013-08-01 18:51:08 +0000 | [diff] [blame] | 685 | int (*pthread_setname_np_func)(pthread_t thread, const char *name); |
| 686 | *reinterpret_cast<void **> (&pthread_setname_np_func) = fn; |
| 687 | |
| 688 | if (pthread_setname_np_func (::pthread_self(), name) == 0) |
Matt Kopec | 62502c6 | 2013-05-13 19:33:58 +0000 | [diff] [blame] | 689 | return true; |
| 690 | } |
| 691 | } |
| 692 | return false; |
Jim Ingham | 5c42d8a | 2013-05-15 18:27:08 +0000 | [diff] [blame] | 693 | #else |
| 694 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 695 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Ed Maste | 02983be | 2013-07-25 19:10:32 +0000 | [diff] [blame] | 698 | bool |
| 699 | Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, |
| 700 | const char *thread_name, size_t len) |
| 701 | { |
| 702 | char *namebuf = (char *)::malloc (len + 1); |
| 703 | |
| 704 | // Thread names are coming in like '<lldb.comm.debugger.edit>' and |
| 705 | // '<lldb.comm.debugger.editline>'. So just chopping the end of the string |
| 706 | // off leads to a lot of similar named threads. Go through the thread name |
| 707 | // and search for the last dot and use that. |
| 708 | const char *lastdot = ::strrchr (thread_name, '.'); |
| 709 | |
| 710 | if (lastdot && lastdot != thread_name) |
| 711 | thread_name = lastdot + 1; |
| 712 | ::strncpy (namebuf, thread_name, len); |
| 713 | namebuf[len] = 0; |
| 714 | |
| 715 | int namebuflen = strlen(namebuf); |
| 716 | if (namebuflen > 0) |
| 717 | { |
| 718 | if (namebuf[namebuflen - 1] == '(' || namebuf[namebuflen - 1] == '>') |
| 719 | { |
| 720 | // Trim off trailing '(' and '>' characters for a bit more cleanup. |
| 721 | namebuflen--; |
| 722 | namebuf[namebuflen] = 0; |
| 723 | } |
| 724 | return Host::SetThreadName (pid, tid, namebuf); |
| 725 | } |
| 726 | return false; |
| 727 | } |
| 728 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 729 | FileSpec |
| 730 | Host::GetProgramFileSpec () |
| 731 | { |
| 732 | static FileSpec g_program_filespec; |
| 733 | if (!g_program_filespec) |
| 734 | { |
| 735 | #if defined (__APPLE__) |
| 736 | char program_fullpath[PATH_MAX]; |
| 737 | // If DST is NULL, then return the number of bytes needed. |
| 738 | uint32_t len = sizeof(program_fullpath); |
| 739 | int err = _NSGetExecutablePath (program_fullpath, &len); |
| 740 | if (err == 0) |
Greg Clayton | b332639 | 2011-01-13 01:23:43 +0000 | [diff] [blame] | 741 | g_program_filespec.SetFile (program_fullpath, false); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 742 | else if (err == -1) |
| 743 | { |
| 744 | char *large_program_fullpath = (char *)::malloc (len + 1); |
| 745 | |
| 746 | err = _NSGetExecutablePath (large_program_fullpath, &len); |
| 747 | if (err == 0) |
Greg Clayton | b332639 | 2011-01-13 01:23:43 +0000 | [diff] [blame] | 748 | g_program_filespec.SetFile (large_program_fullpath, false); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 749 | |
| 750 | ::free (large_program_fullpath); |
| 751 | } |
| 752 | #elif defined (__linux__) |
| 753 | char exe_path[PATH_MAX]; |
Stephen Wilson | e5b94a9 | 2011-01-12 04:21:21 +0000 | [diff] [blame] | 754 | ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); |
| 755 | if (len > 0) { |
| 756 | exe_path[len] = 0; |
Greg Clayton | b332639 | 2011-01-13 01:23:43 +0000 | [diff] [blame] | 757 | g_program_filespec.SetFile(exe_path, false); |
Stephen Wilson | e5b94a9 | 2011-01-12 04:21:21 +0000 | [diff] [blame] | 758 | } |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 759 | #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 760 | int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() }; |
| 761 | size_t exe_path_size; |
| 762 | if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0) |
| 763 | { |
Greg Clayton | 87ff1ac | 2011-01-13 01:27:55 +0000 | [diff] [blame] | 764 | char *exe_path = new char[exe_path_size]; |
| 765 | if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0) |
| 766 | g_program_filespec.SetFile(exe_path, false); |
| 767 | delete[] exe_path; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 768 | } |
| 769 | #endif |
| 770 | } |
| 771 | return g_program_filespec; |
| 772 | } |
| 773 | |
| 774 | FileSpec |
| 775 | Host::GetModuleFileSpecForHostAddress (const void *host_addr) |
| 776 | { |
| 777 | FileSpec module_filespec; |
| 778 | Dl_info info; |
| 779 | if (::dladdr (host_addr, &info)) |
| 780 | { |
| 781 | if (info.dli_fname) |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 782 | module_filespec.SetFile(info.dli_fname, true); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 783 | } |
| 784 | return module_filespec; |
| 785 | } |
| 786 | |
| 787 | #if !defined (__APPLE__) // see Host.mm |
Greg Clayton | c859e2d | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 788 | |
| 789 | bool |
| 790 | Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle) |
| 791 | { |
| 792 | bundle.Clear(); |
| 793 | return false; |
| 794 | } |
| 795 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 796 | bool |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 797 | Host::ResolveExecutableInBundle (FileSpec &file) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 798 | { |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 799 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 800 | } |
| 801 | #endif |
| 802 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 803 | // Opaque info that tracks a dynamic library that was loaded |
| 804 | struct DynamicLibraryInfo |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 805 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 806 | DynamicLibraryInfo (const FileSpec &fs, int o, void *h) : |
| 807 | file_spec (fs), |
| 808 | open_options (o), |
| 809 | handle (h) |
| 810 | { |
| 811 | } |
| 812 | |
| 813 | const FileSpec file_spec; |
| 814 | uint32_t open_options; |
| 815 | void * handle; |
| 816 | }; |
| 817 | |
| 818 | void * |
| 819 | Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error) |
| 820 | { |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 821 | char path[PATH_MAX]; |
| 822 | if (file_spec.GetPath(path, sizeof(path))) |
| 823 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 824 | int mode = 0; |
| 825 | |
| 826 | if (options & eDynamicLibraryOpenOptionLazy) |
| 827 | mode |= RTLD_LAZY; |
Greg Clayton | f939945 | 2011-02-08 05:24:57 +0000 | [diff] [blame] | 828 | else |
| 829 | mode |= RTLD_NOW; |
| 830 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 831 | |
| 832 | if (options & eDynamicLibraryOpenOptionLocal) |
| 833 | mode |= RTLD_LOCAL; |
| 834 | else |
| 835 | mode |= RTLD_GLOBAL; |
| 836 | |
| 837 | #ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED |
| 838 | if (options & eDynamicLibraryOpenOptionLimitGetSymbol) |
| 839 | mode |= RTLD_FIRST; |
Greg Clayton | 75852f5 | 2011-02-07 17:43:47 +0000 | [diff] [blame] | 840 | #endif |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 841 | |
| 842 | void * opaque = ::dlopen (path, mode); |
| 843 | |
| 844 | if (opaque) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 845 | { |
| 846 | error.Clear(); |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 847 | return new DynamicLibraryInfo (file_spec, options, opaque); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 848 | } |
| 849 | else |
| 850 | { |
| 851 | error.SetErrorString(::dlerror()); |
| 852 | } |
| 853 | } |
| 854 | else |
| 855 | { |
| 856 | error.SetErrorString("failed to extract path"); |
| 857 | } |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 858 | return NULL; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | Error |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 862 | Host::DynamicLibraryClose (void *opaque) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 863 | { |
| 864 | Error error; |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 865 | if (opaque == NULL) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 866 | { |
| 867 | error.SetErrorString ("invalid dynamic library handle"); |
| 868 | } |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 869 | else |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 870 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 871 | DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque; |
| 872 | if (::dlclose (dylib_info->handle) != 0) |
| 873 | { |
| 874 | error.SetErrorString(::dlerror()); |
| 875 | } |
| 876 | |
| 877 | dylib_info->open_options = 0; |
| 878 | dylib_info->handle = 0; |
| 879 | delete dylib_info; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 880 | } |
| 881 | return error; |
| 882 | } |
| 883 | |
| 884 | void * |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 885 | Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 886 | { |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 887 | if (opaque == NULL) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 888 | { |
| 889 | error.SetErrorString ("invalid dynamic library handle"); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 890 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 891 | else |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 892 | { |
| 893 | DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque; |
| 894 | |
| 895 | void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name); |
| 896 | if (symbol_addr) |
| 897 | { |
| 898 | #ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED |
| 899 | // This host doesn't support limiting searches to this shared library |
| 900 | // so we need to verify that the match came from this shared library |
| 901 | // if it was requested in the Host::DynamicLibraryOpen() function. |
Greg Clayton | f939945 | 2011-02-08 05:24:57 +0000 | [diff] [blame] | 902 | if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol) |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 903 | { |
| 904 | FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr)); |
| 905 | if (match_dylib_spec != dylib_info->file_spec) |
| 906 | { |
| 907 | char dylib_path[PATH_MAX]; |
| 908 | if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path))) |
| 909 | error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path); |
| 910 | else |
| 911 | error.SetErrorString ("symbol not found"); |
| 912 | return NULL; |
| 913 | } |
| 914 | } |
| 915 | #endif |
| 916 | error.Clear(); |
| 917 | return symbol_addr; |
| 918 | } |
| 919 | else |
| 920 | { |
| 921 | error.SetErrorString(::dlerror()); |
| 922 | } |
| 923 | } |
| 924 | return NULL; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 925 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 926 | |
| 927 | bool |
| 928 | Host::GetLLDBPath (PathType path_type, FileSpec &file_spec) |
| 929 | { |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 930 | // 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] | 931 | // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB", |
| 932 | // 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] | 933 | // 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] | 934 | // need to be modified to "do the right thing". |
| 935 | |
| 936 | switch (path_type) |
| 937 | { |
| 938 | case ePathTypeLLDBShlibDir: |
| 939 | { |
| 940 | static ConstString g_lldb_so_dir; |
| 941 | if (!g_lldb_so_dir) |
| 942 | { |
| 943 | FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath)); |
| 944 | g_lldb_so_dir = lldb_file_spec.GetDirectory(); |
| 945 | } |
| 946 | file_spec.GetDirectory() = g_lldb_so_dir; |
| 947 | return file_spec.GetDirectory(); |
| 948 | } |
| 949 | break; |
| 950 | |
| 951 | case ePathTypeSupportExecutableDir: |
| 952 | { |
| 953 | static ConstString g_lldb_support_exe_dir; |
| 954 | if (!g_lldb_support_exe_dir) |
| 955 | { |
| 956 | FileSpec lldb_file_spec; |
| 957 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 958 | { |
| 959 | char raw_path[PATH_MAX]; |
| 960 | char resolved_path[PATH_MAX]; |
| 961 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 962 | |
| 963 | #if defined (__APPLE__) |
| 964 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 965 | if (framework_pos) |
| 966 | { |
| 967 | framework_pos += strlen("LLDB.framework"); |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 968 | #if !defined (__arm__) |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 969 | ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path)); |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 970 | #endif |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 971 | } |
| 972 | #endif |
| 973 | FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); |
| 974 | g_lldb_support_exe_dir.SetCString(resolved_path); |
| 975 | } |
| 976 | } |
| 977 | file_spec.GetDirectory() = g_lldb_support_exe_dir; |
| 978 | return file_spec.GetDirectory(); |
| 979 | } |
| 980 | break; |
| 981 | |
| 982 | case ePathTypeHeaderDir: |
| 983 | { |
| 984 | static ConstString g_lldb_headers_dir; |
| 985 | if (!g_lldb_headers_dir) |
| 986 | { |
| 987 | #if defined (__APPLE__) |
| 988 | FileSpec lldb_file_spec; |
| 989 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 990 | { |
| 991 | char raw_path[PATH_MAX]; |
| 992 | char resolved_path[PATH_MAX]; |
| 993 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 994 | |
| 995 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 996 | if (framework_pos) |
| 997 | { |
| 998 | framework_pos += strlen("LLDB.framework"); |
| 999 | ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path)); |
| 1000 | } |
| 1001 | FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); |
| 1002 | g_lldb_headers_dir.SetCString(resolved_path); |
| 1003 | } |
| 1004 | #else |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1005 | // TODO: Anyone know how we can determine this for linux? Other systems?? |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1006 | g_lldb_headers_dir.SetCString ("/opt/local/include/lldb"); |
| 1007 | #endif |
| 1008 | } |
| 1009 | file_spec.GetDirectory() = g_lldb_headers_dir; |
| 1010 | return file_spec.GetDirectory(); |
| 1011 | } |
| 1012 | break; |
| 1013 | |
Ed Maste | a85d364 | 2013-07-02 19:30:52 +0000 | [diff] [blame] | 1014 | #ifndef LLDB_DISABLE_PYTHON |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1015 | case ePathTypePythonDir: |
| 1016 | { |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1017 | static ConstString g_lldb_python_dir; |
| 1018 | if (!g_lldb_python_dir) |
| 1019 | { |
| 1020 | FileSpec lldb_file_spec; |
| 1021 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 1022 | { |
| 1023 | char raw_path[PATH_MAX]; |
| 1024 | char resolved_path[PATH_MAX]; |
| 1025 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1026 | |
| 1027 | #if defined (__APPLE__) |
| 1028 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 1029 | if (framework_pos) |
| 1030 | { |
| 1031 | framework_pos += strlen("LLDB.framework"); |
| 1032 | ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path)); |
| 1033 | } |
Filipe Cabecinhas | 0b75116 | 2012-07-30 16:46:32 +0000 | [diff] [blame] | 1034 | #else |
Daniel Malea | fa7425d | 2013-08-06 21:40:08 +0000 | [diff] [blame^] | 1035 | llvm::SmallString<256> python_version_dir; |
| 1036 | llvm::raw_svector_ostream os(python_version_dir); |
| 1037 | os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "/site-packages"; |
| 1038 | os.flush(); |
Daniel Malea | 53430eb | 2013-01-04 23:35:13 +0000 | [diff] [blame] | 1039 | |
Filipe Cabecinhas | cffbd09 | 2012-07-30 18:56:10 +0000 | [diff] [blame] | 1040 | // We may get our string truncated. Should we protect |
| 1041 | // this with an assert? |
Daniel Malea | 53430eb | 2013-01-04 23:35:13 +0000 | [diff] [blame] | 1042 | |
Daniel Malea | fa7425d | 2013-08-06 21:40:08 +0000 | [diff] [blame^] | 1043 | ::strncat(raw_path, python_version_dir.c_str(), |
Daniel Malea | 53430eb | 2013-01-04 23:35:13 +0000 | [diff] [blame] | 1044 | sizeof(raw_path) - strlen(raw_path) - 1); |
| 1045 | |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1046 | #endif |
| 1047 | FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); |
| 1048 | g_lldb_python_dir.SetCString(resolved_path); |
| 1049 | } |
| 1050 | } |
| 1051 | file_spec.GetDirectory() = g_lldb_python_dir; |
| 1052 | return file_spec.GetDirectory(); |
| 1053 | } |
| 1054 | break; |
Ed Maste | a85d364 | 2013-07-02 19:30:52 +0000 | [diff] [blame] | 1055 | #endif |
| 1056 | |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1057 | case ePathTypeLLDBSystemPlugins: // System plug-ins directory |
| 1058 | { |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1059 | #if defined (__APPLE__) || defined(__linux__) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1060 | static ConstString g_lldb_system_plugin_dir; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1061 | static bool g_lldb_system_plugin_dir_located = false; |
| 1062 | if (!g_lldb_system_plugin_dir_located) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1063 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1064 | g_lldb_system_plugin_dir_located = true; |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1065 | #if defined (__APPLE__) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1066 | FileSpec lldb_file_spec; |
| 1067 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 1068 | { |
| 1069 | char raw_path[PATH_MAX]; |
| 1070 | char resolved_path[PATH_MAX]; |
| 1071 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 1072 | |
| 1073 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 1074 | if (framework_pos) |
| 1075 | { |
| 1076 | framework_pos += strlen("LLDB.framework"); |
| 1077 | ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path)); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1078 | FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); |
| 1079 | g_lldb_system_plugin_dir.SetCString(resolved_path); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1080 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1081 | return false; |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1082 | } |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1083 | #elif defined (__linux__) |
| 1084 | FileSpec lldb_file_spec("/usr/lib/lldb", true); |
| 1085 | if (lldb_file_spec.Exists()) |
| 1086 | { |
| 1087 | g_lldb_system_plugin_dir.SetCString(lldb_file_spec.GetPath().c_str()); |
| 1088 | } |
| 1089 | #endif // __APPLE__ || __linux__ |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1090 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (g_lldb_system_plugin_dir) |
| 1093 | { |
| 1094 | file_spec.GetDirectory() = g_lldb_system_plugin_dir; |
| 1095 | return true; |
| 1096 | } |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1097 | #else |
| 1098 | // TODO: where would system LLDB plug-ins be located on other systems? |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1099 | return false; |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1100 | #endif |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1101 | } |
| 1102 | break; |
| 1103 | |
| 1104 | case ePathTypeLLDBUserPlugins: // User plug-ins directory |
| 1105 | { |
| 1106 | #if defined (__APPLE__) |
| 1107 | static ConstString g_lldb_user_plugin_dir; |
| 1108 | if (!g_lldb_user_plugin_dir) |
| 1109 | { |
| 1110 | char user_plugin_path[PATH_MAX]; |
| 1111 | if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns", |
| 1112 | user_plugin_path, |
| 1113 | sizeof(user_plugin_path))) |
| 1114 | { |
| 1115 | g_lldb_user_plugin_dir.SetCString(user_plugin_path); |
| 1116 | } |
| 1117 | } |
| 1118 | file_spec.GetDirectory() = g_lldb_user_plugin_dir; |
| 1119 | return file_spec.GetDirectory(); |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1120 | #elif defined (__linux__) |
| 1121 | static ConstString g_lldb_user_plugin_dir; |
| 1122 | if (!g_lldb_user_plugin_dir) |
| 1123 | { |
| 1124 | // XDG Base Directory Specification |
| 1125 | // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
| 1126 | // If XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb. |
| 1127 | FileSpec lldb_file_spec; |
| 1128 | const char *xdg_data_home = getenv("XDG_DATA_HOME"); |
| 1129 | if (xdg_data_home && xdg_data_home[0]) |
| 1130 | { |
| 1131 | std::string user_plugin_dir (xdg_data_home); |
| 1132 | user_plugin_dir += "/lldb"; |
| 1133 | lldb_file_spec.SetFile (user_plugin_dir.c_str(), true); |
| 1134 | } |
| 1135 | else |
| 1136 | { |
| 1137 | const char *home_dir = getenv("HOME"); |
| 1138 | if (home_dir && home_dir[0]) |
| 1139 | { |
| 1140 | std::string user_plugin_dir (home_dir); |
| 1141 | user_plugin_dir += "/.local/share/lldb"; |
| 1142 | lldb_file_spec.SetFile (user_plugin_dir.c_str(), true); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | if (lldb_file_spec.Exists()) |
| 1147 | g_lldb_user_plugin_dir.SetCString(lldb_file_spec.GetPath().c_str()); |
| 1148 | } |
| 1149 | file_spec.GetDirectory() = g_lldb_user_plugin_dir; |
| 1150 | return file_spec.GetDirectory(); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1151 | #endif |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1152 | // TODO: where would user LLDB plug-ins be located on other systems? |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1153 | return false; |
| 1154 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | return false; |
| 1158 | } |
| 1159 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1160 | |
| 1161 | bool |
| 1162 | Host::GetHostname (std::string &s) |
| 1163 | { |
| 1164 | char hostname[PATH_MAX]; |
| 1165 | hostname[sizeof(hostname) - 1] = '\0'; |
| 1166 | if (::gethostname (hostname, sizeof(hostname) - 1) == 0) |
| 1167 | { |
| 1168 | struct hostent* h = ::gethostbyname (hostname); |
| 1169 | if (h) |
| 1170 | s.assign (h->h_name); |
| 1171 | else |
| 1172 | s.assign (hostname); |
| 1173 | return true; |
| 1174 | } |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1178 | const char * |
| 1179 | Host::GetUserName (uint32_t uid, std::string &user_name) |
| 1180 | { |
| 1181 | struct passwd user_info; |
| 1182 | struct passwd *user_info_ptr = &user_info; |
| 1183 | char user_buffer[PATH_MAX]; |
| 1184 | size_t user_buffer_size = sizeof(user_buffer); |
| 1185 | if (::getpwuid_r (uid, |
| 1186 | &user_info, |
| 1187 | user_buffer, |
| 1188 | user_buffer_size, |
| 1189 | &user_info_ptr) == 0) |
| 1190 | { |
| 1191 | if (user_info_ptr) |
| 1192 | { |
| 1193 | user_name.assign (user_info_ptr->pw_name); |
| 1194 | return user_name.c_str(); |
| 1195 | } |
| 1196 | } |
| 1197 | user_name.clear(); |
| 1198 | return NULL; |
| 1199 | } |
| 1200 | |
| 1201 | const char * |
| 1202 | Host::GetGroupName (uint32_t gid, std::string &group_name) |
| 1203 | { |
| 1204 | char group_buffer[PATH_MAX]; |
| 1205 | size_t group_buffer_size = sizeof(group_buffer); |
| 1206 | struct group group_info; |
| 1207 | struct group *group_info_ptr = &group_info; |
| 1208 | // Try the threadsafe version first |
| 1209 | if (::getgrgid_r (gid, |
| 1210 | &group_info, |
| 1211 | group_buffer, |
| 1212 | group_buffer_size, |
| 1213 | &group_info_ptr) == 0) |
| 1214 | { |
| 1215 | if (group_info_ptr) |
| 1216 | { |
| 1217 | group_name.assign (group_info_ptr->gr_name); |
| 1218 | return group_name.c_str(); |
| 1219 | } |
| 1220 | } |
| 1221 | else |
| 1222 | { |
| 1223 | // The threadsafe version isn't currently working |
| 1224 | // for me on darwin, but the non-threadsafe version |
| 1225 | // is, so I am calling it below. |
| 1226 | group_info_ptr = ::getgrgid (gid); |
| 1227 | if (group_info_ptr) |
| 1228 | { |
| 1229 | group_name.assign (group_info_ptr->gr_name); |
| 1230 | return group_name.c_str(); |
| 1231 | } |
| 1232 | } |
| 1233 | group_name.clear(); |
| 1234 | return NULL; |
| 1235 | } |
| 1236 | |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 1237 | #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) // see macosx/Host.mm |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1238 | bool |
| 1239 | Host::GetOSBuildString (std::string &s) |
| 1240 | { |
| 1241 | s.clear(); |
| 1242 | return false; |
| 1243 | } |
| 1244 | |
| 1245 | bool |
| 1246 | Host::GetOSKernelDescription (std::string &s) |
| 1247 | { |
| 1248 | s.clear(); |
| 1249 | return false; |
| 1250 | } |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 1251 | #endif |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1252 | |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 1253 | uint32_t |
| 1254 | Host::GetUserID () |
| 1255 | { |
| 1256 | return getuid(); |
| 1257 | } |
| 1258 | |
| 1259 | uint32_t |
| 1260 | Host::GetGroupID () |
| 1261 | { |
| 1262 | return getgid(); |
| 1263 | } |
| 1264 | |
| 1265 | uint32_t |
| 1266 | Host::GetEffectiveUserID () |
| 1267 | { |
| 1268 | return geteuid(); |
| 1269 | } |
| 1270 | |
| 1271 | uint32_t |
| 1272 | Host::GetEffectiveGroupID () |
| 1273 | { |
| 1274 | return getegid(); |
| 1275 | } |
| 1276 | |
Daniel Malea | 25d7eb0 | 2013-05-15 17:54:07 +0000 | [diff] [blame] | 1277 | #if !defined (__APPLE__) && !defined(__linux__) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1278 | uint32_t |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1279 | Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1280 | { |
| 1281 | process_infos.Clear(); |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1282 | return process_infos.GetSize(); |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1283 | } |
Daniel Malea | 25d7eb0 | 2013-05-15 17:54:07 +0000 | [diff] [blame] | 1284 | #endif // #if !defined (__APPLE__) && !defined(__linux__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1285 | |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 1286 | #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined(__linux__) |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1287 | bool |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1288 | Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1289 | { |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 1290 | process_info.Clear(); |
| 1291 | return false; |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1292 | } |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 1293 | #endif |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1294 | |
Matt Kopec | 085d6ce | 2013-05-31 22:00:07 +0000 | [diff] [blame] | 1295 | #if !defined(__linux__) |
| 1296 | bool |
| 1297 | Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach) |
| 1298 | { |
| 1299 | return false; |
| 1300 | } |
| 1301 | #endif |
| 1302 | |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 1303 | lldb::TargetSP |
| 1304 | Host::GetDummyTarget (lldb_private::Debugger &debugger) |
| 1305 | { |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1306 | static TargetSP g_dummy_target_sp; |
Filipe Cabecinhas | b018345 | 2012-05-17 15:48:02 +0000 | [diff] [blame] | 1307 | |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1308 | // FIXME: Maybe the dummy target should be per-Debugger |
| 1309 | if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid()) |
| 1310 | { |
| 1311 | ArchSpec arch(Target::GetDefaultArchitecture()); |
| 1312 | if (!arch.IsValid()) |
| 1313 | arch = Host::GetArchitecture (); |
| 1314 | Error err = debugger.GetTargetList().CreateTarget(debugger, |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 1315 | NULL, |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1316 | arch.GetTriple().getTriple().c_str(), |
| 1317 | false, |
| 1318 | NULL, |
| 1319 | g_dummy_target_sp); |
| 1320 | } |
Filipe Cabecinhas | b018345 | 2012-05-17 15:48:02 +0000 | [diff] [blame] | 1321 | |
Filipe Cabecinhas | 721ba3f | 2012-05-19 09:59:08 +0000 | [diff] [blame] | 1322 | return g_dummy_target_sp; |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1325 | struct ShellInfo |
| 1326 | { |
| 1327 | ShellInfo () : |
| 1328 | process_reaped (false), |
| 1329 | can_delete (false), |
| 1330 | pid (LLDB_INVALID_PROCESS_ID), |
| 1331 | signo(-1), |
| 1332 | status(-1) |
| 1333 | { |
| 1334 | } |
| 1335 | |
| 1336 | lldb_private::Predicate<bool> process_reaped; |
| 1337 | lldb_private::Predicate<bool> can_delete; |
| 1338 | lldb::pid_t pid; |
| 1339 | int signo; |
| 1340 | int status; |
| 1341 | }; |
| 1342 | |
| 1343 | static bool |
| 1344 | MonitorShellCommand (void *callback_baton, |
| 1345 | lldb::pid_t pid, |
| 1346 | bool exited, // True if the process did exit |
| 1347 | int signo, // Zero for no signal |
| 1348 | int status) // Exit value of process if signal is zero |
| 1349 | { |
| 1350 | ShellInfo *shell_info = (ShellInfo *)callback_baton; |
| 1351 | shell_info->pid = pid; |
| 1352 | shell_info->signo = signo; |
| 1353 | shell_info->status = status; |
| 1354 | // Let the thread running Host::RunShellCommand() know that the process |
| 1355 | // exited and that ShellInfo has been filled in by broadcasting to it |
| 1356 | shell_info->process_reaped.SetValue(1, eBroadcastAlways); |
| 1357 | // Now wait for a handshake back from that thread running Host::RunShellCommand |
| 1358 | // so we know that we can delete shell_info_ptr |
| 1359 | shell_info->can_delete.WaitForValueEqualTo(true); |
| 1360 | // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete... |
| 1361 | usleep(1000); |
| 1362 | // Now delete the shell info that was passed into this function |
| 1363 | delete shell_info; |
| 1364 | return true; |
| 1365 | } |
| 1366 | |
| 1367 | Error |
| 1368 | Host::RunShellCommand (const char *command, |
| 1369 | const char *working_dir, |
| 1370 | int *status_ptr, |
| 1371 | int *signo_ptr, |
| 1372 | std::string *command_output_ptr, |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1373 | uint32_t timeout_sec, |
| 1374 | const char *shell) |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1375 | { |
| 1376 | Error error; |
| 1377 | ProcessLaunchInfo launch_info; |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1378 | if (shell && shell[0]) |
| 1379 | { |
| 1380 | // Run the command in a shell |
| 1381 | launch_info.SetShell(shell); |
| 1382 | launch_info.GetArguments().AppendArgument(command); |
| 1383 | const bool localhost = true; |
| 1384 | const bool will_debug = false; |
| 1385 | const bool first_arg_is_full_shell_command = true; |
| 1386 | launch_info.ConvertArgumentsForLaunchingInShell (error, |
| 1387 | localhost, |
| 1388 | will_debug, |
| 1389 | first_arg_is_full_shell_command); |
| 1390 | } |
| 1391 | else |
| 1392 | { |
| 1393 | // No shell, just run it |
| 1394 | Args args (command); |
| 1395 | const bool first_arg_is_executable = true; |
Greg Clayton | 4539255 | 2012-10-17 22:57:12 +0000 | [diff] [blame] | 1396 | launch_info.SetArguments(args, first_arg_is_executable); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1397 | } |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1398 | |
| 1399 | if (working_dir) |
| 1400 | launch_info.SetWorkingDirectory(working_dir); |
| 1401 | char output_file_path_buffer[L_tmpnam]; |
| 1402 | const char *output_file_path = NULL; |
| 1403 | if (command_output_ptr) |
| 1404 | { |
| 1405 | // Create a temporary file to get the stdout/stderr and redirect the |
| 1406 | // output of the command into this file. We will later read this file |
| 1407 | // if all goes well and fill the data into "command_output_ptr" |
| 1408 | output_file_path = ::tmpnam(output_file_path_buffer); |
| 1409 | launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false); |
| 1410 | launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true); |
Greg Clayton | c8f814d | 2012-09-27 03:13:55 +0000 | [diff] [blame] | 1411 | launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1412 | } |
| 1413 | else |
| 1414 | { |
| 1415 | launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false); |
| 1416 | launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 1417 | launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true); |
| 1418 | } |
| 1419 | |
| 1420 | // The process monitor callback will delete the 'shell_info_ptr' below... |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 1421 | std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo()); |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1422 | |
| 1423 | const bool monitor_signals = false; |
| 1424 | launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals); |
| 1425 | |
| 1426 | error = LaunchProcess (launch_info); |
| 1427 | const lldb::pid_t pid = launch_info.GetProcessID(); |
| 1428 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 1429 | { |
| 1430 | // The process successfully launched, so we can defer ownership of |
| 1431 | // "shell_info" to the MonitorShellCommand callback function that will |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 1432 | // 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] | 1433 | // doesn't need to delete the ShellInfo anymore. |
| 1434 | ShellInfo *shell_info = shell_info_ap.release(); |
| 1435 | TimeValue timeout_time(TimeValue::Now()); |
| 1436 | timeout_time.OffsetWithSeconds(timeout_sec); |
| 1437 | bool timed_out = false; |
| 1438 | shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out); |
| 1439 | if (timed_out) |
| 1440 | { |
| 1441 | error.SetErrorString("timed out waiting for shell command to complete"); |
| 1442 | |
| 1443 | // Kill the process since it didn't complete withint the timeout specified |
| 1444 | ::kill (pid, SIGKILL); |
| 1445 | // Wait for the monitor callback to get the message |
| 1446 | timeout_time = TimeValue::Now(); |
| 1447 | timeout_time.OffsetWithSeconds(1); |
| 1448 | timed_out = false; |
| 1449 | shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out); |
| 1450 | } |
| 1451 | else |
| 1452 | { |
| 1453 | if (status_ptr) |
| 1454 | *status_ptr = shell_info->status; |
| 1455 | |
| 1456 | if (signo_ptr) |
| 1457 | *signo_ptr = shell_info->signo; |
| 1458 | |
| 1459 | if (command_output_ptr) |
| 1460 | { |
| 1461 | command_output_ptr->clear(); |
| 1462 | FileSpec file_spec(output_file_path, File::eOpenOptionRead); |
| 1463 | uint64_t file_size = file_spec.GetByteSize(); |
| 1464 | if (file_size > 0) |
| 1465 | { |
| 1466 | if (file_size > command_output_ptr->max_size()) |
| 1467 | { |
| 1468 | error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string"); |
| 1469 | } |
| 1470 | else |
| 1471 | { |
| 1472 | command_output_ptr->resize(file_size); |
| 1473 | file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error); |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | shell_info->can_delete.SetValue(true, eBroadcastAlways); |
| 1479 | } |
| 1480 | else |
| 1481 | { |
| 1482 | error.SetErrorString("failed to get process ID"); |
| 1483 | } |
| 1484 | |
| 1485 | if (output_file_path) |
| 1486 | ::unlink (output_file_path); |
| 1487 | // Handshake with the monitor thread, or just let it know in advance that |
| 1488 | // it can delete "shell_info" in case we timed out and were not able to kill |
| 1489 | // the process... |
| 1490 | return error; |
| 1491 | } |
| 1492 | |
| 1493 | |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 1494 | uint32_t |
| 1495 | Host::GetNumberCPUS () |
| 1496 | { |
| 1497 | static uint32_t g_num_cores = UINT32_MAX; |
| 1498 | if (g_num_cores == UINT32_MAX) |
| 1499 | { |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 1500 | #if defined(__APPLE__) or defined (__linux__) or defined (__FreeBSD__) or defined (__FreeBSD_kernel__) |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 1501 | |
| 1502 | g_num_cores = ::sysconf(_SC_NPROCESSORS_ONLN); |
| 1503 | |
| 1504 | #elif defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) |
| 1505 | |
| 1506 | // Header file for this might need to be included at the top of this file |
| 1507 | SYSTEM_INFO system_info; |
| 1508 | ::GetSystemInfo (&system_info); |
| 1509 | g_num_cores = system_info.dwNumberOfProcessors; |
| 1510 | |
| 1511 | #else |
| 1512 | |
| 1513 | // Assume POSIX support if a host specific case has not been supplied above |
| 1514 | g_num_cores = 0; |
| 1515 | int num_cores = 0; |
| 1516 | size_t num_cores_len = sizeof(num_cores); |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 1517 | #ifdef HW_AVAILCPU |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 1518 | int mib[] = { CTL_HW, HW_AVAILCPU }; |
Sylvestre Ledru | 5940583 | 2013-07-01 08:21:36 +0000 | [diff] [blame] | 1519 | #else |
| 1520 | int mib[] = { CTL_HW, HW_NCPU }; |
| 1521 | #endif |
Greg Clayton | e3e3fee | 2013-02-17 20:46:30 +0000 | [diff] [blame] | 1522 | |
| 1523 | /* get the number of CPUs from the system */ |
| 1524 | if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0)) |
| 1525 | { |
| 1526 | g_num_cores = num_cores; |
| 1527 | } |
| 1528 | else |
| 1529 | { |
| 1530 | mib[1] = HW_NCPU; |
| 1531 | num_cores_len = sizeof(num_cores); |
| 1532 | if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0)) |
| 1533 | { |
| 1534 | if (num_cores > 0) |
| 1535 | g_num_cores = num_cores; |
| 1536 | } |
| 1537 | } |
| 1538 | #endif |
| 1539 | } |
| 1540 | return g_num_cores; |
| 1541 | } |
| 1542 | |
| 1543 | |
Greg Clayton | d1cf11a | 2012-04-14 01:42:46 +0000 | [diff] [blame] | 1544 | |
Johnny Chen | 8f3d838 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 1545 | #if !defined (__APPLE__) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1546 | bool |
Greg Clayton | 3b14763 | 2010-12-18 01:54:34 +0000 | [diff] [blame] | 1547 | Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no) |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1548 | { |
| 1549 | return false; |
| 1550 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1551 | |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 1552 | void |
| 1553 | Host::SetCrashDescriptionWithFormat (const char *format, ...) |
| 1554 | { |
| 1555 | } |
| 1556 | |
| 1557 | void |
| 1558 | Host::SetCrashDescription (const char *description) |
| 1559 | { |
| 1560 | } |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 1561 | |
| 1562 | lldb::pid_t |
| 1563 | LaunchApplication (const FileSpec &app_file_spec) |
| 1564 | { |
| 1565 | return LLDB_INVALID_PROCESS_ID; |
| 1566 | } |
| 1567 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 1568 | #endif |