]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Target/Memory.h
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / include / lldb / Target / Memory.h
1 //===-- Memory.h ------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_Memory_h_
11 #define liblldb_Memory_h_
12
13 // C Includes
14 // C++ Includes
15 #include <map>
16 #include <mutex>
17 #include <vector>
18
19 // Other libraries and framework includes
20
21 // Project includes
22 #include "lldb/Core/RangeMap.h"
23 #include "lldb/lldb-private.h"
24
25 namespace lldb_private {
26 //----------------------------------------------------------------------
27 // A class to track memory that was read from a live process between
28 // runs.
29 //----------------------------------------------------------------------
30 class MemoryCache {
31 public:
32   //------------------------------------------------------------------
33   // Constructors and Destructors
34   //------------------------------------------------------------------
35   MemoryCache(Process &process);
36
37   ~MemoryCache();
38
39   void Clear(bool clear_invalid_ranges = false);
40
41   void Flush(lldb::addr_t addr, size_t size);
42
43   size_t Read(lldb::addr_t addr, void *dst, size_t dst_len, Error &error);
44
45   uint32_t GetMemoryCacheLineSize() const { return m_L2_cache_line_byte_size; }
46
47   void AddInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
48
49   bool RemoveInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
50
51   // Allow external sources to populate data into the L1 memory cache
52   void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len);
53
54   void AddL1CacheData(lldb::addr_t addr,
55                       const lldb::DataBufferSP &data_buffer_sp);
56
57 protected:
58   typedef std::map<lldb::addr_t, lldb::DataBufferSP> BlockMap;
59   typedef RangeArray<lldb::addr_t, lldb::addr_t, 4> InvalidRanges;
60   typedef Range<lldb::addr_t, lldb::addr_t> AddrRange;
61   //------------------------------------------------------------------
62   // Classes that inherit from MemoryCache can see and modify these
63   //------------------------------------------------------------------
64   std::recursive_mutex m_mutex;
65   BlockMap m_L1_cache; // A first level memory cache whose chunk sizes vary that
66                        // will be used only if the memory read fits entirely in
67                        // a chunk
68   BlockMap m_L2_cache; // A memory cache of fixed size chinks
69                        // (m_L2_cache_line_byte_size bytes in size each)
70   InvalidRanges m_invalid_ranges;
71   Process &m_process;
72   uint32_t m_L2_cache_line_byte_size;
73
74 private:
75   DISALLOW_COPY_AND_ASSIGN(MemoryCache);
76 };
77
78 class AllocatedBlock {
79 public:
80   AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions,
81                  uint32_t chunk_size);
82
83   ~AllocatedBlock();
84
85   lldb::addr_t ReserveBlock(uint32_t size);
86
87   bool FreeBlock(lldb::addr_t addr);
88
89   lldb::addr_t GetBaseAddress() const { return m_addr; }
90
91   uint32_t GetByteSize() const { return m_byte_size; }
92
93   uint32_t GetPermissions() const { return m_permissions; }
94
95   uint32_t GetChunkSize() const { return m_chunk_size; }
96
97   bool Contains(lldb::addr_t addr) const {
98     return ((addr >= m_addr) && addr < (m_addr + m_byte_size));
99   }
100
101 protected:
102   uint32_t TotalChunks() const { return m_byte_size / m_chunk_size; }
103
104   uint32_t CalculateChunksNeededForSize(uint32_t size) const {
105     return (size + m_chunk_size - 1) / m_chunk_size;
106   }
107   const lldb::addr_t m_addr;    // Base address of this block of memory
108   const uint32_t m_byte_size;   // 4GB of chunk should be enough...
109   const uint32_t m_permissions; // Permissions for this memory (logical OR of
110                                 // lldb::Permissions bits)
111   const uint32_t m_chunk_size;  // The size of chunks that the memory at m_addr
112                                 // is divied up into
113   typedef std::map<uint32_t, uint32_t> OffsetToChunkSize;
114   OffsetToChunkSize m_offset_to_chunk_size;
115 };
116
117 //----------------------------------------------------------------------
118 // A class that can track allocated memory and give out allocated memory
119 // without us having to make an allocate/deallocate call every time we
120 // need some memory in a process that is being debugged.
121 //----------------------------------------------------------------------
122 class AllocatedMemoryCache {
123 public:
124   //------------------------------------------------------------------
125   // Constructors and Destructors
126   //------------------------------------------------------------------
127   AllocatedMemoryCache(Process &process);
128
129   ~AllocatedMemoryCache();
130
131   void Clear();
132
133   lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions,
134                               Error &error);
135
136   bool DeallocateMemory(lldb::addr_t ptr);
137
138 protected:
139   typedef std::shared_ptr<AllocatedBlock> AllocatedBlockSP;
140
141   AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions,
142                                 uint32_t chunk_size, Error &error);
143
144   //------------------------------------------------------------------
145   // Classes that inherit from MemoryCache can see and modify these
146   //------------------------------------------------------------------
147   Process &m_process;
148   std::recursive_mutex m_mutex;
149   typedef std::multimap<uint32_t, AllocatedBlockSP> PermissionsToBlockMap;
150   PermissionsToBlockMap m_memory_map;
151
152 private:
153   DISALLOW_COPY_AND_ASSIGN(AllocatedMemoryCache);
154 };
155
156 } // namespace lldb_private
157
158 #endif // liblldb_Memory_h_