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
15 #include <sys/types.h>
19 #include <sys/mman.h> // PROT_*
25 #include <libunwind.h>
26 #include <libunwind-ptrace.h>
28 #include <xbt/mmalloc.h>
30 #include "mc_object_info.h"
32 #include "mc_snapshot.h"
33 #include "mc_ignore.h"
35 #include "mc_server.h"
37 #include "src/mc/Process.hpp"
38 #include "src/mc/AddressSpace.hpp"
39 #include "src/mc/ObjectInformation.hpp"
40 #include "src/mc/Variable.hpp"
42 using simgrid::mc::remote;
46 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
47 "MC process information");
51 #define SO_RE "\\.so[\\.0-9]*$"
52 #define VERSION_RE "-[\\.0-9-]*$"
54 static const char *const FILTERED_LIBS[] = {
84 static bool MC_is_simgrid_lib(const char* libname)
86 return !strcmp(libname, "libsimgrid");
89 static bool MC_is_filtered_lib(const char* libname)
91 for (const char* filtered_lib : FILTERED_LIBS)
92 if (strcmp(libname, filtered_lib)==0)
97 struct s_mc_memory_map_re {
102 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res)
104 const char* map_basename = basename((char*) pathname);
107 if(regexec(&res->so_re, map_basename, 1, &match, 0))
110 char* libname = strndup(map_basename, match.rm_so);
112 // Strip the version suffix:
113 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
114 char* temp = libname;
115 libname = strndup(temp, match.rm_so);
122 static ssize_t pread_whole(int fd, void *buf, size_t count, std::uint64_t offset)
124 char* buffer = (char*) buf;
125 ssize_t real_count = count;
127 ssize_t res = pread(fd, buffer, count, (std::int64_t) offset);
134 } else if (errno != EINTR) {
135 perror("pread_whole");
142 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
144 const char* buffer = (const char*) buf;
145 ssize_t real_count = count;
147 ssize_t res = pwrite(fd, buffer, count, offset);
154 } else if (errno != EINTR) {
161 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
162 static const void* zero_buffer;
163 static const size_t zero_buffer_size = 10 * 4096;
165 static void MC_zero_buffer_init(void)
167 int fd = open("/dev/zero", O_RDONLY);
169 xbt_die("Could not open /dev/zero");
170 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
171 if (zero_buffer == MAP_FAILED)
172 xbt_die("Could not map the zero buffer");
177 int open_process_file(pid_t pid, const char* file, int flags)
180 snprintf(buff, sizeof(buff), "/proc/%li/%s", (long) pid, file);
181 return open(buff, flags);
189 int open_vm(pid_t pid, int flags)
191 const size_t buffer_size = 30;
192 char buffer[buffer_size];
193 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
194 if (res < 0 || (size_t) res >= buffer_size) {
195 errno = ENAMETOOLONG;
198 return open(buffer, flags);
210 Process::Process(pid_t pid, int sockfd) : AddressSpace(this)
212 Process* process = this;
213 process->socket_ = sockfd;
215 process->running_ = true;
216 process->status_ = 0;
217 process->memory_map_ = get_memory_map(pid);
218 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
219 process->heap_info = NULL;
220 process->init_memory_map_info();
221 process->clear_refs_fd_ = -1;
222 process->pagemap_fd_ = -1;
224 int fd = open_vm(process->pid_, O_RDWR);
226 xbt_die("Could not open file for process virtual address space");
227 process->memory_file = fd;
229 // Read std_heap (is a struct mdesc*):
230 simgrid::mc::Variable* std_heap_var = process->find_variable("__mmalloc_default_mdp");
232 xbt_die("No heap information in the target process");
233 if(!std_heap_var->address)
234 xbt_die("No constant address for this variable");
235 process->read_bytes(&process->heap_address, sizeof(struct mdesc*),
236 remote(std_heap_var->address),
237 simgrid::mc::ProcessIndexDisabled);
239 process->smx_process_infos = MC_smx_process_info_list_new();
240 process->smx_old_process_infos = MC_smx_process_info_list_new();
241 process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
242 process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
243 process->unw_underlying_context = _UPT_create(pid);
248 Process* process = this;
250 if (this->socket_ >= 0 && close(this->socket_) < 0)
251 xbt_die("Could not close communication socket");
255 process->maestro_stack_start_ = nullptr;
256 process->maestro_stack_end_ = nullptr;
258 xbt_dynar_free(&process->smx_process_infos);
259 xbt_dynar_free(&process->smx_old_process_infos);
261 if (process->memory_file >= 0) {
262 close(process->memory_file);
265 if (process->unw_underlying_addr_space != unw_local_addr_space) {
266 unw_destroy_addr_space(process->unw_underlying_addr_space);
267 _UPT_destroy(process->unw_underlying_context);
269 process->unw_underlying_context = NULL;
270 process->unw_underlying_addr_space = NULL;
272 unw_destroy_addr_space(process->unw_addr_space);
273 process->unw_addr_space = NULL;
275 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
277 free(process->heap_info);
278 process->heap_info = NULL;
280 if (process->clear_refs_fd_ >= 0)
281 close(process->clear_refs_fd_);
282 if (process->pagemap_fd_ >= 0)
283 close(process->pagemap_fd_);
286 /** Refresh the information about the process
288 * Do not use direclty, this is used by the getters when appropriate
289 * in order to have fresh data.
291 void Process::refresh_heap()
293 xbt_assert(mc_mode == MC_MODE_SERVER);
294 // Read/dereference/refresh the std_heap pointer:
296 this->heap = std::unique_ptr<s_xbt_mheap_t>(new s_xbt_mheap_t());
297 this->read_bytes(this->heap.get(), sizeof(struct mdesc),
298 remote(this->heap_address), simgrid::mc::ProcessIndexDisabled);
299 this->cache_flags |= MC_PROCESS_CACHE_FLAG_HEAP;
302 /** Refresh the information about the process
304 * Do not use direclty, this is used by the getters when appropriate
305 * in order to have fresh data.
307 void Process::refresh_malloc_info()
309 xbt_assert(mc_mode == MC_MODE_SERVER);
310 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
311 this->refresh_heap();
312 // Refresh process->heapinfo:
313 size_t malloc_info_bytesize =
314 (this->heap->heaplimit + 1) * sizeof(malloc_info);
315 this->heap_info = (malloc_info*) realloc(this->heap_info, malloc_info_bytesize);
316 this->read_bytes(this->heap_info, malloc_info_bytesize,
317 remote(this->heap->heapinfo), simgrid::mc::ProcessIndexDisabled);
318 this->cache_flags |= MC_PROCESS_CACHE_FLAG_MALLOC_INFO;
321 /** @brief Finds the range of the different memory segments and binary paths */
322 void Process::init_memory_map_info()
324 XBT_DEBUG("Get debug information ...");
325 this->maestro_stack_start_ = nullptr;
326 this->maestro_stack_end_ = nullptr;
327 this->object_infos.resize(0);
328 this->binary_info = NULL;
329 this->libsimgrid_info = NULL;
331 struct s_mc_memory_map_re res;
333 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
334 xbt_die(".so regexp did not compile");
336 std::vector<simgrid::mc::VmMap> const& maps = this->memory_map_;
338 const char* current_name = NULL;
340 this->object_infos.resize(0);
342 for (size_t i=0; i < maps.size(); i++) {
343 simgrid::mc::VmMap const& reg = maps[i];
344 const char* pathname = maps[i].pathname.c_str();
347 if (maps[i].pathname.empty()) {
352 // [stack], [vvar], [vsyscall], [vdso] ...
353 if (pathname[0] == '[') {
354 if ((reg.prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
355 this->maestro_stack_start_ = remote(reg.start_addr);
356 this->maestro_stack_end_ = remote(reg.end_addr);
362 if (current_name && strcmp(current_name, pathname)==0)
365 current_name = pathname;
366 if (!(reg.prot & PROT_READ) && (reg.prot & PROT_EXEC))
369 const bool is_executable = !i;
370 char* libname = NULL;
371 if (!is_executable) {
372 libname = MC_get_lib_name(pathname, &res);
375 if (MC_is_filtered_lib(libname)) {
381 std::shared_ptr<simgrid::mc::ObjectInformation> info =
382 MC_find_object_info(this->memory_map_, pathname);
383 this->object_infos.push_back(info);
385 this->binary_info = info;
386 else if (libname && MC_is_simgrid_lib(libname))
387 this->libsimgrid_info = info;
392 regfree(&res.version_re);
394 // Resolve time (including accross differents objects):
395 for (auto const& object_info : this->object_infos)
396 MC_post_process_object_info(this, object_info.get());
398 xbt_assert(this->maestro_stack_start_, "Did not find maestro_stack_start");
399 xbt_assert(this->maestro_stack_end_, "Did not find maestro_stack_end");
401 XBT_DEBUG("Get debug information done !");
404 std::shared_ptr<simgrid::mc::ObjectInformation> Process::find_object_info(remote_ptr<void> addr) const
406 for (auto const& object_info : this->object_infos) {
407 if (addr.address() >= (std::uint64_t)object_info->start
408 && addr.address() <= (std::uint64_t)object_info->end) {
415 std::shared_ptr<ObjectInformation> Process::find_object_info_exec(remote_ptr<void> addr) const
417 for (std::shared_ptr<ObjectInformation> const& info : this->object_infos) {
418 if (addr.address() >= (std::uint64_t) info->start_exec
419 && addr.address() <= (std::uint64_t) info->end_exec) {
426 std::shared_ptr<ObjectInformation> Process::find_object_info_rw(remote_ptr<void> addr) const
428 for (std::shared_ptr<ObjectInformation> const& info : this->object_infos) {
429 if (addr.address() >= (std::uint64_t)info->start_rw
430 && addr.address() <= (std::uint64_t)info->end_rw) {
437 simgrid::mc::Frame* Process::find_function(remote_ptr<void> ip) const
439 std::shared_ptr<simgrid::mc::ObjectInformation> info = this->find_object_info_exec(ip);
440 return info ? info->find_function((void*) ip.address()) : nullptr;
443 /** Find (one occurence of) the named variable definition
445 simgrid::mc::Variable* Process::find_variable(const char* name) const
447 // First lookup the variable in the executable shared object.
448 // A global variable used directly by the executable code from a library
449 // is reinstanciated in the executable memory .data/.bss.
450 // We need to look up the variable in the execvutable first.
451 if (this->binary_info) {
452 std::shared_ptr<simgrid::mc::ObjectInformation> const& info = this->binary_info;
453 simgrid::mc::Variable* var = info->find_variable(name);
458 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos) {
459 simgrid::mc::Variable* var = info->find_variable(name);
467 void Process::read_variable(const char* name, void* target, size_t size) const
469 simgrid::mc::Variable* var = this->find_variable(name);
471 xbt_die("No simple location for this variable");
472 if (!var->type->full_type)
473 xbt_die("Partial type for %s, cannot check size", name);
474 if ((size_t) var->type->full_type->byte_size != size)
475 xbt_die("Unexpected size for %s (expected %zi, was %zi)",
476 name, size, (size_t) var->type->full_type->byte_size);
477 this->read_bytes(target, size, remote(var->address));
480 char* Process::read_string(remote_ptr<void> address) const
486 char* res = (char*) malloc(len);
490 ssize_t c = pread(this->memory_file, res + off, len - off, (off_t) address.address() + off);
495 xbt_die("Could not read from from remote process");
498 xbt_die("Could not read string from remote process");
500 void* p = memchr(res + off, '\0', c);
507 res = (char*) realloc(res, len);
512 const void *Process::read_bytes(void* buffer, std::size_t size,
513 remote_ptr<void> address, int process_index,
514 AddressSpace::ReadMode mode) const
516 if (process_index != simgrid::mc::ProcessIndexDisabled) {
517 std::shared_ptr<simgrid::mc::ObjectInformation> const& info =
518 this->find_object_info_rw((void*)address.address());
519 // Segment overlap is not handled.
521 if (info.get() && info.get()->privatized()) {
522 if (process_index < 0)
523 xbt_die("Missing process index");
524 if (process_index >= (int) MC_smpi_process_count())
525 xbt_die("Invalid process index");
527 // Read smpi_privatisation_regions from MCed:
528 smpi_privatisation_region_t remote_smpi_privatisation_regions =
529 mc_model_checker->process().read_variable<smpi_privatisation_region_t>(
530 "smpi_privatisation_regions");
532 s_smpi_privatisation_region_t privatisation_region =
533 mc_model_checker->process().read<s_smpi_privatisation_region_t>(
534 remote(remote_smpi_privatisation_regions + process_index));
536 // Address translation in the privaization segment:
537 size_t offset = address.address() - (std::uint64_t)info->start_rw;
538 address = remote((char*)privatisation_region.address + offset);
543 if (pread_whole(this->memory_file, buffer, size, address.address()) < 0)
544 xbt_die("Read from process %lli failed", (long long) this->pid_);
548 /** Write data to a process memory
550 * @param process the process
551 * @param local local memory address (source)
552 * @param remote target process memory address (target)
553 * @param len data size
555 void Process::write_bytes(const void* buffer, size_t len, remote_ptr<void> address)
557 if (pwrite_whole(this->memory_file, buffer, len, address.address()) < 0)
558 xbt_die("Write to process %lli failed", (long long) this->pid_);
561 void Process::clear_bytes(remote_ptr<void> address, size_t len)
563 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
565 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
566 this->write_bytes(zero_buffer, s, address);
567 address = remote((char*) address.address() + s);
572 void Process::ignore_region(std::uint64_t addr, std::size_t size)
574 IgnoredRegion region;
578 if (ignored_regions_.empty()) {
579 ignored_regions_.push_back(region);
583 unsigned int cursor = 0;
584 IgnoredRegion* current_region = nullptr;
587 int end = ignored_regions_.size() - 1;
588 while (start <= end) {
589 cursor = (start + end) / 2;
590 current_region = &ignored_regions_[cursor];
591 if (current_region->addr == addr) {
592 if (current_region->size == size)
594 else if (current_region->size < size)
598 } else if (current_region->addr < addr)
604 std::size_t position;
605 if (current_region->addr == addr) {
606 if (current_region->size < size) {
607 position = cursor + 1;
611 } else if (current_region->addr < addr) {
612 position = cursor + 1;
616 ignored_regions_.insert(
617 ignored_regions_.begin() + position, region);
620 void Process::reset_soft_dirty()
622 if (this->clear_refs_fd_ < 0) {
623 this->clear_refs_fd_ = open_process_file(pid_, "clear_refs", O_WRONLY|O_CLOEXEC);
624 if (this->clear_refs_fd_ < 0)
625 xbt_die("Could not open clear_refs file for soft-dirty tracking. Run as root?");
627 if(::write(this->clear_refs_fd_, "4\n", 2) != 2)
628 xbt_die("Could not reset softdirty bits");
631 void Process::read_pagemap(uint64_t* pagemap, size_t page_start, size_t page_count)
633 if (pagemap_fd_ < 0) {
634 pagemap_fd_ = open_process_file(pid_, "pagemap", O_RDONLY|O_CLOEXEC);
636 xbt_die("Could not open pagemap file for soft-dirty tracking. Run as root?");
638 ssize_t bytesize = sizeof(uint64_t) * page_count;
639 off_t offset = sizeof(uint64_t) * page_start;
640 if (pread_whole(pagemap_fd_, pagemap, bytesize, offset) != bytesize)
641 xbt_die("Could not read pagemap");