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  // 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.
27 public:
28  UUID() = default;
29 
30  /// Creates a uuid from the data pointed to by the bytes argument.
31  UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {
32  if (llvm::all_of(m_bytes, [](uint8_t b) { return b == 0; })) {
33  Clear();
34  }
35  }
36 
37  // Reference:
38  // https://crashpad.chromium.org/doxygen/structcrashpad_1_1CodeViewRecordPDB70.html
39  struct CvRecordPdb70 {
40  struct {
41  llvm::support::ulittle32_t Data1;
42  llvm::support::ulittle16_t Data2;
43  llvm::support::ulittle16_t Data3;
44  uint8_t Data4[8];
45  } Uuid;
46  llvm::support::ulittle32_t Age;
47  // char PDBFileName[];
48  };
49 
50  /// Create a UUID from CvRecordPdb70.
51  UUID(CvRecordPdb70 debug_info);
52 
53  /// Creates a UUID from the data pointed to by the bytes argument.
54  UUID(const void *bytes, uint32_t num_bytes) {
55  if (!bytes)
56  return;
57  *this
58  = UUID(llvm::ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(bytes),
59  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,
89  llvm::SmallVectorImpl<uint8_t> &uuid_bytes);
90 
91 private:
92  // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
93  // for this case.
94  llvm::SmallVector<uint8_t, 20> m_bytes;
95 
96  friend bool operator==(const UUID &LHS, const UUID &RHS) {
97  return LHS.m_bytes == RHS.m_bytes;
98  }
99  friend bool operator!=(const UUID &LHS, const UUID &RHS) {
100  return !(LHS == RHS);
101  }
102  friend bool operator<(const UUID &LHS, const UUID &RHS) {
103  return LHS.m_bytes < RHS.m_bytes;
104  }
105  friend bool operator<=(const UUID &LHS, const UUID &RHS) {
106  return !(RHS < LHS);
107  }
108  friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
109  friend bool operator>=(const UUID &LHS, const UUID &RHS) {
110  return !(LHS < RHS);
111  }
112 };
113 } // namespace lldb_private
114 
115 #endif // LLDB_UTILITY_UUID_H
lldb_private::UUID::CvRecordPdb70::Age
llvm::support::ulittle32_t Age
Definition: UUID.h:46
lldb_private::UUID
Definition: UUID.h:23
lldb_private::UUID::CvRecordPdb70::Data3
llvm::support::ulittle16_t Data3
Definition: UUID.h:43
lldb_private::UUID::operator<
friend bool operator<(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:102
lldb_private::UUID::IsValid
bool IsValid() const
Definition: UUID.h:69
lldb_private::Stream
Definition: Stream.h:28
lldb_private::UUID::CvRecordPdb70::Data2
llvm::support::ulittle16_t Data2
Definition: UUID.h:42
lldb_private::UUID::UUID
UUID()=default
lldb_private::UUID::GetAsString
std::string GetAsString(llvm::StringRef separator="-") const
Definition: UUID.cpp:49
lldb_private::UUID::CvRecordPdb70::Data4
uint8_t Data4[8]
Definition: UUID.h:44
lldb_private::UUID::operator>=
friend bool operator>=(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:109
lldb_private::UUID::UUID
UUID(llvm::ArrayRef< uint8_t > bytes)
Creates a uuid from the data pointed to by the bytes argument.
Definition: UUID.h:31
lldb_private::UUID::operator==
friend bool operator==(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:96
lldb_private::UUID::CvRecordPdb70::Data1
llvm::support::ulittle32_t Data1
Definition: UUID.h:41
lldb_private::UUID::CvRecordPdb70::Uuid
struct lldb_private::UUID::CvRecordPdb70::@44 Uuid
bool
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::UUID::Clear
void Clear()
Definition: UUID.h:62
lldb_private::UUID::operator<=
friend bool operator<=(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:105
lldb_private::UUID::operator!=
friend bool operator!=(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:99
lldb_private::UUID::Dump
void Dump(Stream *s) const
Definition: UUID.cpp:64
uint32_t
lldb_private::UUID::CvRecordPdb70
Definition: UUID.h:39
lldb_private::UUID::SetFromStringRef
bool SetFromStringRef(llvm::StringRef str)
Definition: UUID.cpp:97
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:74
lldb_private::UUID::operator>
friend bool operator>(const UUID &LHS, const UUID &RHS)
Definition: UUID.h:108
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::UUID::UUID
UUID(const void *bytes, uint32_t num_bytes)
Creates a UUID from the data pointed to by the bytes argument.
Definition: UUID.h:54
lldb_private::UUID::m_bytes
llvm::SmallVector< uint8_t, 20 > m_bytes
Definition: UUID.h:94
llvm::SmallVectorImpl
Definition: Disassembler.h:42
lldb_private::UUID::GetBytes
llvm::ArrayRef< uint8_t > GetBytes() const
Definition: UUID.h:66