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