[Lldb-commits] [lldb] 0a39a9c - Modernize and simplify HostInfo::GetOSKernelDescription

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Tue Oct 26 02:23:28 PDT 2021


Author: Pavel Labath
Date: 2021-10-26T11:17:02+02:00
New Revision: 0a39a9c2cb43f93c82908cabdf73fdd4918a26e5

URL: https://github.com/llvm/llvm-project/commit/0a39a9c2cb43f93c82908cabdf73fdd4918a26e5
DIFF: https://github.com/llvm/llvm-project/commit/0a39a9c2cb43f93c82908cabdf73fdd4918a26e5.diff

LOG: Modernize and simplify HostInfo::GetOSKernelDescription

Replace bool+by-ref argument with llvm::Optional, and move the common
implementation into HostInfoPOSIX. Based on my (simple) experiment,
the uname and the sysctl approach return the same value on MacOS, so
there's no need for a mac-specific implementation of this functionality.

Differential Revision: https://reviews.llvm.org/D112457

Added: 
    

Modified: 
    lldb/include/lldb/Host/freebsd/HostInfoFreeBSD.h
    lldb/include/lldb/Host/linux/HostInfoLinux.h
    lldb/include/lldb/Host/macosx/HostInfoMacOSX.h
    lldb/include/lldb/Host/netbsd/HostInfoNetBSD.h
    lldb/include/lldb/Host/openbsd/HostInfoOpenBSD.h
    lldb/include/lldb/Host/posix/HostInfoPosix.h
    lldb/include/lldb/Host/windows/HostInfoWindows.h
    lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
    lldb/source/Host/linux/HostInfoLinux.cpp
    lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
    lldb/source/Host/netbsd/HostInfoNetBSD.cpp
    lldb/source/Host/openbsd/HostInfoOpenBSD.cpp
    lldb/source/Host/posix/HostInfoPosix.cpp
    lldb/source/Host/windows/HostInfoWindows.cpp
    lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
    lldb/source/Target/Platform.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Host/freebsd/HostInfoFreeBSD.h b/lldb/include/lldb/Host/freebsd/HostInfoFreeBSD.h
