Logo AND Algorithmique Numérique Distribuée

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