LLDB  mainline
MemoryTagMap.h
Go to the documentation of this file.
1 //===-- MemoryTagMap.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_TARGET_MEMORYTAGMAP_H
10 #define LLDB_TARGET_MEMORYTAGMAP_H
11 
13 #include "lldb/lldb-private.h"
14 #include "llvm/ADT/Optional.h"
15 #include <map>
16 
17 namespace lldb_private {
18 
19 /// MemoryTagMap provides a way to give a sparse read result
20 /// when reading memory tags for a range. This is useful when
21 /// you want to annotate some large memory dump that might include
22 /// tagged memory but you don't know that it is all tagged.
23 class MemoryTagMap {
24 public:
25  /// Init an empty tag map
26  ///
27  /// \param [in] manager
28  /// Non-null pointer to a memory tag manager.
29  MemoryTagMap(const MemoryTagManager *manager);
30 
31  /// Insert tags into the map starting from addr.
32  ///
33  /// \param [in] addr
34  /// Start address of the range to insert tags for.
35  /// This address should be granule aligned and have had
36  /// any non address bits removed.
37  /// (ideally you would use the base of the range you used
38  /// to read the tags in the first place)
39  ///
40  /// \param [in] tags
41  /// Vector of tags to insert. The first tag will be inserted
42  /// at addr, the next at addr+granule size and so on until
43  /// all tags have been inserted.
44  void InsertTags(lldb::addr_t addr, const std::vector<lldb::addr_t> tags);
45 
46  bool Empty() const;
47 
48  /// Lookup memory tags for a range of memory from addr to addr+len.
49  ///
50  /// \param [in] addr
51  /// The start of the range. This may include non address bits and
52  /// does not have to be granule aligned.
53  ///
54  /// \param [in] len
55  /// The length in bytes of the range to read tags for. This does
56  /// not need to be multiple of the granule size.
57  ///
58  /// \return
59  /// A vector containing the tags found for the granules in the
60  /// range. (which is the result of granule aligning the given range)
61  ///
62  /// Each item in the vector is an optional tag. Meaning that if
63  /// it is valid then the granule had a tag and if not, it didn't.
64  ///
65  /// If the range had no tags at all, the vector will be empty.
66  /// If some of the range was tagged it will have items and some
67  /// of them may be llvm::None.
68  /// (this saves the caller checking whether all items are llvm::None)
69  std::vector<llvm::Optional<lldb::addr_t>> GetTags(lldb::addr_t addr,
70  size_t len) const;
71 
72 private:
73  /// Lookup the tag for address
74  ///
75  /// \param [in] address
76  /// The address to lookup a tag for. This should be aligned
77  /// to a granule boundary.
78  ///
79  /// \return
80  /// The tag for the granule that address refers to, or llvm::None
81  /// if it has no memory tag.
82  llvm::Optional<lldb::addr_t> GetTag(lldb::addr_t addr) const;
83 
84  // A map of granule aligned addresses to their memory tag
85  std::map<lldb::addr_t, lldb::addr_t> m_addr_to_tag;
86 
87  // Memory tag manager used to align addresses and get granule size.
88  // Ideally this would be a const& but only certain architectures will
89  // have a memory tag manager class to provide here. So for a method
90  // returning a MemoryTagMap, Optional<MemoryTagMap> allows it to handle
91  // architectures without memory tagging. Optionals cannot hold references
92  // so we go with a pointer that we assume will be not be null.
94 };
95 
96 } // namespace lldb_private
97 
98 #endif // LLDB_TARGET_MEMORYTAGMAP_H
lldb_private::MemoryTagMap::Empty
bool Empty() const
Definition: MemoryTagMap.cpp:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::MemoryTagManager
Definition: MemoryTagManager.h:28
lldb-private.h
lldb_private::MemoryTagMap::m_addr_to_tag
std::map< lldb::addr_t, lldb::addr_t > m_addr_to_tag
Definition: MemoryTagMap.h:85
lldb_private::MemoryTagMap::InsertTags
void InsertTags(lldb::addr_t addr, const std::vector< lldb::addr_t > tags)
Insert tags into the map starting from addr.
Definition: MemoryTagMap.cpp:18
lldb_private::MemoryTagMap::MemoryTagMap
MemoryTagMap(const MemoryTagManager *manager)
Init an empty tag map.
Definition: MemoryTagMap.cpp:13
lldb_private::MemoryTagMap::GetTag
llvm::Optional< lldb::addr_t > GetTag(lldb::addr_t addr) const
Lookup the tag for address.
Definition: MemoryTagMap.cpp:57
lldb_private::MemoryTagMap
MemoryTagMap provides a way to give a sparse read result when reading memory tags for a range.
Definition: MemoryTagMap.h:23
lldb_private::MemoryTagMap::GetTags
std::vector< llvm::Optional< lldb::addr_t > > GetTags(lldb::addr_t addr, size_t len) const
Lookup memory tags for a range of memory from addr to addr+len.
Definition: MemoryTagMap.cpp:31
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::MemoryTagMap::m_manager
const MemoryTagManager * m_manager
Definition: MemoryTagMap.h:93
MemoryTagManager.h