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" |
| 13 | #include "lldb/Core/Error.h" |
| 14 | #include "lldb/Core/FileSpec.h" |
| 15 | #include "lldb/Core/Log.h" |
| 16 | #include "lldb/Core/StreamString.h" |
| 17 | #include "lldb/Host/Mutex.h" |
| 18 | |
| 19 | #include <dlfcn.h> |
| 20 | #include <errno.h> |
| 21 | #include <sys/sysctl.h> |
| 22 | #include <sys/wait.h> |
| 23 | |
| 24 | #if defined (__APPLE__) |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 25 | #include <dispatch/dispatch.h> |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 26 | #include <libproc.h> |
| 27 | #include <mach-o/dyld.h> |
| 28 | #endif |
| 29 | |
| 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
| 33 | struct MonitorInfo |
| 34 | { |
| 35 | lldb::pid_t pid; // The process ID to monitor |
| 36 | Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals |
| 37 | void *callback_baton; // The callback baton for the callback function |
| 38 | bool monitor_signals; // If true, call the callback when "pid" gets signaled. |
| 39 | }; |
| 40 | |
| 41 | static void * |
| 42 | MonitorChildProcessThreadFunction (void *arg); |
| 43 | |
| 44 | lldb::thread_t |
| 45 | Host::StartMonitoringChildProcess |
| 46 | ( |
| 47 | Host::MonitorChildProcessCallback callback, |
| 48 | void *callback_baton, |
| 49 | lldb::pid_t pid, |
| 50 | bool monitor_signals |
| 51 | ) |
| 52 | { |
| 53 | lldb::thread_t thread = LLDB_INVALID_HOST_THREAD; |
| 54 | if (callback) |
| 55 | { |
| 56 | std::auto_ptr<MonitorInfo> info_ap(new MonitorInfo); |
| 57 | |
| 58 | info_ap->pid = pid; |
| 59 | info_ap->callback = callback; |
| 60 | info_ap->callback_baton = callback_baton; |
| 61 | info_ap->monitor_signals = monitor_signals; |
| 62 | |
| 63 | char thread_name[256]; |
| 64 | ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%i)>", pid); |
| 65 | thread = ThreadCreate (thread_name, |
| 66 | MonitorChildProcessThreadFunction, |
| 67 | info_ap.get(), |
| 68 | NULL); |
| 69 | |
| 70 | if (thread != LLDB_INVALID_HOST_THREAD) |
| 71 | info_ap.release(); |
| 72 | } |
| 73 | return thread; |
| 74 | } |
| 75 | |
| 76 | //------------------------------------------------------------------ |
| 77 | // Scoped class that will disable thread canceling when it is |
| 78 | // constructed, and exception safely restore the previous value it |
| 79 | // when it goes out of scope. |
| 80 | //------------------------------------------------------------------ |
| 81 | class ScopedPThreadCancelDisabler |
| 82 | { |
| 83 | public: |
| 84 | ScopedPThreadCancelDisabler() |
| 85 | { |
| 86 | // Disable the ability for this thread to be cancelled |
| 87 | int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state); |
| 88 | if (err != 0) |
| 89 | m_old_state = -1; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | ~ScopedPThreadCancelDisabler() |
| 94 | { |
| 95 | // Restore the ability for this thread to be cancelled to what it |
| 96 | // previously was. |
| 97 | if (m_old_state != -1) |
| 98 | ::pthread_setcancelstate (m_old_state, 0); |
| 99 | } |
| 100 | private: |
| 101 | int m_old_state; // Save the old cancelability state. |
| 102 | }; |
| 103 | |
| 104 | static void * |
| 105 | MonitorChildProcessThreadFunction (void *arg) |
| 106 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 107 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 108 | const char *function = __FUNCTION__; |
| 109 | if (log) |
| 110 | log->Printf ("%s (arg = %p) thread starting...", function, arg); |
| 111 | |
| 112 | MonitorInfo *info = (MonitorInfo *)arg; |
| 113 | |
| 114 | const Host::MonitorChildProcessCallback callback = info->callback; |
| 115 | void * const callback_baton = info->callback_baton; |
| 116 | const lldb::pid_t pid = info->pid; |
| 117 | const bool monitor_signals = info->monitor_signals; |
| 118 | |
| 119 | delete info; |
| 120 | |
| 121 | int status = -1; |
| 122 | const int options = 0; |
| 123 | struct rusage *rusage = NULL; |
| 124 | while (1) |
| 125 | { |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 126 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 127 | if (log) |
| 128 | log->Printf("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p)...", function, pid, options, rusage); |
| 129 | |
| 130 | // Wait for all child processes |
| 131 | ::pthread_testcancel (); |
| 132 | const lldb::pid_t wait_pid = ::wait4 (pid, &status, options, rusage); |
| 133 | ::pthread_testcancel (); |
| 134 | |
| 135 | if (wait_pid == -1) |
| 136 | { |
| 137 | if (errno == EINTR) |
| 138 | continue; |
| 139 | else |
| 140 | break; |
| 141 | } |
| 142 | else if (wait_pid == pid) |
| 143 | { |
| 144 | bool exited = false; |
| 145 | int signal = 0; |
| 146 | int exit_status = 0; |
| 147 | const char *status_cstr = NULL; |
| 148 | if (WIFSTOPPED(status)) |
| 149 | { |
| 150 | signal = WSTOPSIG(status); |
| 151 | status_cstr = "STOPPED"; |
| 152 | } |
| 153 | else if (WIFEXITED(status)) |
| 154 | { |
| 155 | exit_status = WEXITSTATUS(status); |
| 156 | status_cstr = "EXITED"; |
| 157 | exited = true; |
| 158 | } |
| 159 | else if (WIFSIGNALED(status)) |
| 160 | { |
| 161 | signal = WTERMSIG(status); |
| 162 | status_cstr = "SIGNALED"; |
| 163 | exited = true; |
| 164 | exit_status = -1; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | status_cstr = "(???)"; |
| 169 | } |
| 170 | |
| 171 | // Scope for pthread_cancel_disabler |
| 172 | { |
| 173 | ScopedPThreadCancelDisabler pthread_cancel_disabler; |
| 174 | |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 175 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 176 | if (log) |
| 177 | log->Printf ("%s ::wait4 (pid = %i, &status, options = %i, rusage = %p) => pid = %i, status = 0x%8.8x (%s), signal = %i, exit_state = %i", |
| 178 | function, |
| 179 | wait_pid, |
| 180 | options, |
| 181 | rusage, |
| 182 | pid, |
| 183 | status, |
| 184 | status_cstr, |
| 185 | signal, |
| 186 | exit_status); |
| 187 | |
| 188 | if (exited || (signal != 0 && monitor_signals)) |
| 189 | { |
| 190 | bool callback_return = callback (callback_baton, pid, signal, exit_status); |
| 191 | |
| 192 | // If our process exited, then this thread should exit |
| 193 | if (exited) |
| 194 | break; |
| 195 | // If the callback returns true, it means this process should |
| 196 | // exit |
| 197 | if (callback_return) |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 204 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 205 | if (log) |
| 206 | log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg); |
| 207 | |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | size_t |
| 212 | Host::GetPageSize() |
| 213 | { |
| 214 | return ::getpagesize(); |
| 215 | } |
| 216 | |
| 217 | //------------------------------------------------------------------ |
| 218 | // Returns true if the host system is Big Endian. |
| 219 | //------------------------------------------------------------------ |
| 220 | ByteOrder |
| 221 | Host::GetByteOrder () |
| 222 | { |
| 223 | union EndianTest |
| 224 | { |
| 225 | uint32_t num; |
| 226 | uint8_t bytes[sizeof(uint32_t)]; |
| 227 | } endian = { (uint16_t)0x11223344 }; |
| 228 | switch (endian.bytes[0]) |
| 229 | { |
| 230 | case 0x11: return eByteOrderLittle; |
| 231 | case 0x44: return eByteOrderBig; |
| 232 | case 0x33: return eByteOrderPDP; |
| 233 | } |
| 234 | return eByteOrderInvalid; |
| 235 | } |
| 236 | |
| 237 | const ArchSpec & |
| 238 | Host::GetArchitecture () |
| 239 | { |
| 240 | static ArchSpec g_host_arch; |
| 241 | if (!g_host_arch.IsValid()) |
| 242 | { |
| 243 | #if defined (__APPLE__) |
| 244 | uint32_t cputype, cpusubtype; |
| 245 | uint32_t is_64_bit_capable; |
| 246 | size_t len = sizeof(cputype); |
| 247 | if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0) |
| 248 | { |
| 249 | len = sizeof(cpusubtype); |
| 250 | if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) == 0) |
| 251 | g_host_arch.SetArch(cputype, cpusubtype); |
| 252 | |
| 253 | len = sizeof (is_64_bit_capable); |
| 254 | if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0) |
| 255 | { |
| 256 | if (is_64_bit_capable) |
| 257 | { |
| 258 | if (cputype == CPU_TYPE_I386 && cpusubtype == CPU_SUBTYPE_486) |
| 259 | cpusubtype = CPU_SUBTYPE_I386_ALL; |
| 260 | |
| 261 | cputype |= CPU_ARCH_ABI64; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | #elif defined (__linux__) |
| 266 | g_host_arch.SetArch(7u, 144u); |
| 267 | #endif |
| 268 | } |
| 269 | return g_host_arch; |
| 270 | } |
| 271 | |
| 272 | const ConstString & |
| 273 | Host::GetVendorString() |
| 274 | { |
| 275 | static ConstString g_vendor; |
| 276 | if (!g_vendor) |
| 277 | { |
| 278 | #if defined (__APPLE__) |
| 279 | char ostype[64]; |
| 280 | size_t len = sizeof(ostype); |
| 281 | if (::sysctlbyname("kern.ostype", &ostype, &len, NULL, 0) == 0) |
| 282 | g_vendor.SetCString (ostype); |
| 283 | else |
| 284 | g_vendor.SetCString("apple"); |
| 285 | #elif defined (__linux__) |
| 286 | g_vendor.SetCString("gnu"); |
| 287 | #endif |
| 288 | } |
| 289 | return g_vendor; |
| 290 | } |
| 291 | |
| 292 | const ConstString & |
| 293 | Host::GetOSString() |
| 294 | { |
| 295 | static ConstString g_os_string; |
| 296 | if (!g_os_string) |
| 297 | { |
| 298 | #if defined (__APPLE__) |
| 299 | g_os_string.SetCString("darwin"); |
| 300 | #elif defined (__linux__) |
| 301 | g_os_string.SetCString("linux"); |
| 302 | #endif |
| 303 | } |
| 304 | return g_os_string; |
| 305 | } |
| 306 | |
| 307 | const ConstString & |
| 308 | Host::GetTargetTriple() |
| 309 | { |
| 310 | static ConstString g_host_triple; |
| 311 | if (!(g_host_triple)) |
| 312 | { |
| 313 | StreamString triple; |
| 314 | triple.Printf("%s-%s-%s", |
| 315 | GetArchitecture().AsCString(), |
| 316 | GetVendorString().AsCString(), |
| 317 | GetOSString().AsCString()); |
| 318 | |
| 319 | std::transform (triple.GetString().begin(), |
| 320 | triple.GetString().end(), |
| 321 | triple.GetString().begin(), |
| 322 | ::tolower); |
| 323 | |
| 324 | g_host_triple.SetCString(triple.GetString().c_str()); |
| 325 | } |
| 326 | return g_host_triple; |
| 327 | } |
| 328 | |
| 329 | lldb::pid_t |
| 330 | Host::GetCurrentProcessID() |
| 331 | { |
| 332 | return ::getpid(); |
| 333 | } |
| 334 | |
| 335 | lldb::tid_t |
| 336 | Host::GetCurrentThreadID() |
| 337 | { |
| 338 | #if defined (__APPLE__) |
| 339 | return ::mach_thread_self(); |
| 340 | #else |
| 341 | return lldb::tid_t(pthread_self()); |
| 342 | #endif |
| 343 | } |
| 344 | |
| 345 | const char * |
| 346 | Host::GetSignalAsCString (int signo) |
| 347 | { |
| 348 | switch (signo) |
| 349 | { |
| 350 | case SIGHUP: return "SIGHUP"; // 1 hangup |
| 351 | case SIGINT: return "SIGINT"; // 2 interrupt |
| 352 | case SIGQUIT: return "SIGQUIT"; // 3 quit |
| 353 | case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught) |
| 354 | case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught) |
| 355 | case SIGABRT: return "SIGABRT"; // 6 abort() |
| 356 | #if defined(_POSIX_C_SOURCE) |
| 357 | case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported) |
| 358 | #else // !_POSIX_C_SOURCE |
| 359 | case SIGEMT: return "SIGEMT"; // 7 EMT instruction |
| 360 | #endif // !_POSIX_C_SOURCE |
| 361 | case SIGFPE: return "SIGFPE"; // 8 floating point exception |
| 362 | case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored) |
| 363 | case SIGBUS: return "SIGBUS"; // 10 bus error |
| 364 | case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation |
| 365 | case SIGSYS: return "SIGSYS"; // 12 bad argument to system call |
| 366 | case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it |
| 367 | case SIGALRM: return "SIGALRM"; // 14 alarm clock |
| 368 | case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill |
| 369 | case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel |
| 370 | case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty |
| 371 | case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty |
| 372 | case SIGCONT: return "SIGCONT"; // 19 continue a stopped process |
| 373 | case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit |
| 374 | case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read |
| 375 | case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local<OSTOP) |
| 376 | #if !defined(_POSIX_C_SOURCE) |
| 377 | case SIGIO: return "SIGIO"; // 23 input/output possible signal |
| 378 | #endif |
| 379 | case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit |
| 380 | case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit |
| 381 | case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm |
| 382 | case SIGPROF: return "SIGPROF"; // 27 profiling time alarm |
| 383 | #if !defined(_POSIX_C_SOURCE) |
| 384 | case SIGWINCH: return "SIGWINCH"; // 28 window size changes |
| 385 | case SIGINFO: return "SIGINFO"; // 29 information request |
| 386 | #endif |
| 387 | case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1 |
| 388 | case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2 |
| 389 | default: |
| 390 | break; |
| 391 | } |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | void |
| 396 | Host::WillTerminate () |
| 397 | { |
| 398 | } |
| 399 | |
| 400 | #if !defined (__APPLE__) // see macosx/Host.mm |
| 401 | void |
| 402 | Host::ThreadCreated (const char *thread_name) |
| 403 | { |
| 404 | } |
Greg Clayton | b749a26 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 405 | |
| 406 | void |
| 407 | Host::Backtrace (Stream &strm, uint32_t max_frames) |
| 408 | { |
| 409 | // TODO: Is there a way to backtrace the current process on linux? |
| 410 | } |
| 411 | |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 412 | |
| 413 | size_t |
| 414 | Host::GetEnvironment (StringList &env) |
| 415 | { |
| 416 | // TODO: Is there a way to the host environment for this process on linux? |
| 417 | return 0; |
| 418 | } |
| 419 | |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 420 | #endif |
| 421 | |
| 422 | struct HostThreadCreateInfo |
| 423 | { |
| 424 | std::string thread_name; |
| 425 | thread_func_t thread_fptr; |
| 426 | thread_arg_t thread_arg; |
| 427 | |
| 428 | HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) : |
| 429 | thread_name (name ? name : ""), |
| 430 | thread_fptr (fptr), |
| 431 | thread_arg (arg) |
| 432 | { |
| 433 | } |
| 434 | }; |
| 435 | |
| 436 | static thread_result_t |
| 437 | ThreadCreateTrampoline (thread_arg_t arg) |
| 438 | { |
| 439 | HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg; |
| 440 | Host::ThreadCreated (info->thread_name.c_str()); |
| 441 | thread_func_t thread_fptr = info->thread_fptr; |
| 442 | thread_arg_t thread_arg = info->thread_arg; |
| 443 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 444 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 445 | if (log) |
| 446 | log->Printf("thread created"); |
| 447 | |
| 448 | delete info; |
| 449 | return thread_fptr (thread_arg); |
| 450 | } |
| 451 | |
| 452 | lldb::thread_t |
| 453 | Host::ThreadCreate |
| 454 | ( |
| 455 | const char *thread_name, |
| 456 | thread_func_t thread_fptr, |
| 457 | thread_arg_t thread_arg, |
| 458 | Error *error |
| 459 | ) |
| 460 | { |
| 461 | lldb::thread_t thread = LLDB_INVALID_HOST_THREAD; |
| 462 | |
| 463 | // Host::ThreadCreateTrampoline will delete this pointer for us. |
| 464 | HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg); |
| 465 | |
| 466 | int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr); |
| 467 | if (err == 0) |
| 468 | { |
| 469 | if (error) |
| 470 | error->Clear(); |
| 471 | return thread; |
| 472 | } |
| 473 | |
| 474 | if (error) |
| 475 | error->SetError (err, eErrorTypePOSIX); |
| 476 | |
| 477 | return LLDB_INVALID_HOST_THREAD; |
| 478 | } |
| 479 | |
| 480 | bool |
| 481 | Host::ThreadCancel (lldb::thread_t thread, Error *error) |
| 482 | { |
| 483 | int err = ::pthread_cancel (thread); |
| 484 | if (error) |
| 485 | error->SetError(err, eErrorTypePOSIX); |
| 486 | return err == 0; |
| 487 | } |
| 488 | |
| 489 | bool |
| 490 | Host::ThreadDetach (lldb::thread_t thread, Error *error) |
| 491 | { |
| 492 | int err = ::pthread_detach (thread); |
| 493 | if (error) |
| 494 | error->SetError(err, eErrorTypePOSIX); |
| 495 | return err == 0; |
| 496 | } |
| 497 | |
| 498 | bool |
| 499 | Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error) |
| 500 | { |
| 501 | int err = ::pthread_join (thread, thread_result_ptr); |
| 502 | if (error) |
| 503 | error->SetError(err, eErrorTypePOSIX); |
| 504 | return err == 0; |
| 505 | } |
| 506 | |
| 507 | //------------------------------------------------------------------ |
| 508 | // Control access to a static file thread name map using a single |
| 509 | // static function to avoid a static constructor. |
| 510 | //------------------------------------------------------------------ |
| 511 | static const char * |
| 512 | ThreadNameAccessor (bool get, lldb::pid_t pid, lldb::tid_t tid, const char *name) |
| 513 | { |
| 514 | uint64_t pid_tid = ((uint64_t)pid << 32) | (uint64_t)tid; |
| 515 | |
| 516 | static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 517 | Mutex::Locker locker(&g_mutex); |
| 518 | |
| 519 | typedef std::map<uint64_t, std::string> thread_name_map; |
| 520 | // rdar://problem/8153284 |
| 521 | // Fixed a crasher where during shutdown, loggings attempted to access the |
| 522 | // thread name but the static map instance had already been destructed. |
| 523 | // Another approach is to introduce a static guard object which monitors its |
| 524 | // own destruction and raises a flag, but this incurs more overhead. |
| 525 | static thread_name_map *g_thread_names_ptr = new thread_name_map(); |
| 526 | thread_name_map &g_thread_names = *g_thread_names_ptr; |
| 527 | |
| 528 | if (get) |
| 529 | { |
| 530 | // See if the thread name exists in our thread name pool |
| 531 | thread_name_map::iterator pos = g_thread_names.find(pid_tid); |
| 532 | if (pos != g_thread_names.end()) |
| 533 | return pos->second.c_str(); |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | // Set the thread name |
| 538 | g_thread_names[pid_tid] = name; |
| 539 | } |
| 540 | return NULL; |
| 541 | } |
| 542 | |
| 543 | const char * |
| 544 | Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid) |
| 545 | { |
| 546 | const char *name = ThreadNameAccessor (true, pid, tid, NULL); |
| 547 | if (name == NULL) |
| 548 | { |
| 549 | #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5 |
| 550 | // We currently can only get the name of a thread in the current process. |
| 551 | if (pid == Host::GetCurrentProcessID()) |
| 552 | { |
| 553 | char pthread_name[1024]; |
| 554 | if (::pthread_getname_np (::pthread_from_mach_thread_np (tid), pthread_name, sizeof(pthread_name)) == 0) |
| 555 | { |
| 556 | if (pthread_name[0]) |
| 557 | { |
| 558 | // Set the thread in our string pool |
| 559 | ThreadNameAccessor (false, pid, tid, pthread_name); |
| 560 | // Get our copy of the thread name string |
| 561 | name = ThreadNameAccessor (true, pid, tid, NULL); |
| 562 | } |
| 563 | } |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 564 | |
| 565 | if (name == NULL) |
| 566 | { |
| 567 | dispatch_queue_t current_queue = ::dispatch_get_current_queue (); |
| 568 | if (current_queue != NULL) |
| 569 | name = dispatch_queue_get_label (current_queue); |
| 570 | } |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 571 | } |
| 572 | #endif |
| 573 | } |
| 574 | return name; |
| 575 | } |
| 576 | |
| 577 | void |
| 578 | Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name) |
| 579 | { |
| 580 | lldb::pid_t curr_pid = Host::GetCurrentProcessID(); |
| 581 | lldb::tid_t curr_tid = Host::GetCurrentThreadID(); |
| 582 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 583 | pid = curr_pid; |
| 584 | |
| 585 | if (tid == LLDB_INVALID_THREAD_ID) |
| 586 | tid = curr_tid; |
| 587 | |
| 588 | #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5 |
| 589 | // Set the pthread name if possible |
| 590 | if (pid == curr_pid && tid == curr_tid) |
| 591 | { |
| 592 | ::pthread_setname_np (name); |
| 593 | } |
| 594 | #endif |
| 595 | ThreadNameAccessor (false, pid, tid, name); |
| 596 | } |
| 597 | |
| 598 | FileSpec |
| 599 | Host::GetProgramFileSpec () |
| 600 | { |
| 601 | static FileSpec g_program_filespec; |
| 602 | if (!g_program_filespec) |
| 603 | { |
| 604 | #if defined (__APPLE__) |
| 605 | char program_fullpath[PATH_MAX]; |
| 606 | // If DST is NULL, then return the number of bytes needed. |
| 607 | uint32_t len = sizeof(program_fullpath); |
| 608 | int err = _NSGetExecutablePath (program_fullpath, &len); |
| 609 | if (err == 0) |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 610 | g_program_filespec.SetFile (program_fullpath, true); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 611 | else if (err == -1) |
| 612 | { |
| 613 | char *large_program_fullpath = (char *)::malloc (len + 1); |
| 614 | |
| 615 | err = _NSGetExecutablePath (large_program_fullpath, &len); |
| 616 | if (err == 0) |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 617 | g_program_filespec.SetFile (large_program_fullpath, true); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 618 | |
| 619 | ::free (large_program_fullpath); |
| 620 | } |
| 621 | #elif defined (__linux__) |
| 622 | char exe_path[PATH_MAX]; |
Stephen Wilson | f302a9e | 2011-01-12 04:21:21 +0000 | [diff] [blame^] | 623 | ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); |
| 624 | if (len > 0) { |
| 625 | exe_path[len] = 0; |
Johnny Chen | 9e3f746 | 2010-12-20 21:52:18 +0000 | [diff] [blame] | 626 | g_program_filespec = FileSpec(exe_path, true); |
Stephen Wilson | f302a9e | 2011-01-12 04:21:21 +0000 | [diff] [blame^] | 627 | } |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 628 | #elif defined (__FreeBSD__) |
| 629 | int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() }; |
| 630 | size_t exe_path_size; |
| 631 | if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0) |
| 632 | { |
| 633 | char *exe_path = new char[exe_path_size]; |
| 634 | if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0) |
Johnny Chen | 9e3f746 | 2010-12-20 21:52:18 +0000 | [diff] [blame] | 635 | g_program_filespec = FileSpec(exe_path, true); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 636 | } |
| 637 | #endif |
| 638 | } |
| 639 | return g_program_filespec; |
| 640 | } |
| 641 | |
| 642 | FileSpec |
| 643 | Host::GetModuleFileSpecForHostAddress (const void *host_addr) |
| 644 | { |
| 645 | FileSpec module_filespec; |
| 646 | Dl_info info; |
| 647 | if (::dladdr (host_addr, &info)) |
| 648 | { |
| 649 | if (info.dli_fname) |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 650 | module_filespec.SetFile(info.dli_fname, true); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 651 | } |
| 652 | return module_filespec; |
| 653 | } |
| 654 | |
| 655 | #if !defined (__APPLE__) // see Host.mm |
| 656 | bool |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 657 | Host::ResolveExecutableInBundle (FileSpec &file) |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 658 | { |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 659 | return false; |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 660 | } |
| 661 | #endif |
| 662 | |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 663 | |
| 664 | bool |
| 665 | Host::GetLLDBPath (PathType path_type, FileSpec &file_spec) |
| 666 | { |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 667 | // 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] | 668 | // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB", |
| 669 | // on linux this is assumed to be the "lldb" main executable. If LLDB on |
| 670 | // linux is actually in a shared library (lldb.so??) then this function will |
| 671 | // need to be modified to "do the right thing". |
| 672 | |
| 673 | switch (path_type) |
| 674 | { |
| 675 | case ePathTypeLLDBShlibDir: |
| 676 | { |
| 677 | static ConstString g_lldb_so_dir; |
| 678 | if (!g_lldb_so_dir) |
| 679 | { |
| 680 | FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath)); |
| 681 | g_lldb_so_dir = lldb_file_spec.GetDirectory(); |
| 682 | } |
| 683 | file_spec.GetDirectory() = g_lldb_so_dir; |
| 684 | return file_spec.GetDirectory(); |
| 685 | } |
| 686 | break; |
| 687 | |
| 688 | case ePathTypeSupportExecutableDir: |
| 689 | { |
| 690 | static ConstString g_lldb_support_exe_dir; |
| 691 | if (!g_lldb_support_exe_dir) |
| 692 | { |
| 693 | FileSpec lldb_file_spec; |
| 694 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 695 | { |
| 696 | char raw_path[PATH_MAX]; |
| 697 | char resolved_path[PATH_MAX]; |
| 698 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 699 | |
| 700 | #if defined (__APPLE__) |
| 701 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 702 | if (framework_pos) |
| 703 | { |
| 704 | framework_pos += strlen("LLDB.framework"); |
| 705 | ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path)); |
| 706 | } |
| 707 | #endif |
| 708 | FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); |
| 709 | g_lldb_support_exe_dir.SetCString(resolved_path); |
| 710 | } |
| 711 | } |
| 712 | file_spec.GetDirectory() = g_lldb_support_exe_dir; |
| 713 | return file_spec.GetDirectory(); |
| 714 | } |
| 715 | break; |
| 716 | |
| 717 | case ePathTypeHeaderDir: |
| 718 | { |
| 719 | static ConstString g_lldb_headers_dir; |
| 720 | if (!g_lldb_headers_dir) |
| 721 | { |
| 722 | #if defined (__APPLE__) |
| 723 | FileSpec lldb_file_spec; |
| 724 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 725 | { |
| 726 | char raw_path[PATH_MAX]; |
| 727 | char resolved_path[PATH_MAX]; |
| 728 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 729 | |
| 730 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 731 | if (framework_pos) |
| 732 | { |
| 733 | framework_pos += strlen("LLDB.framework"); |
| 734 | ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path)); |
| 735 | } |
| 736 | FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); |
| 737 | g_lldb_headers_dir.SetCString(resolved_path); |
| 738 | } |
| 739 | #else |
| 740 | // TODO: Anyone know how we can determine this for linux?? |
| 741 | g_lldb_headers_dir.SetCString ("/opt/local/include/lldb"); |
| 742 | #endif |
| 743 | } |
| 744 | file_spec.GetDirectory() = g_lldb_headers_dir; |
| 745 | return file_spec.GetDirectory(); |
| 746 | } |
| 747 | break; |
| 748 | |
| 749 | case ePathTypePythonDir: |
| 750 | { |
| 751 | // TODO: Anyone know how we can determine this for linux?? |
| 752 | // For linux we are currently assuming the location of the lldb |
| 753 | // binary that contains this function is the directory that will |
| 754 | // contain lldb.so, lldb.py and embedded_interpreter.py... |
| 755 | |
| 756 | static ConstString g_lldb_python_dir; |
| 757 | if (!g_lldb_python_dir) |
| 758 | { |
| 759 | FileSpec lldb_file_spec; |
| 760 | if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec)) |
| 761 | { |
| 762 | char raw_path[PATH_MAX]; |
| 763 | char resolved_path[PATH_MAX]; |
| 764 | lldb_file_spec.GetPath(raw_path, sizeof(raw_path)); |
| 765 | |
| 766 | #if defined (__APPLE__) |
| 767 | char *framework_pos = ::strstr (raw_path, "LLDB.framework"); |
| 768 | if (framework_pos) |
| 769 | { |
| 770 | framework_pos += strlen("LLDB.framework"); |
| 771 | ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path)); |
| 772 | } |
| 773 | #endif |
| 774 | FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path)); |
| 775 | g_lldb_python_dir.SetCString(resolved_path); |
| 776 | } |
| 777 | } |
| 778 | file_spec.GetDirectory() = g_lldb_python_dir; |
| 779 | return file_spec.GetDirectory(); |
| 780 | } |
| 781 | break; |
| 782 | |
| 783 | default: |
| 784 | assert (!"Unhandled PathType"); |
| 785 | break; |
| 786 | } |
| 787 | |
| 788 | return false; |
| 789 | } |
| 790 | |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 791 | uint32_t |
| 792 | Host::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 793 | { |
| 794 | uint32_t num_matches = 0; |
| 795 | |
| 796 | #if defined (__APPLE__) |
| 797 | int num_pids; |
| 798 | int size_of_pids; |
Greg Clayton | fb8876d | 2010-10-10 22:07:18 +0000 | [diff] [blame] | 799 | std::vector<int> pid_list; |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 800 | |
| 801 | size_of_pids = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0); |
| 802 | if (size_of_pids == -1) |
| 803 | return 0; |
| 804 | |
| 805 | num_pids = size_of_pids/sizeof(int); |
Greg Clayton | fb8876d | 2010-10-10 22:07:18 +0000 | [diff] [blame] | 806 | |
| 807 | pid_list.resize (size_of_pids); |
| 808 | size_of_pids = proc_listpids(PROC_ALL_PIDS, 0, &pid_list[0], size_of_pids); |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 809 | if (size_of_pids == -1) |
| 810 | return 0; |
| 811 | |
| 812 | lldb::pid_t our_pid = getpid(); |
| 813 | |
| 814 | for (int i = 0; i < num_pids; i++) |
| 815 | { |
| 816 | struct proc_bsdinfo bsd_info; |
| 817 | int error = proc_pidinfo (pid_list[i], PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE); |
| 818 | if (error == 0) |
| 819 | continue; |
| 820 | |
| 821 | // Don't offer to attach to zombie processes, already traced or exiting |
| 822 | // processes, and of course, ourselves... It looks like passing the second arg of |
| 823 | // 0 to proc_listpids will exclude zombies anyway, but that's not documented so... |
| 824 | if (((bsd_info.pbi_flags & (PROC_FLAG_TRACED | PROC_FLAG_INEXIT)) != 0) |
| 825 | || (bsd_info.pbi_status == SZOMB) |
| 826 | || (bsd_info.pbi_pid == our_pid)) |
| 827 | continue; |
| 828 | char pid_name[MAXCOMLEN * 2 + 1]; |
| 829 | int name_len; |
| 830 | name_len = proc_name(bsd_info.pbi_pid, pid_name, MAXCOMLEN * 2); |
| 831 | if (name_len == 0) |
| 832 | continue; |
| 833 | |
| 834 | if (strstr(pid_name, name) != pid_name) |
| 835 | continue; |
| 836 | matches.AppendString (pid_name); |
| 837 | pids.push_back (bsd_info.pbi_pid); |
| 838 | num_matches++; |
| 839 | } |
| 840 | #endif |
| 841 | |
| 842 | return num_matches; |
| 843 | } |
| 844 | |
| 845 | ArchSpec |
| 846 | Host::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 847 | { |
| 848 | ArchSpec return_spec; |
| 849 | |
| 850 | #if defined (__APPLE__) |
| 851 | struct proc_bsdinfo bsd_info; |
| 852 | int error = proc_pidinfo (pid, PROC_PIDTBSDINFO, (uint64_t) 0, &bsd_info, PROC_PIDTBSDINFO_SIZE); |
| 853 | if (error == 0) |
| 854 | return return_spec; |
| 855 | if (bsd_info.pbi_flags & PROC_FLAG_LP64) |
| 856 | return_spec.SetArch(LLDB_ARCH_DEFAULT_64BIT); |
| 857 | else |
| 858 | return_spec.SetArch(LLDB_ARCH_DEFAULT_32BIT); |
| 859 | #endif |
| 860 | |
| 861 | return return_spec; |
| 862 | } |
| 863 | |
| 864 | ArchSpec |
| 865 | Host::GetArchSpecForExistingProcess (const char *process_name) |
| 866 | { |
| 867 | ArchSpec returnSpec; |
| 868 | StringList matches; |
| 869 | std::vector<lldb::pid_t> pids; |
| 870 | if (ListProcessesMatchingName(process_name, matches, pids)) |
| 871 | { |
| 872 | if (matches.GetSize() == 1) |
| 873 | { |
| 874 | return GetArchSpecForExistingProcess(pids[0]); |
| 875 | } |
| 876 | } |
| 877 | return returnSpec; |
| 878 | } |
| 879 | |
| 880 | #if !defined (__APPLE__) // see macosx/Host.mm |
| 881 | bool |
Greg Clayton | b73620c | 2010-12-18 01:54:34 +0000 | [diff] [blame] | 882 | Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no) |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 883 | { |
| 884 | return false; |
| 885 | } |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 886 | |
Greg Clayton | e98ac25 | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 887 | void |
| 888 | Host::SetCrashDescriptionWithFormat (const char *format, ...) |
| 889 | { |
| 890 | } |
| 891 | |
| 892 | void |
| 893 | Host::SetCrashDescription (const char *description) |
| 894 | { |
| 895 | } |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 896 | |
| 897 | lldb::pid_t |
| 898 | LaunchApplication (const FileSpec &app_file_spec) |
| 899 | { |
| 900 | return LLDB_INVALID_PROCESS_ID; |
| 901 | } |
| 902 | |
| 903 | lldb::pid_t |
| 904 | Host::LaunchInNewTerminal |
| 905 | ( |
Greg Clayton | b73620c | 2010-12-18 01:54:34 +0000 | [diff] [blame] | 906 | const char *tty_name, |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 907 | const char **argv, |
| 908 | const char **envp, |
| 909 | const ArchSpec *arch_spec, |
| 910 | bool stop_at_entry, |
| 911 | bool disable_aslr |
| 912 | ) |
| 913 | { |
| 914 | return LLDB_INVALID_PROCESS_ID; |
| 915 | } |
| 916 | |
Greg Clayton | 8f3b21d | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 917 | #endif |