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_process.h"
31 #include "mc_object_info.h"
32 #include "AddressSpace.hpp"
34 #include "mc_snapshot.h"
35 #include "mc_ignore.h"
37 #include "mc_server.h"
39 using simgrid::mc::remote;
43 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
44 "MC process information");
48 #define SO_RE "\\.so[\\.0-9]*$"
49 #define VERSION_RE "-[\\.0-9-]*$"
51 static const char *const FILTERED_LIBS[] = {
76 static bool MC_is_simgrid_lib(const char* libname)
78 return !strcmp(libname, "libsimgrid");
81 static bool MC_is_filtered_lib(const char* libname)
83 for (const char* filtered_lib : FILTERED_LIBS)
84 if (strcmp(libname, filtered_lib)==0)
89 struct s_mc_memory_map_re {
94 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res)
96 const char* map_basename = basename((char*) pathname);
99 if(regexec(&res->so_re, map_basename, 1, &match, 0))
102 char* libname = strndup(map_basename, match.rm_so);
104 // Strip the version suffix:
105 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
106 char* temp = libname;
107 libname = strndup(temp, match.rm_so);
114 static ssize_t pread_whole(int fd, void *buf, size_t count, std::uint64_t offset)
116 char* buffer = (char*) buf;
117 ssize_t real_count = count;
119 ssize_t res = pread(fd, buffer, count, (std::int64_t) offset);
126 } else if (errno != EINTR) {
133 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
135 const char* buffer = (const char*) buf;
136 ssize_t real_count = count;
138 ssize_t res = pwrite(fd, buffer, count, offset);
145 } else if (errno != EINTR) {
152 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
153 static const void* zero_buffer;
154 static const size_t zero_buffer_size = 10 * 4096;
156 static void MC_zero_buffer_init(void)
158 int fd = open("/dev/zero", O_RDONLY);
160 xbt_die("Could not open /dev/zero");
161 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
162 if (zero_buffer == MAP_FAILED)
163 xbt_die("Could not map the zero buffer");
172 int open_vm(pid_t pid, int flags)
174 const size_t buffer_size = 30;
175 char buffer[buffer_size];
176 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
177 if (res < 0 || (size_t) res >= buffer_size) {
178 errno = ENAMETOOLONG;
181 return open(buffer, flags);
193 Process::Process(pid_t pid, int sockfd)
195 Process* process = this;
197 process->process_flags = MC_PROCESS_NO_FLAG;
198 process->socket_ = sockfd;
201 process->process_flags |= MC_PROCESS_SELF_FLAG;
202 process->running_ = true;
203 process->status_ = 0;
204 process->memory_map_ = get_memory_map(pid);
205 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
206 process->heap = NULL;
207 process->heap_info = NULL;
208 process->init_memory_map_info();
210 // Open the memory file
211 if (process->is_self())
212 process->memory_file = -1;
214 int fd = open_vm(process->pid_, O_RDWR);
216 xbt_die("Could not open file for process virtual address space");
217 process->memory_file = fd;
220 // Read std_heap (is a struct mdesc*):
221 dw_variable_t std_heap_var = process->find_variable("__mmalloc_default_mdp");
223 xbt_die("No heap information in the target process");
224 if(!std_heap_var->address)
225 xbt_die("No constant address for this variable");
226 process->read_bytes(&process->heap_address, sizeof(struct mdesc*),
227 remote(std_heap_var->address),
228 simgrid::mc::ProcessIndexDisabled);
230 process->smx_process_infos = MC_smx_process_info_list_new();
231 process->smx_old_process_infos = MC_smx_process_info_list_new();
233 process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
234 if (process->process_flags & MC_PROCESS_SELF_FLAG) {
235 process->unw_underlying_addr_space = unw_local_addr_space;
236 process->unw_underlying_context = NULL;
238 process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
239 process->unw_underlying_context = _UPT_create(pid);
245 Process* process = this;
247 if (this->socket_ >= 0 && close(this->socket_) < 0)
248 xbt_die("Could not close communication socket");
250 process->process_flags = MC_PROCESS_NO_FLAG;
253 process->maestro_stack_start_ = nullptr;
254 process->maestro_stack_end_ = nullptr;
256 xbt_dynar_free(&process->smx_process_infos);
257 xbt_dynar_free(&process->smx_old_process_infos);
259 if (process->memory_file >= 0) {
260 close(process->memory_file);
263 if (process->unw_underlying_addr_space != unw_local_addr_space) {
264 unw_destroy_addr_space(process->unw_underlying_addr_space);
265 _UPT_destroy(process->unw_underlying_context);
267 process->unw_underlying_context = NULL;
268 process->unw_underlying_addr_space = NULL;
270 unw_destroy_addr_space(process->unw_addr_space);
271 process->unw_addr_space = NULL;
273 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
276 process->heap = NULL;
278 free(process->heap_info);
279 process->heap_info = NULL;
282 /** Refresh the information about the process
284 * Do not use direclty, this is used by the getters when appropriate
285 * in order to have fresh data.
287 void Process::refresh_heap()
289 xbt_assert(mc_mode == MC_MODE_SERVER);
290 xbt_assert(!this->is_self());
291 // Read/dereference/refresh the std_heap pointer:
293 this->heap = (struct mdesc*) malloc(sizeof(struct mdesc));
295 this->read_bytes(this->heap, sizeof(struct mdesc), remote(this->heap_address),
296 simgrid::mc::ProcessIndexDisabled);
297 this->cache_flags |= MC_PROCESS_CACHE_FLAG_HEAP;
300 /** Refresh the information about the process
302 * Do not use direclty, this is used by the getters when appropriate
303 * in order to have fresh data.
305 void Process::refresh_malloc_info()
307 xbt_assert(mc_mode == MC_MODE_SERVER);
308 xbt_assert(!this->is_self());
309 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
310 this->refresh_heap();
311 // Refresh process->heapinfo:
312 size_t malloc_info_bytesize =
313 (this->heap->heaplimit + 1) * sizeof(malloc_info);
314 this->heap_info = (malloc_info*) realloc(this->heap_info, malloc_info_bytesize);
315 this->read_bytes(this->heap_info, malloc_info_bytesize,
316 remote(this->heap->heapinfo), simgrid::mc::ProcessIndexDisabled);
317 this->cache_flags |= MC_PROCESS_CACHE_FLAG_MALLOC_INFO;
320 /** @brief Finds the range of the different memory segments and binary paths */
321 void Process::init_memory_map_info()
323 XBT_DEBUG("Get debug information ...");
324 this->maestro_stack_start_ = nullptr;
325 this->maestro_stack_end_ = nullptr;
326 this->object_infos.resize(0);
327 this->binary_info = NULL;
328 this->libsimgrid_info = NULL;
330 struct s_mc_memory_map_re res;
332 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
333 xbt_die(".so regexp did not compile");
335 std::vector<simgrid::mc::VmMap> const& maps = this->memory_map_;
337 const char* current_name = NULL;
339 this->object_infos.resize(0);
341 for (size_t i=0; i < maps.size(); i++) {
342 simgrid::mc::VmMap const& reg = maps[i];
343 const char* pathname = maps[i].pathname.c_str();
346 if (maps[i].pathname.empty()) {
351 // [stack], [vvar], [vsyscall], [vdso] ...
352 if (pathname[0] == '[') {
353 if ((reg.prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
354 this->maestro_stack_start_ = remote(reg.start_addr);
355 this->maestro_stack_end_ = remote(reg.end_addr);
361 if (current_name && strcmp(current_name, pathname)==0)
364 current_name = pathname;
365 if (!(reg.prot & PROT_READ) && (reg.prot & PROT_EXEC))
368 const bool is_executable = !i;
369 char* libname = NULL;
370 if (!is_executable) {
371 libname = MC_get_lib_name(pathname, &res);
374 if (MC_is_filtered_lib(libname)) {
380 std::shared_ptr<s_mc_object_info_t> info =
381 MC_find_object_info(this->memory_map_, pathname, is_executable);
382 this->object_infos.push_back(info);
384 this->binary_info = info;
385 else if (libname && MC_is_simgrid_lib(libname))
386 this->libsimgrid_info = info;
391 regfree(&res.version_re);
393 // Resolve time (including accross differents objects):
394 for (auto const& object_info : this->object_infos)
395 MC_post_process_object_info(this, object_info.get());
397 xbt_assert(this->maestro_stack_start_, "Did not find maestro_stack_start");
398 xbt_assert(this->maestro_stack_end_, "Did not find maestro_stack_end");
400 XBT_DEBUG("Get debug information done !");
403 std::shared_ptr<s_mc_object_info_t> Process::find_object_info(remote_ptr<void> addr) const
405 for (auto const& object_info : this->object_infos) {
406 if (addr.address() >= (std::uint64_t)object_info->start
407 && addr.address() <= (std::uint64_t)object_info->end) {
414 std::shared_ptr<ObjectInformation> Process::find_object_info_exec(remote_ptr<void> addr) const
416 for (std::shared_ptr<ObjectInformation> const& info : this->object_infos) {
417 if (addr.address() >= (std::uint64_t) info->start_exec
418 && addr.address() <= (std::uint64_t) info->end_exec) {
425 std::shared_ptr<ObjectInformation> Process::find_object_info_rw(remote_ptr<void> addr) const
427 for (std::shared_ptr<ObjectInformation> const& info : this->object_infos) {
428 if (addr.address() >= (std::uint64_t)info->start_rw
429 && addr.address() <= (std::uint64_t)info->end_rw) {
436 dw_frame_t Process::find_function(remote_ptr<void> ip) const
438 std::shared_ptr<s_mc_object_info_t> info = this->find_object_info_exec(ip);
439 return info ? info->find_function((void*) ip.address()) : nullptr;
442 /** Find (one occurence of) the named variable definition
444 dw_variable_t Process::find_variable(const char* name) const
446 // First lookup the variable in the executable shared object.
447 // A global variable used directly by the executable code from a library
448 // is reinstanciated in the executable memory .data/.bss.
449 // We need to look up the variable in the execvutable first.
450 if (this->binary_info) {
451 std::shared_ptr<s_mc_object_info_t> const& info = this->binary_info;
452 dw_variable_t var = info->find_variable(name);
457 for (std::shared_ptr<s_mc_object_info_t> const& info : this->object_infos) {
458 dw_variable_t var = info->find_variable(name);
466 void Process::read_variable(const char* name, void* target, size_t size) const
468 dw_variable_t var = this->find_variable(name);
470 xbt_die("No simple location for this variable");
471 if (!var->type->full_type)
472 xbt_die("Partial type for %s, cannot check size", name);
473 if ((size_t) var->type->full_type->byte_size != size)
474 xbt_die("Unexpected size for %s (expected %zi, was %zi)",
475 name, size, (size_t) var->type->full_type->byte_size);
476 this->read_bytes(target, size, remote(var->address));
479 char* Process::read_string(remote_ptr<void> address) const
484 return xbt_strdup((char*) address.address());
487 char* res = (char*) malloc(len);
491 ssize_t c = pread(this->memory_file, res + off, len - off, (off_t) address.address() + off);
496 xbt_die("Could not read from from remote process");
499 xbt_die("Could not read string from remote process");
501 void* p = memchr(res + off, '\0', c);
508 res = (char*) realloc(res, len);
513 const void *Process::read_bytes(void* buffer, std::size_t size,
514 remote_ptr<void> address, int process_index,
515 AddressSpace::ReadMode mode) const
517 if (process_index != simgrid::mc::ProcessIndexDisabled) {
518 std::shared_ptr<s_mc_object_info_t> const& info =
519 this->find_object_info_rw((void*)address.address());
520 // 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);
542 if (this->is_self()) {
543 if (mode == simgrid::mc::AddressSpace::Lazy)
544 return (void*)address.address();
546 memcpy(buffer, (void*)address.address(), size);
550 if (pread_whole(this->memory_file, buffer, size, address.address()) < 0)
551 xbt_die("Read from process %lli failed", (long long) this->pid_);
556 /** Write data to a process memory
558 * @param process the process
559 * @param local local memory address (source)
560 * @param remote target process memory address (target)
561 * @param len data size
563 void Process::write_bytes(const void* buffer, size_t len, remote_ptr<void> address)
565 if (this->is_self()) {
566 memcpy((void*)address.address(), buffer, len);
568 if (pwrite_whole(this->memory_file, buffer, len, address.address()) < 0)
569 xbt_die("Write to process %lli failed", (long long) this->pid_);
573 void Process::clear_bytes(remote_ptr<void> address, size_t len)
575 if (this->is_self()) {
576 memset((void*)address.address(), 0, len);
578 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
580 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
581 this->write_bytes(zero_buffer, s, address);
582 address = remote((char*) address.address() + s);
588 void Process::ignore_region(std::uint64_t addr, std::size_t size)
590 IgnoredRegion region;
594 if (ignored_regions_.empty()) {
595 ignored_regions_.push_back(region);
599 unsigned int cursor = 0;
600 IgnoredRegion* current_region = nullptr;
603 int end = ignored_regions_.size() - 1;
604 while (start <= end) {
605 cursor = (start + end) / 2;
606 current_region = &ignored_regions_[cursor];
607 if (current_region->addr == addr) {
608 if (current_region->size == size)
610 else if (current_region->size < size)
614 } else if (current_region->addr < addr)
620 std::size_t position;
621 if (current_region->addr == addr) {
622 if (current_region->size < size) {
623 position = cursor + 1;
627 } else if (current_region->addr < addr) {
628 position = cursor + 1;
632 ignored_regions_.insert(
633 ignored_regions_.begin() + position, region);