1 /* Copyright (c) 2014-2019. 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 simgrid::mc::Snapshot::snapshot_regions(simgrid::mc::RemoteClient* process)
21 snapshot_regions_.clear();
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);
27 xbt_mheap_t heap = process->get_heap();
28 void* start_heap = heap->base;
29 void* end_heap = heap->breakval;
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());
35 /** @brief Checks whether the variable is in scope for a given IP.
37 * A variable may be defined only from a given value of IP.
39 * @param var Variable description
40 * @param scope Scope description
41 * @param ip Instruction pointer
42 * @return true if the variable is valid
44 static bool valid_variable(simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
46 // The variable is not yet valid:
47 if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
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)
56 if (not scope || not scope->range.contain(stack_frame->ip))
59 for (simgrid::mc::Variable& current_variable : scope->variables) {
61 if (not valid_variable(¤t_variable, scope, (void*)stack_frame->ip))
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;
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());
78 if (not location.in_memory())
79 xbt_die("Cannot handle non-address variable");
80 new_var.address = location.address();
83 xbt_die("No address");
85 result.push_back(std::move(new_var));
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);
93 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames)
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);
101 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindContext* stack_context)
103 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
104 std::vector<s_mc_stack_frame_t> result;
106 unw_cursor_t c = stack_context->cursor();
108 // TODO, check condition check (unw_init_local==0 means end of frame)
112 s_mc_stack_frame_t stack_frame;
114 stack_frame.unw_cursor = c;
119 unw_get_reg(&c, UNW_REG_IP, &ip);
120 unw_get_reg(&c, UNW_REG_SP, &sp);
125 // TODO, use real addresses in frame_t instead of fixing it here
127 simgrid::mc::Frame* frame = process->find_function(remote(ip));
128 stack_frame.frame = frame;
131 stack_frame.frame_name = frame->name;
132 stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
134 stack_frame.frame_base = 0;
135 stack_frame.frame_name = std::string();
138 result.push_back(std::move(stack_frame));
140 /* Stop before context switch with maestro */
141 if (frame != nullptr && frame->name == "smx_ctx_wrapper")
144 int ret = unw_step(&c);
146 xbt_die("Unexpected end of stack.");
148 xbt_die("Error while unwinding stack");
151 xbt_assert(not result.empty(), "unw_init_local failed");
156 void simgrid::mc::Snapshot::snapshot_stacks(simgrid::mc::RemoteClient* process)
158 for (auto const& stack : process->stack_areas()) {
159 s_mc_snapshot_stack_t st;
161 // Read the context from remote process:
162 unw_context_t context;
163 process->read_bytes(&context, sizeof(context), remote(stack.context));
165 st.context.initialize(process, &context);
167 st.stack_frames = unwind_stack_frames(&st.context);
168 st.local_variables = get_local_variables_values(st.stack_frames);
170 unw_word_t sp = st.stack_frames[0].sp;
172 stacks_.push_back(std::move(st));
174 size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
175 stack_sizes_.push_back(stack_size);
179 static void snapshot_handle_ignore(simgrid::mc::Snapshot* snapshot)
181 xbt_assert(snapshot->process());
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));
194 for (auto const& region : snapshot->process()->ignored_regions())
195 snapshot->process()->clear_bytes(remote(region.addr), region.size);
197 static void snapshot_ignore_restore(simgrid::mc::Snapshot* snapshot)
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));
203 Snapshot::Snapshot(int num_state, RemoteClient* process)
204 : AddressSpace(process), num_state_(num_state), heap_bytes_used_(0), enabled_processes_(), hash_(0)
206 XBT_DEBUG("Taking snapshot %i", num_state);
208 for (auto const& p : process->actors())
209 enabled_processes_.insert(p.copy.get_buffer()->get_pid());
211 snapshot_handle_ignore(this);
213 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
214 snapshot_regions(process);
216 to_ignore_ = process->ignored_heap();
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);
223 snapshot_ignore_restore(this);
226 void Snapshot::add_region(RegionType type, ObjectInformation* object_info, void* start_addr, std::size_t size)
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.");
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)));
238 void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, ReadOptions options) const
240 Region* region = this->get_region((void*)address.address());
242 void* res = region->read(buffer, (void*)address.address(), size);
243 if (buffer == res || options & ReadOptions::lazy())
246 memcpy(buffer, res, size);
250 return this->process()->read_bytes(buffer, size, address, options);
252 /** @brief Find the snapshoted region from a pointer
254 * @param addr Pointer
256 Region* Snapshot::get_region(const void* addr) const
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))))
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
273 if (hinted_region->contain(simgrid::mc::remote(addr)))
274 return hinted_region;
276 return get_region(addr);
279 void Snapshot::restore(RemoteClient* process)
281 XBT_DEBUG("Restore snapshot %i", num_state_);
284 for (std::unique_ptr<simgrid::mc::Region> const& region : snapshot_regions_) {
285 if (region) // privatized variables are not snapshoted
286 region.get()->restore();
289 snapshot_ignore_restore(this);
290 process->clear_cache();
294 } // namespace simgrid