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 <cstddef>
16 #include <cstdint>
17 #include <string>
18 
19 namespace lldb_private {
20 
21  class Stream;
22 
23 class UUID {
24 public:
25  UUID() = default;
26 
27  // Reference:
28  // https://crashpad.chromium.org/doxygen/structcrashpad_1_1CodeViewRecordPDB70.html
29  struct CvRecordPdb70 {
30  struct {
31  llvm::support::ulittle32_t Data1;
32  llvm::support::ulittle16_t Data2;
33  llvm::support::ulittle16_t Data3;
34  uint8_t Data4[8];
35  } Uuid;
36  llvm::support::ulittle32_t Age;
37  // char PDBFileName[];
38  };
39 
40  /// Create a UUID from CvRecordPdb70.
41  static UUID fromCvRecord(CvRecordPdb70 debug_info);
42 
43  /// Creates a UUID from the data pointed to by the bytes argument. No special
44  /// significance is attached to any of the values.
45  static UUID fromData(const void *bytes, uint32_t num_bytes) {
46  if (bytes)
47  return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes});
48  return UUID();
49  }
50 
51  /// Creates a uuid from the data pointed to by the bytes argument. No special
52  /// significance is attached to any of the values.
53  static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); }
54 
55  /// Creates a UUID from the data pointed to by the bytes argument. Data
56  /// consisting purely of zero bytes is treated as an invalid UUID.
57  static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) {
58  if (bytes)
59  return fromOptionalData(
60  {reinterpret_cast<const uint8_t *>(bytes), num_bytes});
61  return UUID();
62  }
63 
64  /// Creates a UUID from the data pointed to by the bytes argument. Data
65  /// consisting purely of zero bytes is treated as an invalid UUID.
66  static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) {
67  if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; }))
68  return UUID();
69  return UUID(bytes);
70  }
71 
72  void Clear() { m_bytes.clear(); }
73 
74  void Dump(Stream *s) const;
75 
76  llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
77 
78  explicit operator bool() const { return IsValid(); }
79  bool IsValid() const { return !m_bytes.empty(); }
80 
81  std::string GetAsString(llvm::StringRef separator = "-") const;
82 
83  bool SetFromStringRef(llvm::StringRef str);
84 
85  // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the
86  // UUID to invalid.
87  bool SetFromOptionalStringRef(llvm::StringRef str);
88 
89  /// Decode as many UUID bytes as possible from the C string \a cstr.
90  ///
91  /// \param[in] str
92  /// An llvm::StringRef that points at a UUID string value (no leading
93  /// spaces). The string must contain only hex characters and optionally
94  /// can contain the '-' sepearators.
95  ///
96  /// \param[in] uuid_bytes
97  /// A buffer of bytes that will contain a full or partially decoded UUID.
98  ///
99  /// \return
100  /// The original string, with all decoded bytes removed.
101  static llvm::StringRef
102  DecodeUUIDBytesFromString(llvm::StringRef str,
103  llvm::SmallVectorImpl<uint8_t> &uuid_bytes);
104 
105 private:
106  UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {}
107 
108  // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
109  // for this case.
110  llvm::SmallVector<uint8_t, 20> m_bytes;
111 
112  friend bool operator==(const UUID &LHS, const UUID &RHS) {
113  return LHS.m_bytes == RHS.m_bytes;
114  }
115  friend bool operator!=(const UUID &LHS, const UUID &RHS) {
116  return !(LHS == RHS);
117  }
118  friend bool operator<(const UUID &LHS, const UUID &RHS) {
119  return LHS.m_bytes < RHS.m_bytes;
120  }
121  friend bool operator<=(const UUID &LHS, const UUID &RHS) {
122  return !(RHS < LHS);
123  }
124  friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
125  friend bool operator>=(const UUID &LHS, const UUID &RHS) {
126  return !(LHS < RHS);
127  }
128 };
129 } // namespace lldb_private
130 
131 #endif // LLDB_UTILITY_UUID_H
lldb_private::UUID::CvRecordPdb70::Age
llvm::support::ulittle32_t Age
Definition: UUID.h:36
lldb_private::UUID::fromData
static UUID fromData(const void *bytes, uint32_t num_bytes)
Creates a UUID from the data pointed to by the bytes argument.
Definition: UUID.h:45
lldb_private::UUID
Definition: UUID.h:23
lldb_private::UUID::CvRecordPdb70::Data3
llvm::support::ulittle16_t Data3
Definition: UUID.h:33
lldb_private::UUID::operator<
friend bool operator<(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:118
lldb_private::UUID::CvRecordPdb70::Uuid
struct lldb_private::UUID::CvRecordPdb70::@45 Uuid
lldb_private::UUID::IsValid
bool IsValid() const
Definition: UUID.h:79
lldb_private::Stream
Definition: Stream.h:28
lldb_private::UUID::CvRecordPdb70::Data2
llvm::support::ulittle16_t Data2
Definition: UUID.h:32
lldb_private::UUID::UUID
UUID()=default
lldb_private::UUID::GetAsString
std::string GetAsString(llvm::StringRef separator="-") const
Definition: UUID.cpp:48
lldb_private::UUID::fromData
static UUID fromData(llvm::ArrayRef< uint8_t > bytes)
Creates a uuid from the data pointed to by the bytes argument.
Definition: UUID.h:53
lldb_private::UUID::CvRecordPdb70::Data4
uint8_t Data4[8]
Definition: UUID.h:34
lldb_private::UUID::operator>=
friend bool operator>=(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:125
lldb_private::UUID::UUID
UUID(llvm::ArrayRef< uint8_t > bytes)
Definition: UUID.h:106
lldb_private::UUID::operator==
friend bool operator==(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:112
lldb_private::UUID::fromOptionalData
static UUID fromOptionalData(llvm::ArrayRef< uint8_t > bytes)
Creates a UUID from the data pointed to by the bytes argument.
Definition: UUID.h:66
lldb_private::UUID::CvRecordPdb70::Data1
llvm::support::ulittle32_t Data1
Definition: UUID.h:31
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:39
lldb_private::UUID::Clear
void Clear()
Definition: UUID.h:72
lldb_private::UUID::operator<=
friend bool operator<=(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:121
lldb_private::UUID::operator!=
friend bool operator!=(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:115
lldb_private::UUID::Dump
void Dump(Stream *s) const
Definition: UUID.cpp:63
uint32_t
lldb_private::UUID::CvRecordPdb70
Definition: UUID.h:29
lldb_private::UUID::SetFromStringRef
bool SetFromStringRef(llvm::StringRef str)
Definition: UUID.cpp:96
lldb_private::UUID::DecodeUUIDBytesFromString
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:73
lldb_private::UUID::operator>
friend bool operator>(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:124
lldb_private::UUID::fromCvRecord
static UUID fromCvRecord(CvRecordPdb70 debug_info)
Create a UUID from CvRecordPdb70.
Definition: UUID.cpp:38
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::UUID::m_bytes
llvm::SmallVector< uint8_t, 20 > m_bytes
Definition: UUID.h:110
llvm::SmallVectorImpl
Definition: Disassembler.h:42
lldb_private::UUID::fromOptionalData
static UUID fromOptionalData(const void *bytes, uint32_t num_bytes)
Creates a UUID from the data pointed to by the bytes argument.
Definition: UUID.h:57
lldb_private::UUID::SetFromOptionalStringRef
bool SetFromOptionalStringRef(llvm::StringRef str)
Definition: UUID.cpp:114
lldb_private::UUID::GetBytes
llvm::ArrayRef< uint8_t > GetBytes() const
Definition: UUID.h:76