From a21fee0ee403731a198926ce63e7b6958419f53a Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Thu, 21 Aug 2014 21:49:24 +0000 Subject: [PATCH] Move the rest of the HostInfo functions over. This should bring HostInfo up to 99% completion. The remainder of code in Host will be split into instantiatable classes representing host processes, threads, dynamic libraries, and process launching strategies. llvm-svn: 216230 --- lldb/include/lldb/Host/Host.h | 42 --------- .../lldb/Host/freebsd/HostInfoFreeBSD.h | 2 + lldb/include/lldb/Host/linux/HostInfoLinux.h | 3 + .../include/lldb/Host/macosx/HostInfoMacOSX.h | 2 + lldb/include/lldb/Host/posix/HostInfoPosix.h | 5 ++ .../lldb/Host/windows/HostInfoWindows.h | 5 ++ lldb/source/API/SBHostOS.cpp | 2 +- lldb/source/Host/common/Host.cpp | 90 ------------------- lldb/source/Host/freebsd/HostInfoFreeBSD.cpp | 19 ++++ lldb/source/Host/linux/HostInfoLinux.cpp | 19 ++++ lldb/source/Host/macosx/HostInfoMacOSX.mm | 27 ++++++ lldb/source/Host/posix/HostInfoPosix.cpp | 24 +++++ lldb/source/Host/windows/Host.cpp | 27 ------ lldb/source/Host/windows/HostInfoWindows.cpp | 17 ++++ .../source/Interpreter/CommandInterpreter.cpp | 9 +- 15 files changed, 131 insertions(+), 162 deletions(-) diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h index 8d210fbdb1d2..7ad2922accd0 100644 --- a/lldb/include/lldb/Host/Host.h +++ b/lldb/include/lldb/Host/Host.h @@ -91,25 +91,6 @@ public: lldb::pid_t pid, bool monitor_signals); - static const char * - GetUserName (uint32_t uid, std::string &user_name); - - static const char * - GetGroupName (uint32_t gid, std::string &group_name); - - static uint32_t - GetUserID (); - - static uint32_t - GetGroupID (); - - static uint32_t - GetEffectiveUserID (); - - static uint32_t - GetEffectiveGroupID (); - - enum SystemLogType { eSystemLogWarning, @@ -260,27 +241,6 @@ public: static bool SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name, size_t len); - //------------------------------------------------------------------ - /// Gets the FileSpec of the user profile directory. On Posix-platforms - /// this is ~, and on windows this is generally something like - /// C:\Users\Alice. - /// - /// @return - /// \b A file spec with the path to the user's home directory. - //------------------------------------------------------------------ - static FileSpec - GetUserProfileFileSpec (); - - //------------------------------------------------------------------ - /// Gets the FileSpec of the current process (the process that - /// that is running the LLDB code). - /// - /// @return - /// \b A file spec with the program name. - //------------------------------------------------------------------ - static FileSpec - GetProgramFileSpec (); - //------------------------------------------------------------------ /// Given an address in the current process (the process that /// is running the LLDB code), return the name of the module that @@ -298,8 +258,6 @@ public: //------------------------------------------------------------------ static FileSpec GetModuleFileSpecForHostAddress (const void *host_addr); - - //------------------------------------------------------------------ /// If you have an executable that is in a bundle and want to get diff --git a/lldb/include/lldb/Host/freebsd/HostInfoFreeBSD.h b/lldb/include/lldb/Host/freebsd/HostInfoFreeBSD.h index 79247c5f2213..1404a4b1525c 100644 --- a/lldb/include/lldb/Host/freebsd/HostInfoFreeBSD.h +++ b/lldb/include/lldb/Host/freebsd/HostInfoFreeBSD.h @@ -10,6 +10,7 @@ #ifndef lldb_Host_freebsd_HostInfoFreeBSD_h_ #define lldb_Host_freebsd_HostInfoFreeBSD_h_ +#include "lldb/Host/FileSpec.h" #include "lldb/Host/posix/HostInfoPosix.h" namespace lldb_private @@ -21,6 +22,7 @@ class HostInfoFreeBSD : public HostInfoPosix static bool GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update); static bool GetOSBuildString(std::string &s); static bool GetOSKernelDescription(std::string &s); + static FileSpec GetProgramFileSpec(); }; } diff --git a/lldb/include/lldb/Host/linux/HostInfoLinux.h b/lldb/include/lldb/Host/linux/HostInfoLinux.h index 3e728779637d..a04995e21fef 100644 --- a/lldb/include/lldb/Host/linux/HostInfoLinux.h +++ b/lldb/include/lldb/Host/linux/HostInfoLinux.h @@ -10,7 +10,9 @@ #ifndef lldb_Host_linux_HostInfoLinux_h_ #define lldb_Host_linux_HostInfoLinux_h_ +#include "lldb/Host/FileSpec.h" #include "lldb/Host/posix/HostInfoPosix.h" + #include "llvm/ADT/StringRef.h" #include @@ -32,6 +34,7 @@ class HostInfoLinux : public HostInfoPosix static bool GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update); static llvm::StringRef GetDistributionId(); + static FileSpec GetProgramFileSpec(); protected: static bool ComputeSystemPluginsDirectory(FileSpec &file_spec); diff --git a/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h b/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h index e8b3bb3e8504..ec76bb003ec2 100644 --- a/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h +++ b/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h @@ -10,6 +10,7 @@ #ifndef lldb_Host_macosx_HostInfoMacOSX_h_ #define lldb_Host_macosx_HostInfoMacOSX_h_ +#include "lldb/Host/FileSpec.h" #include "lldb/Host/posix/HostInfoPosix.h" namespace lldb_private @@ -30,6 +31,7 @@ class HostInfoMacOSX : public HostInfoPosix static bool GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update); static bool GetOSBuildString(std::string &s); static bool GetOSKernelDescription(std::string &s); + static FileSpec GetProgramFileSpec(); protected: static bool ComputeSupportExeDirectory(FileSpec &file_spec); diff --git a/lldb/include/lldb/Host/posix/HostInfoPosix.h b/lldb/include/lldb/Host/posix/HostInfoPosix.h index 2a39344d5f8a..6e0dcbe48021 100644 --- a/lldb/include/lldb/Host/posix/HostInfoPosix.h +++ b/lldb/include/lldb/Host/posix/HostInfoPosix.h @@ -25,6 +25,11 @@ class HostInfoPosix : public HostInfoBase static const char *LookupUserName(uint32_t uid, std::string &user_name); static const char *LookupGroupName(uint32_t gid, std::string &group_name); + static uint32_t GetUserID(); + static uint32_t GetGroupID(); + static uint32_t GetEffectiveUserID(); + static uint32_t GetEffectiveGroupID(); + protected: static bool ComputeSupportExeDirectory(FileSpec &file_spec); static bool ComputeHeaderDirectory(FileSpec &file_spec); diff --git a/lldb/include/lldb/Host/windows/HostInfoWindows.h b/lldb/include/lldb/Host/windows/HostInfoWindows.h index cf4b9e3fa8a4..7077a5173d75 100644 --- a/lldb/include/lldb/Host/windows/HostInfoWindows.h +++ b/lldb/include/lldb/Host/windows/HostInfoWindows.h @@ -11,6 +11,7 @@ #define lldb_Host_windows_HostInfoWindows_h_ #include "lldb/Host/HostInfoBase.h" +#include "lldb/Host/FileSpec.h" namespace lldb_private { @@ -31,9 +32,13 @@ class HostInfoWindows : public HostInfoBase static bool GetOSBuildString(std::string &s); static bool GetOSKernelDescription(std::string &s); static bool GetHostname(std::string &s); + static FileSpec GetProgramFileSpec(); protected: static bool ComputePythonDirectory(FileSpec &file_spec); + + private: + static FileSpec m_program_filespec; }; } diff --git a/lldb/source/API/SBHostOS.cpp b/lldb/source/API/SBHostOS.cpp index 5c32887c6478..ec1e2f2e9cba 100644 --- a/lldb/source/API/SBHostOS.cpp +++ b/lldb/source/API/SBHostOS.cpp @@ -23,7 +23,7 @@ SBFileSpec SBHostOS::GetProgramFileSpec () { SBFileSpec sb_filespec; - sb_filespec.SetFileSpec (Host::GetProgramFileSpec ()); + sb_filespec.SetFileSpec(HostInfo::GetProgramFileSpec()); return sb_filespec; } diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp index 8d89f80862d9..fc9953849cd8 100644 --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -646,68 +646,6 @@ Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid, #endif -FileSpec -Host::GetUserProfileFileSpec () -{ - static FileSpec g_profile_filespec; - if (!g_profile_filespec) - { - llvm::SmallString<64> path; - llvm::sys::path::home_directory(path); - return FileSpec(path.c_str(), false); - } - return g_profile_filespec; -} - -FileSpec -Host::GetProgramFileSpec () -{ - static FileSpec g_program_filespec; - if (!g_program_filespec) - { -#if defined (__APPLE__) - char program_fullpath[PATH_MAX]; - // If DST is NULL, then return the number of bytes needed. - uint32_t len = sizeof(program_fullpath); - int err = _NSGetExecutablePath (program_fullpath, &len); - if (err == 0) - g_program_filespec.SetFile (program_fullpath, false); - else if (err == -1) - { - char *large_program_fullpath = (char *)::malloc (len + 1); - - err = _NSGetExecutablePath (large_program_fullpath, &len); - if (err == 0) - g_program_filespec.SetFile (large_program_fullpath, false); - - ::free (large_program_fullpath); - } -#elif defined (__linux__) - char exe_path[PATH_MAX]; - ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); - if (len > 0) { - exe_path[len] = 0; - g_program_filespec.SetFile(exe_path, false); - } -#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__) - int exe_path_mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid() }; - size_t exe_path_size; - if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0) - { - char *exe_path = new char[exe_path_size]; - if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0) - g_program_filespec.SetFile(exe_path, false); - delete[] exe_path; - } -#elif defined(_WIN32) - std::vector buffer(PATH_MAX); - ::GetModuleFileName(NULL, &buffer[0], buffer.size()); - g_program_filespec.SetFile(&buffer[0], false); -#endif - } - return g_program_filespec; -} - #if !defined (__APPLE__) // see Host.mm bool @@ -865,34 +803,6 @@ Host::GetModuleFileSpecForHostAddress (const void *host_addr) #endif -#ifndef _WIN32 - -uint32_t -Host::GetUserID () -{ - return getuid(); -} - -uint32_t -Host::GetGroupID () -{ - return getgid(); -} - -uint32_t -Host::GetEffectiveUserID () -{ - return geteuid(); -} - -uint32_t -Host::GetEffectiveGroupID () -{ - return getegid(); -} - -#endif - #if !defined(__linux__) bool Host::FindProcessThreads (const lldb::pid_t pid, TidMap &tids_to_attach) diff --git a/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp b/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp index 9b872ea0686d..78458259f0a2 100644 --- a/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp +++ b/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp @@ -64,3 +64,22 @@ HostInfoFreeBSD::GetOSKernelDescription(std::string &s) return true; } + +FileSpec +HostInfoFreeBSD::GetProgramFileSpec() +{ + static FileSpec g_program_filespec; + if (!g_program_filespec) + { + int exe_path_mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, getpid()}; + size_t exe_path_size; + if (sysctl(exe_path_mib, 4, NULL, &exe_path_size, NULL, 0) == 0) + { + char *exe_path = new char[exe_path_size]; + if (sysctl(exe_path_mib, 4, exe_path, &exe_path_size, NULL, 0) == 0) + g_program_filespec.SetFile(exe_path, false); + delete[] exe_path; + } + } + return g_program_filespec; +} \ No newline at end of file diff --git a/lldb/source/Host/linux/HostInfoLinux.cpp b/lldb/source/Host/linux/HostInfoLinux.cpp index 73a0fd5bf994..6b4923882b22 100644 --- a/lldb/source/Host/linux/HostInfoLinux.cpp +++ b/lldb/source/Host/linux/HostInfoLinux.cpp @@ -172,6 +172,25 @@ HostInfoLinux::GetDistributionId() return g_fields->m_distribution_id.c_str(); } +FileSpec +HostInfoLinux::GetProgramFileSpec() +{ + static FileSpec g_program_filespec; + + if (!g_program_filespec) + { + char exe_path[PATH_MAX]; + ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1); + if (len > 0) + { + exe_path[len] = 0; + g_program_filespec.SetFile(exe_path, false); + } + } + + return g_program_filespec; +} + bool HostInfoLinux::ComputeSystemPluginsDirectory(FileSpec &file_spec) { diff --git a/lldb/source/Host/macosx/HostInfoMacOSX.mm b/lldb/source/Host/macosx/HostInfoMacOSX.mm index 116ed764a6ac..83d56a6793e5 100644 --- a/lldb/source/Host/macosx/HostInfoMacOSX.mm +++ b/lldb/source/Host/macosx/HostInfoMacOSX.mm @@ -27,6 +27,7 @@ // Objective C/C++ includes #include #include +#include #include using namespace lldb_private; @@ -91,6 +92,32 @@ HostInfoMacOSX::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update) return false; } +FileSpec +HostInfoMacOSX::GetProgramFileSpec() +{ + static FileSpec g_program_filespec; + if (!g_program_filespec) + { + char program_fullpath[PATH_MAX]; + // If DST is NULL, then return the number of bytes needed. + uint32_t len = sizeof(program_fullpath); + int err = _NSGetExecutablePath(program_fullpath, &len); + if (err == 0) + g_program_filespec.SetFile(program_fullpath, false); + else if (err == -1) + { + char *large_program_fullpath = (char *)::malloc(len + 1); + + err = _NSGetExecutablePath(large_program_fullpath, &len); + if (err == 0) + g_program_filespec.SetFile(large_program_fullpath, false); + + ::free(large_program_fullpath); + } + } + return g_program_filespec; +} + bool HostInfoMacOSX::ComputeSupportExeDirectory(FileSpec &file_spec) { diff --git a/lldb/source/Host/posix/HostInfoPosix.cpp b/lldb/source/Host/posix/HostInfoPosix.cpp index bb95a0d26c9e..78dc031fff26 100644 --- a/lldb/source/Host/posix/HostInfoPosix.cpp +++ b/lldb/source/Host/posix/HostInfoPosix.cpp @@ -97,6 +97,30 @@ HostInfoPosix::LookupGroupName(uint32_t gid, std::string &group_name) return NULL; } +uint32_t +HostInfoPosix::GetUserID() +{ + return getuid(); +} + +uint32_t +HostInfoPosix::GetGroupID() +{ + return getgid(); +} + +uint32_t +HostInfoPosix::GetEffectiveUserID() +{ + return geteuid(); +} + +uint32_t +HostInfoPosix::GetEffectiveGroupID() +{ + return getegid(); +} + bool HostInfoPosix::ComputeSupportExeDirectory(FileSpec &file_spec) { diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp index 07ab4f9cc72c..3f2870cb5fa0 100644 --- a/lldb/source/Host/windows/Host.cpp +++ b/lldb/source/Host/windows/Host.cpp @@ -235,33 +235,6 @@ Host::DynamicLibraryGetSymbol(void *opaque, const char *symbol_name, Error &erro return NULL; } -uint32_t -Host::GetUserID () -{ - llvm_unreachable("Windows does not support uid"); -} - -uint32_t -Host::GetGroupID () -{ - llvm_unreachable("Windows does not support gid"); - return 0; -} - -uint32_t -Host::GetEffectiveUserID () -{ - llvm_unreachable("Windows does not support euid"); - return 0; -} - -uint32_t -Host::GetEffectiveGroupID () -{ - llvm_unreachable("Windows does not support egid"); - return 0; -} - uint32_t Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos) { diff --git a/lldb/source/Host/windows/HostInfoWindows.cpp b/lldb/source/Host/windows/HostInfoWindows.cpp index e0487356a56d..f7d7411c08dc 100644 --- a/lldb/source/Host/windows/HostInfoWindows.cpp +++ b/lldb/source/Host/windows/HostInfoWindows.cpp @@ -15,6 +15,8 @@ using namespace lldb_private; +FileSpec HostInfoWindows::m_program_filespec; + size_t HostInfoWindows::GetPageSize() { @@ -79,6 +81,21 @@ HostInfoWindows::GetHostname(std::string &s) return true; } +FileSpec +HostInfoWindows::GetProgramFileSpec() +{ + static bool is_initialized = false; + if (!is_initialized) + { + is_initialized = true; + + std::vector buffer(PATH_MAX); + ::GetModuleFileName(NULL, &buffer[0], buffer.size()); + m_program_filespec.SetFile(&buffer[0], false); + } + return m_program_filespec; +} + bool HostInfoWindows::ComputePythonDirectory(FileSpec &file_spec) { diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp index 11ae7a502740..56c8f8c0ad6a 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -51,6 +51,7 @@ #include "lldb/Host/Editline.h" #include "lldb/Host/Host.h" +#include "lldb/Host/HostInfo.h" #include "lldb/Interpreter/Args.h" #include "lldb/Interpreter/CommandCompletions.h" @@ -67,7 +68,9 @@ #include "lldb/Utility/CleanUp.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Path.h" using namespace lldb; using namespace lldb_private; @@ -2381,13 +2384,15 @@ CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result) // "-" and the name of the program. If this file doesn't exist, we fall // back to just the "~/.lldbinit" file. We also obey any requests to not // load the init files. - FileSpec profilePath = Host::GetUserProfileFileSpec(); + llvm::SmallString<64> home_dir_path; + llvm::sys::path::home_directory(home_dir_path); + FileSpec profilePath(home_dir_path.c_str(), false); profilePath.AppendPathComponent(".lldbinit"); std::string init_file_path = profilePath.GetPath(); if (m_skip_app_init_files == false) { - FileSpec program_file_spec (Host::GetProgramFileSpec()); + FileSpec program_file_spec(HostInfo::GetProgramFileSpec()); const char *program_name = program_file_spec.GetFilename().AsCString(); if (program_name)