LLDB mainline
UUID.h
Go to the documentation of this file.
1//===-- UUID.h --------------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_UTILITY_UUID_H
10#define LLDB_UTILITY_UUID_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/Support/Endian.h"
15#include "llvm/Support/Error.h"
16#include <cstddef>
17#include <cstdint>
18#include <string>
19
20namespace lldb_private {
21
22class Stream;
23
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.
27class UUID {
28public:
29 UUID() = default;
30
31 /// Create a uuid from the data pointed to by the bytes argument.
32 UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes) {
33 if (llvm::all_of(m_bytes, [](uint8_t b) { return b == 0; })) {
34 Clear();
35 }
36 }
37
38 // Reference:
39 // https://crashpad.chromium.org/doxygen/structcrashpad_1_1CodeViewRecordPDB70.html
41 struct {
42 llvm::support::ulittle32_t Data1;
43 llvm::support::ulittle16_t Data2;
44 llvm::support::ulittle16_t Data3;
45 uint8_t Data4[8];
47 llvm::support::ulittle32_t Age;
48 // char PDBFileName[];
49 };
50
51 /// Create a UUID from CvRecordPdb70.
52 UUID(CvRecordPdb70 debug_info);
53
54 /// Create a UUID from the data pointed to by the bytes argument.
55 UUID(const void *bytes, uint32_t num_bytes) {
56 if (!bytes)
57 return;
58 *this = UUID(llvm::ArrayRef<uint8_t>(
59 reinterpret_cast<const uint8_t *>(bytes), num_bytes));
60 }
61
62 void Clear() { m_bytes.clear(); }
63
64 void Dump(Stream &s) const;
65
66 llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
67
68 explicit operator bool() const { return IsValid(); }
69 bool IsValid() const { return !m_bytes.empty(); }
70
71 std::string GetAsString(llvm::StringRef separator = "-") const;
72
73 bool SetFromStringRef(llvm::StringRef str);
74
75 /// Decode as many UUID bytes as possible from the C string \a cstr.
76 ///
77 /// \param[in] str
78 /// An llvm::StringRef that points at a UUID string value (no leading
79 /// spaces). The string must contain only hex characters and optionally
80 /// can contain the '-' sepearators.
81 ///
82 /// \param[in] uuid_bytes
83 /// A buffer of bytes that will contain a full or partially decoded UUID.
84 ///
85 /// \return
86 /// The original string, with all decoded bytes removed.
87 static llvm::StringRef
88 DecodeUUIDBytesFromString(llvm::StringRef str,
90
91 /// Create a random UUID.
92 static UUID Generate(uint32_t num_bytes = 16);
93
94private:
95 // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
96 // for this case.
97 llvm::SmallVector<uint8_t, 20> m_bytes;
98
99 friend bool operator==(const UUID &LHS, const UUID &RHS) {
100 return LHS.m_bytes == RHS.m_bytes;
101 }
102 friend bool operator!=(const UUID &LHS, const UUID &RHS) {
103 return !(LHS == RHS);
104 }
105 friend bool operator<(const UUID &LHS, const UUID &RHS) {
106 return LHS.m_bytes < RHS.m_bytes;
107 }
108 friend bool operator<=(const UUID &LHS, const UUID &RHS) {
109 return !(RHS < LHS);
110 }
111 friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
112 friend bool operator>=(const UUID &LHS, const UUID &RHS) {
113 return !(LHS < RHS);
114 }
115};
116} // namespace lldb_private
117
118#endif // LLDB_UTILITY_UUID_H
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Represents UUID's of various sizes.
Definition UUID.h:27
bool SetFromStringRef(llvm::StringRef str)
Definition UUID.cpp:101
void Dump(Stream &s) const
Definition UUID.cpp:68
friend bool operator!=(const UUID &LHS, const UUID &RHS)
Definition UUID.h:102
friend bool operator==(const UUID &LHS, const UUID &RHS)
Definition UUID.h:99
llvm::ArrayRef< uint8_t > GetBytes() const
Definition UUID.h:66
friend bool operator<(const UUID &LHS, const UUID &RHS)
Definition UUID.h:105
static UUID Generate(uint32_t num_bytes=16)
Create a random UUID.
Definition UUID.cpp:119
void Clear()
Definition UUID.h:62
std::string GetAsString(llvm::StringRef separator="-") const
Definition UUID.cpp:54
friend bool operator<=(const UUID &LHS, const UUID &RHS)
Definition UUID.h:108
llvm::SmallVector< uint8_t, 20 > m_bytes
Definition UUID.h:97
UUID(llvm::ArrayRef< uint8_t > bytes)
Create a uuid from the data pointed to by the bytes argument.
Definition UUID.h:32
bool IsValid() const
Definition UUID.h:69
friend bool operator>(const UUID &LHS, const UUID &RHS)
Definition UUID.h:111
friend bool operator>=(const UUID &LHS, const UUID &RHS)
Definition UUID.h:112
UUID(const void *bytes, uint32_t num_bytes)
Create a UUID from the data pointed to by the bytes argument.
Definition UUID.h:55
static llvm::StringRef DecodeUUIDBytesFromString(llvm::StringRef str, llvm::SmallVectorImpl< uint8_t > &uuid_bytes)
Decode as many UUID bytes as possible from the C string cstr.
Definition UUID.cpp:78
A class that represents a running process on the host machine.
llvm::support::ulittle16_t Data2
Definition UUID.h:43
llvm::support::ulittle32_t Data1
Definition UUID.h:42
llvm::support::ulittle16_t Data3
Definition UUID.h:44
struct lldb_private::UUID::CvRecordPdb70::@031031370151201011001273071037042037235035311007 Uuid
llvm::support::ulittle32_t Age
Definition UUID.h:47