IRMemoryMap.h revision 9e6f6a6f394bb570c5f1f9a850ec0befe4a59fef
1//===-- IRExecutionUnit.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 lldb_IRMemoryMap_h_
11#define lldb_IRMemoryMap_h_
12
13#include "lldb/lldb-public.h"
14#include "lldb/Core/DataBufferHeap.h"
15#include "lldb/Core/UserID.h"
16
17#include <map>
18
19namespace lldb_private
20{
21
22//----------------------------------------------------------------------
23/// @class IRMemoryMap IRMemoryMap.h "lldb/Expression/IRMemoryMap.h"
24/// @brief Encapsulates memory that may exist in the process but must
25///     also be available in the host process.
26///
27/// This class encapsulates a group of memory objects that must be readable
28/// or writable from the host process regardless of whether the process
29/// exists.  This allows the IR interpreter as well as JITted code to access
30/// the same memory.
31///
32/// Point queries against this group of memory objects can be made by the
33/// address in the tar at which they reside.  If the inferior does not
34/// exist, allocations still get made-up addresses.  If an inferior appears
35/// at some point, then those addresses need to be re-mapped.
36//----------------------------------------------------------------------
37class IRMemoryMap
38{
39public:
40    IRMemoryMap (lldb::ProcessSP process_sp);
41    ~IRMemoryMap ();
42
43    enum AllocationPolicy {
44        eAllocationPolicyInvalid        = 0,    ///< It is an error for an allocation to have this policy.
45        eAllocationPolicyHostOnly,              ///< This allocation was created in the host and will never make it into the process.
46                                                ///< It is an error to create other types of allocations while such allocations exist.
47        eAllocationPolicyMirror,                ///< The intent is that this allocation exist both in the host and the process and have
48                                                ///< the same content in both.
49        eAllocationPolicyProcessOnly            ///< The intent is that this allocation exist only in the process.
50    };
51
52    lldb::addr_t Malloc (size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, Error &error);
53    void Free (lldb::addr_t process_address, Error &error);
54
55    void WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, size_t size, Error &error);
56    void ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error);
57
58protected:
59    lldb::ProcessWP GetProcessWP ()
60    {
61        return m_process_wp;
62    }
63
64private:
65    struct Allocation
66    {
67        lldb::addr_t    m_process_alloc;    ///< The (unaligned) base for the remote allocation
68        lldb::addr_t    m_process_start;    ///< The base address of the allocation in the process
69        size_t          m_size;             ///< The size of the requested allocation
70        uint32_t        m_permissions;      ///< The access permissions on the memory in the process.  In the host, the memory is always read/write.
71        uint8_t         m_alignment;        ///< The alignment of the requested allocation
72
73        std::unique_ptr<DataBufferHeap> m_data;
74
75        AllocationPolicy    m_policy;
76    };
77
78    lldb::ProcessWP                             m_process_wp;
79    typedef std::map<lldb::addr_t, Allocation>  AllocationMap;
80    AllocationMap                               m_allocations;
81
82    lldb::addr_t FindSpace (size_t size);
83    bool ContainsHostOnlyAllocations ();
84    AllocationMap::iterator FindAllocation (lldb::addr_t addr, size_t size);
85};
86
87}
88
89#endif
90