1 //===-- MachVMRegion.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 //  Created by Greg Clayton on 6/26/07.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHVMREGION_H
14 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHVMREGION_H
15 
16 #include "DNBDefs.h"
17 #include "DNBError.h"
18 #include <mach/mach.h>
19 
20 class MachVMRegion {
21 public:
22   MachVMRegion(task_t task);
23   ~MachVMRegion();
24 
25   void Clear();
StartAddress()26   mach_vm_address_t StartAddress() const { return m_start; }
EndAddress()27   mach_vm_address_t EndAddress() const { return m_start + m_size; }
GetByteSize()28   mach_vm_size_t GetByteSize() const { return m_size; }
BytesRemaining(mach_vm_address_t addr)29   mach_vm_address_t BytesRemaining(mach_vm_address_t addr) const {
30     if (ContainsAddress(addr))
31       return m_size - (addr - m_start);
32     else
33       return 0;
34   }
ContainsAddress(mach_vm_address_t addr)35   bool ContainsAddress(mach_vm_address_t addr) const {
36     return addr >= StartAddress() && addr < EndAddress();
37   }
38 
39   bool SetProtections(mach_vm_address_t addr, mach_vm_size_t size,
40                       vm_prot_t prot);
41   bool RestoreProtections();
42   bool GetRegionForAddress(nub_addr_t addr);
43 
44   uint32_t GetDNBPermissions() const;
45 
GetError()46   const DNBError &GetError() { return m_err; }
47 
48 protected:
49 #if defined(VM_REGION_SUBMAP_SHORT_INFO_COUNT_64)
50   typedef vm_region_submap_short_info_data_64_t RegionInfo;
51   enum { kRegionInfoSize = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64 };
52 #else
53   typedef vm_region_submap_info_data_64_t RegionInfo;
54   enum { kRegionInfoSize = VM_REGION_SUBMAP_INFO_COUNT_64 };
55 #endif
56 
57   task_t m_task;
58   mach_vm_address_t m_addr;
59   DNBError m_err;
60   mach_vm_address_t m_start;
61   mach_vm_size_t m_size;
62   natural_t m_depth;
63   RegionInfo m_data;
64   vm_prot_t m_curr_protection; // The current, possibly modified protections.
65                                // Original value is saved in m_data.protections.
66   mach_vm_address_t
67       m_protection_addr; // The start address at which protections were changed
68   mach_vm_size_t
69       m_protection_size; // The size of memory that had its protections changed
70 };
71 
72 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHVMREGION_H
73