1 /* Copyright (c) 2008-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #include "src/internal_config.h"
15 #include "src/mc/mc_private.h"
16 #include "xbt/module.h"
17 #include <xbt/mmalloc.h>
18 #include <xbt/memory.hpp>
19 #include "src/smpi/private.h"
21 #include "src/xbt/mmalloc/mmprivate.h"
23 #include "src/simix/smx_private.h"
25 #include <libunwind.h>
28 #include "src/mc/mc_private.h"
31 #include "src/mc/mc_snapshot.h"
32 #include "src/mc/mc_mmu.h"
33 #include "src/mc/mc_unw.h"
34 #include "src/mc/mc_protocol.h"
35 #include "src/mc/mc_smx.h"
36 #include "mc_hash.hpp"
38 #include "src/mc/RegionSnapshot.hpp"
39 #include "src/mc/ObjectInformation.hpp"
40 #include "src/mc/Frame.hpp"
41 #include "src/mc/Variable.hpp"
43 using simgrid::mc::remote;
45 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
46 "Logging specific to mc_checkpoint");
51 /************************************ Free functions **************************************/
52 /*****************************************************************************************/
54 /** @brief Restore a region from a snapshot
56 * @param reg Target region
58 static void restore(mc_mem_region_t region)
60 switch(region->storage_type()) {
61 case simgrid::mc::StorageType::NoData:
63 xbt_die("Storage type not supported");
66 case simgrid::mc::StorageType::Flat:
67 mc_model_checker->process().write_bytes(region->flat_data().get(),
68 region->size(), region->permanent_address());
71 case simgrid::mc::StorageType::Chunked:
72 mc_region_restore_sparse(&mc_model_checker->process(), region);
75 case simgrid::mc::StorageType::Privatized:
76 for (auto& p : region->privatized_data())
83 RegionSnapshot privatized_region(
84 RegionType region_type, void *start_addr, void* permanent_addr,
85 std::size_t size, const RegionSnapshot* ref_region
88 size_t process_count = MC_smpi_process_count();
90 // Read smpi_privatisation_regions from MCed:
91 smpi_privatisation_region_t remote_smpi_privatisation_regions;
92 mc_model_checker->process().read_variable(
93 "smpi_privatisation_regions",
94 &remote_smpi_privatisation_regions, sizeof(remote_smpi_privatisation_regions));
95 s_smpi_privatisation_region_t privatisation_regions[process_count];
96 mc_model_checker->process().read_bytes(
97 &privatisation_regions, sizeof(privatisation_regions),
98 remote(remote_smpi_privatisation_regions));
100 std::vector<simgrid::mc::RegionSnapshot> data;
101 data.reserve(process_count);
102 for (size_t i = 0; i < process_count; i++) {
103 const simgrid::mc::RegionSnapshot* ref_privatized_region = nullptr;
104 if (ref_region && ref_region->storage_type() == StorageType::Privatized)
105 ref_privatized_region = &ref_region->privatized_data()[i];
106 data.push_back(simgrid::mc::region(region_type, start_addr,
107 privatisation_regions[i].address, size, ref_privatized_region));
110 simgrid::mc::RegionSnapshot region = simgrid::mc::RegionSnapshot(
111 region_type, start_addr, permanent_addr, size);
112 region.privatized_data(std::move(data));
113 return std::move(region);
118 void add_region(int index, simgrid::mc::Snapshot* snapshot,
119 simgrid::mc::RegionType type,
120 simgrid::mc::ObjectInformation* object_info,
121 void *start_addr, void* permanent_addr,
124 if (type == simgrid::mc::RegionType::Data)
125 xbt_assert(object_info, "Missing object info for object.");
126 else if (type == simgrid::mc::RegionType::Heap)
127 xbt_assert(!object_info, "Unexpected object info for heap region.");
129 simgrid::mc::RegionSnapshot const* ref_region = nullptr;
130 if (mc_model_checker->parent_snapshot_)
131 ref_region = mc_model_checker->parent_snapshot_->snapshot_regions[index].get();
133 simgrid::mc::RegionSnapshot region;
135 const bool privatization_aware = object_info
136 && mc_model_checker->process().privatized(*object_info);
137 if (privatization_aware && MC_smpi_process_count())
138 region = simgrid::mc::privatized_region(
139 type, start_addr, permanent_addr, size, ref_region);
142 region = simgrid::mc::region(type, start_addr, permanent_addr, size, ref_region);
144 region.object_info(object_info);
145 snapshot->snapshot_regions[index]
146 = std::unique_ptr<simgrid::mc::RegionSnapshot>(
147 new simgrid::mc::RegionSnapshot(std::move(region)));
151 static void get_memory_regions(simgrid::mc::Process* process, simgrid::mc::Snapshot* snapshot)
153 const size_t n = process->object_infos.size();
154 snapshot->snapshot_regions.resize(n + 1);
156 for (auto const& object_info : process->object_infos)
157 add_region(i++, snapshot, simgrid::mc::RegionType::Data,
159 object_info->start_rw, object_info->start_rw,
160 object_info->end_rw - object_info->start_rw);
162 xbt_mheap_t heap = process->get_heap();
163 void *start_heap = heap->base;
164 void *end_heap = heap->breakval;
166 add_region(n, snapshot, simgrid::mc::RegionType::Heap, nullptr,
167 start_heap, start_heap,
168 (char *) end_heap - (char *) start_heap);
169 snapshot->heap_bytes_used = mmalloc_get_bytes_used_remote(
171 process->get_malloc_info());
174 if (mc_model_checker->process().privatized() && MC_smpi_process_count())
175 // snapshot->privatization_index = smpi_loaded_page
176 mc_model_checker->process().read_variable(
177 "smpi_loaded_page", &snapshot->privatization_index,
178 sizeof(snapshot->privatization_index));
181 snapshot->privatization_index = simgrid::mc::ProcessIndexMissing;
184 /** \brief Fills the position of the segments (executable, read-only, read/write).
186 * `dl_iterate_phdr` would be more robust but would not work in cross-process.
188 void find_object_address(
189 std::vector<simgrid::xbt::VmMap> const& maps,
190 simgrid::mc::ObjectInformation* result)
192 char* name = xbt_basename(result->file_name.c_str());
194 for (size_t i = 0; i < maps.size(); ++i) {
195 simgrid::xbt::VmMap const& reg = maps[i];
196 if (maps[i].pathname.empty())
198 char* map_basename = xbt_basename(maps[i].pathname.c_str());
199 if (strcmp(name, map_basename) != 0) {
204 if ((reg.prot & PROT_WRITE)) {
205 xbt_assert(!result->start_rw,
206 "Multiple read-write segments for %s, not supported",
207 maps[i].pathname.c_str());
208 result->start_rw = (char*) reg.start_addr;
209 result->end_rw = (char*) reg.end_addr;
210 // .bss is usually after the .data:
211 simgrid::xbt::VmMap const& next = maps[i + 1];
212 if (next.pathname.empty() && (next.prot & PROT_WRITE)
213 && next.start_addr == reg.end_addr)
214 result->end_rw = (char*) maps[i + 1].end_addr;
215 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
216 xbt_assert(!result->start_exec,
217 "Multiple executable segments for %s, not supported",
218 maps[i].pathname.c_str());
219 result->start_exec = (char*) reg.start_addr;
220 result->end_exec = (char*) reg.end_addr;
221 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
222 xbt_assert(!result->start_ro,
223 "Multiple read only segments for %s, not supported",
224 maps[i].pathname.c_str());
225 result->start_ro = (char*) reg.start_addr;
226 result->end_ro = (char*) reg.end_addr;
230 result->start = result->start_rw;
231 if ((const void*) result->start_ro > result->start)
232 result->start = result->start_ro;
233 if ((const void*) result->start_exec > result->start)
234 result->start = result->start_exec;
236 result->end = result->end_rw;
237 if (result->end_ro && (const void*) result->end_ro < result->end)
238 result->end = result->end_ro;
239 if (result->end_exec && (const void*) result->end_exec > result->end)
240 result->end = result->end_exec;
242 xbt_assert(result->start_rw);
243 xbt_assert(result->start_exec);
248 /************************************* Take Snapshot ************************************/
249 /****************************************************************************************/
251 /** \brief Checks whether the variable is in scope for a given IP.
253 * A variable may be defined only from a given value of IP.
255 * \param var Variable description
256 * \param frame Scope description
257 * \param ip Instruction pointer
258 * \return true if the variable is valid
260 static bool valid_variable(simgrid::mc::Variable* var,
261 simgrid::mc::Frame* scope,
264 // The variable is not yet valid:
265 if (scope->range.begin() + var->start_scope > (std::uint64_t) ip)
271 static void fill_local_variables_values(mc_stack_frame_t stack_frame,
272 simgrid::mc::Frame* scope,
274 std::vector<s_local_variable>& result)
276 simgrid::mc::Process* process = &mc_model_checker->process();
278 if (!scope->range.contain(stack_frame->ip))
281 for(simgrid::mc::Variable& current_variable :
284 if (!valid_variable(¤t_variable, scope, (void *) stack_frame->ip))
288 // FIXME, get rid of `region_type`
289 if ((long) stack_frame->ip > (long) process->libsimgrid_info->start_exec)
294 s_local_variable_t new_var;
295 new_var.subprogram = stack_frame->frame;
296 new_var.ip = stack_frame->ip;
297 new_var.name = current_variable.name;
298 new_var.type = current_variable.type;
299 new_var.region = region_type;
300 new_var.address = nullptr;
302 if (current_variable.address != nullptr)
303 new_var.address = current_variable.address;
304 else if (!current_variable.location_list.empty()) {
305 simgrid::dwarf::Location location =
306 simgrid::dwarf::resolve(
307 current_variable.location_list,
308 current_variable.object_info,
309 &(stack_frame->unw_cursor),
310 (void *) stack_frame->frame_base,
311 &mc_model_checker->process(), process_index);
313 if (!location.in_memory())
314 xbt_die("Cannot handle non-address variable");
315 new_var.address = location.address();
318 xbt_die("No address");
320 result.push_back(std::move(new_var));
323 // Recursive processing of nested scopes:
324 for(simgrid::mc::Frame& nested_scope : scope->scopes)
325 fill_local_variables_values(
326 stack_frame, &nested_scope, process_index, result);
329 static std::vector<s_local_variable> get_local_variables_values(
330 std::vector<s_mc_stack_frame_t>& stack_frames, int process_index)
332 std::vector<s_local_variable> variables;
333 for (s_mc_stack_frame_t& stack_frame : stack_frames)
334 fill_local_variables_values(&stack_frame, stack_frame.frame, process_index, variables);
335 return std::move(variables);
338 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindContext* stack_context)
340 simgrid::mc::Process* process = &mc_model_checker->process();
341 std::vector<s_mc_stack_frame_t> result;
343 unw_cursor_t c = stack_context->cursor();
345 // TODO, check condition check (unw_init_local==0 means end of frame)
349 s_mc_stack_frame_t stack_frame;
351 stack_frame.unw_cursor = c;
355 unw_get_reg(&c, UNW_REG_IP, &ip);
356 unw_get_reg(&c, UNW_REG_SP, &sp);
361 // TODO, use real addresses in frame_t instead of fixing it here
363 simgrid::mc::Frame* frame = process->find_function(remote(ip));
364 stack_frame.frame = frame;
367 stack_frame.frame_name = frame->name;
368 stack_frame.frame_base =
369 (unw_word_t) frame->frame_base(c);
371 stack_frame.frame_base = 0;
372 stack_frame.frame_name = std::string();
375 result.push_back(std::move(stack_frame));
377 /* Stop before context switch with maestro */
378 if (frame != nullptr &&
379 frame->name == "smx_ctx_sysv_wrapper")
382 int ret = unw_step(&c);
384 xbt_die("Unexpected end of stack.");
386 xbt_die("Error while unwinding stack");
389 if (result.empty()) {
390 XBT_INFO("unw_init_local failed");
394 return std::move(result);
397 static std::vector<s_mc_snapshot_stack_t> take_snapshot_stacks(simgrid::mc::Snapshot* * snapshot)
399 std::vector<s_mc_snapshot_stack_t> res;
401 for (auto const& stack : mc_model_checker->process().stack_areas()) {
402 s_mc_snapshot_stack_t st;
404 // Read the context from remote process:
405 unw_context_t context;
406 mc_model_checker->process().read_bytes(
407 &context, sizeof(context), remote(stack.context));
409 st.context.initialize(&mc_model_checker->process(), &context);
411 st.stack_frames = unwind_stack_frames(&st.context);
412 st.local_variables = get_local_variables_values(st.stack_frames, stack.process_index);
413 st.process_index = stack.process_index;
415 unw_word_t sp = st.stack_frames[0].sp;
417 res.push_back(std::move(st));
420 (char*) stack.address + stack.size - (char*) sp;
421 (*snapshot)->stack_sizes.push_back(stack_size);
424 return std::move(res);
428 static void snapshot_handle_ignore(simgrid::mc::Snapshot* snapshot)
430 xbt_assert(snapshot->process());
433 for (auto const& region : mc_model_checker->process().ignored_regions()) {
434 s_mc_snapshot_ignored_data_t ignored_data;
435 ignored_data.start = (void*)region.addr;
436 ignored_data.data.resize(region.size);
437 // TODO, we should do this once per privatization segment:
438 snapshot->process()->read_bytes(
439 ignored_data.data.data(), region.size, remote(region.addr),
440 simgrid::mc::ProcessIndexDisabled);
441 snapshot->ignored_data.push_back(std::move(ignored_data));
445 for(auto const& region : mc_model_checker->process().ignored_regions())
446 snapshot->process()->clear_bytes(remote(region.addr), region.size);
450 static void snapshot_ignore_restore(simgrid::mc::Snapshot* snapshot)
452 for (auto const& ignored_data : snapshot->ignored_data)
453 snapshot->process()->write_bytes(
454 ignored_data.data.data(), ignored_data.data.size(),
455 remote(ignored_data.start));
458 static std::vector<s_fd_infos_t> get_current_fds(pid_t pid)
460 const size_t fd_dir_path_size = 20;
461 char fd_dir_path[fd_dir_path_size];
462 int res = snprintf(fd_dir_path, fd_dir_path_size,
463 "/proc/%lli/fd", (long long int) pid);
464 xbt_assert(res >= 0);
465 if ((size_t) res > fd_dir_path_size)
466 xbt_die("Unexpected buffer is too small for fd_dir_path");
468 DIR* fd_dir = opendir(fd_dir_path);
469 if (fd_dir == nullptr)
470 xbt_die("Cannot open directory '/proc/self/fd'\n");
472 std::vector<s_fd_infos_t> fds;
474 struct dirent* fd_number;
475 while ((fd_number = readdir(fd_dir))) {
477 int fd_value = xbt_str_parse_int(fd_number->d_name, "Found a non-numerical FD: %s. Freaking out!");
482 const size_t source_size = 25;
484 int res = snprintf(source, source_size, "/proc/%lli/fd/%s",
485 (long long int) pid, fd_number->d_name);
486 xbt_assert(res >= 0);
487 if ((size_t) res > source_size)
488 xbt_die("Unexpected buffer is too small for fd %s", fd_number->d_name);
490 const size_t link_size = 200;
492 res = readlink(source, link, link_size);
495 xbt_die("Could not read link for %s", source);
497 xbt_die("Buffer to small for link of %s", source);
502 if(smpi_is_privatisation_file(link))
506 // This is (probably) the DIR* we are reading:
507 // TODO, read all the file entries at once and close the DIR.*
508 if(strcmp(fd_dir_path, link) == 0)
511 // We don't handle them.
512 // It does not mean we should silently ignore them however.
513 if (strncmp(link, "pipe:", 5) == 0 || strncmp(link, "socket:", 7) == 0)
516 // If dot_output enabled, do not handle the corresponding file
517 if (dot_output != nullptr) {
518 char* link_basename = xbt_basename(link);
519 if (strcmp(link_basename, _sg_mc_dot_output_file) == 0) {
526 // This is probably a shared memory used by lttng-ust:
527 if(strncmp("/dev/shm/ust-shm-tmp-", link, 21)==0)
530 // Add an entry for this FD in the snapshot:
532 fd.filename = std::string(link);
533 fd.number = fd_value;
534 fd.flags = fcntl(fd_value, F_GETFL) | fcntl(fd_value, F_GETFD) ;
535 fd.current_position = lseek(fd_value, 0, SEEK_CUR);
536 fds.push_back(std::move(fd));
540 return std::move(fds);
543 simgrid::mc::Snapshot* take_snapshot(int num_state)
545 XBT_DEBUG("Taking snapshot %i", num_state);
547 simgrid::mc::Process* mc_process = &mc_model_checker->process();
549 simgrid::mc::Snapshot* snapshot = new simgrid::mc::Snapshot(mc_process);
551 snapshot->num_state = num_state;
553 for (auto& p : mc_model_checker->process().simix_processes())
554 snapshot->enabled_processes.insert(p.copy.pid);
556 snapshot_handle_ignore(snapshot);
558 if (_sg_mc_snapshot_fds)
559 snapshot->current_fds = get_current_fds(mc_model_checker->process().pid());
561 const bool use_soft_dirty = _sg_mc_sparse_checkpoint && _sg_mc_soft_dirty;
563 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
564 get_memory_regions(mc_process, snapshot);
566 mc_process->reset_soft_dirty();
568 snapshot->to_ignore = mc_model_checker->process().ignored_heap();
570 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
572 take_snapshot_stacks(&snapshot);
574 snapshot->hash = simgrid::mc::hash(*snapshot);
580 snapshot_ignore_restore(snapshot);
582 mc_model_checker->parent_snapshot_ = snapshot;
587 void restore_snapshot_regions(simgrid::mc::Snapshot* snapshot)
589 for(std::unique_ptr<s_mc_mem_region_t> const& region : snapshot->snapshot_regions) {
590 // For privatized, variables we decided it was not necessary to take the snapshot:
592 restore(region.get());
596 // TODO, send a message to implement this in the MCed process
597 if(snapshot->privatization_index >= 0) {
598 // Fix the privatization mmap:
599 s_mc_restore_message message;
600 message.type = MC_MESSAGE_RESTORE;
601 message.index = snapshot->privatization_index;
602 mc_model_checker->process().getChannel().send(message);
608 void restore_snapshot_fds(simgrid::mc::Snapshot* snapshot)
610 if (mc_mode == MC_MODE_SERVER)
611 xbt_die("FD snapshot not implemented in client/server mode.");
613 for (auto const& fd : snapshot->current_fds) {
615 int new_fd = open(fd.filename.c_str(), fd.flags);
617 xbt_die("Could not reopen the file %s fo restoring the file descriptor",
618 fd.filename.c_str());
619 if (new_fd != fd.number) {
620 dup2(new_fd, fd.number);
623 lseek(fd.number, fd.current_position, SEEK_SET);
627 void restore_snapshot(simgrid::mc::Snapshot* snapshot)
629 XBT_DEBUG("Restore snapshot %i", snapshot->num_state);
630 const bool use_soft_dirty = _sg_mc_sparse_checkpoint && _sg_mc_soft_dirty;
631 restore_snapshot_regions(snapshot);
632 if (_sg_mc_snapshot_fds)
633 restore_snapshot_fds(snapshot);
635 mc_model_checker->process().reset_soft_dirty();
636 snapshot_ignore_restore(snapshot);
637 mc_model_checker->process().clear_cache();
639 mc_model_checker->parent_snapshot_ = snapshot;