1 //===-- Host.cpp ------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
19 #include <sys/types.h>
20 #include <sys/sysctl.h>
23 #if defined (__APPLE__)
25 #include <dispatch/dispatch.h>
27 #include <mach-o/dyld.h>
28 #include <mach/mach_port.h>
32 #if defined (__linux__) || defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
34 #include <sys/syscall.h>
37 #if defined (__FreeBSD__)
38 #include <pthread_np.h>
41 #include "lldb/Host/Host.h"
42 #include "lldb/Core/ArchSpec.h"
43 #include "lldb/Core/ConstString.h"
44 #include "lldb/Core/Debugger.h"
45 #include "lldb/Core/Error.h"
46 #include "lldb/Core/Log.h"
47 #include "lldb/Core/StreamString.h"
48 #include "lldb/Core/ThreadSafeSTLMap.h"
49 #include "lldb/Host/Config.h"
50 #include "lldb/Host/Endian.h"
51 #include "lldb/Host/FileSpec.h"
52 #include "lldb/Host/Mutex.h"
53 #include "lldb/Target/Process.h"
54 #include "lldb/Target/TargetList.h"
56 #include "llvm/ADT/SmallString.h"
57 #include "llvm/Support/Host.h"
58 #include "llvm/Support/MachO.h"
59 #include "llvm/Support/raw_ostream.h"
66 using namespace lldb_private;
69 #if !defined (__APPLE__)
72 lldb::pid_t pid; // The process ID to monitor
73 Host::MonitorChildProcessCallback callback; // The callback function to call when "pid" exits or signals
74 void *callback_baton; // The callback baton for the callback function
75 bool monitor_signals; // If true, call the callback when "pid" gets signaled.
79 MonitorChildProcessThreadFunction (void *arg);
82 Host::StartMonitoringChildProcess
84 Host::MonitorChildProcessCallback callback,
90 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
91 MonitorInfo * info_ptr = new MonitorInfo();
94 info_ptr->callback = callback;
95 info_ptr->callback_baton = callback_baton;
96 info_ptr->monitor_signals = monitor_signals;
98 char thread_name[256];
99 ::snprintf (thread_name, sizeof(thread_name), "<lldb.host.wait4(pid=%" PRIu64 ")>", pid);
100 thread = ThreadCreate (thread_name,
101 MonitorChildProcessThreadFunction,
108 //------------------------------------------------------------------
109 // Scoped class that will disable thread canceling when it is
110 // constructed, and exception safely restore the previous value it
111 // when it goes out of scope.
112 //------------------------------------------------------------------
113 class ScopedPThreadCancelDisabler
116 ScopedPThreadCancelDisabler()
118 // Disable the ability for this thread to be cancelled
119 int err = ::pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &m_old_state);
125 ~ScopedPThreadCancelDisabler()
127 // Restore the ability for this thread to be cancelled to what it
129 if (m_old_state != -1)
130 ::pthread_setcancelstate (m_old_state, 0);
133 int m_old_state; // Save the old cancelability state.
137 MonitorChildProcessThreadFunction (void *arg)
139 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
140 const char *function = __FUNCTION__;
142 log->Printf ("%s (arg = %p) thread starting...", function, arg);
144 MonitorInfo *info = (MonitorInfo *)arg;
146 const Host::MonitorChildProcessCallback callback = info->callback;
147 void * const callback_baton = info->callback_baton;
148 const lldb::pid_t pid = info->pid;
149 const bool monitor_signals = info->monitor_signals;
154 #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
157 const int options = __WALL;
161 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
163 log->Printf("%s ::wait_pid (pid = %" PRIu64 ", &status, options = %i)...", function, pid, options);
165 // Wait for all child processes
166 ::pthread_testcancel ();
167 // Get signals from all children with same process group of pid
168 const lldb::pid_t wait_pid = ::waitpid (-1*pid, &status, options);
169 ::pthread_testcancel ();
178 log->Printf ("%s (arg = %p) thread exiting because waitpid failed (%s)...", __FUNCTION__, arg, strerror(errno));
182 else if (wait_pid > 0)
187 const char *status_cstr = NULL;
188 if (WIFSTOPPED(status))
190 signal = WSTOPSIG(status);
191 status_cstr = "STOPPED";
193 else if (WIFEXITED(status))
195 exit_status = WEXITSTATUS(status);
196 status_cstr = "EXITED";
199 else if (WIFSIGNALED(status))
201 signal = WTERMSIG(status);
202 status_cstr = "SIGNALED";
203 if (wait_pid == pid) {
210 status_cstr = "(\?\?\?)";
213 // Scope for pthread_cancel_disabler
215 ScopedPThreadCancelDisabler pthread_cancel_disabler;
217 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
219 log->Printf ("%s ::waitpid (pid = %" PRIu64 ", &status, options = %i) => pid = %" PRIu64 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i",
229 if (exited || (signal != 0 && monitor_signals))
231 bool callback_return = false;
233 callback_return = callback (callback_baton, wait_pid, exited, signal, exit_status);
235 // If our process exited, then this thread should exit
236 if (exited && wait_pid == pid)
239 log->Printf ("%s (arg = %p) thread exiting because pid received exit signal...", __FUNCTION__, arg);
242 // If the callback returns true, it means this process should
247 log->Printf ("%s (arg = %p) thread exiting because callback returned true...", __FUNCTION__, arg);
255 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS);
257 log->Printf ("%s (arg = %p) thread exiting...", __FUNCTION__, arg);
264 Host::SystemLog (SystemLogType type, const char *format, va_list args)
266 vfprintf (stderr, format, args);
269 #endif // #if !defined (__APPLE__)
272 Host::SystemLog (SystemLogType type, const char *format, ...)
275 va_start (args, format);
276 SystemLog (type, format, args);
283 return ::getpagesize();
287 Host::GetArchitecture (SystemDefaultArchitecture arch_kind)
289 static bool g_supports_32 = false;
290 static bool g_supports_64 = false;
291 static ArchSpec g_host_arch_32;
292 static ArchSpec g_host_arch_64;
294 #if defined (__APPLE__)
296 // Apple is different in that it can support both 32 and 64 bit executables
297 // in the same operating system running concurrently. Here we detect the
298 // correct host architectures for both 32 and 64 bit including if 64 bit
299 // executables are supported on the system.
301 if (g_supports_32 == false && g_supports_64 == false)
303 // All apple systems support 32 bit execution.
304 g_supports_32 = true;
305 uint32_t cputype, cpusubtype;
306 uint32_t is_64_bit_capable = false;
307 size_t len = sizeof(cputype);
309 // These will tell us about the kernel architecture, which even on a 64
310 // bit machine can be 32 bit...
311 if (::sysctlbyname("hw.cputype", &cputype, &len, NULL, 0) == 0)
313 len = sizeof (cpusubtype);
314 if (::sysctlbyname("hw.cpusubtype", &cpusubtype, &len, NULL, 0) != 0)
315 cpusubtype = CPU_TYPE_ANY;
317 len = sizeof (is_64_bit_capable);
318 if (::sysctlbyname("hw.cpu64bit_capable", &is_64_bit_capable, &len, NULL, 0) == 0)
320 if (is_64_bit_capable)
321 g_supports_64 = true;
324 if (is_64_bit_capable)
326 #if defined (__i386__) || defined (__x86_64__)
327 if (cpusubtype == CPU_SUBTYPE_486)
328 cpusubtype = CPU_SUBTYPE_I386_ALL;
330 if (cputype & CPU_ARCH_ABI64)
332 // We have a 64 bit kernel on a 64 bit system
333 g_host_arch_32.SetArchitecture (eArchTypeMachO, ~(CPU_ARCH_MASK) & cputype, cpusubtype);
334 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
338 // We have a 32 bit kernel on a 64 bit system
339 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
340 cputype |= CPU_ARCH_ABI64;
341 g_host_arch_64.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
346 g_host_arch_32.SetArchitecture (eArchTypeMachO, cputype, cpusubtype);
347 g_host_arch_64.Clear();
352 #else // #if defined (__APPLE__)
354 if (g_supports_32 == false && g_supports_64 == false)
356 llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
358 g_host_arch_32.Clear();
359 g_host_arch_64.Clear();
361 // If the OS is Linux, "unknown" in the vendor slot isn't what we want
362 // for the default triple. It's probably an artifact of config.guess.
363 if (triple.getOS() == llvm::Triple::Linux && triple.getVendor() == llvm::Triple::UnknownVendor)
364 triple.setVendorName("");
366 switch (triple.getArch())
369 g_host_arch_32.SetTriple(triple);
370 g_supports_32 = true;
373 case llvm::Triple::x86_64:
374 g_host_arch_64.SetTriple(triple);
375 g_supports_64 = true;
376 g_host_arch_32.SetTriple(triple.get32BitArchVariant());
377 g_supports_32 = true;
380 case llvm::Triple::sparcv9:
381 case llvm::Triple::ppc64:
382 g_host_arch_64.SetTriple(triple);
383 g_supports_64 = true;
387 g_supports_32 = g_host_arch_32.IsValid();
388 g_supports_64 = g_host_arch_64.IsValid();
391 #endif // #else for #if defined (__APPLE__)
393 if (arch_kind == eSystemDefaultArchitecture32)
394 return g_host_arch_32;
395 else if (arch_kind == eSystemDefaultArchitecture64)
396 return g_host_arch_64;
399 return g_host_arch_64;
401 return g_host_arch_32;
405 Host::GetVendorString()
407 static ConstString g_vendor;
410 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
411 const llvm::StringRef &str_ref = host_arch.GetTriple().getVendorName();
412 g_vendor.SetCStringWithLength(str_ref.data(), str_ref.size());
420 static ConstString g_os_string;
423 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
424 const llvm::StringRef &str_ref = host_arch.GetTriple().getOSName();
425 g_os_string.SetCStringWithLength(str_ref.data(), str_ref.size());
431 Host::GetTargetTriple()
433 static ConstString g_host_triple;
434 if (!(g_host_triple))
436 const ArchSpec &host_arch = GetArchitecture (eSystemDefaultArchitecture);
437 g_host_triple.SetCString(host_arch.GetTriple().getTriple().c_str());
439 return g_host_triple;
443 Host::GetCurrentProcessID()
449 Host::GetCurrentThreadID()
451 #if defined (__APPLE__)
452 // Calling "mach_port_deallocate()" bumps the reference count on the thread
453 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
455 thread_port_t thread_self = mach_thread_self();
456 mach_port_deallocate(mach_task_self(), thread_self);
458 #elif defined(__FreeBSD__)
459 return lldb::tid_t(pthread_getthreadid_np());
460 #elif defined(__linux__)
461 return lldb::tid_t(syscall(SYS_gettid));
463 return lldb::tid_t(pthread_self());
468 Host::GetCurrentThread ()
470 return lldb::thread_t(pthread_self());
474 Host::GetSignalAsCString (int signo)
478 case SIGHUP: return "SIGHUP"; // 1 hangup
479 case SIGINT: return "SIGINT"; // 2 interrupt
480 case SIGQUIT: return "SIGQUIT"; // 3 quit
481 case SIGILL: return "SIGILL"; // 4 illegal instruction (not reset when caught)
482 case SIGTRAP: return "SIGTRAP"; // 5 trace trap (not reset when caught)
483 case SIGABRT: return "SIGABRT"; // 6 abort()
484 #if (defined(_POSIX_C_SOURCE) && !defined(_DARWIN_C_SOURCE))
485 case SIGPOLL: return "SIGPOLL"; // 7 pollable event ([XSR] generated, not supported)
487 #if !defined(_POSIX_C_SOURCE)
488 case SIGEMT: return "SIGEMT"; // 7 EMT instruction
490 case SIGFPE: return "SIGFPE"; // 8 floating point exception
491 case SIGKILL: return "SIGKILL"; // 9 kill (cannot be caught or ignored)
492 case SIGBUS: return "SIGBUS"; // 10 bus error
493 case SIGSEGV: return "SIGSEGV"; // 11 segmentation violation
494 case SIGSYS: return "SIGSYS"; // 12 bad argument to system call
495 case SIGPIPE: return "SIGPIPE"; // 13 write on a pipe with no one to read it
496 case SIGALRM: return "SIGALRM"; // 14 alarm clock
497 case SIGTERM: return "SIGTERM"; // 15 software termination signal from kill
498 case SIGURG: return "SIGURG"; // 16 urgent condition on IO channel
499 case SIGSTOP: return "SIGSTOP"; // 17 sendable stop signal not from tty
500 case SIGTSTP: return "SIGTSTP"; // 18 stop signal from tty
501 case SIGCONT: return "SIGCONT"; // 19 continue a stopped process
502 case SIGCHLD: return "SIGCHLD"; // 20 to parent on child stop or exit
503 case SIGTTIN: return "SIGTTIN"; // 21 to readers pgrp upon background tty read
504 case SIGTTOU: return "SIGTTOU"; // 22 like TTIN for output if (tp->t_local<OSTOP)
505 #if !defined(_POSIX_C_SOURCE)
506 case SIGIO: return "SIGIO"; // 23 input/output possible signal
508 case SIGXCPU: return "SIGXCPU"; // 24 exceeded CPU time limit
509 case SIGXFSZ: return "SIGXFSZ"; // 25 exceeded file size limit
510 case SIGVTALRM: return "SIGVTALRM"; // 26 virtual time alarm
511 case SIGPROF: return "SIGPROF"; // 27 profiling time alarm
512 #if !defined(_POSIX_C_SOURCE)
513 case SIGWINCH: return "SIGWINCH"; // 28 window size changes
514 case SIGINFO: return "SIGINFO"; // 29 information request
516 case SIGUSR1: return "SIGUSR1"; // 30 user defined signal 1
517 case SIGUSR2: return "SIGUSR2"; // 31 user defined signal 2
525 Host::WillTerminate ()
529 #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__) // see macosx/Host.mm
532 Host::ThreadCreated (const char *thread_name)
537 Host::Backtrace (Stream &strm, uint32_t max_frames)
539 // TODO: Is there a way to backtrace the current process on other systems?
543 Host::GetEnvironment (StringList &env)
545 // TODO: Is there a way to the host environment for this process on other systems?
549 #endif // #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined (__linux__)
551 struct HostThreadCreateInfo
553 std::string thread_name;
554 thread_func_t thread_fptr;
555 thread_arg_t thread_arg;
557 HostThreadCreateInfo (const char *name, thread_func_t fptr, thread_arg_t arg) :
558 thread_name (name ? name : ""),
565 static thread_result_t
566 ThreadCreateTrampoline (thread_arg_t arg)
568 HostThreadCreateInfo *info = (HostThreadCreateInfo *)arg;
569 Host::ThreadCreated (info->thread_name.c_str());
570 thread_func_t thread_fptr = info->thread_fptr;
571 thread_arg_t thread_arg = info->thread_arg;
573 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
575 log->Printf("thread created");
578 return thread_fptr (thread_arg);
584 const char *thread_name,
585 thread_func_t thread_fptr,
586 thread_arg_t thread_arg,
590 lldb::thread_t thread = LLDB_INVALID_HOST_THREAD;
592 // Host::ThreadCreateTrampoline will delete this pointer for us.
593 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo (thread_name, thread_fptr, thread_arg);
595 int err = ::pthread_create (&thread, NULL, ThreadCreateTrampoline, info_ptr);
604 error->SetError (err, eErrorTypePOSIX);
606 return LLDB_INVALID_HOST_THREAD;
610 Host::ThreadCancel (lldb::thread_t thread, Error *error)
612 int err = ::pthread_cancel (thread);
614 error->SetError(err, eErrorTypePOSIX);
619 Host::ThreadDetach (lldb::thread_t thread, Error *error)
621 int err = ::pthread_detach (thread);
623 error->SetError(err, eErrorTypePOSIX);
628 Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
630 int err = ::pthread_join (thread, thread_result_ptr);
632 error->SetError(err, eErrorTypePOSIX);
637 Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
639 #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
640 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
641 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
642 if (pid == LLDB_INVALID_PROCESS_ID)
645 if (tid == LLDB_INVALID_THREAD_ID)
648 // Set the pthread name if possible
649 if (pid == curr_pid && tid == curr_tid)
651 if (::pthread_setname_np (name) == 0)
655 #elif defined (__FreeBSD__)
656 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
657 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
658 if (pid == LLDB_INVALID_PROCESS_ID)
661 if (tid == LLDB_INVALID_THREAD_ID)
664 // Set the pthread name if possible
665 if (pid == curr_pid && tid == curr_tid)
667 ::pthread_set_name_np (::pthread_self(), name);
671 #elif defined (__linux__) || defined (__GLIBC__)
672 void *fn = dlsym (RTLD_DEFAULT, "pthread_setname_np");
675 lldb::pid_t curr_pid = Host::GetCurrentProcessID();
676 lldb::tid_t curr_tid = Host::GetCurrentThreadID();
677 if (pid == LLDB_INVALID_PROCESS_ID)
680 if (tid == LLDB_INVALID_THREAD_ID)
683 if (pid == curr_pid && tid == curr_tid)
685 int (*pthread_setname_np_func)(pthread_t thread, const char *name);
686 *reinterpret_cast<void **> (&pthread_setname_np_func) = fn;
688 if (pthread_setname_np_func (::pthread_self(), name) == 0)
699 Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid,
700 const char *thread_name, size_t len)
702 char *namebuf = (char *)::malloc (len + 1);
704 // Thread names are coming in like '<lldb.comm.debugger.edit>' and
705 // '<lldb.comm.debugger.editline>'. So just chopping the end of the string
706 // off leads to a lot of similar named threads. Go through the thread name
707 // and search for the last dot and use that.
708 const char *lastdot = ::strrchr (thread_name, '.');
710 if (lastdot && lastdot != thread_name)
711 thread_name = lastdot + 1;
712 ::strncpy (namebuf, thread_name, len);
715 int namebuflen = strlen(namebuf);
718 if (namebuf[namebuflen - 1] == '(' || namebuf[namebuflen - 1] == '>')
720 // Trim off trailing '(' and '>' characters for a bit more cleanup.
722 namebuf[namebuflen] = 0;
724 return Host::SetThreadName (pid, tid, namebuf);
730 Host::GetProgramFileSpec ()
732 static FileSpec g_program_filespec;
733 if (!g_program_filespec)
735 #if defined (__APPLE__)
736 char program_fullpath[PATH_MAX];
737 // If DST is NULL, then return the number of bytes needed.
738 uint32_t len = sizeof(program_fullpath);
739 int err = _NSGetExecutablePath (program_fullpath, &len);
741 g_program_filespec.SetFile (program_fullpath, false);
744 char *large_program_fullpath = (char *)::malloc (len + 1);
746 err = _NSGetExecutablePath (large_program_fullpath, &len);
748 g_program_filespec.SetFile (large_program_fullpath, false);
750 ::free (large_program_fullpath);
752 #elif defined (__linux__)
753 char exe_path[PATH_MAX];
754 ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
757 g_program_filespec.SetFile(exe_path, false);
759 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
760 int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() };
761 size_t exe_path_size;
762 if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0)
764 char *exe_path = new char[exe_path_size];
765 if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0)
766 g_program_filespec.SetFile(exe_path, false);
771 return g_program_filespec;
775 Host::GetModuleFileSpecForHostAddress (const void *host_addr)
777 FileSpec module_filespec;
779 if (::dladdr (host_addr, &info))
782 module_filespec.SetFile(info.dli_fname, true);
784 return module_filespec;
787 #if !defined (__APPLE__) // see Host.mm
790 Host::GetBundleDirectory (const FileSpec &file, FileSpec &bundle)
797 Host::ResolveExecutableInBundle (FileSpec &file)
803 // Opaque info that tracks a dynamic library that was loaded
804 struct DynamicLibraryInfo
806 DynamicLibraryInfo (const FileSpec &fs, int o, void *h) :
813 const FileSpec file_spec;
814 uint32_t open_options;
819 Host::DynamicLibraryOpen (const FileSpec &file_spec, uint32_t options, Error &error)
822 if (file_spec.GetPath(path, sizeof(path)))
826 if (options & eDynamicLibraryOpenOptionLazy)
832 if (options & eDynamicLibraryOpenOptionLocal)
837 #ifdef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
838 if (options & eDynamicLibraryOpenOptionLimitGetSymbol)
842 void * opaque = ::dlopen (path, mode);
847 return new DynamicLibraryInfo (file_spec, options, opaque);
851 error.SetErrorString(::dlerror());
856 error.SetErrorString("failed to extract path");
862 Host::DynamicLibraryClose (void *opaque)
867 error.SetErrorString ("invalid dynamic library handle");
871 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
872 if (::dlclose (dylib_info->handle) != 0)
874 error.SetErrorString(::dlerror());
877 dylib_info->open_options = 0;
878 dylib_info->handle = 0;
885 Host::DynamicLibraryGetSymbol (void *opaque, const char *symbol_name, Error &error)
889 error.SetErrorString ("invalid dynamic library handle");
893 DynamicLibraryInfo *dylib_info = (DynamicLibraryInfo *) opaque;
895 void *symbol_addr = ::dlsym (dylib_info->handle, symbol_name);
898 #ifndef LLDB_CONFIG_DLOPEN_RTLD_FIRST_SUPPORTED
899 // This host doesn't support limiting searches to this shared library
900 // so we need to verify that the match came from this shared library
901 // if it was requested in the Host::DynamicLibraryOpen() function.
902 if (dylib_info->open_options & eDynamicLibraryOpenOptionLimitGetSymbol)
904 FileSpec match_dylib_spec (Host::GetModuleFileSpecForHostAddress (symbol_addr));
905 if (match_dylib_spec != dylib_info->file_spec)
907 char dylib_path[PATH_MAX];
908 if (dylib_info->file_spec.GetPath (dylib_path, sizeof(dylib_path)))
909 error.SetErrorStringWithFormat ("symbol not found in \"%s\"", dylib_path);
911 error.SetErrorString ("symbol not found");
921 error.SetErrorString(::dlerror());
928 Host::GetLLDBPath (PathType path_type, FileSpec &file_spec)
930 // To get paths related to LLDB we get the path to the executable that
931 // contains this function. On MacOSX this will be "LLDB.framework/.../LLDB",
932 // on linux this is assumed to be the "lldb" main executable. If LLDB on
933 // linux is actually in a shared library (liblldb.so) then this function will
934 // need to be modified to "do the right thing".
938 case ePathTypeLLDBShlibDir:
940 static ConstString g_lldb_so_dir;
943 FileSpec lldb_file_spec (Host::GetModuleFileSpecForHostAddress ((void *)Host::GetLLDBPath));
944 g_lldb_so_dir = lldb_file_spec.GetDirectory();
946 file_spec.GetDirectory() = g_lldb_so_dir;
947 return file_spec.GetDirectory();
951 case ePathTypeSupportExecutableDir:
953 static ConstString g_lldb_support_exe_dir;
954 if (!g_lldb_support_exe_dir)
956 FileSpec lldb_file_spec;
957 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
959 char raw_path[PATH_MAX];
960 char resolved_path[PATH_MAX];
961 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
963 #if defined (__APPLE__)
964 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
967 framework_pos += strlen("LLDB.framework");
968 #if !defined (__arm__)
969 ::strncpy (framework_pos, "/Resources", PATH_MAX - (framework_pos - raw_path));
973 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
974 g_lldb_support_exe_dir.SetCString(resolved_path);
977 file_spec.GetDirectory() = g_lldb_support_exe_dir;
978 return file_spec.GetDirectory();
982 case ePathTypeHeaderDir:
984 static ConstString g_lldb_headers_dir;
985 if (!g_lldb_headers_dir)
987 #if defined (__APPLE__)
988 FileSpec lldb_file_spec;
989 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
991 char raw_path[PATH_MAX];
992 char resolved_path[PATH_MAX];
993 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
995 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
998 framework_pos += strlen("LLDB.framework");
999 ::strncpy (framework_pos, "/Headers", PATH_MAX - (framework_pos - raw_path));
1001 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1002 g_lldb_headers_dir.SetCString(resolved_path);
1005 // TODO: Anyone know how we can determine this for linux? Other systems??
1006 g_lldb_headers_dir.SetCString ("/opt/local/include/lldb");
1009 file_spec.GetDirectory() = g_lldb_headers_dir;
1010 return file_spec.GetDirectory();
1014 #ifndef LLDB_DISABLE_PYTHON
1015 case ePathTypePythonDir:
1017 static ConstString g_lldb_python_dir;
1018 if (!g_lldb_python_dir)
1020 FileSpec lldb_file_spec;
1021 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1023 char raw_path[PATH_MAX];
1024 char resolved_path[PATH_MAX];
1025 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1027 #if defined (__APPLE__)
1028 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1031 framework_pos += strlen("LLDB.framework");
1032 ::strncpy (framework_pos, "/Resources/Python", PATH_MAX - (framework_pos - raw_path));
1035 llvm::SmallString<256> python_version_dir;
1036 llvm::raw_svector_ostream os(python_version_dir);
1037 os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "/site-packages";
1040 // We may get our string truncated. Should we protect
1041 // this with an assert?
1043 ::strncat(raw_path, python_version_dir.c_str(),
1044 sizeof(raw_path) - strlen(raw_path) - 1);
1047 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1048 g_lldb_python_dir.SetCString(resolved_path);
1051 file_spec.GetDirectory() = g_lldb_python_dir;
1052 return file_spec.GetDirectory();
1057 case ePathTypeLLDBSystemPlugins: // System plug-ins directory
1059 #if defined (__APPLE__) || defined(__linux__)
1060 static ConstString g_lldb_system_plugin_dir;
1061 static bool g_lldb_system_plugin_dir_located = false;
1062 if (!g_lldb_system_plugin_dir_located)
1064 g_lldb_system_plugin_dir_located = true;
1065 #if defined (__APPLE__)
1066 FileSpec lldb_file_spec;
1067 if (GetLLDBPath (ePathTypeLLDBShlibDir, lldb_file_spec))
1069 char raw_path[PATH_MAX];
1070 char resolved_path[PATH_MAX];
1071 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
1073 char *framework_pos = ::strstr (raw_path, "LLDB.framework");
1076 framework_pos += strlen("LLDB.framework");
1077 ::strncpy (framework_pos, "/Resources/PlugIns", PATH_MAX - (framework_pos - raw_path));
1078 FileSpec::Resolve (raw_path, resolved_path, sizeof(resolved_path));
1079 g_lldb_system_plugin_dir.SetCString(resolved_path);
1083 #elif defined (__linux__)
1084 FileSpec lldb_file_spec("/usr/lib/lldb", true);
1085 if (lldb_file_spec.Exists())
1087 g_lldb_system_plugin_dir.SetCString(lldb_file_spec.GetPath().c_str());
1089 #endif // __APPLE__ || __linux__
1092 if (g_lldb_system_plugin_dir)
1094 file_spec.GetDirectory() = g_lldb_system_plugin_dir;
1098 // TODO: where would system LLDB plug-ins be located on other systems?
1104 case ePathTypeLLDBUserPlugins: // User plug-ins directory
1106 #if defined (__APPLE__)
1107 static ConstString g_lldb_user_plugin_dir;
1108 if (!g_lldb_user_plugin_dir)
1110 char user_plugin_path[PATH_MAX];
1111 if (FileSpec::Resolve ("~/Library/Application Support/LLDB/PlugIns",
1113 sizeof(user_plugin_path)))
1115 g_lldb_user_plugin_dir.SetCString(user_plugin_path);
1118 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1119 return file_spec.GetDirectory();
1120 #elif defined (__linux__)
1121 static ConstString g_lldb_user_plugin_dir;
1122 if (!g_lldb_user_plugin_dir)
1124 // XDG Base Directory Specification
1125 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
1126 // If XDG_DATA_HOME exists, use that, otherwise use ~/.local/share/lldb.
1127 FileSpec lldb_file_spec;
1128 const char *xdg_data_home = getenv("XDG_DATA_HOME");
1129 if (xdg_data_home && xdg_data_home[0])
1131 std::string user_plugin_dir (xdg_data_home);
1132 user_plugin_dir += "/lldb";
1133 lldb_file_spec.SetFile (user_plugin_dir.c_str(), true);
1137 const char *home_dir = getenv("HOME");
1138 if (home_dir && home_dir[0])
1140 std::string user_plugin_dir (home_dir);
1141 user_plugin_dir += "/.local/share/lldb";
1142 lldb_file_spec.SetFile (user_plugin_dir.c_str(), true);
1146 if (lldb_file_spec.Exists())
1147 g_lldb_user_plugin_dir.SetCString(lldb_file_spec.GetPath().c_str());
1149 file_spec.GetDirectory() = g_lldb_user_plugin_dir;
1150 return file_spec.GetDirectory();
1152 // TODO: where would user LLDB plug-ins be located on other systems?
1162 Host::GetHostname (std::string &s)
1164 char hostname[PATH_MAX];
1165 hostname[sizeof(hostname) - 1] = '\0';
1166 if (::gethostname (hostname, sizeof(hostname) - 1) == 0)
1168 struct hostent* h = ::gethostbyname (hostname);
1170 s.assign (h->h_name);
1172 s.assign (hostname);
1179 Host::GetUserName (uint32_t uid, std::string &user_name)
1181 struct passwd user_info;
1182 struct passwd *user_info_ptr = &user_info;
1183 char user_buffer[PATH_MAX];
1184 size_t user_buffer_size = sizeof(user_buffer);
1185 if (::getpwuid_r (uid,
1189 &user_info_ptr) == 0)
1193 user_name.assign (user_info_ptr->pw_name);
1194 return user_name.c_str();
1202 Host::GetGroupName (uint32_t gid, std::string &group_name)
1204 char group_buffer[PATH_MAX];
1205 size_t group_buffer_size = sizeof(group_buffer);
1206 struct group group_info;
1207 struct group *group_info_ptr = &group_info;
1208 // Try the threadsafe version first
1209 if (::getgrgid_r (gid,
1213 &group_info_ptr) == 0)
1217 group_name.assign (group_info_ptr->gr_name);
1218 return group_name.c_str();
1223 // The threadsafe version isn't currently working
1224 // for me on darwin, but the non-threadsafe version
1225 // is, so I am calling it below.
1226 group_info_ptr = ::getgrgid (gid);
1229 group_name.assign (group_info_ptr->gr_name);
1230 return group_name.c_str();
1237 #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) // see macosx/Host.mm
1239 Host::GetOSBuildString (std::string &s)
1246 Host::GetOSKernelDescription (std::string &s)
1266 Host::GetEffectiveUserID ()
1272 Host::GetEffectiveGroupID ()
1277 #if !defined (__APPLE__) && !defined(__linux__)
1279 Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
1281 process_infos.Clear();
1282 return process_infos.GetSize();
1284 #endif // #if !defined (__APPLE__) && !defined(__linux__)
1286 #if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) && !defined(__linux__)
1288 Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
1290 process_info.Clear();
1295 #if !defined(__linux__)
1297 Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach)
1304 Host::GetDummyTarget (lldb_private::Debugger &debugger)
1306 static TargetSP g_dummy_target_sp;
1308 // FIXME: Maybe the dummy target should be per-Debugger
1309 if (!g_dummy_target_sp || !g_dummy_target_sp->IsValid())
1311 ArchSpec arch(Target::GetDefaultArchitecture());
1312 if (!arch.IsValid())
1313 arch = Host::GetArchitecture ();
1314 Error err = debugger.GetTargetList().CreateTarget(debugger,
1316 arch.GetTriple().getTriple().c_str(),
1322 return g_dummy_target_sp;
1328 process_reaped (false),
1330 pid (LLDB_INVALID_PROCESS_ID),
1336 lldb_private::Predicate<bool> process_reaped;
1337 lldb_private::Predicate<bool> can_delete;
1344 MonitorShellCommand (void *callback_baton,
1346 bool exited, // True if the process did exit
1347 int signo, // Zero for no signal
1348 int status) // Exit value of process if signal is zero
1350 ShellInfo *shell_info = (ShellInfo *)callback_baton;
1351 shell_info->pid = pid;
1352 shell_info->signo = signo;
1353 shell_info->status = status;
1354 // Let the thread running Host::RunShellCommand() know that the process
1355 // exited and that ShellInfo has been filled in by broadcasting to it
1356 shell_info->process_reaped.SetValue(1, eBroadcastAlways);
1357 // Now wait for a handshake back from that thread running Host::RunShellCommand
1358 // so we know that we can delete shell_info_ptr
1359 shell_info->can_delete.WaitForValueEqualTo(true);
1360 // Sleep a bit to allow the shell_info->can_delete.SetValue() to complete...
1362 // Now delete the shell info that was passed into this function
1368 Host::RunShellCommand (const char *command,
1369 const char *working_dir,
1372 std::string *command_output_ptr,
1373 uint32_t timeout_sec,
1377 ProcessLaunchInfo launch_info;
1378 if (shell && shell[0])
1380 // Run the command in a shell
1381 launch_info.SetShell(shell);
1382 launch_info.GetArguments().AppendArgument(command);
1383 const bool localhost = true;
1384 const bool will_debug = false;
1385 const bool first_arg_is_full_shell_command = true;
1386 launch_info.ConvertArgumentsForLaunchingInShell (error,
1389 first_arg_is_full_shell_command);
1393 // No shell, just run it
1394 Args args (command);
1395 const bool first_arg_is_executable = true;
1396 launch_info.SetArguments(args, first_arg_is_executable);
1400 launch_info.SetWorkingDirectory(working_dir);
1401 char output_file_path_buffer[L_tmpnam];
1402 const char *output_file_path = NULL;
1403 if (command_output_ptr)
1405 // Create a temporary file to get the stdout/stderr and redirect the
1406 // output of the command into this file. We will later read this file
1407 // if all goes well and fill the data into "command_output_ptr"
1408 output_file_path = ::tmpnam(output_file_path_buffer);
1409 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1410 launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path, false, true);
1411 launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO);
1415 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
1416 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
1417 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
1420 // The process monitor callback will delete the 'shell_info_ptr' below...
1421 std::unique_ptr<ShellInfo> shell_info_ap (new ShellInfo());
1423 const bool monitor_signals = false;
1424 launch_info.SetMonitorProcessCallback(MonitorShellCommand, shell_info_ap.get(), monitor_signals);
1426 error = LaunchProcess (launch_info);
1427 const lldb::pid_t pid = launch_info.GetProcessID();
1428 if (pid != LLDB_INVALID_PROCESS_ID)
1430 // The process successfully launched, so we can defer ownership of
1431 // "shell_info" to the MonitorShellCommand callback function that will
1432 // get called when the process dies. We release the unique pointer as it
1433 // doesn't need to delete the ShellInfo anymore.
1434 ShellInfo *shell_info = shell_info_ap.release();
1435 TimeValue timeout_time(TimeValue::Now());
1436 timeout_time.OffsetWithSeconds(timeout_sec);
1437 bool timed_out = false;
1438 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1441 error.SetErrorString("timed out waiting for shell command to complete");
1443 // Kill the process since it didn't complete withint the timeout specified
1444 ::kill (pid, SIGKILL);
1445 // Wait for the monitor callback to get the message
1446 timeout_time = TimeValue::Now();
1447 timeout_time.OffsetWithSeconds(1);
1449 shell_info->process_reaped.WaitForValueEqualTo(true, &timeout_time, &timed_out);
1454 *status_ptr = shell_info->status;
1457 *signo_ptr = shell_info->signo;
1459 if (command_output_ptr)
1461 command_output_ptr->clear();
1462 FileSpec file_spec(output_file_path, File::eOpenOptionRead);
1463 uint64_t file_size = file_spec.GetByteSize();
1466 if (file_size > command_output_ptr->max_size())
1468 error.SetErrorStringWithFormat("shell command output is too large to fit into a std::string");
1472 command_output_ptr->resize(file_size);
1473 file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error);
1478 shell_info->can_delete.SetValue(true, eBroadcastAlways);
1482 error.SetErrorString("failed to get process ID");
1485 if (output_file_path)
1486 ::unlink (output_file_path);
1487 // Handshake with the monitor thread, or just let it know in advance that
1488 // it can delete "shell_info" in case we timed out and were not able to kill
1495 Host::GetNumberCPUS ()
1497 static uint32_t g_num_cores = UINT32_MAX;
1498 if (g_num_cores == UINT32_MAX)
1500 #if defined(__APPLE__) or defined (__linux__) or defined (__FreeBSD__) or defined (__FreeBSD_kernel__)
1502 g_num_cores = ::sysconf(_SC_NPROCESSORS_ONLN);
1504 #elif defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
1506 // Header file for this might need to be included at the top of this file
1507 SYSTEM_INFO system_info;
1508 ::GetSystemInfo (&system_info);
1509 g_num_cores = system_info.dwNumberOfProcessors;
1513 // Assume POSIX support if a host specific case has not been supplied above
1516 size_t num_cores_len = sizeof(num_cores);
1518 int mib[] = { CTL_HW, HW_AVAILCPU };
1520 int mib[] = { CTL_HW, HW_NCPU };
1523 /* get the number of CPUs from the system */
1524 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1526 g_num_cores = num_cores;
1531 num_cores_len = sizeof(num_cores);
1532 if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
1535 g_num_cores = num_cores;
1545 #if !defined (__APPLE__)
1547 Host::OpenFileInExternalEditor (const FileSpec &file_spec, uint32_t line_no)
1553 Host::SetCrashDescriptionWithFormat (const char *format, ...)
1558 Host::SetCrashDescription (const char *description)
1563 LaunchApplication (const FileSpec &app_file_spec)
1565 return LLDB_INVALID_PROCESS_ID;