[Lldb-commits] [lldb] [lldb] Hoist UUID generation into the UUID class (PR #133662)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Sun Mar 30 21:56:38 PDT 2025


https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/133662

>From 15e07ce18961e63c22510fa34c7baa5b5985dfe7 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Sun, 30 Mar 2025 16:10:05 -0700
Subject: [PATCH 1/3] [lldb] Hoist UUID generation into the UUID class

Hoist UUID generation into the UUID class and add a trivial unit test.
This also changes the telemetry code to drop the double underscore if we
failed to generate a UUID and subsequently logs to the Host instead of
Object log channel.
---
 lldb/include/lldb/Utility/UUID.h    | 15 +++++++++------
 lldb/source/Core/Telemetry.cpp      | 21 +++++++++++----------
 lldb/source/Utility/UUID.cpp        |  8 ++++++++
 lldb/unittests/Utility/UUIDTest.cpp |  8 ++++++--
 4 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/lldb/include/lldb/Utility/UUID.h b/lldb/include/lldb/Utility/UUID.h
index bc4b4acd5a7d8..42c8844148776 100644
--- a/lldb/include/lldb/Utility/UUID.h
+++ b/lldb/include/lldb/Utility/UUID.h
@@ -12,6 +12,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Endian.h"
+#include "llvm/Support/Error.h"
 #include <cstddef>
 #include <cstdint>
 #include <string>
@@ -26,7 +27,7 @@ class UUID {
   // will return false for IsValid.
 public:
   UUID() = default;
-  
+
   /// Creates a uuid from the data pointed to by the bytes argument.
   UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes) {
     if (llvm::all_of(m_bytes, [](uint8_t b) { return b == 0; })) {
@@ -50,13 +51,12 @@ class UUID {
   /// Create a UUID from CvRecordPdb70.
   UUID(CvRecordPdb70 debug_info);
 
-  /// Creates a UUID from the data pointed to by the bytes argument. 
+  /// Creates a UUID from the data pointed to by the bytes argument.
   UUID(const void *bytes, uint32_t num_bytes) {
     if (!bytes)
       return;
-    *this 
-        = UUID(llvm::ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(bytes), 
-               num_bytes));
+    *this = UUID(llvm::ArrayRef<uint8_t>(
+        reinterpret_cast<const uint8_t *>(bytes), num_bytes));
   }
 
   void Clear() { m_bytes.clear(); }
@@ -67,7 +67,7 @@ class UUID {
 
   explicit operator bool() const { return IsValid(); }
   bool IsValid() const { return !m_bytes.empty(); }
-  
+
   std::string GetAsString(llvm::StringRef separator = "-") const;
 
   bool SetFromStringRef(llvm::StringRef str);
@@ -88,6 +88,9 @@ class UUID {
   DecodeUUIDBytesFromString(llvm::StringRef str,
                             llvm::SmallVectorImpl<uint8_t> &uuid_bytes);
 
+  /// Generate a random UUID.
+  static llvm::Expected<UUID> Generate();
+
 private:
   // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
   // for this case.
diff --git a/lldb/source/Core/Telemetry.cpp b/lldb/source/Core/Telemetry.cpp
index c7789d43c7899..e9ba7d1845bb4 100644
--- a/lldb/source/Core/Telemetry.cpp
+++ b/lldb/source/Core/Telemetry.cpp
@@ -9,15 +9,18 @@
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/Telemetry.h"
 #include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
 #include "lldb/Utility/UUID.h"
 #include "lldb/lldb-enumerations.h"
 #include "lldb/lldb-forward.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/Format.h"
 #include "llvm/Support/RandomNumberGenerator.h"
 #include "llvm/Telemetry/Telemetry.h"
 #include <chrono>
 #include <cstdlib>
+#include <ctime>
 #include <memory>
 #include <string>
 #include <utility>
@@ -37,18 +40,16 @@ static uint64_t ToNanosec(const SteadyTimePoint Point) {
 // This reduces the chances of getting the same UUID, even when the same
 // user runs the two copies of binary at the same time.
 static std::string MakeUUID() {
-  uint8_t random_bytes[16];
-  std::string randomString = "_";
-  if (auto ec = llvm::getRandomBytes(random_bytes, 16)) {
-    LLDB_LOG(GetLog(LLDBLog::Object),
-             "Failed to generate random bytes for UUID: {0}", ec.message());
-  } else {
-    randomString = UUID(random_bytes).GetAsString();
+  auto timestmap = std::chrono::steady_clock::now().time_since_epoch().count();
+
+  llvm::Expected<UUID> maybe_uuid = UUID::Generate();
+  if (!maybe_uuid) {
+    LLDB_LOG_ERROR(GetLog(LLDBLog::Host), maybe_uuid.takeError(),
+                   "Failed to generate random bytes for UUID: {0}");
+    return llvm::formatv("{0}", timestmap);
   }
 
-  return llvm::formatv(
-      "{0}_{1}", randomString,
-      std::chrono::steady_clock::now().time_since_epoch().count());
+  return llvm::formatv("{0}_{1}", maybe_uuid->GetAsString(), timestmap);
 }
 
 void LLDBBaseTelemetryInfo::serialize(Serializer &serializer) const {
diff --git a/lldb/source/Utility/UUID.cpp b/lldb/source/Utility/UUID.cpp
index 370b8b6848c7e..80c829e40b2c9 100644
--- a/lldb/source/Utility/UUID.cpp
+++ b/lldb/source/Utility/UUID.cpp
@@ -11,6 +11,7 @@
 #include "lldb/Utility/Stream.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Format.h"
+#include "llvm/Support/RandomNumberGenerator.h"
 
 #include <cctype>
 #include <cstdio>
@@ -110,3 +111,10 @@ bool UUID::SetFromStringRef(llvm::StringRef str) {
   *this = UUID(bytes);
   return true;
 }
+
+llvm::Expected<UUID> UUID::Generate() {
+  uint8_t random_bytes[16];
+  if (auto ec = llvm::getRandomBytes(random_bytes, 16))
+    return llvm::errorCodeToError(ec);
+  return UUID(random_bytes);
+}
diff --git a/lldb/unittests/Utility/UUIDTest.cpp b/lldb/unittests/Utility/UUIDTest.cpp
index 0852f07a4faa2..66fef4ddc3763 100644
--- a/lldb/unittests/Utility/UUIDTest.cpp
+++ b/lldb/unittests/Utility/UUIDTest.cpp
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "gtest/gtest.h"
-
 #include "lldb/Utility/UUID.h"
+#include "llvm/Testing/Support/Error.h"
+#include "gtest/gtest.h"
 
 using namespace lldb_private;
 
@@ -86,3 +86,7 @@ TEST(UUIDTest, StringConverion) {
   EXPECT_EQ("40414243-4445-4647-4849-4A4B4C4D4E4F-50515253",
             UUID("@ABCDEFGHIJKLMNOPQRS", 20).GetAsString());
 }
+
+TEST(UUIDTest, Generate) {
+  ASSERT_THAT_EXPECTED(UUID::Generate(), llvm::Succeeded());
+}

>From 49933b294aa00e0abbfb08cd86b4b60adf65167f Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Sun, 30 Mar 2025 21:48:08 -0700
Subject: [PATCH 2/3] Change comments to be imperative

---
 lldb/include/lldb/Utility/UUID.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lldb/include/lldb/Utility/UUID.h b/lldb/include/lldb/Utility/UUID.h
index 42c8844148776..4bd0499ed288b 100644
--- a/lldb/include/lldb/Utility/UUID.h
+++ b/lldb/include/lldb/Utility/UUID.h
@@ -28,7 +28,7 @@ class UUID {
 public:
   UUID() = default;
 
-  /// Creates a uuid from the data pointed to by the bytes argument.
+  /// Create a uuid from the data pointed to by the bytes argument.
   UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes) {
     if (llvm::all_of(m_bytes, [](uint8_t b) { return b == 0; })) {
       Clear();
@@ -51,7 +51,7 @@ class UUID {
   /// Create a UUID from CvRecordPdb70.
   UUID(CvRecordPdb70 debug_info);
 
-  /// Creates a UUID from the data pointed to by the bytes argument.
+  /// Create a UUID from the data pointed to by the bytes argument.
   UUID(const void *bytes, uint32_t num_bytes) {
     if (!bytes)
       return;
@@ -88,7 +88,7 @@ class UUID {
   DecodeUUIDBytesFromString(llvm::StringRef str,
                             llvm::SmallVectorImpl<uint8_t> &uuid_bytes);
 
-  /// Generate a random UUID.
+  /// Create a random UUID.
   static llvm::Expected<UUID> Generate();
 
 private:

>From d6c438b9e66109fb5864975055890e249147f614 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Sun, 30 Mar 2025 21:56:24 -0700
Subject: [PATCH 3/3] Fix remainign comments, explicitly state 16 bytes

---
 lldb/include/lldb/Utility/UUID.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/lldb/include/lldb/Utility/UUID.h b/lldb/include/lldb/Utility/UUID.h
index 4bd0499ed288b..f228c1530a9ec 100644
--- a/lldb/include/lldb/Utility/UUID.h
+++ b/lldb/include/lldb/Utility/UUID.h
@@ -19,12 +19,12 @@
 
 namespace lldb_private {
 
-  class Stream;
+class Stream;
 
+/// Represents UUID's of various sizes.  In all cases, a uuid of all zeros is
+/// treated as an "Invalid UUID" marker, and the UUID created from such data
+/// will return false for IsValid.
 class UUID {
-  // Represents UUID's of various sizes.  In all cases, a uuid of all zeros is
-  // treated as an "Invalid UUID" marker, and the UUID created from such data
-  // will return false for IsValid.
 public:
   UUID() = default;
 
@@ -32,7 +32,7 @@ class UUID {
   UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes) {
     if (llvm::all_of(m_bytes, [](uint8_t b) { return b == 0; })) {
       Clear();
-   }
+    }
   }
 
   // Reference:
@@ -88,7 +88,7 @@ class UUID {
   DecodeUUIDBytesFromString(llvm::StringRef str,
                             llvm::SmallVectorImpl<uint8_t> &uuid_bytes);
 
-  /// Create a random UUID.
+  /// Create a random 16 byte UUID.
   static llvm::Expected<UUID> Generate();
 
 private:



More information about the lldb-commits mailing list