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