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