1 //===-- HostInfoPosix.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 #if !defined(LLDB_DISABLE_PYTHON)
11 #include "Plugins/ScriptInterpreter/Python/lldb-python.h"
14 #include "lldb/Host/posix/HostInfoPosix.h"
15 #include "lldb/Utility/Log.h"
17 #include "clang/Basic/Version.h"
18 #include "clang/Config/config.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/raw_ostream.h"
30 #include <sys/types.h>
33 using namespace lldb_private;
35 size_t HostInfoPosix::GetPageSize() { return ::getpagesize(); }
37 bool HostInfoPosix::GetHostname(std::string &s) {
38 char hostname[PATH_MAX];
39 hostname[sizeof(hostname) - 1] = '\0';
40 if (::gethostname(hostname, sizeof(hostname) - 1) == 0) {
41 struct hostent *h = ::gethostbyname(hostname);
52 #include <android/api-level.h>
54 #if defined(__ANDROID_API__) && __ANDROID_API__ < 21
59 static std::mutex s_getpwuid_lock;
62 const char *HostInfoPosix::LookupUserName(uint32_t uid,
63 std::string &user_name) {
65 // getpwuid_r is missing from android-9
66 // make getpwuid thread safe with a mutex
67 std::lock_guard<std::mutex> lock(s_getpwuid_lock);
68 struct passwd *user_info_ptr = ::getpwuid(uid);
70 user_name.assign(user_info_ptr->pw_name);
71 return user_name.c_str();
74 struct passwd user_info;
75 struct passwd *user_info_ptr = &user_info;
76 char user_buffer[PATH_MAX];
77 size_t user_buffer_size = sizeof(user_buffer);
78 if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size,
79 &user_info_ptr) == 0) {
81 user_name.assign(user_info_ptr->pw_name);
82 return user_name.c_str();
90 const char *HostInfoPosix::LookupGroupName(uint32_t gid,
91 std::string &group_name) {
93 char group_buffer[PATH_MAX];
94 size_t group_buffer_size = sizeof(group_buffer);
95 struct group group_info;
96 struct group *group_info_ptr = &group_info;
97 // Try the threadsafe version first
98 if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size,
99 &group_info_ptr) == 0) {
100 if (group_info_ptr) {
101 group_name.assign(group_info_ptr->gr_name);
102 return group_name.c_str();
105 // The threadsafe version isn't currently working for me on darwin, but the
106 // non-threadsafe version
107 // is, so I am calling it below.
108 group_info_ptr = ::getgrgid(gid);
109 if (group_info_ptr) {
110 group_name.assign(group_info_ptr->gr_name);
111 return group_name.c_str();
116 assert(false && "getgrgid_r() not supported on Android");
121 uint32_t HostInfoPosix::GetUserID() { return getuid(); }
123 uint32_t HostInfoPosix::GetGroupID() { return getgid(); }
125 uint32_t HostInfoPosix::GetEffectiveUserID() { return geteuid(); }
127 uint32_t HostInfoPosix::GetEffectiveGroupID() { return getegid(); }
129 FileSpec HostInfoPosix::GetDefaultShell() { return FileSpec("/bin/sh", false); }
131 bool HostInfoPosix::ComputePathRelativeToLibrary(FileSpec &file_spec,
132 llvm::StringRef dir) {
133 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
135 FileSpec lldb_file_spec;
136 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
139 std::string raw_path = lldb_file_spec.GetPath();
140 // drop library directory
141 llvm::StringRef parent_path = llvm::sys::path::parent_path(raw_path);
143 // Most Posix systems (e.g. Linux/*BSD) will attempt to replace a */lib with
144 // */bin as the base directory for helper exe programs. This will fail if the
145 // /lib and /bin directories are rooted in entirely different trees.
147 log->Printf("HostInfoPosix::ComputePathRelativeToLibrary() attempting to "
148 "derive the %s path from this path: %s",
149 dir.data(), raw_path.c_str());
151 if (!parent_path.empty()) {
152 // Now write in bin in place of lib.
153 raw_path = (parent_path + dir).str();
156 log->Printf("Host::%s() derived the bin path as: %s", __FUNCTION__,
160 log->Printf("Host::%s() failed to find /lib/liblldb within the shared "
161 "lib path, bailing on bin path construction",
164 file_spec.GetDirectory().SetString(raw_path);
165 return (bool)file_spec.GetDirectory();
168 bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) {
169 return ComputePathRelativeToLibrary(file_spec, "/bin");
172 bool HostInfoPosix::ComputeClangDirectory(FileSpec &file_spec) {
173 return ComputePathRelativeToLibrary(
174 file_spec, (llvm::Twine("/lib") + CLANG_LIBDIR_SUFFIX + "/clang/" +
175 CLANG_VERSION_STRING)
179 bool HostInfoPosix::ComputeHeaderDirectory(FileSpec &file_spec) {
180 FileSpec temp_file("/opt/local/include/lldb", false);
181 file_spec.GetDirectory().SetCString(temp_file.GetPath().c_str());
185 bool HostInfoPosix::ComputePythonDirectory(FileSpec &file_spec) {
186 #ifndef LLDB_DISABLE_PYTHON
187 FileSpec lldb_file_spec;
188 if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
191 char raw_path[PATH_MAX];
192 lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
194 #if defined(LLDB_PYTHON_RELATIVE_LIBDIR)
195 // Build the path by backing out of the lib dir, then building
196 // with whatever the real python interpreter uses. (e.g. lib
197 // for most, lib64 on RHEL x86_64).
198 char python_path[PATH_MAX];
199 ::snprintf(python_path, sizeof(python_path), "%s/../%s", raw_path,
200 LLDB_PYTHON_RELATIVE_LIBDIR);
202 char final_path[PATH_MAX];
203 realpath(python_path, final_path);
204 file_spec.GetDirectory().SetCString(final_path);
208 llvm::SmallString<256> python_version_dir;
209 llvm::raw_svector_ostream os(python_version_dir);
210 os << "/python" << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION
213 // We may get our string truncated. Should we protect this with an assert?
214 ::strncat(raw_path, python_version_dir.c_str(),
215 sizeof(raw_path) - strlen(raw_path) - 1);
217 file_spec.GetDirectory().SetCString(raw_path);
225 bool HostInfoPosix::GetEnvironmentVar(const std::string &var_name,
227 if (const char *pvar = ::getenv(var_name.c_str())) {
228 var = std::string(pvar);