Skip to content

Commit ee60f7c

Browse files
authored
[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.
1 parent c132bd6 commit ee60f7c

File tree

4 files changed

+51
-26
lines changed

4 files changed

+51
-26
lines changed

lldb/include/lldb/Utility/UUID.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@
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>
1819

1920
namespace lldb_private {
2021

21-
class Stream;
22+
class Stream;
2223

24+
/// Represents UUID's of various sizes. In all cases, a uuid of all zeros is
25+
/// treated as an "Invalid UUID" marker, and the UUID created from such data
26+
/// will return false for IsValid.
2327
class UUID {
24-
// Represents UUID's of various sizes. In all cases, a uuid of all zeros is
25-
// treated as an "Invalid UUID" marker, and the UUID created from such data
26-
// will return false for IsValid.
2728
public:
2829
UUID() = default;
29-
30-
/// Creates a uuid from the data pointed to by the bytes argument.
30+
31+
/// Create 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; })) {
3334
Clear();
34-
}
35+
}
3536
}
3637

3738
// Reference:
@@ -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+
/// Create 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+
/// Create a random UUID.
92+
static UUID Generate(uint32_t num_bytes = 16);
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: 6 additions & 12 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,9 @@ 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();
47-
}
48-
49-
return llvm::formatv(
50-
"{0}_{1}", randomString,
51-
std::chrono::steady_clock::now().time_since_epoch().count());
43+
auto timestmap = std::chrono::steady_clock::now().time_since_epoch().count();
44+
UUID uuid = UUID::Generate();
45+
return llvm::formatv("{0}_{1}", uuid.GetAsString(), timestmap);
5246
}
5347

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

lldb/source/Utility/UUID.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
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>
17+
#include <chrono>
18+
#include <climits>
19+
#include <cstdint>
1620
#include <cstdio>
1721
#include <cstring>
22+
#include <random>
1823

1924
using namespace lldb_private;
2025

@@ -110,3 +115,18 @@ bool UUID::SetFromStringRef(llvm::StringRef str) {
110115
*this = UUID(bytes);
111116
return true;
112117
}
118+
119+
UUID UUID::Generate(uint32_t num_bytes) {
120+
llvm::SmallVector<uint8_t, 20> bytes(num_bytes);
121+
auto ec = llvm::getRandomBytes(bytes.data(), bytes.size());
122+
123+
// If getRandomBytes failed, fall back to a lower entropy source.
124+
if (ec) {
125+
auto seed = std::chrono::steady_clock::now().time_since_epoch().count();
126+
std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>
127+
engine(seed);
128+
std::generate(bytes.begin(), bytes.end(), std::ref(engine));
129+
}
130+
131+
return UUID(bytes);
132+
}

lldb/unittests/Utility/UUIDTest.cpp

Lines changed: 10 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,11 @@ TEST(UUIDTest, StringConverion) {
8686
EXPECT_EQ("40414243-4445-4647-4849-4A4B4C4D4E4F-50515253",
8787
UUID("@ABCDEFGHIJKLMNOPQRS", 20).GetAsString());
8888
}
89+
90+
TEST(UUIDTest, Generate) {
91+
UUID u16 = UUID::Generate();
92+
EXPECT_EQ(u16.GetBytes().size(), 16UL);
93+
94+
UUID u20 = UUID::Generate(20);
95+
EXPECT_EQ(u20.GetBytes().size(), 20UL);
96+
}

0 commit comments

Comments
 (0)