From b245ecac79459de6db2d1443190a1e67eac56fff Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Thu, 21 Aug 2014 20:02:17 +0000 Subject: [PATCH] Move GetUsername and GetGroupname to HostInfoPosix llvm-svn: 216210 --- lldb/include/lldb/Host/posix/HostInfoPosix.h | 2 + lldb/source/Host/common/Host.cpp | 76 ------------------- lldb/source/Host/posix/HostInfoPosix.cpp | 55 +++++++++++++- lldb/source/Host/windows/Host.cpp | 13 ---- .../GDBRemoteCommunicationServer.cpp | 8 +- lldb/source/Target/Platform.cpp | 8 +- 6 files changed, 68 insertions(+), 94 deletions(-) diff --git a/lldb/include/lldb/Host/posix/HostInfoPosix.h b/lldb/include/lldb/Host/posix/HostInfoPosix.h index 4f3d69ca1036..6c6f676e3228 100644 --- a/lldb/include/lldb/Host/posix/HostInfoPosix.h +++ b/lldb/include/lldb/Host/posix/HostInfoPosix.h @@ -22,6 +22,8 @@ class HostInfoPosix : public HostInfoBase public: static size_t GetPageSize(); static bool GetHostname(std::string &s); + static bool LookupUserName(uint32_t uid, std::string &user_name); + static bool LookupGroupName(uint32_t gid, std::string &group_name); protected: static bool ComputeSupportExeDirectory(FileSpec &file_spec); diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp index c409a01d4715..8d89f80862d9 100644 --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -867,65 +867,6 @@ Host::GetModuleFileSpecForHostAddress (const void *host_addr) #ifndef _WIN32 -const char * -Host::GetUserName (uint32_t uid, std::string &user_name) -{ - struct passwd user_info; - struct passwd *user_info_ptr = &user_info; - char user_buffer[PATH_MAX]; - size_t user_buffer_size = sizeof(user_buffer); - if (::getpwuid_r (uid, - &user_info, - user_buffer, - user_buffer_size, - &user_info_ptr) == 0) - { - if (user_info_ptr) - { - user_name.assign (user_info_ptr->pw_name); - return user_name.c_str(); - } - } - user_name.clear(); - return NULL; -} - -const char * -Host::GetGroupName (uint32_t gid, std::string &group_name) -{ - char group_buffer[PATH_MAX]; - size_t group_buffer_size = sizeof(group_buffer); - struct group group_info; - struct group *group_info_ptr = &group_info; - // Try the threadsafe version first - if (::getgrgid_r (gid, - &group_info, - group_buffer, - group_buffer_size, - &group_info_ptr) == 0) - { - if (group_info_ptr) - { - group_name.assign (group_info_ptr->gr_name); - return group_name.c_str(); - } - } - else - { - // The threadsafe version isn't currently working - // for me on darwin, but the non-threadsafe version - // is, so I am calling it below. - group_info_ptr = ::getgrgid (gid); - if (group_info_ptr) - { - group_name.assign (group_info_ptr->gr_name); - return group_name.c_str(); - } - } - group_name.clear(); - return NULL; -} - uint32_t Host::GetUserID () { @@ -952,23 +893,6 @@ Host::GetEffectiveGroupID () #endif -#if !defined (__APPLE__) && !defined (__FreeBSD__) && !defined (__FreeBSD_kernel__) \ - && !defined(__linux__) && !defined(_WIN32) -uint32_t -Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos) -{ - process_infos.Clear(); - return process_infos.GetSize(); -} - -bool -Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) -{ - process_info.Clear(); - return false; -} -#endif - #if !defined(__linux__) bool Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach) diff --git a/lldb/source/Host/posix/HostInfoPosix.cpp b/lldb/source/Host/posix/HostInfoPosix.cpp index 7c456412cf1f..c80313787fae 100644 --- a/lldb/source/Host/posix/HostInfoPosix.cpp +++ b/lldb/source/Host/posix/HostInfoPosix.cpp @@ -15,8 +15,11 @@ #include "llvm/ADT/SmallString.h" #include "llvm/Support/raw_ostream.h" -#include +#include #include +#include +#include +#include #include using namespace lldb_private; @@ -44,6 +47,56 @@ HostInfoPosix::GetHostname(std::string &s) return false; } +bool +HostInfoPosix::LookupUserName(uint32_t uid, std::string &user_name) +{ + struct passwd user_info; + struct passwd *user_info_ptr = &user_info; + char user_buffer[PATH_MAX]; + size_t user_buffer_size = sizeof(user_buffer); + if (::getpwuid_r(uid, &user_info, user_buffer, user_buffer_size, &user_info_ptr) == 0) + { + if (user_info_ptr) + { + user_name.assign(user_info_ptr->pw_name); + return user_name.c_str(); + } + } + user_name.clear(); + return NULL; +} + +bool +HostInfoPosix::LookupGroupName(uint32_t gid, std::string &group_name) +{ + char group_buffer[PATH_MAX]; + size_t group_buffer_size = sizeof(group_buffer); + struct group group_info; + struct group *group_info_ptr = &group_info; + // Try the threadsafe version first + if (::getgrgid_r(gid, &group_info, group_buffer, group_buffer_size, &group_info_ptr) == 0) + { + if (group_info_ptr) + { + group_name.assign(group_info_ptr->gr_name); + return group_name.c_str(); + } + } + else + { + // The threadsafe version isn't currently working for me on darwin, but the non-threadsafe version + // is, so I am calling it below. + group_info_ptr = ::getgrgid(gid); + if (group_info_ptr) + { + group_name.assign(group_info_ptr->gr_name); + return group_name.c_str(); + } + } + group_name.clear(); + return NULL; +} + bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) { diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp index 6511c5aa6fd7..07ab4f9cc72c 100644 --- a/lldb/source/Host/windows/Host.cpp +++ b/lldb/source/Host/windows/Host.cpp @@ -235,19 +235,6 @@ Host::DynamicLibraryGetSymbol(void *opaque, const char *symbol_name, Error &erro return NULL; } -const char * -Host::GetUserName (uint32_t uid, std::string &user_name) -{ - return NULL; -} - -const char * -Host::GetGroupName (uint32_t gid, std::string &group_name) -{ - llvm_unreachable("Windows does not support group name"); - return NULL; -} - uint32_t Host::GetUserID () { diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp index dc99a990c5f5..bdece3dae1e7 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp @@ -1538,19 +1538,21 @@ GDBRemoteCommunicationServer::Handle_qsProcessInfo (StringExtractorGDBRemote &pa GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServer::Handle_qUserName (StringExtractorGDBRemote &packet) { +#if !defined(LLDB_DISABLE_POSIX) // Packet format: "qUserName:%i" where %i is the uid packet.SetFilePos(::strlen ("qUserName:")); uint32_t uid = packet.GetU32 (UINT32_MAX); if (uid != UINT32_MAX) { std::string name; - if (Host::GetUserName (uid, name)) + if (HostInfo::LookupUserName(uid, name)) { StreamString response; response.PutCStringAsRawHex8 (name.c_str()); return SendPacketNoLock (response.GetData(), response.GetSize()); } } +#endif return SendErrorResponse (5); } @@ -1558,19 +1560,21 @@ GDBRemoteCommunicationServer::Handle_qUserName (StringExtractorGDBRemote &packet GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServer::Handle_qGroupName (StringExtractorGDBRemote &packet) { +#if !defined(LLDB_DISABLE_POSIX) // Packet format: "qGroupName:%i" where %i is the gid packet.SetFilePos(::strlen ("qGroupName:")); uint32_t gid = packet.GetU32 (UINT32_MAX); if (gid != UINT32_MAX) { std::string name; - if (Host::GetGroupName (gid, name)) + if (HostInfo::LookupGroupName(gid, name)) { StreamString response; response.PutCStringAsRawHex8 (name.c_str()); return SendPacketNoLock (response.GetData(), response.GetSize()); } } +#endif return SendErrorResponse (6); } diff --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp index 5082b908b2e6..fe73be2d05b9 100644 --- a/lldb/source/Target/Platform.cpp +++ b/lldb/source/Target/Platform.cpp @@ -776,30 +776,34 @@ Platform::SetRemoteWorkingDirectory(const ConstString &path) const char * Platform::GetUserName (uint32_t uid) { +#if !defined(LLDB_DISABLE_POSIX) const char *user_name = GetCachedUserName(uid); if (user_name) return user_name; if (IsHost()) { std::string name; - if (Host::GetUserName(uid, name)) + if (HostInfo::LookupUserName(uid, name)) return SetCachedUserName (uid, name.c_str(), name.size()); } +#endif return NULL; } const char * Platform::GetGroupName (uint32_t gid) { +#if !defined(LLDB_DISABLE_POSIX) const char *group_name = GetCachedGroupName(gid); if (group_name) return group_name; if (IsHost()) { std::string name; - if (Host::GetGroupName(gid, name)) + if (HostInfo::LookupGroupName(gid, name)) return SetCachedGroupName (gid, name.c_str(), name.size()); } +#endif return NULL; }