Skip to content

Commit 15e07ce

Browse files
committed
[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.
1 parent 2796e41 commit 15e07ce

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

lldb/include/lldb/Utility/UUID.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/ADT/ArrayRef.h"
1313
#include "llvm/ADT/StringRef.h"
1414
#include "llvm/Support/Endian.h"
15+
#include "llvm/Support/Error.h"
1516
#include <cstddef>
1617
#include <cstdint>
1718
#include <string>
@@ -26,7 +27,7 @@ class UUID {
2627
// will return false for IsValid.
2728
public:
2829
UUID() = default;
29-
30+
3031
/// Creates a uuid from the data pointed to by the bytes argument.
3132
UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes) {
3233
if (llvm::all_of(m_bytes, [](uint8_t b) { return b == 0; })) {
@@ -50,13 +51,12 @@ class UUID {
5051
/// Create a UUID from CvRecordPdb70.
5152
UUID(CvRecordPdb70 debug_info);
5253

53-
/// Creates a UUID from the data pointed to by the bytes argument.
54+
/// Creates a UUID from the data pointed to by the bytes argument.
5455
UUID(const void *bytes, uint32_t num_bytes) {
5556
if (!bytes)
5657
return;
57-
*this
58-
= UUID(llvm::ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(bytes),
59-
num_bytes));
58+
*this = UUID(llvm::ArrayRef<uint8_t>(
59+
reinterpret_cast<const uint8_t *>(bytes), num_bytes));
6060
}
6161

6262
void Clear() { m_bytes.clear(); }
@@ -67,7 +67,7 @@ class UUID {
6767

6868
explicit operator bool() const { return IsValid(); }
6969
bool IsValid() const { return !m_bytes.empty(); }
70-
70+
7171
std::string GetAsString(llvm::StringRef separator = "-") const;
7272

7373
bool SetFromStringRef(llvm::StringRef str);
@@ -88,6 +88,9 @@ class UUID {
8888
DecodeUUIDBytesFromString(llvm::StringRef str,
8989
llvm::SmallVectorImpl<uint8_t> &uuid_bytes);
9090

91+
/// Generate a random UUID.
92+
static llvm::Expected<UUID> Generate();
93+
9194
private:
9295
// GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
9396
// for this case.

lldb/source/Core/Telemetry.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
#include "lldb/Core/Debugger.h"
1010
#include "lldb/Core/Telemetry.h"
1111
#include "lldb/Utility/LLDBLog.h"
12+
#include "lldb/Utility/Log.h"
1213
#include "lldb/Utility/UUID.h"
1314
#include "lldb/lldb-enumerations.h"
1415
#include "lldb/lldb-forward.h"
1516
#include "llvm/ADT/StringRef.h"
1617
#include "llvm/Support/Error.h"
18+
#include "llvm/Support/Format.h"
1719
#include "llvm/Support/RandomNumberGenerator.h"
1820
#include "llvm/Telemetry/Telemetry.h"
1921
#include <chrono>
2022
#include <cstdlib>
23+
#include <ctime>
2124
#include <memory>
2225
#include <string>
2326
#include <utility>
@@ -37,18 +40,16 @@ static uint64_t ToNanosec(const SteadyTimePoint Point) {
3740
// This reduces the chances of getting the same UUID, even when the same
3841
// user runs the two copies of binary at the same time.
3942
static std::string MakeUUID() {
40-
uint8_t random_bytes[16];
41-
std::string randomString = "_";
42-
if (auto ec = llvm::getRandomBytes(random_bytes, 16)) {
43-
LLDB_LOG(GetLog(LLDBLog::Object),
44-
"Failed to generate random bytes for UUID: {0}", ec.message());
45-
} else {
46-
randomString = UUID(random_bytes).GetAsString();
43+
auto timestmap = std::chrono::steady_clock::now().time_since_epoch().count();
44+
45+
llvm::Expected<UUID> maybe_uuid = UUID::Generate();
46+
if (!maybe_uuid) {
47+
LLDB_LOG_ERROR(GetLog(LLDBLog::Host), maybe_uuid.takeError(),
48+
"Failed to generate random bytes for UUID: {0}");
49+
return llvm::formatv("{0}", timestmap);
4750
}
4851

49-
return llvm::formatv(
50-
"{0}_{1}", randomString,
51-
std::chrono::steady_clock::now().time_since_epoch().count());
52+
return llvm::formatv("{0}_{1}", maybe_uuid->GetAsString(), timestmap);
5253
}
5354

5455
void LLDBBaseTelemetryInfo::serialize(Serializer &serializer) const {

lldb/source/Utility/UUID.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "lldb/Utility/Stream.h"
1212
#include "llvm/ADT/StringRef.h"
1313
#include "llvm/Support/Format.h"
14+
#include "llvm/Support/RandomNumberGenerator.h"
1415

1516
#include <cctype>
1617
#include <cstdio>
@@ -110,3 +111,10 @@ bool UUID::SetFromStringRef(llvm::StringRef str) {
110111
*this = UUID(bytes);
111112
return true;
112113
}
114+
115+
llvm::Expected<UUID> UUID::Generate() {
116+
uint8_t random_bytes[16];
117+
if (auto ec = llvm::getRandomBytes(random_bytes, 16))
118+
return llvm::errorCodeToError(ec);
119+
return UUID(random_bytes);
120+
}

lldb/unittests/Utility/UUIDTest.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "gtest/gtest.h"
10-
119
#include "lldb/Utility/UUID.h"
10+
#include "llvm/Testing/Support/Error.h"
11+
#include "gtest/gtest.h"
1212

1313
using namespace lldb_private;
1414

@@ -86,3 +86,7 @@ TEST(UUIDTest, StringConverion) {
8686
EXPECT_EQ("40414243-4445-4647-4849-4A4B4C4D4E4F-50515253",
8787
UUID("@ABCDEFGHIJKLMNOPQRS", 20).GetAsString());
8888
}
89+
90+
TEST(UUIDTest, Generate) {
91+
ASSERT_THAT_EXPECTED(UUID::Generate(), llvm::Succeeded());
92+
}

0 commit comments

Comments
 (0)