Host::GetOSBuildString
authorPavel Labath <pavel@labath.sk>
Tue, 19 Oct 2021 14:00:31 +0000 (16:00 +0200)
committerPavel Labath <pavel@labath.sk>
Fri, 22 Oct 2021 10:59:58 +0000 (12:59 +0200)
12 files changed:
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/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/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
lldb/source/Target/Platform.cpp

index 56f20bb..8207e90 100644 (file)
@@ -18,7 +18,7 @@ namespace lldb_private {
 class HostInfoFreeBSD : public HostInfoPosix {
 public:
   static llvm::VersionTuple GetOSVersion();
-  static bool GetOSBuildString(std::string &s);
+  static llvm::Optional<std::string> GetOSBuildString();
   static bool GetOSKernelDescription(std::string &s);
   static FileSpec GetProgramFileSpec();
 };
index 3220046..6287670 100644 (file)
@@ -26,7 +26,7 @@ public:
   static void Terminate();
 
   static llvm::VersionTuple GetOSVersion();
-  static bool GetOSBuildString(std::string &s);
+  static llvm::Optional<std::string> GetOSBuildString();
   static bool GetOSKernelDescription(std::string &s);
   static llvm::StringRef GetDistributionId();
   static FileSpec GetProgramFileSpec();
index 4623932..42c2872 100644 (file)
@@ -24,7 +24,7 @@ class HostInfoMacOSX : public HostInfoPosix {
 public:
   static llvm::VersionTuple GetOSVersion();
   static llvm::VersionTuple GetMacCatalystVersion();
-  static bool GetOSBuildString(std::string &s);
+  static llvm::Optional<std::string> GetOSBuildString();
   static bool GetOSKernelDescription(std::string &s);
   static FileSpec GetProgramFileSpec();
   static FileSpec GetXcodeContentsDirectory();
index f9ad66e..021f862 100644 (file)
@@ -18,7 +18,7 @@ namespace lldb_private {
 class HostInfoNetBSD : public HostInfoPosix {
 public:
   static llvm::VersionTuple GetOSVersion();
-  static bool GetOSBuildString(std::string &s);
+  static llvm::Optional<std::string> GetOSBuildString();
   static bool GetOSKernelDescription(std::string &s);
   static FileSpec GetProgramFileSpec();
 };
index 7ec1d5f..ba5ac8c 100644 (file)
@@ -18,7 +18,7 @@ namespace lldb_private {
 class HostInfoOpenBSD : public HostInfoPosix {
 public:
   static llvm::VersionTuple GetOSVersion();
-  static bool GetOSBuildString(std::string &s);
+  static llvm::Optional<std::string> GetOSBuildString();
   static bool GetOSKernelDescription(std::string &s);
   static FileSpec GetProgramFileSpec();
 };
index f01113e..d7f9e68 100644 (file)
@@ -27,7 +27,7 @@ public:
   static UserIDResolver &GetUserIDResolver();
 
   static llvm::VersionTuple GetOSVersion();
-  static bool GetOSBuildString(std::string &s);
+  static llvm::Optional<std::string> GetOSBuildString();
   static bool GetOSKernelDescription(std::string &s);
   static bool GetHostname(std::string &s);
   static FileSpec GetProgramFileSpec();
index 1b9e3cc..22af5bd 100644 (file)
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Host/freebsd/HostInfoFreeBSD.h"
-
+#include "llvm/Support/FormatVariadic.h"
 #include <cstdio>
 #include <cstring>
 #include <sys/sysctl.h>
@@ -30,20 +30,16 @@ llvm::VersionTuple HostInfoFreeBSD::GetOSVersion() {
   return llvm::VersionTuple();
 }
 
-bool HostInfoFreeBSD::GetOSBuildString(std::string &s) {
+llvm::Optional<std::string> HostInfoFreeBSD::GetOSBuildString() {
   int mib[2] = {CTL_KERN, KERN_OSREV};
   char osrev_str[12];
   uint32_t osrev = 0;
   size_t osrev_len = sizeof(osrev);
 
-  if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0) {
-    ::snprintf(osrev_str, sizeof(osrev_str), "%-8.8u", osrev);
-    s.assign(osrev_str);
-    return true;
-  }
+  if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0)
+    return llvm::formatv("{0,8:8}", osrev).str();
 
-  s.clear();
-  return false;
+  return llvm::None;
 }
 
 bool HostInfoFreeBSD::GetOSKernelDescription(std::string &s) {
index 13dcc5a..32642ed 100644 (file)
@@ -65,16 +65,14 @@ llvm::VersionTuple HostInfoLinux::GetOSVersion() {
   return g_fields->m_os_version;
 }
 
-bool HostInfoLinux::GetOSBuildString(std::string &s) {
+llvm::Optional<std::string> HostInfoLinux::GetOSBuildString() {
   struct utsname un;
   ::memset(&un, 0, sizeof(utsname));
-  s.clear();
 
   if (uname(&un) < 0)
-    return false;
+    return llvm::None;
 
-  s.assign(un.release);
-  return true;
+  return std::string(un.release);
 }
 
 bool HostInfoLinux::GetOSKernelDescription(std::string &s) {
index a0706ec..e32cfb0 100644 (file)
 
 using namespace lldb_private;
 
-bool HostInfoMacOSX::GetOSBuildString(std::string &s) {
+llvm::Optional<std::string> HostInfoMacOSX::GetOSBuildString() {
   int mib[2] = {CTL_KERN, KERN_OSVERSION};
   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;
-  }
+  if (::sysctl(mib, 2, cstr, &cstr_len, NULL, 0) == 0)
+    return std::string(cstr, cstr_len);
 
-  s.clear();
-  return false;
+  return llvm::None;
 }
 
 bool HostInfoMacOSX::GetOSKernelDescription(std::string &s) {
index bddd46c..42bfb20 100644 (file)
@@ -42,20 +42,16 @@ llvm::VersionTuple HostInfoNetBSD::GetOSVersion() {
   return llvm::VersionTuple();
 }
 
-bool HostInfoNetBSD::GetOSBuildString(std::string &s) {
+llvm::Optional<std::string> HostInfoNetBSD::GetOSBuildString() {
   int mib[2] = {CTL_KERN, KERN_OSREV};
   char osrev_str[12];
   int osrev = 0;
   size_t osrev_len = sizeof(osrev);
 
-  if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0) {
-    ::snprintf(osrev_str, sizeof(osrev_str), "%-10.10d", osrev);
-    s.assign(osrev_str);
-    return true;
-  }
+  if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0)
+    return llvm::formatv("{0,10:10}", osrev).str();
 
-  s.clear();
-  return false;
+  return llvm::None;
 }
 
 bool HostInfoNetBSD::GetOSKernelDescription(std::string &s) {
index d4a753d..17b7f17 100644 (file)
@@ -267,12 +267,12 @@ GDBRemoteCommunicationServerCommon::Handle_qHostInfo(
   }
 #endif
 
-  std::string s;
-  if (HostInfo::GetOSBuildString(s)) {
+  if (llvm::Optional<std::string> s = HostInfo::GetOSBuildString()) {
     response.PutCString("os_build:");
-    response.PutStringAsRawHex8(s);
+    response.PutStringAsRawHex8(*s);
     response.PutChar(';');
   }
+  std::string s;
   if (HostInfo::GetOSKernelDescription(s)) {
     response.PutCString("os_kernel:");
     response.PutStringAsRawHex8(s);
index 9c57689..0f2eee8 100644 (file)
@@ -487,10 +487,12 @@ llvm::VersionTuple Platform::GetOSVersion(Process *process) {
 }
 
 bool Platform::GetOSBuildString(std::string &s) {
+  if (IsHost()) {
+    llvm::Optional<std::string> str = HostInfo::GetOSBuildString();
+    s = str.getValueOr("");
+    return str.hasValue();
+  }
   s.clear();
-
-  if (IsHost())
-    return HostInfo::GetOSBuildString(s);
   return GetRemoteOSBuildString(s);
 }