index 8207e9093f71..b2f3f08cd145 100644
--- a/lldb/include/lldb/Host/freebsd/HostInfoFreeBSD.h
+++ b/lldb/include/lldb/Host/freebsd/HostInfoFreeBSD.h
@@ -19,7 +19,6 @@ class HostInfoFreeBSD : public HostInfoPosix {
 public:
   static llvm::VersionTuple GetOSVersion();
   static llvm::Optional<std::string> GetOSBuildString();
-  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 628767060664..e2167b8a883d 100644
--- a/lldb/include/lldb/Host/linux/HostInfoLinux.h
+++ b/lldb/include/lldb/Host/linux/HostInfoLinux.h
@@ -27,7 +27,6 @@ class HostInfoLinux : public HostInfoPosix {
 
   static llvm::VersionTuple GetOSVersion();
   static llvm::Optional<std::string> GetOSBuildString();
-  static bool GetOSKernelDescription(std::string &s);
   static llvm::StringRef GetDistributionId();
   static FileSpec GetProgramFileSpec();
 

diff  --git a/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h b/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h
index 42c2872af218..de7ecc1e6d80 100644
--- a/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h
+++ b/lldb/include/lldb/Host/macosx/HostInfoMacOSX.h
@@ -25,7 +25,6 @@ class HostInfoMacOSX : public HostInfoPosix {
   static llvm::VersionTuple GetOSVersion();
   static llvm::VersionTuple GetMacCatalystVersion();
   static llvm::Optional<std::string> GetOSBuildString();
-  static bool GetOSKernelDescription(std::string &s);
   static FileSpec GetProgramFileSpec();
   static FileSpec GetXcodeContentsDirectory();
   static FileSpec GetXcodeDeveloperDirectory();

diff  --git a/lldb/include/lldb/Host/netbsd/HostInfoNetBSD.h b/lldb/include/lldb/Host/netbsd/HostInfoNetBSD.h
index 021f8626733b..32644ce79a69 100644
--- a/lldb/include/lldb/Host/netbsd/HostInfoNetBSD.h
+++ b/lldb/include/lldb/Host/netbsd/HostInfoNetBSD.h
@@ -19,7 +19,6 @@ class HostInfoNetBSD : public HostInfoPosix {
 public:
   static llvm::VersionTuple GetOSVersion();
   static llvm::Optional<std::string> GetOSBuildString();
-  static bool GetOSKernelDescription(std::string &s);
   static FileSpec GetProgramFileSpec();
 };
 }

diff  --git a/lldb/include/lldb/Host/openbsd/HostInfoOpenBSD.h b/lldb/include/lldb/Host/openbsd/HostInfoOpenBSD.h
index ba5ac8cbb169..01879ad5c0e4 100644
--- a/lldb/include/lldb/Host/openbsd/HostInfoOpenBSD.h
+++ b/lldb/include/lldb/Host/openbsd/HostInfoOpenBSD.h
@@ -19,7 +19,6 @@ class HostInfoOpenBSD : public HostInfoPosix {
 public:
   static llvm::VersionTuple GetOSVersion();
   static llvm::Optional<std::string> GetOSBuildString();
-  static bool GetOSKernelDescription(std::string &s);
   static FileSpec GetProgramFileSpec();
 };
 }

diff  --git a/lldb/include/lldb/Host/posix/HostInfoPosix.h b/lldb/include/lldb/Host/posix/HostInfoPosix.h
index 825c79f53ecb..f1ff6b860864 100644
--- a/lldb/include/lldb/Host/posix/HostInfoPosix.h
+++ b/lldb/include/lldb/Host/posix/HostInfoPosix.h
@@ -22,6 +22,7 @@ class HostInfoPosix : public HostInfoBase {
 public:
   static size_t GetPageSize();
   static bool GetHostname(std::string &s);
+  static llvm::Optional<std::string> GetOSKernelDescription();
 
   static uint32_t GetUserID();
   static uint32_t GetGroupID();

diff  --git a/lldb/include/lldb/Host/windows/HostInfoWindows.h b/lldb/include/lldb/Host/windows/HostInfoWindows.h
index d7f9e68254c9..d359ee21984c 100644
--- a/lldb/include/lldb/Host/windows/HostInfoWindows.h
+++ b/lldb/include/lldb/Host/windows/HostInfoWindows.h
@@ -28,7 +28,7 @@ class HostInfoWindows : public HostInfoBase {
 
   static llvm::VersionTuple GetOSVersion();
   static llvm::Optional<std::string> GetOSBuildString();
-  static bool GetOSKernelDescription(std::string &s);
+  static llvm::Optional<std::string> GetOSKernelDescription();
   static bool GetHostname(std::string &s);
   static FileSpec GetProgramFileSpec();
   static FileSpec GetDefaultShell();

diff  --git a/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp b/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
index 50f43999f369..f9ff45666c1e 100644
--- a/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
+++ b/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
@@ -41,20 +41,6 @@ llvm::Optional<std::string> HostInfoFreeBSD::GetOSBuildString() {
   return llvm::None;
 }
 
-bool HostInfoFreeBSD::GetOSKernelDescription(std::string &s) {
-  struct utsname un;
-
-  ::memset(&un, 0, sizeof(utsname));
-  s.clear();
-
-  if (uname(&un) < 0)
-    return false;
-
-  s.assign(un.version);
-
-  return true;
-}
-
 FileSpec HostInfoFreeBSD::GetProgramFileSpec() {
   static FileSpec g_program_filespec;
   if (!g_program_filespec) {

diff  --git a/lldb/source/Host/linux/HostInfoLinux.cpp b/lldb/source/Host/linux/HostInfoLinux.cpp
index 32642ed375f0..fa82da2e1e8a 100644
--- a/lldb/source/Host/linux/HostInfoLinux.cpp
+++ b/lldb/source/Host/linux/HostInfoLinux.cpp
@@ -75,19 +75,6 @@ llvm::Optional<std::string> HostInfoLinux::GetOSBuildString() {
   return std::string(un.release);
 }
 
-bool HostInfoLinux::GetOSKernelDescription(std::string &s) {
-  struct utsname un;
-
-  ::memset(&un, 0, sizeof(utsname));
-  s.clear();
-
-  if (uname(&un) < 0)
-    return false;
-
-  s.assign(un.version);
-  return true;
-}
-
 llvm::StringRef HostInfoLinux::GetDistributionId() {
   assert(g_fields && "Missing call to Initialize?");
   // Try to run 'lbs_release -i', and use that response for the distribution

diff  --git a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
index 5f141b08e866..6d025551723c 100644
--- a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
+++ b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
@@ -65,18 +65,6 @@
   return llvm::None;
 }
 
-bool HostInfoMacOSX::GetOSKernelDescription(std::string &s) {
-  int mib[2] = {CTL_KERN, KERN_VERSION};
-  char cstr[PATH_MAX];
-  size_t cstr_len = sizeof(cstr);
-  if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0) {
-    s.assign(cstr, cstr_len);
-    return true;
-  }
-  s.clear();
-  return false;
-}
-
 static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
   @autoreleasepool {
     NSDictionary *version_info =

diff  --git a/lldb/source/Host/netbsd/HostInfoNetBSD.cpp b/lldb/source/Host/netbsd/HostInfoNetBSD.cpp
index 42bfb205b27c..234dd3d5e103 100644
--- a/lldb/source/Host/netbsd/HostInfoNetBSD.cpp
+++ b/lldb/source/Host/netbsd/HostInfoNetBSD.cpp
@@ -54,20 +54,6 @@ llvm::Optional<std::string> HostInfoNetBSD::GetOSBuildString() {
   return llvm::None;
 }
 
-bool HostInfoNetBSD::GetOSKernelDescription(std::string &s) {
-  struct utsname un;
-
-  ::memset(&un, 0, sizeof(un));
-  s.clear();
-
-  if (::uname(&un) < 0)
-    return false;
-
-  s.assign(un.version);
-
-  return true;
-}
-
 FileSpec HostInfoNetBSD::GetProgramFileSpec() {
   static FileSpec g_program_filespec;
 

diff  --git a/lldb/source/Host/openbsd/HostInfoOpenBSD.cpp b/lldb/source/Host/openbsd/HostInfoOpenBSD.cpp
index 18ffa381fbb4..5db843ff628d 100644
--- a/lldb/source/Host/openbsd/HostInfoOpenBSD.cpp
+++ b/lldb/source/Host/openbsd/HostInfoOpenBSD.cpp
@@ -41,20 +41,6 @@ llvm::Optional<std::string> HostInfoOpenBSD::GetOSBuildString() {
   return llvm::None;
 }
 
-bool HostInfoOpenBSD::GetOSKernelDescription(std::string &s) {
-  struct utsname un;
-
-  ::memset(&un, 0, sizeof(utsname));
-  s.clear();
-
-  if (uname(&un) < 0)
-    return false;
-
-  s.assign(un.version);
-
-  return true;
-}
-
 FileSpec HostInfoOpenBSD::GetProgramFileSpec() {
   static FileSpec g_program_filespec;
   return g_program_filespec;

diff  --git a/lldb/source/Host/posix/HostInfoPosix.cpp b/lldb/source/Host/posix/HostInfoPosix.cpp
index b633acf6fec6..63553590dff5 100644
--- a/lldb/source/Host/posix/HostInfoPosix.cpp
+++ b/lldb/source/Host/posix/HostInfoPosix.cpp
@@ -21,6 +21,7 @@
 #include <mutex>
 #include <pwd.h>
 #include <sys/types.h>
+#include <sys/utsname.h>
 #include <unistd.h>
 
 using namespace lldb_private;
@@ -37,6 +38,14 @@ bool HostInfoPosix::GetHostname(std::string &s) {
   return false;
 }
 
+llvm::Optional<std::string> HostInfoPosix::GetOSKernelDescription() {
+  struct utsname un;
+  if (uname(&un) < 0)
+    return llvm::None;
+
+  return std::string(un.version);
+}
+
 #ifdef __ANDROID__
 #include <android/api-level.h>
 #endif

diff  --git a/lldb/source/Host/windows/HostInfoWindows.cpp b/lldb/source/Host/windows/HostInfoWindows.cpp
index 1e6ce0ce5b2d..c6fa96d674bb 100644
--- a/lldb/source/Host/windows/HostInfoWindows.cpp
+++ b/lldb/source/Host/windows/HostInfoWindows.cpp
@@ -82,10 +82,8 @@ llvm::Optional<std::string> HostInfoWindows::GetOSBuildString() {
   return "Windows NT " + version.getAsString();
 }
 
-bool HostInfoWindows::GetOSKernelDescription(std::string &s) {
-  llvm::Optional<std::string> build = GetOSBuildString();
-  s = build.getValueOr("");
-  return build.hasValue();
+llvm::Optional<std::string> HostInfoWindows::GetOSKernelDescription() {
+  return GetOSBuildString();
 }
 
 bool HostInfoWindows::GetHostname(std::string &s) {

diff  --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
index 17b7f1759d17..15a1f0e80b16 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
@@ -272,13 +272,13 @@ GDBRemoteCommunicationServerCommon::Handle_qHostInfo(
     response.PutStringAsRawHex8(*s);
     response.PutChar(';');
   }
-  std::string s;
-  if (HostInfo::GetOSKernelDescription(s)) {
+  if (llvm::Optional<std::string> s = HostInfo::GetOSKernelDescription()) {
     response.PutCString("os_kernel:");
-    response.PutStringAsRawHex8(s);
+    response.PutStringAsRawHex8(*s);
     response.PutChar(';');
   }
 
+  std::string s;
 #if defined(__APPLE__)
 
 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)

diff  --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp
index 6dd091b5b31d..2d4c292c7510 100644
--- a/lldb/source/Target/Platform.cpp
+++ b/lldb/source/Target/Platform.cpp
@@ -493,8 +493,11 @@ llvm::Optional<std::string> Platform::GetOSBuildString() {
 }
 
 bool Platform::GetOSKernelDescription(std::string &s) {
-  if (IsHost())
-    return HostInfo::GetOSKernelDescription(s);
+  if (IsHost()) {
+    llvm::Optional<std::string> desc = HostInfo::GetOSKernelDescription();
+    s = desc.getValueOr("");
+    return desc.hasValue();
+  }
   return GetRemoteOSKernelDescription(s);
 }
 


        


More information about the lldb-commits mailing list