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. */
18 #include "src/internal_config.h"
19 #include "src/mc/mc_private.h"
20 #include "src/smpi/private.h"
21 #include "xbt/mmalloc.h"
22 #include "xbt/module.h"
24 #include "src/xbt/mmalloc/mmprivate.h"
26 #include "src/simix/smx_private.h"
28 #include <libunwind.h>
31 #include "src/mc/mc_private.h"
34 #include "src/mc/mc_hash.hpp"
35 #include "src/mc/mc_mmu.h"
36 #include "src/mc/mc_smx.h"
37 #include "src/mc/mc_snapshot.h"
38 #include "src/mc/mc_unw.h"
39 #include "src/mc/remote/mc_protocol.h"
41 #include "src/mc/RegionSnapshot.hpp"
42 #include "src/mc/ObjectInformation.hpp"
43 #include "src/mc/Frame.hpp"
44 #include "src/mc/Variable.hpp"
46 using simgrid::mc::remote;
48 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc, "Logging specific to mc_checkpoint");
53 /************************************ Free functions **************************************/
54 /*****************************************************************************************/
56 /** @brief Restore a region from a snapshot
58 * @param region Target region
60 static void restore(mc_mem_region_t region)
62 switch(region->storage_type()) {
63 case simgrid::mc::StorageType::NoData:
65 xbt_die("Storage type not supported");
68 case simgrid::mc::StorageType::Flat:
69 mc_model_checker->process().write_bytes(region->flat_data().get(),
70 region->size(), region->permanent_address());
73 case simgrid::mc::StorageType::Chunked:
74 mc_region_restore_sparse(&mc_model_checker->process(), region);
77 case simgrid::mc::StorageType::Privatized:
78 for (auto& p : region->privatized_data())
85 RegionSnapshot privatized_region(
86 RegionType region_type, void *start_addr, void* permanent_addr,
90 size_t process_count = MC_smpi_process_count();
92 // Read smpi_privatisation_regions from MCed:
93 smpi_privatisation_region_t remote_smpi_privatisation_regions;
94 mc_model_checker->process().read_variable(
95 "smpi_privatisation_regions",
96 &remote_smpi_privatisation_regions, sizeof(remote_smpi_privatisation_regions));
97 s_smpi_privatisation_region_t privatisation_regions[process_count];
98 mc_model_checker->process().read_bytes(
99 &privatisation_regions, sizeof(privatisation_regions),
100 remote(remote_smpi_privatisation_regions));
102 std::vector<simgrid::mc::RegionSnapshot> data;
103 data.reserve(process_count);
104 for (size_t i = 0; i < process_count; i++)
105 data.push_back(simgrid::mc::region(region_type, start_addr,
106 privatisation_regions[i].address, size));
108 simgrid::mc::RegionSnapshot region = simgrid::mc::RegionSnapshot(
109 region_type, start_addr, permanent_addr, size);
110 region.privatized_data(std::move(data));
116 void add_region(int index, simgrid::mc::Snapshot* snapshot,
117 simgrid::mc::RegionType type,
118 simgrid::mc::ObjectInformation* object_info,
119 void *start_addr, void* permanent_addr,
122 if (type == simgrid::mc::RegionType::Data)
123 xbt_assert(object_info, "Missing object info for object.");
124 else if (type == simgrid::mc::RegionType::Heap)
125 xbt_assert(not object_info, "Unexpected object info for heap region.");
127 simgrid::mc::RegionSnapshot region;
129 const bool privatization_aware = object_info
130 && mc_model_checker->process().privatized(*object_info);
131 if (privatization_aware && MC_smpi_process_count())
132 region = simgrid::mc::privatized_region(
133 type, start_addr, permanent_addr, size);
136 region = simgrid::mc::region(type, start_addr, permanent_addr, size);
138 region.object_info(object_info);
139 snapshot->snapshot_regions[index]
140 = std::unique_ptr<simgrid::mc::RegionSnapshot>(
141 new simgrid::mc::RegionSnapshot(std::move(region)));
145 static void get_memory_regions(simgrid::mc::Process* process, simgrid::mc::Snapshot* snapshot)
147 const size_t n = process->object_infos.size();
148 snapshot->snapshot_regions.resize(n + 1);
150 for (auto const& object_info : process->object_infos)
151 add_region(i++, snapshot, simgrid::mc::RegionType::Data,
153 object_info->start_rw, object_info->start_rw,
154 object_info->end_rw - object_info->start_rw);
156 xbt_mheap_t heap = process->get_heap();
157 void *start_heap = heap->base;
158 void *end_heap = heap->breakval;
160 add_region(n, snapshot, simgrid::mc::RegionType::Heap, nullptr,
161 start_heap, start_heap,
162 (char *) end_heap - (char *) start_heap);
163 snapshot->heap_bytes_used = mmalloc_get_bytes_used_remote(
165 process->get_malloc_info());
168 if (mc_model_checker->process().privatized() && MC_smpi_process_count())
169 // snapshot->privatization_index = smpi_loaded_page
170 mc_model_checker->process().read_variable(
171 "smpi_loaded_page", &snapshot->privatization_index,
172 sizeof(snapshot->privatization_index));
175 snapshot->privatization_index = simgrid::mc::ProcessIndexMissing;
178 #define PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
179 #define PROT_RW (PROT_READ | PROT_WRITE)
180 #define PROT_RX (PROT_READ | PROT_EXEC)
182 /** \brief Fills the position of the segments (executable, read-only, read/write).
184 // TODO, use the ELF segment information for more robustness
185 void find_object_address(
186 std::vector<simgrid::xbt::VmMap> const& maps,
187 simgrid::mc::ObjectInformation* result)
189 char* name = xbt_basename(result->file_name.c_str());
191 for (size_t i = 0; i < maps.size(); ++i) {
192 simgrid::xbt::VmMap const& reg = maps[i];
193 if (maps[i].pathname.empty())
195 char* map_basename = xbt_basename(maps[i].pathname.c_str());
196 if (strcmp(name, map_basename) != 0) {
202 // This is the non-GNU_RELRO-part of the data segment:
203 if (reg.prot == PROT_RW) {
204 xbt_assert(not result->start_rw, "Multiple read-write segments for %s, not supported", maps[i].pathname.c_str());
205 result->start_rw = (char*) reg.start_addr;
206 result->end_rw = (char*) reg.end_addr;
208 // The next VMA might be end of the data segment:
209 if (i + 1 < maps.size()
210 && maps[i + 1].pathname.empty()
211 && maps[i + 1].prot == PROT_RW
212 && maps[i + 1].start_addr == reg.end_addr)
213 result->end_rw = (char*) maps[i + 1].end_addr;
216 // This is the text segment:
217 else if (reg.prot == PROT_RX) {
218 xbt_assert(not result->start_exec, "Multiple executable segments for %s, not supported",
219 maps[i].pathname.c_str());
220 result->start_exec = (char*) reg.start_addr;
221 result->end_exec = (char*) reg.end_addr;
223 // The next VMA might be end of the data segment:
224 if (i + 1 < maps.size()
225 && maps[i + 1].pathname.empty()
226 && maps[i + 1].prot == PROT_RW
227 && maps[i + 1].start_addr == reg.end_addr) {
228 result->start_rw = (char*) maps[i + 1].start_addr;
229 result->end_rw = (char*) maps[i + 1].end_addr;
233 // This is the GNU_RELRO-part of the data segment:
234 else if (reg.prot == PROT_READ) {
235 xbt_assert(not result->start_ro, "Multiple read only segments for %s, not supported", maps[i].pathname.c_str());
236 result->start_ro = (char*) reg.start_addr;
237 result->end_ro = (char*) reg.end_addr;
241 result->start = result->start_rw;
242 if ((const void*) result->start_ro < result->start)
243 result->start = result->start_ro;
244 if ((const void*) result->start_exec < result->start)
245 result->start = result->start_exec;
247 result->end = result->end_rw;
248 if (result->end_ro && (const void*) result->end_ro > result->end)
249 result->end = result->end_ro;
250 if (result->end_exec && (const void*) result->end_exec > result->end)
251 result->end = result->end_exec;
253 xbt_assert(result->start_exec || result->start_rw || result->start_ro);
258 /************************************* Take Snapshot ************************************/
259 /****************************************************************************************/
261 /** \brief Checks whether the variable is in scope for a given IP.
263 * A variable may be defined only from a given value of IP.
265 * \param var Variable description
266 * \param scope Scope description
267 * \param ip Instruction pointer
268 * \return true if the variable is valid
270 static bool valid_variable(simgrid::mc::Variable* var,
271 simgrid::mc::Frame* scope,
274 // The variable is not yet valid:
275 if (scope->range.begin() + var->start_scope > (std::uint64_t) ip)
281 static void fill_local_variables_values(mc_stack_frame_t stack_frame,
282 simgrid::mc::Frame* scope,
284 std::vector<s_local_variable>& result)
286 simgrid::mc::Process* process = &mc_model_checker->process();
288 if (not scope || not scope->range.contain(stack_frame->ip))
291 for(simgrid::mc::Variable& current_variable :
294 if (not valid_variable(¤t_variable, scope, (void*)stack_frame->ip))
298 // FIXME, get rid of `region_type`
299 if ((long) stack_frame->ip > (long) process->libsimgrid_info->start_exec)
304 s_local_variable_t new_var;
305 new_var.subprogram = stack_frame->frame;
306 new_var.ip = stack_frame->ip;
307 new_var.name = current_variable.name;
308 new_var.type = current_variable.type;
309 new_var.region = region_type;
310 new_var.address = nullptr;
312 if (current_variable.address != nullptr)
313 new_var.address = current_variable.address;
314 else if (not current_variable.location_list.empty()) {
315 simgrid::dwarf::Location location =
316 simgrid::dwarf::resolve(
317 current_variable.location_list,
318 current_variable.object_info,
319 &(stack_frame->unw_cursor),
320 (void *) stack_frame->frame_base,
321 &mc_model_checker->process(), process_index);
323 if (not location.in_memory())
324 xbt_die("Cannot handle non-address variable");
325 new_var.address = location.address();
328 xbt_die("No address");
330 result.push_back(std::move(new_var));
333 // Recursive processing of nested scopes:
334 for(simgrid::mc::Frame& nested_scope : scope->scopes)
335 fill_local_variables_values(
336 stack_frame, &nested_scope, process_index, result);
339 static std::vector<s_local_variable> get_local_variables_values(
340 std::vector<s_mc_stack_frame_t>& stack_frames, int process_index)
342 std::vector<s_local_variable> variables;
343 for (s_mc_stack_frame_t& stack_frame : stack_frames)
344 fill_local_variables_values(&stack_frame, stack_frame.frame, process_index, variables);
348 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindContext* stack_context)
350 simgrid::mc::Process* process = &mc_model_checker->process();
351 std::vector<s_mc_stack_frame_t> result;
353 unw_cursor_t c = stack_context->cursor();
355 // TODO, check condition check (unw_init_local==0 means end of frame)
359 s_mc_stack_frame_t stack_frame;
361 stack_frame.unw_cursor = c;
366 unw_get_reg(&c, UNW_REG_IP, &ip);
367 unw_get_reg(&c, UNW_REG_SP, &sp);
372 // TODO, use real addresses in frame_t instead of fixing it here
374 simgrid::mc::Frame* frame = process->find_function(remote(ip));
375 stack_frame.frame = frame;
378 stack_frame.frame_name = frame->name;
379 stack_frame.frame_base =
380 (unw_word_t) frame->frame_base(c);
382 stack_frame.frame_base = 0;
383 stack_frame.frame_name = std::string();
386 result.push_back(std::move(stack_frame));
388 /* Stop before context switch with maestro */
389 if (frame != nullptr &&
390 frame->name == "smx_ctx_sysv_wrapper")
393 int ret = unw_step(&c);
395 xbt_die("Unexpected end of stack.");
397 xbt_die("Error while unwinding stack");
400 if (result.empty()) {
401 XBT_INFO("unw_init_local failed");
408 static std::vector<s_mc_snapshot_stack_t> take_snapshot_stacks(simgrid::mc::Snapshot* snapshot)
410 std::vector<s_mc_snapshot_stack_t> res;
412 for (auto const& stack : mc_model_checker->process().stack_areas()) {
413 s_mc_snapshot_stack_t st;
415 // Read the context from remote process:
416 unw_context_t context;
417 mc_model_checker->process().read_bytes(
418 &context, sizeof(context), remote(stack.context));
420 st.context.initialize(&mc_model_checker->process(), &context);
422 st.stack_frames = unwind_stack_frames(&st.context);
423 st.local_variables = get_local_variables_values(st.stack_frames, stack.process_index);
424 st.process_index = stack.process_index;
426 unw_word_t sp = st.stack_frames[0].sp;
428 res.push_back(std::move(st));
431 (char*) stack.address + stack.size - (char*) sp;
432 snapshot->stack_sizes.push_back(stack_size);
439 static void snapshot_handle_ignore(simgrid::mc::Snapshot* snapshot)
441 xbt_assert(snapshot->process());
444 for (auto const& region : mc_model_checker->process().ignored_regions()) {
445 s_mc_snapshot_ignored_data_t ignored_data;
446 ignored_data.start = (void*)region.addr;
447 ignored_data.data.resize(region.size);
448 // TODO, we should do this once per privatization segment:
449 snapshot->process()->read_bytes(
450 ignored_data.data.data(), region.size, remote(region.addr),
451 simgrid::mc::ProcessIndexDisabled);
452 snapshot->ignored_data.push_back(std::move(ignored_data));
456 for(auto const& region : mc_model_checker->process().ignored_regions())
457 snapshot->process()->clear_bytes(remote(region.addr), region.size);
461 static void snapshot_ignore_restore(simgrid::mc::Snapshot* snapshot)
463 for (auto const& ignored_data : snapshot->ignored_data)
464 snapshot->process()->write_bytes(
465 ignored_data.data.data(), ignored_data.data.size(),
466 remote(ignored_data.start));
469 static std::vector<s_fd_infos_t> get_current_fds(pid_t pid)
471 const size_t fd_dir_path_size = 20;
472 char fd_dir_path[fd_dir_path_size];
473 int res = snprintf(fd_dir_path, fd_dir_path_size,
474 "/proc/%lli/fd", (long long int) pid);
475 xbt_assert(res >= 0);
476 if ((size_t) res > fd_dir_path_size)
477 xbt_die("Unexpected buffer is too small for fd_dir_path");
479 DIR* fd_dir = opendir(fd_dir_path);
480 if (fd_dir == nullptr)
481 xbt_die("Cannot open directory '/proc/self/fd'\n");
483 std::vector<s_fd_infos_t> fds;
485 struct dirent* fd_number;
486 while ((fd_number = readdir(fd_dir))) {
488 int fd_value = xbt_str_parse_int(fd_number->d_name, "Found a non-numerical FD: %s. Freaking out!");
493 const size_t source_size = 25;
495 int res = snprintf(source, source_size, "/proc/%lli/fd/%s",
496 (long long int) pid, fd_number->d_name);
497 xbt_assert(res >= 0);
498 if ((size_t) res > source_size)
499 xbt_die("Unexpected buffer is too small for fd %s", fd_number->d_name);
501 const size_t link_size = 200;
503 res = readlink(source, link, link_size);
506 xbt_die("Could not read link for %s", source);
508 xbt_die("Buffer to small for link of %s", source);
513 if(smpi_is_privatisation_file(link))
517 // This is (probably) the DIR* we are reading:
518 // TODO, read all the file entries at once and close the DIR.*
519 if(strcmp(fd_dir_path, link) == 0)
522 // We don't handle them.
523 // It does not mean we should silently ignore them however.
524 if (strncmp(link, "pipe:", std::strlen("pipe:")) == 0 || strncmp(link, "socket:", std::strlen("socket:")) == 0)
527 // If dot_output enabled, do not handle the corresponding file
528 if (dot_output != nullptr) {
529 char* link_basename = xbt_basename(link);
530 if (strcmp(link_basename, _sg_mc_dot_output_file) == 0) {
537 // This is probably a shared memory used by lttng-ust:
538 if(strncmp("/dev/shm/ust-shm-tmp-", link, std::strlen("/dev/shm/ust-shm-tmp-"))==0)
541 // Add an entry for this FD in the snapshot:
543 fd.filename = std::string(link);
544 fd.number = fd_value;
545 fd.flags = fcntl(fd_value, F_GETFL) | fcntl(fd_value, F_GETFD) ;
546 fd.current_position = lseek(fd_value, 0, SEEK_CUR);
547 fds.push_back(std::move(fd));
554 std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state)
556 XBT_DEBUG("Taking snapshot %i", num_state);
558 simgrid::mc::Process* mc_process = &mc_model_checker->process();
560 std::shared_ptr<simgrid::mc::Snapshot> snapshot = std::make_shared<simgrid::mc::Snapshot>(mc_process, num_state);
562 for (auto& p : mc_model_checker->process().actors())
563 snapshot->enabled_processes.insert(p.copy.getBuffer()->pid);
565 snapshot_handle_ignore(snapshot.get());
567 if (_sg_mc_snapshot_fds)
568 snapshot->current_fds = get_current_fds(mc_model_checker->process().pid());
570 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
571 get_memory_regions(mc_process, snapshot.get());
573 snapshot->to_ignore = mc_model_checker->process().ignored_heap();
575 if (_sg_mc_max_visited_states > 0 || strcmp(_sg_mc_property_file, "")) {
576 snapshot->stacks = take_snapshot_stacks(snapshot.get());
578 snapshot->hash = simgrid::mc::hash(*snapshot);
584 snapshot_ignore_restore(snapshot.get());
589 void restore_snapshot_regions(simgrid::mc::Snapshot* snapshot)
591 for(std::unique_ptr<s_mc_mem_region_t> const& region : snapshot->snapshot_regions) {
592 // For privatized, variables we decided it was not necessary to take the snapshot:
594 restore(region.get());
598 // TODO, send a message to implement this in the MCed process
599 if(snapshot->privatization_index >= 0) {
600 // Fix the privatization mmap:
601 s_mc_restore_message message;
602 message.type = MC_MESSAGE_RESTORE;
603 message.index = snapshot->privatization_index;
604 mc_model_checker->process().getChannel().send(message);
610 void restore_snapshot_fds(simgrid::mc::Snapshot* snapshot)
612 xbt_die("FD snapshot not implemented in client/server mode.");
614 for (auto const& fd : snapshot->current_fds) {
616 int new_fd = open(fd.filename.c_str(), fd.flags);
618 xbt_die("Could not reopen the file %s fo restoring the file descriptor", 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(std::shared_ptr<simgrid::mc::Snapshot> snapshot)
629 XBT_DEBUG("Restore snapshot %i", snapshot->num_state);
630 restore_snapshot_regions(snapshot.get());
631 if (_sg_mc_snapshot_fds)
632 restore_snapshot_fds(snapshot.get());
633 snapshot_ignore_restore(snapshot.get());
634 mc_model_checker->process().clear_cache();