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. */
13 #include <sys/types.h>
17 #include <sys/mman.h> // PROT_*
23 #include <libunwind.h>
24 #include <libunwind-ptrace.h>
26 #include <xbt/mmalloc.h>
28 #include "mc_process.h"
29 #include "mc_object_info.h"
30 #include "AddressSpace.hpp"
32 #include "mc_snapshot.h"
33 #include "mc_ignore.h"
35 #include "mc_server.h"
37 using simgrid::mc::remote;
41 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
42 "MC process information");
46 #define SO_RE "\\.so[\\.0-9]*$"
47 #define VERSION_RE "-[\\.0-9]*$"
49 static const char *const FILTERED_LIBS[] = {
72 static bool MC_is_simgrid_lib(const char* libname)
74 return !strcmp(libname, "libsimgrid");
77 static bool MC_is_filtered_lib(const char* libname)
79 for (const char* filtered_lib : FILTERED_LIBS)
80 if (strcmp(libname, filtered_lib)==0)
85 struct s_mc_memory_map_re {
90 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res)
92 const char* map_basename = basename((char*) pathname);
95 if(regexec(&res->so_re, map_basename, 1, &match, 0))
98 char* libname = strndup(map_basename, match.rm_so);
100 // Strip the version suffix:
101 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
102 char* temp = libname;
103 libname = strndup(temp, match.rm_so);
110 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
112 char* buffer = (char*) buf;
113 ssize_t real_count = count;
115 ssize_t res = pread(fd, buffer, count, offset);
122 } else if (errno != EINTR) {
129 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
131 const char* buffer = (const char*) buf;
132 ssize_t real_count = count;
134 ssize_t res = pwrite(fd, buffer, count, offset);
141 } else if (errno != EINTR) {
148 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
149 static const void* zero_buffer;
150 static const size_t zero_buffer_size = 10 * 4096;
152 static void MC_zero_buffer_init(void)
154 int fd = open("/dev/zero", O_RDONLY);
156 xbt_die("Could not open /dev/zero");
157 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
158 if (zero_buffer == MAP_FAILED)
159 xbt_die("Could not map the zero buffer");
168 int open_vm(pid_t pid, int flags)
170 const size_t buffer_size = 30;
171 char buffer[buffer_size];
172 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
173 if (res < 0 || (size_t) res >= buffer_size) {
174 errno = ENAMETOOLONG;
177 return open(buffer, flags);
189 Process::Process(pid_t pid, int sockfd)
191 Process* process = this;
193 process->process_flags = MC_PROCESS_NO_FLAG;
194 process->socket_ = sockfd;
197 process->process_flags |= MC_PROCESS_SELF_FLAG;
198 process->running_ = true;
199 process->status_ = 0;
200 process->memory_map_ = get_memory_map(pid);
201 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
202 process->heap = NULL;
203 process->heap_info = NULL;
204 process->init_memory_map_info();
206 // Open the memory file
207 if (process->is_self())
208 process->memory_file = -1;
210 int fd = open_vm(process->pid_, O_RDWR);
212 xbt_die("Could not open file for process virtual address space");
213 process->memory_file = fd;
216 // Read std_heap (is a struct mdesc*):
217 dw_variable_t std_heap_var = process->find_variable("__mmalloc_default_mdp");
219 xbt_die("No heap information in the target process");
220 if(!std_heap_var->address)
221 xbt_die("No constant address for this variable");
222 process->read_bytes(&process->heap_address, sizeof(struct mdesc*),
223 remote(std_heap_var->address),
224 simgrid::mc::ProcessIndexDisabled);
226 process->smx_process_infos = MC_smx_process_info_list_new();
227 process->smx_old_process_infos = MC_smx_process_info_list_new();
229 process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
230 if (process->process_flags & MC_PROCESS_SELF_FLAG) {
231 process->unw_underlying_addr_space = unw_local_addr_space;
232 process->unw_underlying_context = NULL;
234 process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
235 process->unw_underlying_context = _UPT_create(pid);
241 Process* process = this;
243 if (this->socket_ >= 0 && close(this->socket_) < 0)
244 xbt_die("Could not close communication socket");
246 process->process_flags = MC_PROCESS_NO_FLAG;
249 process->maestro_stack_start_ = nullptr;
250 process->maestro_stack_end_ = nullptr;
252 xbt_dynar_free(&process->smx_process_infos);
253 xbt_dynar_free(&process->smx_old_process_infos);
256 for (i=0; i!=process->object_infos_size; ++i) {
257 delete process->object_infos[i];
258 process->object_infos[i] = nullptr;
260 free(process->object_infos);
261 process->object_infos = NULL;
262 process->object_infos_size = 0;
263 if (process->memory_file >= 0) {
264 close(process->memory_file);
267 if (process->unw_underlying_addr_space != unw_local_addr_space) {
268 unw_destroy_addr_space(process->unw_underlying_addr_space);
269 _UPT_destroy(process->unw_underlying_context);
271 process->unw_underlying_context = NULL;
272 process->unw_underlying_addr_space = NULL;
274 unw_destroy_addr_space(process->unw_addr_space);
275 process->unw_addr_space = NULL;
277 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
280 process->heap = NULL;
282 free(process->heap_info);
283 process->heap_info = NULL;
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 xbt_assert(!this->is_self());
295 // Read/dereference/refresh the std_heap pointer:
297 this->heap = (struct mdesc*) malloc(sizeof(struct mdesc));
299 this->read_bytes(this->heap, sizeof(struct mdesc), remote(this->heap_address),
300 simgrid::mc::ProcessIndexDisabled);
301 this->cache_flags |= MC_PROCESS_CACHE_FLAG_HEAP;
304 /** Refresh the information about the process
306 * Do not use direclty, this is used by the getters when appropriate
307 * in order to have fresh data.
309 void Process::refresh_malloc_info()
311 xbt_assert(mc_mode == MC_MODE_SERVER);
312 xbt_assert(!this->is_self());
313 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
314 this->refresh_heap();
315 // Refresh process->heapinfo:
316 size_t malloc_info_bytesize =
317 (this->heap->heaplimit + 1) * sizeof(malloc_info);
318 this->heap_info = (malloc_info*) realloc(this->heap_info, malloc_info_bytesize);
319 this->read_bytes(this->heap_info, malloc_info_bytesize,
320 remote(this->heap->heapinfo), simgrid::mc::ProcessIndexDisabled);
321 this->cache_flags |= MC_PROCESS_CACHE_FLAG_MALLOC_INFO;
324 /** @brief Finds the range of the different memory segments and binary paths */
325 void Process::init_memory_map_info()
327 XBT_DEBUG("Get debug information ...");
328 this->maestro_stack_start_ = nullptr;
329 this->maestro_stack_end_ = nullptr;
330 this->object_infos = NULL;
331 this->object_infos_size = 0;
332 this->binary_info = NULL;
333 this->libsimgrid_info = NULL;
335 struct s_mc_memory_map_re res;
337 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
338 xbt_die(".so regexp did not compile");
340 std::vector<simgrid::mc::VmMap> const& maps = this->memory_map_;
342 const char* current_name = NULL;
344 for (size_t i=0; i < maps.size(); i++) {
345 simgrid::mc::VmMap const& reg = maps[i];
346 const char* pathname = maps[i].pathname.c_str();
349 if (maps[i].pathname.empty()) {
354 // [stack], [vvar], [vsyscall], [vdso] ...
355 if (pathname[0] == '[') {
356 if ((reg.prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
357 this->maestro_stack_start_ = remote(reg.start_addr);
358 this->maestro_stack_end_ = remote(reg.end_addr);
364 if (current_name && strcmp(current_name, pathname)==0)
367 current_name = pathname;
368 if (!(reg.prot & PROT_READ) && (reg.prot & PROT_EXEC))
371 const bool is_executable = !i;
372 char* libname = NULL;
373 if (!is_executable) {
374 libname = MC_get_lib_name(pathname, &res);
377 if (MC_is_filtered_lib(libname)) {
383 mc_object_info_t info =
384 MC_find_object_info(this->memory_map_, pathname, is_executable);
385 this->object_infos = (mc_object_info_t*) realloc(this->object_infos,
386 (this->object_infos_size+1) * sizeof(mc_object_info_t));
387 this->object_infos[this->object_infos_size] = info;
388 this->object_infos_size++;
390 this->binary_info = info;
391 else if (libname && MC_is_simgrid_lib(libname))
392 this->libsimgrid_info = info;
397 regfree(&res.version_re);
399 // Resolve time (including accross differents objects):
400 for (size_t i=0; i!=this->object_infos_size; ++i)
401 MC_post_process_object_info(this, this->object_infos[i]);
403 xbt_assert(this->maestro_stack_start_, "Did not find maestro_stack_start");
404 xbt_assert(this->maestro_stack_end_, "Did not find maestro_stack_end");
406 XBT_DEBUG("Get debug information done !");
409 mc_object_info_t Process::find_object_info(remote_ptr<void> addr) const
412 for (i = 0; i != this->object_infos_size; ++i) {
413 if (addr.address() >= (std::uint64_t)this->object_infos[i]->start
414 && addr.address() <= (std::uint64_t)this->object_infos[i]->end) {
415 return this->object_infos[i];
421 mc_object_info_t Process::find_object_info_exec(remote_ptr<void> addr) const
424 for (i = 0; i != this->object_infos_size; ++i) {
425 if (addr.address() >= (std::uint64_t)this->object_infos[i]->start_exec
426 && addr.address() <= (std::uint64_t)this->object_infos[i]->end_exec) {
427 return this->object_infos[i];
433 mc_object_info_t Process::find_object_info_rw(remote_ptr<void> addr) const
436 for (i = 0; i != this->object_infos_size; ++i) {
437 if (addr.address() >= (std::uint64_t)this->object_infos[i]->start_rw
438 && addr.address() <= (std::uint64_t)this->object_infos[i]->end_rw) {
439 return this->object_infos[i];
445 dw_frame_t Process::find_function(remote_ptr<void> ip) const
447 mc_object_info_t info = this->find_object_info_exec(ip);
451 return MC_file_object_info_find_function(info, (void*) ip.address());
454 /** Find (one occurence of) the named variable definition
456 dw_variable_t Process::find_variable(const char* name) const
458 const size_t n = this->object_infos_size;
461 // First lookup the variable in the executable shared object.
462 // A global variable used directly by the executable code from a library
463 // is reinstanciated in the executable memory .data/.bss.
464 // We need to look up the variable in the execvutable first.
465 if (this->binary_info) {
466 mc_object_info_t info = this->binary_info;
467 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
472 for (i=0; i!=n; ++i) {
473 mc_object_info_t info = this->object_infos[i];
474 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
482 void Process::read_variable(const char* name, void* target, size_t size) const
484 dw_variable_t var = this->find_variable(name);
486 xbt_die("No simple location for this variable");
487 if (!var->type->full_type)
488 xbt_die("Partial type for %s, cannot check size", name);
489 if ((size_t) var->type->full_type->byte_size != size)
490 xbt_die("Unexpected size for %s (expected %zi, was %zi)",
491 name, size, (size_t) var->type->full_type->byte_size);
492 this->read_bytes(target, size, remote(var->address));
495 char* Process::read_string(remote_ptr<void> address) const
500 return strdup((char*) address.address());
503 char* res = (char*) malloc(len);
507 ssize_t c = pread(this->memory_file, res + off, len - off, (off_t) address.address() + off);
512 xbt_die("Could not read from from remote process");
515 xbt_die("Could not read string from remote process");
517 void* p = memchr(res + off, '\0', c);
524 res = (char*) realloc(res, len);
529 const void *Process::read_bytes(void* buffer, std::size_t size,
530 remote_ptr<void> address, int process_index,
531 AddressSpace::ReadMode mode) const
533 if (process_index != simgrid::mc::ProcessIndexDisabled) {
534 mc_object_info_t info = this->find_object_info_rw((void*)address.address());
535 // Segment overlap is not handled.
536 if (MC_object_info_is_privatized(info)) {
537 if (process_index < 0)
538 xbt_die("Missing process index");
539 if (process_index >= (int) MC_smpi_process_count())
540 xbt_die("Invalid process index");
542 // Read smpi_privatisation_regions from MCed:
543 smpi_privatisation_region_t remote_smpi_privatisation_regions =
544 mc_model_checker->process().read_variable<smpi_privatisation_region_t>(
545 "smpi_privatisation_regions");
547 s_smpi_privatisation_region_t privatisation_region =
548 mc_model_checker->process().read<s_smpi_privatisation_region_t>(
549 remote(remote_smpi_privatisation_regions + process_index));
551 // Address translation in the privaization segment:
552 size_t offset = address.address() - (std::uint64_t)info->start_rw;
553 address = remote((char*)privatisation_region.address + offset);
557 if (this->is_self()) {
558 if (mode == simgrid::mc::AddressSpace::Lazy)
559 return (void*)address.address();
561 memcpy(buffer, (void*)address.address(), size);
565 if (pread_whole(this->memory_file, buffer, size, (off_t) address.address()) < 0)
566 xbt_die("Read from process %lli failed", (long long) this->pid_);
571 /** Write data to a process memory
573 * @param process the process
574 * @param local local memory address (source)
575 * @param remote target process memory address (target)
576 * @param len data size
578 void Process::write_bytes(const void* buffer, size_t len, remote_ptr<void> address)
580 if (this->is_self()) {
581 memcpy((void*)address.address(), buffer, len);
583 if (pwrite_whole(this->memory_file, buffer, len, address.address()) < 0)
584 xbt_die("Write to process %lli failed", (long long) this->pid_);
588 void Process::clear_bytes(remote_ptr<void> address, size_t len)
590 if (this->is_self()) {
591 memset((void*)address.address(), 0, len);
593 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
595 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
596 this->write_bytes(zero_buffer, s, address);
597 address = remote((char*) address.address() + s);
603 void Process::ignore_region(std::uint64_t addr, std::size_t size)
605 IgnoredRegion region;
609 if (ignored_regions_.empty()) {
610 ignored_regions_.push_back(region);
614 unsigned int cursor = 0;
615 IgnoredRegion* current_region = nullptr;
618 int end = ignored_regions_.size() - 1;
619 while (start <= end) {
620 cursor = (start + end) / 2;
621 current_region = &ignored_regions_[cursor];
622 if (current_region->addr == addr) {
623 if (current_region->size == size)
625 else if (current_region->size < size)
629 } else if (current_region->addr < addr)
635 std::size_t position;
636 if (current_region->addr == addr) {
637 if (current_region->size < size) {
638 position = cursor + 1;
642 } else if (current_region->addr < addr) {
643 position = cursor + 1;
647 ignored_regions_.insert(
648 ignored_regions_.begin() + position, region);