MemoryRegionInfo.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===-- MemoryRegionInfo.h ---------------------------------------*- C++
  2. //-*-===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLDB_TARGET_MEMORYREGIONINFO_H
  10. #define LLDB_TARGET_MEMORYREGIONINFO_H
  11. #include "lldb/Utility/ConstString.h"
  12. #include "lldb/Utility/RangeMap.h"
  13. #include "llvm/Support/FormatProviders.h"
  14. namespace lldb_private {
  15. class MemoryRegionInfo {
  16. public:
  17. typedef Range<lldb::addr_t, lldb::addr_t> RangeType;
  18. enum OptionalBool { eDontKnow = -1, eNo = 0, eYes = 1 };
  19. MemoryRegionInfo() = default;
  20. MemoryRegionInfo(RangeType range, OptionalBool read, OptionalBool write,
  21. OptionalBool execute, OptionalBool mapped, ConstString name,
  22. OptionalBool flash, lldb::offset_t blocksize,
  23. OptionalBool memory_tagged)
  24. : m_range(range), m_read(read), m_write(write), m_execute(execute),
  25. m_mapped(mapped), m_name(name), m_flash(flash), m_blocksize(blocksize),
  26. m_memory_tagged(memory_tagged) {}
  27. RangeType &GetRange() { return m_range; }
  28. void Clear() {
  29. m_range.Clear();
  30. m_read = m_write = m_execute = m_memory_tagged = eDontKnow;
  31. }
  32. const RangeType &GetRange() const { return m_range; }
  33. OptionalBool GetReadable() const { return m_read; }
  34. OptionalBool GetWritable() const { return m_write; }
  35. OptionalBool GetExecutable() const { return m_execute; }
  36. OptionalBool GetMapped() const { return m_mapped; }
  37. ConstString GetName() const { return m_name; }
  38. OptionalBool GetMemoryTagged() const { return m_memory_tagged; }
  39. void SetReadable(OptionalBool val) { m_read = val; }
  40. void SetWritable(OptionalBool val) { m_write = val; }
  41. void SetExecutable(OptionalBool val) { m_execute = val; }
  42. void SetMapped(OptionalBool val) { m_mapped = val; }
  43. void SetName(const char *name) { m_name = ConstString(name); }
  44. OptionalBool GetFlash() const { return m_flash; }
  45. void SetFlash(OptionalBool val) { m_flash = val; }
  46. lldb::offset_t GetBlocksize() const { return m_blocksize; }
  47. void SetBlocksize(lldb::offset_t blocksize) { m_blocksize = blocksize; }
  48. void SetMemoryTagged(OptionalBool val) { m_memory_tagged = val; }
  49. // Get permissions as a uint32_t that is a mask of one or more bits from the
  50. // lldb::Permissions
  51. uint32_t GetLLDBPermissions() const {
  52. uint32_t permissions = 0;
  53. if (m_read)
  54. permissions |= lldb::ePermissionsReadable;
  55. if (m_write)
  56. permissions |= lldb::ePermissionsWritable;
  57. if (m_execute)
  58. permissions |= lldb::ePermissionsExecutable;
  59. return permissions;
  60. }
  61. // Set permissions from a uint32_t that contains one or more bits from the
  62. // lldb::Permissions
  63. void SetLLDBPermissions(uint32_t permissions) {
  64. m_read = (permissions & lldb::ePermissionsReadable) ? eYes : eNo;
  65. m_write = (permissions & lldb::ePermissionsWritable) ? eYes : eNo;
  66. m_execute = (permissions & lldb::ePermissionsExecutable) ? eYes : eNo;
  67. }
  68. bool operator==(const MemoryRegionInfo &rhs) const {
  69. return m_range == rhs.m_range && m_read == rhs.m_read &&
  70. m_write == rhs.m_write && m_execute == rhs.m_execute &&
  71. m_mapped == rhs.m_mapped && m_name == rhs.m_name &&
  72. m_flash == rhs.m_flash && m_blocksize == rhs.m_blocksize &&
  73. m_memory_tagged == rhs.m_memory_tagged;
  74. }
  75. bool operator!=(const MemoryRegionInfo &rhs) const { return !(*this == rhs); }
  76. protected:
  77. RangeType m_range;
  78. OptionalBool m_read = eDontKnow;
  79. OptionalBool m_write = eDontKnow;
  80. OptionalBool m_execute = eDontKnow;
  81. OptionalBool m_mapped = eDontKnow;
  82. ConstString m_name;
  83. OptionalBool m_flash = eDontKnow;
  84. lldb::offset_t m_blocksize = 0;
  85. OptionalBool m_memory_tagged = eDontKnow;
  86. };
  87. inline bool operator<(const MemoryRegionInfo &lhs,
  88. const MemoryRegionInfo &rhs) {
  89. return lhs.GetRange() < rhs.GetRange();
  90. }
  91. inline bool operator<(const MemoryRegionInfo &lhs, lldb::addr_t rhs) {
  92. return lhs.GetRange().GetRangeBase() < rhs;
  93. }
  94. inline bool operator<(lldb::addr_t lhs, const MemoryRegionInfo &rhs) {
  95. return lhs < rhs.GetRange().GetRangeBase();
  96. }
  97. llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
  98. const MemoryRegionInfo &Info);
  99. // Forward-declarable wrapper.
  100. class MemoryRegionInfos : public std::vector<lldb_private::MemoryRegionInfo> {
  101. public:
  102. using std::vector<lldb_private::MemoryRegionInfo>::vector;
  103. };
  104. }
  105. namespace llvm {
  106. template <>
  107. /// If Options is empty, prints a textual representation of the value. If
  108. /// Options is a single character, it uses that character for the "yes" value,
  109. /// while "no" is printed as "-", and "don't know" as "?". This can be used to
  110. /// print the permissions in the traditional "rwx" form.
  111. struct format_provider<lldb_private::MemoryRegionInfo::OptionalBool> {
  112. static void format(const lldb_private::MemoryRegionInfo::OptionalBool &B,
  113. raw_ostream &OS, StringRef Options);
  114. };
  115. }
  116. #endif // LLDB_TARGET_MEMORYREGIONINFO_H