Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc: Also remove the process_index
[simgrid.git] / src / mc / sosp / mc_snapshot.hpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_SNAPSHOT_HPP
7 #define SIMGRID_MC_SNAPSHOT_HPP
8
9 #include "src/mc/ModelChecker.hpp"
10 #include "src/mc/inspect/mc_unw.hpp"
11 #include "src/mc/remote/RemoteClient.hpp"
12 #include "src/mc/sosp/RegionSnapshot.hpp"
13
14 // ***** Snapshot region
15
16 static XBT_ALWAYS_INLINE void* mc_translate_address_region_chunked(uintptr_t addr, simgrid::mc::RegionSnapshot* region)
17 {
18   auto split                = simgrid::mc::mmu::split(addr - region->start().address());
19   auto pageno               = split.first;
20   auto offset               = split.second;
21   const void* snapshot_page = region->page_data().page(pageno);
22   return (char*)snapshot_page + offset;
23 }
24
25 static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, simgrid::mc::RegionSnapshot* region)
26 {
27   switch (region->storage_type()) {
28     case simgrid::mc::StorageType::Flat: {
29       uintptr_t offset = (uintptr_t)addr - (uintptr_t)region->start().address();
30       return (void*)((uintptr_t)region->flat_data().get() + offset);
31     }
32     case simgrid::mc::StorageType::Chunked:
33       return mc_translate_address_region_chunked(addr, region);
34     default: // includes StorageType::NoData
35       xbt_die("Storage type not supported");
36   }
37 }
38
39 // ***** MC Snapshot
40
41 /** Ignored data
42  *
43  *  Some parts of the snapshot are ignored by zeroing them out: the real
44  *  values is stored here.
45  * */
46 struct s_mc_snapshot_ignored_data_t {
47   void* start;
48   std::vector<char> data;
49 };
50
51 /** Information about a given stack frame */
52 struct s_mc_stack_frame_t {
53   /** Instruction pointer */
54   unw_word_t ip;
55   /** Stack pointer */
56   unw_word_t sp;
57   unw_word_t frame_base;
58   simgrid::mc::Frame* frame;
59   std::string frame_name;
60   unw_cursor_t unw_cursor;
61 };
62 typedef s_mc_stack_frame_t* mc_stack_frame_t;
63
64 struct s_local_variable_t {
65   simgrid::mc::Frame* subprogram;
66   unsigned long ip;
67   std::string name;
68   simgrid::mc::Type* type;
69   void* address;
70 };
71 typedef s_local_variable_t* local_variable_t;
72
73 struct XBT_PRIVATE s_mc_snapshot_stack_t {
74   std::vector<s_local_variable_t> local_variables;
75   simgrid::mc::UnwindContext context;
76   std::vector<s_mc_stack_frame_t> stack_frames;
77 };
78 typedef s_mc_snapshot_stack_t* mc_snapshot_stack_t;
79
80 namespace simgrid {
81 namespace mc {
82
83 class XBT_PRIVATE Snapshot final : public AddressSpace {
84 public:
85   Snapshot(int num_state, RemoteClient* process = &mc_model_checker->process());
86   ~Snapshot() = default;
87
88   /* Initialization */
89
90   /* Regular use */
91   const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
92                          ReadOptions options = ReadOptions::none()) const override;
93   RegionSnapshot* get_region(const void* addr) const;
94   RegionSnapshot* get_region(const void* addr, RegionSnapshot* hinted_region) const;
95   void restore(RemoteClient* process);
96
97   // To be private
98   int num_state_;
99   std::size_t heap_bytes_used_;
100   std::vector<std::unique_ptr<RegionSnapshot>> snapshot_regions_;
101   std::set<pid_t> enabled_processes_;
102   std::vector<std::size_t> stack_sizes_;
103   std::vector<s_mc_snapshot_stack_t> stacks_;
104   std::vector<simgrid::mc::IgnoredHeapRegion> to_ignore_;
105   std::uint64_t hash_ = 0;
106   std::vector<s_mc_snapshot_ignored_data_t> ignored_data_;
107
108 private:
109   void add_region(RegionType type, ObjectInformation* object_info, void* start_addr, void* permanent_addr,
110                   std::size_t size);
111   void snapshot_regions(simgrid::mc::RemoteClient* process);
112   void snapshot_stacks(simgrid::mc::RemoteClient* process);
113 };
114 } // namespace mc
115 } // namespace simgrid
116
117 static const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot);
118
119 namespace simgrid {
120 namespace mc {
121
122 XBT_PRIVATE std::shared_ptr<Snapshot> take_snapshot(int num_state);
123 } // namespace mc
124 } // namespace simgrid
125
126 const void* MC_region_read_fragmented(simgrid::mc::RegionSnapshot* region, void* target, const void* addr,
127                                       std::size_t size);
128
129 int MC_snapshot_region_memcmp(const void* addr1, simgrid::mc::RegionSnapshot* region1, const void* addr2,
130                               simgrid::mc::RegionSnapshot* region2, std::size_t size);
131
132 static XBT_ALWAYS_INLINE const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot)
133 {
134   if (snapshot == nullptr)
135     xbt_die("snapshot is nullptr");
136   return mc_model_checker->process().get_heap()->breakval;
137 }
138
139 /** @brief Read memory from a snapshot region
140  *
141  *  @param addr    Process (non-snapshot) address of the data
142  *  @param region  Snapshot memory region where the data is located
143  *  @param target  Buffer to store the value
144  *  @param size    Size of the data to read in bytes
145  *  @return Pointer where the data is located (target buffer of original location)
146  */
147 static XBT_ALWAYS_INLINE const void* MC_region_read(simgrid::mc::RegionSnapshot* region, void* target, const void* addr,
148                                                     std::size_t size)
149 {
150   xbt_assert(region);
151
152   std::uintptr_t offset = (std::uintptr_t)addr - (std::uintptr_t)region->start().address();
153
154   xbt_assert(region->contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
155
156   switch (region->storage_type()) {
157     case simgrid::mc::StorageType::Flat:
158       return (char*)region->flat_data().get() + offset;
159
160     case simgrid::mc::StorageType::Chunked: {
161       // Last byte of the region:
162       void* end = (char*)addr + size - 1;
163       if (simgrid::mc::mmu::same_chunk((std::uintptr_t)addr, (std::uintptr_t)end)) {
164         // The memory is contained in a single page:
165         return mc_translate_address_region_chunked((uintptr_t)addr, region);
166       }
167       // Otherwise, the memory spans several pages:
168       return MC_region_read_fragmented(region, target, addr, size);
169     }
170
171     default:
172       xbt_die("StorageType::NoData not supported");
173   }
174 }
175
176 static XBT_ALWAYS_INLINE void* MC_region_read_pointer(simgrid::mc::RegionSnapshot* region, const void* addr)
177 {
178   void* res;
179   return *(void**)MC_region_read(region, &res, addr, sizeof(void*));
180 }
181
182 #endif