Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename mc::RemoteSimulation into mc::RemoteProcess
[simgrid.git] / src / mc / sosp / Snapshot.cpp
1 /* Copyright (c) 2014-2021. 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 #include "src/mc/sosp/Snapshot.hpp"
7 #include "src/mc/mc_config.hpp"
8 #include "src/mc/mc_hash.hpp"
9
10 #include <cstddef> /* std::size_t */
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_snapshot, mc, "Taking and restoring snapshots");
13 namespace simgrid {
14 namespace mc {
15 /************************************* Take Snapshot ************************************/
16 /****************************************************************************************/
17
18 void Snapshot::snapshot_regions(RemoteProcess* process)
19 {
20   snapshot_regions_.clear();
21
22   for (auto const& object_info : process->object_infos)
23     add_region(RegionType::Data, object_info.get(), object_info->start_rw, object_info->end_rw - object_info->start_rw);
24
25   const s_xbt_mheap_t* heap = process->get_heap();
26   void* start_heap = heap->base;
27   void* end_heap   = heap->breakval;
28
29   add_region(RegionType::Heap, nullptr, start_heap, (char*)end_heap - (char*)start_heap);
30   heap_bytes_used_ = mmalloc_get_bytes_used_remote(heap->heaplimit, process->get_malloc_info());
31 }
32
33 /** @brief Checks whether the variable is in scope for a given IP.
34  *
35  *  A variable may be defined only from a given value of IP.
36  *
37  *  @param var   Variable description
38  *  @param scope Scope description
39  *  @param ip    Instruction pointer
40  *  @return      true if the variable is valid
41  * */
42 static bool valid_variable(const simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
43 {
44   // The variable is not yet valid:
45   if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
46     return false;
47   else
48     return true;
49 }
50
51 static void fill_local_variables_values(mc_stack_frame_t stack_frame, Frame* scope,
52                                         std::vector<s_local_variable_t>& result)
53 {
54   if (not scope || not scope->range.contain(stack_frame->ip))
55     return;
56
57   for (const Variable& current_variable : scope->variables) {
58     if (not valid_variable(&current_variable, scope, (void*)stack_frame->ip))
59       continue;
60
61     s_local_variable_t new_var;
62     new_var.subprogram = stack_frame->frame;
63     new_var.ip         = stack_frame->ip;
64     new_var.name       = current_variable.name;
65     new_var.type       = current_variable.type;
66     new_var.address    = nullptr;
67
68     if (current_variable.address != nullptr)
69       new_var.address = current_variable.address;
70     else if (not current_variable.location_list.empty()) {
71       dwarf::Location location = simgrid::dwarf::resolve(current_variable.location_list, current_variable.object_info,
72                                                          &(stack_frame->unw_cursor), (void*)stack_frame->frame_base,
73                                                          &mc_model_checker->get_remote_simulation());
74
75       if (not location.in_memory())
76         xbt_die("Cannot handle non-address variable");
77       new_var.address = location.address();
78     } else
79       xbt_die("No address");
80
81     result.push_back(std::move(new_var));
82   }
83
84   // Recursive processing of nested scopes:
85   for (Frame& nested_scope : scope->scopes)
86     fill_local_variables_values(stack_frame, &nested_scope, result);
87 }
88
89 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames)
90 {
91   std::vector<s_local_variable_t> variables;
92   for (s_mc_stack_frame_t& stack_frame : stack_frames)
93     fill_local_variables_values(&stack_frame, stack_frame.frame, variables);
94   return variables;
95 }
96
97 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(UnwindContext* stack_context)
98 {
99   const RemoteProcess* process = &mc_model_checker->get_remote_simulation();
100   std::vector<s_mc_stack_frame_t> result;
101
102   unw_cursor_t c = stack_context->cursor();
103
104   // TODO, check condition check (unw_init_local==0 means end of frame)
105
106   while (true) {
107     s_mc_stack_frame_t stack_frame;
108
109     stack_frame.unw_cursor = c;
110
111     unw_word_t ip;
112     unw_word_t sp;
113
114     unw_get_reg(&c, UNW_REG_IP, &ip);
115     unw_get_reg(&c, UNW_REG_SP, &sp);
116
117     stack_frame.ip = ip;
118     stack_frame.sp = sp;
119
120     // TODO, use real addresses in frame_t instead of fixing it here
121
122     Frame* frame              = process->find_function(remote(ip));
123     stack_frame.frame         = frame;
124
125     if (frame) {
126       stack_frame.frame_name = frame->name;
127       stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
128     } else {
129       stack_frame.frame_base = 0;
130       stack_frame.frame_name = std::string();
131     }
132
133     result.push_back(std::move(stack_frame));
134
135     /* Stop before context switch with maestro */
136     if (frame != nullptr && frame->name == "smx_ctx_wrapper")
137       break;
138
139     int ret = unw_step(&c);
140     if (ret == 0)
141       xbt_die("Unexpected end of stack.");
142     else if (ret < 0)
143       xbt_die("Error while unwinding stack");
144   }
145
146   xbt_assert(not result.empty(), "unw_init_local failed");
147
148   return result;
149 }
150
151 void Snapshot::snapshot_stacks(RemoteProcess* process)
152 {
153   for (auto const& stack : process->stack_areas()) {
154     s_mc_snapshot_stack_t st;
155
156     // Read the context from remote process:
157     unw_context_t context;
158     process->read_bytes(&context, sizeof(context), remote(stack.context));
159
160     st.context.initialize(process, &context);
161
162     st.stack_frames    = unwind_stack_frames(&st.context);
163     st.local_variables = get_local_variables_values(st.stack_frames);
164
165     unw_word_t sp = st.stack_frames[0].sp;
166
167     stacks_.push_back(std::move(st));
168
169     size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
170     stack_sizes_.push_back(stack_size);
171   }
172 }
173
174 static void snapshot_handle_ignore(Snapshot* snapshot)
175 {
176   xbt_assert(snapshot->get_remote_simulation());
177
178   // Copy the memory:
179   for (auto const& region : snapshot->get_remote_simulation()->ignored_regions()) {
180     s_mc_snapshot_ignored_data_t ignored_data;
181     ignored_data.start = (void*)region.addr;
182     ignored_data.data.resize(region.size);
183     // TODO, we should do this once per privatization segment:
184     snapshot->get_remote_simulation()->read_bytes(ignored_data.data.data(), region.size, remote(region.addr));
185     snapshot->ignored_data_.push_back(std::move(ignored_data));
186   }
187
188   // Zero the memory:
189   for (auto const& region : snapshot->get_remote_simulation()->ignored_regions())
190     snapshot->get_remote_simulation()->clear_bytes(remote(region.addr), region.size);
191 }
192
193 static void snapshot_ignore_restore(const simgrid::mc::Snapshot* snapshot)
194 {
195   for (auto const& ignored_data : snapshot->ignored_data_)
196     snapshot->get_remote_simulation()->write_bytes(ignored_data.data.data(), ignored_data.data.size(),
197                                                    remote(ignored_data.start));
198 }
199
200 Snapshot::Snapshot(int num_state, RemoteProcess* process) : AddressSpace(process), num_state_(num_state)
201 {
202   XBT_DEBUG("Taking snapshot %i", num_state);
203
204   for (auto const& p : process->actors())
205     enabled_processes_.insert(p.copy.get_buffer()->get_pid());
206
207   snapshot_handle_ignore(this);
208
209   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
210   snapshot_regions(process);
211
212   to_ignore_ = process->ignored_heap();
213
214   if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
215     snapshot_stacks(process);
216     hash_ = simgrid::mc::hash(*this);
217   }
218
219   snapshot_ignore_restore(this);
220 }
221
222 void Snapshot::add_region(RegionType type, ObjectInformation* object_info, void* start_addr, std::size_t size)
223 {
224   if (type == RegionType::Data)
225     xbt_assert(object_info, "Missing object info for object.");
226   else if (type == RegionType::Heap)
227     xbt_assert(not object_info, "Unexpected object info for heap region.");
228
229   auto* region = new Region(type, start_addr, size);
230   region->object_info(object_info);
231   snapshot_regions_.push_back(std::unique_ptr<Region>(region));
232 }
233
234 void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, ReadOptions options) const
235 {
236   const Region* region = this->get_region((void*)address.address());
237   if (region) {
238     void* res = region->read(buffer, (void*)address.address(), size);
239     if (buffer == res || options & ReadOptions::lazy())
240       return res;
241     else {
242       memcpy(buffer, res, size);
243       return buffer;
244     }
245   } else
246     return this->get_remote_simulation()->read_bytes(buffer, size, address, options);
247 }
248 /** @brief Find the snapshotted region from a pointer
249  *
250  *  @param addr     Pointer
251  * */
252 Region* Snapshot::get_region(const void* addr) const
253 {
254   size_t n = snapshot_regions_.size();
255   for (size_t i = 0; i != n; ++i) {
256     Region* region = snapshot_regions_[i].get();
257     if (not(region && region->contain(simgrid::mc::remote(addr))))
258       continue;
259
260     return region;
261   }
262
263   return nullptr;
264 }
265
266 /** @brief Find the snapshotted region from a pointer, with a hinted_region */
267 Region* Snapshot::get_region(const void* addr, Region* hinted_region) const
268 {
269   if (hinted_region->contain(simgrid::mc::remote(addr)))
270     return hinted_region;
271   else
272     return get_region(addr);
273 }
274
275 void Snapshot::restore(RemoteProcess* process) const
276 {
277   XBT_DEBUG("Restore snapshot %i", num_state_);
278
279   // Restore regions
280   for (std::unique_ptr<Region> const& region : snapshot_regions_) {
281     if (region) // privatized variables are not snapshotted
282       region.get()->restore();
283   }
284
285   snapshot_ignore_restore(this);
286   process->clear_cache();
287 }
288
289 } // namespace mc
290 } // namespace simgrid