[Lldb-commits] [lldb] ee60f7c - [lldb] Hoist UUID generation into the UUID class (#133662)
via lldb-commits
lldb-commits at lists.llvm.org
Wed Apr 2 08:50:32 PDT 2025
Author: Jonas Devlieghere
Date: 2025-04-02T08:50:28-07:00
New Revision: ee60f7c7a4e4e400918c83decb6cd498aff94012
URL: https://github.com/llvm/llvm-project/commit/ee60f7c7a4e4e400918c83decb6cd498aff94012
DIFF: https://github.com/llvm/llvm-project/commit/ee60f7c7a4e4e400918c83decb6cd498aff94012.diff
LOG: [lldb] Hoist UUID generation into the UUID class (#133662)
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.
Added:
Modified:
lldb/include/lldb/Utility/UUID.h
lldb/source/Core/Telemetry.cpp
lldb/source/Utility/UUID.cpp
lldb/unittests/Utility/UUIDTest.cpp
Removed:
################################################################################
diff --git a/lldb/include/lldb/Utility/UUID.h b/lldb/include/lldb/Utility/UUID.h
index bc4b4acd5a7d8..b0356dbf4c144 100644
--- a/lldb/include/lldb/Utility/UUID.h
+++ b/lldb/include/lldb/Utility/UUID.h
@@ -12,26 +12,27 @@
#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>
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;
-
- /// 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();
- }
+ }
}
// Reference:
@@ -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.
+ /// Create 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);
+ /// Create a random UUID.
+ static UUID Generate(uint32_t num_bytes = 16);
+
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..8db29889e0846 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,9 @@ 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();
- }
-
- return llvm::formatv(
- "{0}_{1}", randomString,
- std::chrono::steady_clock::now().time_since_epoch().count());
+ auto timestmap = std::chrono::steady_clock::now().time_since_epoch().count();
+ UUID uuid = UUID::Generate();
+ return llvm::formatv("{0}_{1}", 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..0af0c3837627f 100644
--- a/lldb/source/Utility/UUID.cpp
+++ b/lldb/source/Utility/UUID.cpp
@@ -11,10 +11,15 @@
#include "lldb/Utility/Stream.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Format.h"
+#include "llvm/Support/RandomNumberGenerator.h"
#include <cctype>
+#include <chrono>
+#include <climits>
+#include <cstdint>
#include <cstdio>
#include <cstring>
+#include <random>
using namespace lldb_private;
@@ -110,3 +115,18 @@ bool UUID::SetFromStringRef(llvm::StringRef str) {
*this = UUID(bytes);
return true;
}
+
+UUID UUID::Generate(uint32_t num_bytes) {
+ llvm::SmallVector<uint8_t, 20> bytes(num_bytes);
+ auto ec = llvm::getRandomBytes(bytes.data(), bytes.size());
+
+ // If getRandomBytes failed, fall back to a lower entropy source.
+ if (ec) {
+ auto seed = std::chrono::steady_clock::now().time_since_epoch().count();
+ std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>
+ engine(seed);
+ std::generate(bytes.begin(), bytes.end(), std::ref(engine));
+ }
+
+ return UUID(bytes);
+}
diff --git a/lldb/unittests/Utility/UUIDTest.cpp b/lldb/unittests/Utility/UUIDTest.cpp
index 0852f07a4faa2..ad0228c6bba69 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,11 @@ TEST(UUIDTest, StringConverion) {
EXPECT_EQ("40414243-4445-4647-4849-4A4B4C4D4E4F-50515253",
UUID("@ABCDEFGHIJKLMNOPQRS", 20).GetAsString());
}
+
+TEST(UUIDTest, Generate) {
+ UUID u16 = UUID::Generate();
+ EXPECT_EQ(u16.GetBytes().size(), 16UL);
+
+ UUID u20 = UUID::Generate(20);
+ EXPECT_EQ(u20.GetBytes().size(), 20UL);
+}
More information about the lldb-commits
mailing list