1 /* Copyright (c) 2014-2020. The SimGrid Team. All rights reserved. */
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. */
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"
11 #include <cstddef> /* std::size_t */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_snapshot, mc, "Taking and restoring snapshots");
16 /************************************* Take Snapshot ************************************/
17 /****************************************************************************************/
19 void Snapshot::snapshot_regions(RemoteSimulation* process)
21 snapshot_regions_.clear();
23 for (auto const& object_info : process->object_infos)
24 add_region(RegionType::Data, object_info.get(), object_info->start_rw, object_info->end_rw - object_info->start_rw);
26 const s_xbt_mheap_t* heap = process->get_heap();
27 void* start_heap = heap->base;
28 void* end_heap = heap->breakval;
30 add_region(RegionType::Heap, nullptr, start_heap, (char*)end_heap - (char*)start_heap);
31 heap_bytes_used_ = mmalloc_get_bytes_used_remote(heap->heaplimit, process->get_malloc_info());
34 /** @brief Checks whether the variable is in scope for a given IP.
36 * A variable may be defined only from a given value of IP.
38 * @param var Variable description
39 * @param scope Scope description
40 * @param ip Instruction pointer
41 * @return true if the variable is valid
43 static bool valid_variable(const simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
45 // The variable is not yet valid:
46 if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
52 static void fill_local_variables_values(mc_stack_frame_t stack_frame, Frame* scope,
53 std::vector<s_local_variable_t>& result)
55 if (not scope || not scope->range.contain(stack_frame->ip))
58 for (const Variable& current_variable : scope->variables) {
59 if (not valid_variable(¤t_variable, scope, (void*)stack_frame->ip))
62 s_local_variable_t new_var;
63 new_var.subprogram = stack_frame->frame;
64 new_var.ip = stack_frame->ip;
65 new_var.name = current_variable.name;
66 new_var.type = current_variable.type;
67 new_var.address = nullptr;
69 if (current_variable.address != nullptr)
70 new_var.address = current_variable.address;
71 else if (not current_variable.location_list.empty()) {
72 dwarf::Location location = simgrid::dwarf::resolve(current_variable.location_list, current_variable.object_info,
73 &(stack_frame->unw_cursor), (void*)stack_frame->frame_base,
74 &mc_model_checker->get_remote_simulation());
76 if (not location.in_memory())
77 xbt_die("Cannot handle non-address variable");
78 new_var.address = location.address();
80 xbt_die("No address");
82 result.push_back(std::move(new_var));
85 // Recursive processing of nested scopes:
86 for (Frame& nested_scope : scope->scopes)
87 fill_local_variables_values(stack_frame, &nested_scope, result);
90 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames)
92 std::vector<s_local_variable_t> variables;
93 for (s_mc_stack_frame_t& stack_frame : stack_frames)
94 fill_local_variables_values(&stack_frame, stack_frame.frame, variables);
98 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(UnwindContext* stack_context)
100 const RemoteSimulation* process = &mc_model_checker->get_remote_simulation();
101 std::vector<s_mc_stack_frame_t> result;
103 unw_cursor_t c = stack_context->cursor();
105 // TODO, check condition check (unw_init_local==0 means end of frame)
108 s_mc_stack_frame_t stack_frame;
110 stack_frame.unw_cursor = c;
115 unw_get_reg(&c, UNW_REG_IP, &ip);
116 unw_get_reg(&c, UNW_REG_SP, &sp);
121 // TODO, use real addresses in frame_t instead of fixing it here
123 Frame* frame = process->find_function(remote(ip));
124 stack_frame.frame = frame;
127 stack_frame.frame_name = frame->name;
128 stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
130 stack_frame.frame_base = 0;
131 stack_frame.frame_name = std::string();
134 result.push_back(std::move(stack_frame));
136 /* Stop before context switch with maestro */
137 if (frame != nullptr && frame->name == "smx_ctx_wrapper")
140 int ret = unw_step(&c);
142 xbt_die("Unexpected end of stack.");
144 xbt_die("Error while unwinding stack");
147 xbt_assert(not result.empty(), "unw_init_local failed");
152 void Snapshot::snapshot_stacks(RemoteSimulation* process)
154 for (auto const& stack : process->stack_areas()) {
155 s_mc_snapshot_stack_t st;
157 // Read the context from remote process:
158 unw_context_t context;
159 process->read_bytes(&context, sizeof(context), remote(stack.context));
161 st.context.initialize(process, &context);
163 st.stack_frames = unwind_stack_frames(&st.context);
164 st.local_variables = get_local_variables_values(st.stack_frames);
166 unw_word_t sp = st.stack_frames[0].sp;
168 stacks_.push_back(std::move(st));
170 size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
171 stack_sizes_.push_back(stack_size);
175 static void snapshot_handle_ignore(Snapshot* snapshot)
177 xbt_assert(snapshot->get_remote_simulation());
180 for (auto const& region : snapshot->get_remote_simulation()->ignored_regions()) {
181 s_mc_snapshot_ignored_data_t ignored_data;
182 ignored_data.start = (void*)region.addr;
183 ignored_data.data.resize(region.size);
184 // TODO, we should do this once per privatization segment:
185 snapshot->get_remote_simulation()->read_bytes(ignored_data.data.data(), region.size, remote(region.addr));
186 snapshot->ignored_data_.push_back(std::move(ignored_data));
190 for (auto const& region : snapshot->get_remote_simulation()->ignored_regions())
191 snapshot->get_remote_simulation()->clear_bytes(remote(region.addr), region.size);
194 static void snapshot_ignore_restore(const simgrid::mc::Snapshot* snapshot)
196 for (auto const& ignored_data : snapshot->ignored_data_)
197 snapshot->get_remote_simulation()->write_bytes(ignored_data.data.data(), ignored_data.data.size(),
198 remote(ignored_data.start));
201 Snapshot::Snapshot(int num_state, RemoteSimulation* process) : AddressSpace(process), num_state_(num_state)
203 XBT_DEBUG("Taking snapshot %i", num_state);
205 for (auto const& p : process->actors())
206 enabled_processes_.insert(p.copy.get_buffer()->get_pid());
208 snapshot_handle_ignore(this);
210 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
211 snapshot_regions(process);
213 to_ignore_ = process->ignored_heap();
215 if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
216 snapshot_stacks(process);
217 hash_ = simgrid::mc::hash(*this);
220 snapshot_ignore_restore(this);
223 void Snapshot::add_region(RegionType type, ObjectInformation* object_info, void* start_addr, std::size_t size)
225 if (type == RegionType::Data)
226 xbt_assert(object_info, "Missing object info for object.");
227 else if (type == RegionType::Heap)
228 xbt_assert(not object_info, "Unexpected object info for heap region.");
230 auto* region = new Region(type, start_addr, size);
231 region->object_info(object_info);
232 snapshot_regions_.push_back(std::unique_ptr<Region>(std::move(region)));
235 void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, ReadOptions options) const
237 const Region* region = this->get_region((void*)address.address());
239 void* res = region->read(buffer, (void*)address.address(), size);
240 if (buffer == res || options & ReadOptions::lazy())
243 memcpy(buffer, res, size);
247 return this->get_remote_simulation()->read_bytes(buffer, size, address, options);
249 /** @brief Find the snapshotted region from a pointer
251 * @param addr Pointer
253 Region* Snapshot::get_region(const void* addr) const
255 size_t n = snapshot_regions_.size();
256 for (size_t i = 0; i != n; ++i) {
257 Region* region = snapshot_regions_[i].get();
258 if (not(region && region->contain(simgrid::mc::remote(addr))))
267 /** @brief Find the snapshotted region from a pointer, with a hinted_region */
268 Region* Snapshot::get_region(const void* addr, Region* hinted_region) const
270 if (hinted_region->contain(simgrid::mc::remote(addr)))
271 return hinted_region;
273 return get_region(addr);
276 void Snapshot::restore(RemoteSimulation* process) const
278 XBT_DEBUG("Restore snapshot %i", num_state_);
281 for (std::unique_ptr<Region> const& region : snapshot_regions_) {
282 if (region) // privatized variables are not snapshotted
283 region.get()->restore();
286 snapshot_ignore_restore(this);
287 process->clear_cache();
291 } // namespace simgrid