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