1 /* Copyright (c) 2014-2022. 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"
10 #include <cstddef> /* std::size_t */
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_snapshot, mc, "Taking and restoring snapshots");
15 /************************************* Take Snapshot ************************************/
16 /****************************************************************************************/
18 void Snapshot::snapshot_regions(RemoteProcess* process)
20 snapshot_regions_.clear();
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);
25 const s_xbt_mheap_t* heap = process->get_heap();
26 void* start_heap = heap->base;
27 void* end_heap = heap->breakval;
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());
33 /** @brief Checks whether the variable is in scope for a given IP.
35 * A variable may be defined only from a given value of IP.
37 * @param var Variable description
38 * @param scope Scope description
39 * @param ip Instruction pointer
40 * @return true if the variable is valid
42 static bool valid_variable(const simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
44 // The variable is not yet valid:
45 if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
51 static void fill_local_variables_values(mc_stack_frame_t stack_frame, Frame* scope,
52 std::vector<s_local_variable_t>& result)
54 if (not scope || not scope->range.contain(stack_frame->ip))
57 for (const Variable& current_variable : scope->variables) {
58 if (not valid_variable(¤t_variable, scope, (void*)stack_frame->ip))
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;
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_process());
75 xbt_assert(location.in_memory(), "Cannot handle non-address variable");
76 new_var.address = location.address();
78 xbt_die("No address");
80 result.push_back(std::move(new_var));
83 // Recursive processing of nested scopes:
84 for (Frame& nested_scope : scope->scopes)
85 fill_local_variables_values(stack_frame, &nested_scope, result);
88 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames)
90 std::vector<s_local_variable_t> variables;
91 for (s_mc_stack_frame_t& stack_frame : stack_frames)
92 fill_local_variables_values(&stack_frame, stack_frame.frame, variables);
96 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(UnwindContext* stack_context)
98 const RemoteProcess* process = &mc_model_checker->get_remote_process();
99 std::vector<s_mc_stack_frame_t> result;
101 unw_cursor_t c = stack_context->cursor();
103 // TODO, check condition check (unw_init_local==0 means end of frame)
106 s_mc_stack_frame_t stack_frame;
108 stack_frame.unw_cursor = c;
113 unw_get_reg(&c, UNW_REG_IP, &ip);
114 unw_get_reg(&c, UNW_REG_SP, &sp);
119 // TODO, use real addresses in frame_t instead of fixing it here
121 Frame* frame = process->find_function(remote(ip));
122 stack_frame.frame = frame;
125 stack_frame.frame_name = frame->name;
126 stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
128 stack_frame.frame_base = 0;
129 stack_frame.frame_name = std::string();
132 result.push_back(std::move(stack_frame));
134 /* Stop before context switch with maestro */
135 if (frame != nullptr && frame->name == "smx_ctx_wrapper")
138 int ret = unw_step(&c);
139 xbt_assert(ret >= 0, "Error while unwinding stack");
140 xbt_assert(ret != 0, "Unexpected end of stack.");
143 xbt_assert(not result.empty(), "unw_init_local failed");
148 void Snapshot::snapshot_stacks(RemoteProcess* process)
150 for (auto const& stack : process->stack_areas()) {
151 s_mc_snapshot_stack_t st;
153 // Read the context from remote process:
154 unw_context_t context;
155 process->read_bytes(&context, sizeof(context), remote(stack.context));
157 st.context.initialize(process, &context);
159 st.stack_frames = unwind_stack_frames(&st.context);
160 st.local_variables = get_local_variables_values(st.stack_frames);
162 unw_word_t sp = st.stack_frames[0].sp;
164 stacks_.push_back(std::move(st));
166 size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
167 stack_sizes_.push_back(stack_size);
171 static void snapshot_handle_ignore(Snapshot* snapshot)
173 xbt_assert(snapshot->get_remote_process());
176 for (auto const& region : snapshot->get_remote_process()->ignored_regions()) {
177 s_mc_snapshot_ignored_data_t ignored_data;
178 ignored_data.start = (void*)region.addr;
179 ignored_data.data.resize(region.size);
180 // TODO, we should do this once per privatization segment:
181 snapshot->get_remote_process()->read_bytes(ignored_data.data.data(), region.size, remote(region.addr));
182 snapshot->ignored_data_.push_back(std::move(ignored_data));
186 for (auto const& region : snapshot->get_remote_process()->ignored_regions())
187 snapshot->get_remote_process()->clear_bytes(remote(region.addr), region.size);
190 static void snapshot_ignore_restore(const simgrid::mc::Snapshot* snapshot)
192 for (auto const& ignored_data : snapshot->ignored_data_)
193 snapshot->get_remote_process()->write_bytes(ignored_data.data.data(), ignored_data.data.size(),
194 remote(ignored_data.start));
197 Snapshot::Snapshot(long num_state, RemoteProcess* process) : AddressSpace(process), num_state_(num_state)
199 XBT_DEBUG("Taking snapshot %ld", num_state);
201 for (auto const& p : process->actors())
202 enabled_processes_.insert(p.copy.get_buffer()->get_pid());
204 snapshot_handle_ignore(this);
206 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
207 snapshot_regions(process);
209 to_ignore_ = process->ignored_heap();
211 if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
212 snapshot_stacks(process);
213 hash_ = simgrid::mc::hash(*this);
216 snapshot_ignore_restore(this);
219 void Snapshot::add_region(RegionType type, ObjectInformation* object_info, void* start_addr, std::size_t size)
221 if (type == RegionType::Data)
222 xbt_assert(object_info, "Missing object info for object.");
223 else if (type == RegionType::Heap)
224 xbt_assert(not object_info, "Unexpected object info for heap region.");
226 auto* region = new Region(type, start_addr, size);
227 region->object_info(object_info);
228 snapshot_regions_.push_back(std::unique_ptr<Region>(region));
231 void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, ReadOptions options) const
233 const Region* region = this->get_region((void*)address.address());
235 void* res = region->read(buffer, (void*)address.address(), size);
236 if (buffer == res || options & ReadOptions::lazy())
239 memcpy(buffer, res, size);
243 return this->get_remote_process()->read_bytes(buffer, size, address, options);
245 /** @brief Find the snapshotted region from a pointer
247 * @param addr Pointer
249 Region* Snapshot::get_region(const void* addr) const
251 size_t n = snapshot_regions_.size();
252 for (size_t i = 0; i != n; ++i) {
253 Region* region = snapshot_regions_[i].get();
254 if (not(region && region->contain(simgrid::mc::remote(addr))))
263 /** @brief Find the snapshotted region from a pointer, with a hinted_region */
264 Region* Snapshot::get_region(const void* addr, Region* hinted_region) const
266 if (hinted_region->contain(simgrid::mc::remote(addr)))
267 return hinted_region;
269 return get_region(addr);
272 void Snapshot::restore(RemoteProcess* process) const
274 XBT_DEBUG("Restore snapshot %ld", num_state_);
277 for (std::unique_ptr<Region> const& region : snapshot_regions_) {
278 if (region) // privatized variables are not snapshotted
279 region.get()->restore();
282 snapshot_ignore_restore(this);
283 process->clear_cache();
287 } // namespace simgrid