1 /* Copyright (c) 2014-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. */
7 #define _FILE_OFFSET_BITS 64
14 #include <sys/types.h>
18 #include <sys/mman.h> // PROT_*
24 #include <libunwind.h>
25 #include <libunwind-ptrace.h>
27 #include <xbt/mmalloc.h>
29 #include "src/mc/mc_object_info.h"
30 #include "src/mc/mc_unw.h"
31 #include "src/mc/mc_snapshot.h"
32 #include "src/mc/mc_ignore.h"
33 #include "src/mc/mc_smx.h"
35 #include "src/mc/Process.hpp"
36 #include "src/mc/AddressSpace.hpp"
37 #include "src/mc/ObjectInformation.hpp"
38 #include "src/mc/Variable.hpp"
40 using simgrid::mc::remote;
44 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
45 "MC process information");
49 #define SO_RE "\\.so[\\.0-9]*$"
50 #define VERSION_RE "-[\\.0-9-]*$"
52 static const char *const FILTERED_LIBS[] = {
82 static bool MC_is_simgrid_lib(const char* libname)
84 return !strcmp(libname, "libsimgrid");
87 static bool MC_is_filtered_lib(const char* libname)
89 for (const char* filtered_lib : FILTERED_LIBS)
90 if (strcmp(libname, filtered_lib)==0)
95 struct s_mc_memory_map_re {
100 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res)
102 const char* map_basename = xbt_basename((char*) pathname);
105 if(regexec(&res->so_re, map_basename, 1, &match, 0))
108 char* libname = strndup(map_basename, match.rm_so);
110 // Strip the version suffix:
111 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
112 char* temp = libname;
113 libname = strndup(temp, match.rm_so);
120 static ssize_t pread_whole(int fd, void *buf, size_t count, std::uint64_t offset)
122 char* buffer = (char*) buf;
123 ssize_t real_count = count;
125 ssize_t res = pread(fd, buffer, count, (std::int64_t) offset);
132 } else if (errno != EINTR) {
133 perror("pread_whole");
140 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
142 const char* buffer = (const char*) buf;
143 ssize_t real_count = count;
145 ssize_t res = pwrite(fd, buffer, count, offset);
152 } else if (errno != EINTR) {
159 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
160 static const void* zero_buffer;
161 static const size_t zero_buffer_size = 10 * 4096;
163 static void MC_zero_buffer_init(void)
165 int fd = open("/dev/zero", O_RDONLY);
167 xbt_die("Could not open /dev/zero");
168 zero_buffer = mmap(nullptr, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
169 if (zero_buffer == MAP_FAILED)
170 xbt_die("Could not map the zero buffer");
175 int open_process_file(pid_t pid, const char* file, int flags)
178 snprintf(buff, sizeof(buff), "/proc/%li/%s", (long) pid, file);
179 return open(buff, flags);
187 int open_vm(pid_t pid, int flags)
189 const size_t buffer_size = 30;
190 char buffer[buffer_size];
191 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
192 if (res < 0 || (size_t) res >= buffer_size) {
193 errno = ENAMETOOLONG;
196 return open(buffer, flags);
208 Process::Process(pid_t pid, int sockfd) :
209 AddressSpace(this),pid_(pid), socket_(sockfd), running_(true)
214 this->memory_map_ = simgrid::xbt::get_memory_map(this->pid_);
215 this->init_memory_map_info();
217 int fd = open_vm(this->pid_, O_RDWR);
219 xbt_die("Could not open file for process virtual address space");
220 this->memory_file = fd;
222 // Read std_heap (is a struct mdesc*):
223 simgrid::mc::Variable* std_heap_var = this->find_variable("__mmalloc_default_mdp");
225 xbt_die("No heap information in the target process");
226 if(!std_heap_var->address)
227 xbt_die("No constant address for this variable");
228 this->read_bytes(&this->heap_address, sizeof(struct mdesc*),
229 remote(std_heap_var->address),
230 simgrid::mc::ProcessIndexDisabled);
232 this->smx_process_infos = MC_smx_process_info_list_new();
233 this->smx_old_process_infos = MC_smx_process_info_list_new();
234 this->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
235 this->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
236 this->unw_underlying_context = _UPT_create(this->pid_);
241 if (this->socket_ >= 0 && close(this->socket_) < 0)
242 xbt_die("Could not close communication socket");
244 this->maestro_stack_start_ = nullptr;
245 this->maestro_stack_end_ = nullptr;
247 xbt_dynar_free(&this->smx_process_infos);
248 xbt_dynar_free(&this->smx_old_process_infos);
250 if (this->memory_file >= 0) {
251 close(this->memory_file);
254 if (this->unw_underlying_addr_space != unw_local_addr_space) {
255 unw_destroy_addr_space(this->unw_underlying_addr_space);
256 _UPT_destroy(this->unw_underlying_context);
258 this->unw_underlying_context = nullptr;
259 this->unw_underlying_addr_space = nullptr;
261 unw_destroy_addr_space(this->unw_addr_space);
262 this->unw_addr_space = nullptr;
264 this->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
266 if (this->clear_refs_fd_ >= 0)
267 close(this->clear_refs_fd_);
268 if (this->pagemap_fd_ >= 0)
269 close(this->pagemap_fd_);
272 /** Refresh the information about the process
274 * Do not use direclty, this is used by the getters when appropriate
275 * in order to have fresh data.
277 void Process::refresh_heap()
279 xbt_assert(mc_mode == MC_MODE_SERVER);
280 // Read/dereference/refresh the std_heap pointer:
282 this->heap = std::unique_ptr<s_xbt_mheap_t>(new s_xbt_mheap_t());
283 this->read_bytes(this->heap.get(), sizeof(struct mdesc),
284 remote(this->heap_address), simgrid::mc::ProcessIndexDisabled);
285 this->cache_flags |= MC_PROCESS_CACHE_FLAG_HEAP;
288 /** Refresh the information about the process
290 * Do not use direclty, this is used by the getters when appropriate
291 * in order to have fresh data.
293 void Process::refresh_malloc_info()
295 xbt_assert(mc_mode == MC_MODE_SERVER);
296 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
297 this->refresh_heap();
298 // Refresh process->heapinfo:
299 size_t count = this->heap->heaplimit + 1;
300 if (this->heap_info.size() < count)
301 this->heap_info.resize(count);
302 this->read_bytes(this->heap_info.data(), count * sizeof(malloc_info),
303 remote(this->heap->heapinfo), simgrid::mc::ProcessIndexDisabled);
304 this->cache_flags |= MC_PROCESS_CACHE_FLAG_MALLOC_INFO;
307 /** @brief Finds the range of the different memory segments and binary paths */
308 void Process::init_memory_map_info()
310 XBT_DEBUG("Get debug information ...");
311 this->maestro_stack_start_ = nullptr;
312 this->maestro_stack_end_ = nullptr;
313 this->object_infos.resize(0);
314 this->binary_info = nullptr;
315 this->libsimgrid_info = nullptr;
317 struct s_mc_memory_map_re res;
319 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
320 xbt_die(".so regexp did not compile");
322 std::vector<simgrid::xbt::VmMap> const& maps = this->memory_map_;
324 const char* current_name = nullptr;
326 this->object_infos.resize(0);
328 for (size_t i=0; i < maps.size(); i++) {
329 simgrid::xbt::VmMap const& reg = maps[i];
330 const char* pathname = maps[i].pathname.c_str();
333 if (maps[i].pathname.empty()) {
334 current_name = nullptr;
338 // [stack], [vvar], [vsyscall], [vdso] ...
339 if (pathname[0] == '[') {
340 if ((reg.prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
341 this->maestro_stack_start_ = remote(reg.start_addr);
342 this->maestro_stack_end_ = remote(reg.end_addr);
344 current_name = nullptr;
348 if (current_name && strcmp(current_name, pathname)==0)
351 current_name = pathname;
352 if (!(reg.prot & PROT_READ) && (reg.prot & PROT_EXEC))
355 const bool is_executable = !i;
356 char* libname = nullptr;
357 if (!is_executable) {
358 libname = MC_get_lib_name(pathname, &res);
361 if (MC_is_filtered_lib(libname)) {
367 std::shared_ptr<simgrid::mc::ObjectInformation> info =
368 MC_find_object_info(this->memory_map_, pathname);
369 this->object_infos.push_back(info);
371 this->binary_info = info;
372 else if (libname && MC_is_simgrid_lib(libname))
373 this->libsimgrid_info = info;
378 regfree(&res.version_re);
380 // Resolve time (including accross differents objects):
381 for (auto const& object_info : this->object_infos)
382 MC_post_process_object_info(this, object_info.get());
384 xbt_assert(this->maestro_stack_start_, "Did not find maestro_stack_start");
385 xbt_assert(this->maestro_stack_end_, "Did not find maestro_stack_end");
387 XBT_DEBUG("Get debug information done !");
390 std::shared_ptr<simgrid::mc::ObjectInformation> Process::find_object_info(remote_ptr<void> addr) const
392 for (auto const& object_info : this->object_infos) {
393 if (addr.address() >= (std::uint64_t)object_info->start
394 && addr.address() <= (std::uint64_t)object_info->end) {
401 std::shared_ptr<ObjectInformation> Process::find_object_info_exec(remote_ptr<void> addr) const
403 for (std::shared_ptr<ObjectInformation> const& info : this->object_infos) {
404 if (addr.address() >= (std::uint64_t) info->start_exec
405 && addr.address() <= (std::uint64_t) info->end_exec) {
412 std::shared_ptr<ObjectInformation> Process::find_object_info_rw(remote_ptr<void> addr) const
414 for (std::shared_ptr<ObjectInformation> const& info : this->object_infos) {
415 if (addr.address() >= (std::uint64_t)info->start_rw
416 && addr.address() <= (std::uint64_t)info->end_rw) {
423 simgrid::mc::Frame* Process::find_function(remote_ptr<void> ip) const
425 std::shared_ptr<simgrid::mc::ObjectInformation> info = this->find_object_info_exec(ip);
426 return info ? info->find_function((void*) ip.address()) : nullptr;
429 /** Find (one occurence of) the named variable definition
431 simgrid::mc::Variable* Process::find_variable(const char* name) const
433 // First lookup the variable in the executable shared object.
434 // A global variable used directly by the executable code from a library
435 // is reinstanciated in the executable memory .data/.bss.
436 // We need to look up the variable in the execvutable first.
437 if (this->binary_info) {
438 std::shared_ptr<simgrid::mc::ObjectInformation> const& info = this->binary_info;
439 simgrid::mc::Variable* var = info->find_variable(name);
444 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos) {
445 simgrid::mc::Variable* var = info->find_variable(name);
453 void Process::read_variable(const char* name, void* target, size_t size) const
455 simgrid::mc::Variable* var = this->find_variable(name);
457 xbt_die("No simple location for this variable");
458 if (!var->type->full_type)
459 xbt_die("Partial type for %s, cannot check size", name);
460 if ((size_t) var->type->full_type->byte_size != size)
461 xbt_die("Unexpected size for %s (expected %zi, was %zi)",
462 name, size, (size_t) var->type->full_type->byte_size);
463 this->read_bytes(target, size, remote(var->address));
466 char* Process::read_string(remote_ptr<void> address) const
472 char* res = (char*) malloc(len);
476 ssize_t c = pread(this->memory_file, res + off, len - off, (off_t) address.address() + off);
481 xbt_die("Could not read from from remote process");
484 xbt_die("Could not read string from remote process");
486 void* p = memchr(res + off, '\0', c);
493 res = (char*) realloc(res, len);
498 const void *Process::read_bytes(void* buffer, std::size_t size,
499 remote_ptr<void> address, int process_index,
500 ReadOptions options) const
502 if (process_index != simgrid::mc::ProcessIndexDisabled) {
503 std::shared_ptr<simgrid::mc::ObjectInformation> const& info =
504 this->find_object_info_rw((void*)address.address());
505 // Segment overlap is not handled.
507 if (info.get() && this->privatized(*info)) {
508 if (process_index < 0)
509 xbt_die("Missing process index");
510 if (process_index >= (int) MC_smpi_process_count())
511 xbt_die("Invalid process index");
513 // Read smpi_privatisation_regions from MCed:
514 smpi_privatisation_region_t remote_smpi_privatisation_regions =
515 mc_model_checker->process().read_variable<smpi_privatisation_region_t>(
516 "smpi_privatisation_regions");
518 s_smpi_privatisation_region_t privatisation_region =
519 mc_model_checker->process().read<s_smpi_privatisation_region_t>(
520 remote(remote_smpi_privatisation_regions + process_index));
522 // Address translation in the privaization segment:
523 size_t offset = address.address() - (std::uint64_t)info->start_rw;
524 address = remote((char*)privatisation_region.address + offset);
529 if (pread_whole(this->memory_file, buffer, size, address.address()) < 0)
530 xbt_die("Read from process %lli failed", (long long) this->pid_);
534 /** Write data to a process memory
536 * @param process the process
537 * @param local local memory address (source)
538 * @param remote target process memory address (target)
539 * @param len data size
541 void Process::write_bytes(const void* buffer, size_t len, remote_ptr<void> address)
543 if (pwrite_whole(this->memory_file, buffer, len, address.address()) < 0)
544 xbt_die("Write to process %lli failed", (long long) this->pid_);
547 void Process::clear_bytes(remote_ptr<void> address, size_t len)
549 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
551 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
552 this->write_bytes(zero_buffer, s, address);
553 address = remote((char*) address.address() + s);
558 void Process::ignore_region(std::uint64_t addr, std::size_t size)
560 IgnoredRegion region;
564 if (ignored_regions_.empty()) {
565 ignored_regions_.push_back(region);
569 unsigned int cursor = 0;
570 IgnoredRegion* current_region = nullptr;
573 int end = ignored_regions_.size() - 1;
574 while (start <= end) {
575 cursor = (start + end) / 2;
576 current_region = &ignored_regions_[cursor];
577 if (current_region->addr == addr) {
578 if (current_region->size == size)
580 else if (current_region->size < size)
584 } else if (current_region->addr < addr)
590 std::size_t position;
591 if (current_region->addr == addr) {
592 if (current_region->size < size) {
593 position = cursor + 1;
597 } else if (current_region->addr < addr) {
598 position = cursor + 1;
602 ignored_regions_.insert(
603 ignored_regions_.begin() + position, region);
606 void Process::reset_soft_dirty()
608 if (this->clear_refs_fd_ < 0) {
609 this->clear_refs_fd_ = open_process_file(pid_, "clear_refs", O_WRONLY|O_CLOEXEC);
610 if (this->clear_refs_fd_ < 0)
611 xbt_die("Could not open clear_refs file for soft-dirty tracking. Run as root?");
613 if(::write(this->clear_refs_fd_, "4\n", 2) != 2)
614 xbt_die("Could not reset softdirty bits");
617 void Process::read_pagemap(uint64_t* pagemap, size_t page_start, size_t page_count)
619 if (pagemap_fd_ < 0) {
620 pagemap_fd_ = open_process_file(pid_, "pagemap", O_RDONLY|O_CLOEXEC);
622 xbt_die("Could not open pagemap file for soft-dirty tracking. Run as root?");
624 ssize_t bytesize = sizeof(uint64_t) * page_count;
625 off_t offset = sizeof(uint64_t) * page_start;
626 if (pread_whole(pagemap_fd_, pagemap, bytesize, offset) != bytesize)
627 xbt_die("Could not read pagemap");
630 void Process::ignore_heap(IgnoredHeapRegion const& region)
632 if (ignored_heap_.empty()) {
633 ignored_heap_.push_back(std::move(region));
637 typedef std::vector<IgnoredHeapRegion>::size_type size_type;
640 size_type end = ignored_heap_.size() - 1;
642 // Binary search the position of insertion:
644 while (start <= end) {
645 cursor = start + (end - start) / 2;
646 auto& current_region = ignored_heap_[cursor];
647 if (current_region.address == region.address)
649 else if (current_region.address < region.address)
651 else if (cursor != 0)
658 // Insert it mc_heap_ignore_region_t:
659 if (ignored_heap_[cursor].address < region.address)
661 ignored_heap_.insert( ignored_heap_.begin() + cursor, region);
664 void Process::unignore_heap(void *address, size_t size)
666 typedef std::vector<IgnoredHeapRegion>::size_type size_type;
669 size_type end = ignored_heap_.size() - 1;
673 while (start <= end) {
674 cursor = (start + end) / 2;
675 auto& region = ignored_heap_[cursor];
676 if (region.address == address) {
677 ignored_heap_.erase(ignored_heap_.begin() + cursor);
679 } else if (region.address < address)
681 else if ((char *) region.address <= ((char *) address + size)) {
682 ignored_heap_.erase(ignored_heap_.begin() + cursor);
684 } else if (cursor != 0)
692 void Process::ignore_local_variable(const char *var_name, const char *frame_name)
694 if (frame_name != nullptr && strcmp(frame_name, "*") == 0)
695 frame_name = nullptr;
696 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
698 info->remove_local_variable(var_name, frame_name);