From: Gabriel Corona Date: Thu, 21 May 2015 10:47:01 +0000 (+0200) Subject: [mc] Move methods in Process class X-Git-Tag: v3_12~724 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/eb417d0c8064e83fc1211abc819ae93687505003 [mc] Move methods in Process class --- diff --git a/src/mc/AddressSpace.hpp b/src/mc/AddressSpace.hpp index 1f4d176efa..307c81a841 100644 --- a/src/mc/AddressSpace.hpp +++ b/src/mc/AddressSpace.hpp @@ -99,7 +99,7 @@ public: virtual ~AddressSpace(); virtual const void* read_bytes(void* buffer, std::size_t size, remote_ptr address, int process_index = ProcessIndexAny, - ReadMode mode = Normal) = 0; + ReadMode mode = Normal) const = 0; template inline void read(T *buffer, remote_ptr ptr, int process_index = ProcessIndexAny) diff --git a/src/mc/mc_checkpoint.cpp b/src/mc/mc_checkpoint.cpp index 1822189672..e05a59eba8 100644 --- a/src/mc/mc_checkpoint.cpp +++ b/src/mc/mc_checkpoint.cpp @@ -152,8 +152,8 @@ static void MC_region_restore(mc_mem_region_t region) break; case MC_REGION_STORAGE_TYPE_FLAT: - MC_process_write(&mc_model_checker->process(), region->flat.data, - region->permanent_addr, region->size); + mc_model_checker->process().write_bytes(region->flat.data, region->size, + remote(region->permanent_addr)); break; case MC_REGION_STORAGE_TYPE_CHUNKED: @@ -187,7 +187,7 @@ static mc_mem_region_t MC_region_new_privatized( // Read smpi_privatisation_regions from MCed: smpi_privatisation_region_t remote_smpi_privatisation_regions; - MC_process_read_variable(&mc_model_checker->process(), + mc_model_checker->process().read_variable( "smpi_privatisation_regions", &remote_smpi_privatisation_regions, sizeof(remote_smpi_privatisation_regions)); s_smpi_privatisation_region_t privatisation_regions[process_count]; @@ -252,7 +252,7 @@ static void MC_get_memory_regions(mc_process_t process, mc_snapshot_t snapshot) #ifdef HAVE_SMPI if (smpi_privatize_global_variables && MC_smpi_process_count()) { // snapshot->privatization_index = smpi_loaded_page - MC_process_read_variable(&mc_model_checker->process(), + mc_model_checker->process().read_variable( "smpi_loaded_page", &snapshot->privatization_index, sizeof(snapshot->privatization_index)); } else @@ -462,7 +462,7 @@ static xbt_dynar_t MC_unwind_stack_frames(mc_unw_context_t stack_context) // TODO, use real addresses in frame_t instead of fixing it here - dw_frame_t frame = MC_process_find_function(process, (void *) ip); + dw_frame_t frame = process->find_function(remote(ip)); stack_frame->frame = frame; if (frame) { @@ -591,7 +591,7 @@ static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot) // Zero the memory: xbt_dynar_foreach (mc_model_checker->process().checkpoint_ignore, cursor, region) { - MC_process_clear_memory(snapshot->process, region->addr, region->size); + snapshot->process->clear_bytes(remote(region->addr), region->size); } } @@ -601,8 +601,8 @@ static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot) unsigned int cursor = 0; s_mc_snapshot_ignored_data_t ignored_data; xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) { - MC_process_write(snapshot->process, - ignored_data.data, ignored_data.start, ignored_data.size); + snapshot->process->write_bytes(ignored_data.data, ignored_data.size, + remote(ignored_data.start)); } } diff --git a/src/mc/mc_comm_determinism.cpp b/src/mc/mc_comm_determinism.cpp index 6bb42f7b6e..41a5918a3b 100644 --- a/src/mc/mc_comm_determinism.cpp +++ b/src/mc/mc_comm_determinism.cpp @@ -191,8 +191,7 @@ void MC_get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type char* remote_name = mc_model_checker->process().read( (std::uint64_t)(synchro.comm.rdv ? &synchro.comm.rdv->name : &synchro.comm.rdv_cpy->name)); - pattern->rdv = - MC_process_read_string(&mc_model_checker->process(), remote_name); + pattern->rdv = mc_model_checker->process().read_string(remote_name); pattern->src_proc = MC_smx_resolve_process(synchro.comm.src_proc)->pid; pattern->src_host = MC_smx_process_get_host_name(issuer); @@ -239,8 +238,7 @@ void MC_get_comm_pattern(xbt_dynar_t list, smx_simcall_t request, e_mc_call_type char* remote_name; mc_model_checker->process().read(&remote_name, remote(synchro.comm.rdv ? &synchro.comm.rdv->name : &synchro.comm.rdv_cpy->name)); - pattern->rdv = - MC_process_read_string(&mc_model_checker->process(), remote_name); + pattern->rdv = mc_model_checker->process().read_string(remote_name); pattern->dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc)->pid; pattern->dst_host = MC_smx_process_get_host_name(issuer); } else { diff --git a/src/mc/mc_page_snapshot.cpp b/src/mc/mc_page_snapshot.cpp index 5c02208369..7725f414ae 100644 --- a/src/mc/mc_page_snapshot.cpp +++ b/src/mc/mc_page_snapshot.cpp @@ -90,7 +90,7 @@ void mc_restore_page_snapshot_region(mc_process_t process, // Otherwise, copy the page: void* target_page = mc_page_from_number(start_addr, i); const void* source_page = mc_model_checker->page_store().get_page(pagenos[i]); - MC_process_write(process, source_page, target_page, xbt_pagesize); + process->write_bytes(source_page, xbt_pagesize, remote(target_page)); } } diff --git a/src/mc/mc_process.cpp b/src/mc/mc_process.cpp index b0ba0dedce..476a1a9d45 100644 --- a/src/mc/mc_process.cpp +++ b/src/mc/mc_process.cpp @@ -34,25 +34,153 @@ #include "mc_smx.h" #include "mc_server.h" +using simgrid::mc::remote; + extern "C" { XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc, "MC process information"); -static void MC_process_init_memory_map_info(mc_process_t process); -static void MC_process_open_memory_file(mc_process_t process); +// ***** Helper stuff + +#define SO_RE "\\.so[\\.0-9]*$" +#define VERSION_RE "-[\\.0-9]*$" + +static const char *const FILTERED_LIBS[] = { + "libstdc++", + "libc++", + "libm", + "libgcc_s", + "libpthread", + "libunwind", + "libunwind-x86_64", + "libunwind-x86", + "libunwind-ptrace", + "libdw", + "libdl", + "librt", + "liblzma", + "libelf", + "libbz2", + "libz", + "libelf", + "libc", + "ld" +}; + +static bool MC_is_simgrid_lib(const char* libname) +{ + return !strcmp(libname, "libsimgrid"); +} + +static bool MC_is_filtered_lib(const char* libname) +{ + for (const char* filtered_lib : FILTERED_LIBS) + if (strcmp(libname, filtered_lib)==0) + return true; + return false; +} + +struct s_mc_memory_map_re { + regex_t so_re; + regex_t version_re; +}; + +static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) +{ + const char* map_basename = basename((char*) pathname); + + regmatch_t match; + if(regexec(&res->so_re, map_basename, 1, &match, 0)) + return NULL; + + char* libname = strndup(map_basename, match.rm_so); + + // Strip the version suffix: + if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) { + char* temp = libname; + libname = strndup(temp, match.rm_so); + free(temp); + } + + return libname; +} + +static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset) +{ + char* buffer = (char*) buf; + ssize_t real_count = count; + while (count) { + ssize_t res = pread(fd, buffer, count, offset); + if (res > 0) { + count -= res; + buffer += res; + offset += res; + } else if (res==0) { + return -1; + } else if (errno != EINTR) { + return -1; + } + } + return real_count; +} + +static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset) +{ + const char* buffer = (const char*) buf; + ssize_t real_count = count; + while (count) { + ssize_t res = pwrite(fd, buffer, count, offset); + if (res > 0) { + count -= res; + buffer += res; + offset += res; + } else if (res==0) { + return -1; + } else if (errno != EINTR) { + return -1; + } + } + return real_count; +} -// ***** Destructor callbacks +static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT; +static const void* zero_buffer; +static const size_t zero_buffer_size = 10 * 4096; -// ***** mc_address_space methods for mc_process +static void MC_zero_buffer_init(void) +{ + int fd = open("/dev/zero", O_RDONLY); + if (fd<0) + xbt_die("Could not open /dev/zero"); + zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0); + if (zero_buffer == MAP_FAILED) + xbt_die("Could not map the zero buffer"); + close(fd); +} + +} -static mc_process_t MC_process_get_process(mc_process_t p) { - return p; +namespace simgrid { +namespace mc { + +int open_vm(pid_t pid, int flags) +{ + const size_t buffer_size = 30; + char buffer[buffer_size]; + int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid); + if (res < 0 || (size_t) res >= buffer_size) { + errno = ENAMETOOLONG; + return -1; + } + return open(buffer, flags); } + +} } -// ***** mc_process +// ***** Process namespace simgrid { namespace mc { @@ -69,22 +197,29 @@ Process::Process(pid_t pid, int sockfd) process->running = true; process->status = 0; process->memory_map = MC_get_memory_map(pid); - process->memory_file = -1; process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE; process->heap = NULL; process->heap_info = NULL; - MC_process_init_memory_map_info(process); - MC_process_open_memory_file(process); + process->init_memory_map_info(); + + // Open the memory file + if (process->is_self()) + process->memory_file = -1; + else { + int fd = open_vm(process->pid, O_RDWR); + if (fd<0) + xbt_die("Could not open file for process virtual address space"); + process->memory_file = fd; + } // Read std_heap (is a struct mdesc*): - dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, - "__mmalloc_default_mdp"); + dw_variable_t std_heap_var = process->find_variable("__mmalloc_default_mdp"); if (!std_heap_var) xbt_die("No heap information in the target process"); if(!std_heap_var->address) xbt_die("No constant address for this variable"); - MC_process_read(process, simgrid::mc::AddressSpace::Normal, - &process->heap_address, std_heap_var->address, sizeof(struct mdesc*), + process->read_bytes(&process->heap_address, sizeof(struct mdesc*), + remote(std_heap_var->address), simgrid::mc::ProcessIndexDisabled); process->smx_process_infos = MC_smx_process_info_list_new(); @@ -163,10 +298,8 @@ void Process::refresh_heap() if (!this->heap) { this->heap = (struct mdesc*) malloc(sizeof(struct mdesc)); } - MC_process_read(this, simgrid::mc::AddressSpace::Normal, - this->heap, this->heap_address, sizeof(struct mdesc), - simgrid::mc::ProcessIndexDisabled - ); + this->read_bytes(this->heap, sizeof(struct mdesc), remote(this->heap_address), + simgrid::mc::ProcessIndexDisabled); this->cache_flags |= MC_PROCESS_CACHE_FLAG_HEAP; } @@ -185,99 +318,28 @@ void Process::refresh_malloc_info() size_t malloc_info_bytesize = (this->heap->heaplimit + 1) * sizeof(malloc_info); this->heap_info = (malloc_info*) realloc(this->heap_info, malloc_info_bytesize); - MC_process_read(this, simgrid::mc::AddressSpace::Normal, - this->heap_info, - this->heap->heapinfo, malloc_info_bytesize, - simgrid::mc::ProcessIndexDisabled); + this->read_bytes(this->heap_info, malloc_info_bytesize, + remote(this->heap->heapinfo), simgrid::mc::ProcessIndexDisabled); this->cache_flags |= MC_PROCESS_CACHE_FLAG_MALLOC_INFO; } -} -} - -extern "C" { - -#define SO_RE "\\.so[\\.0-9]*$" -#define VERSION_RE "-[\\.0-9]*$" - -const char* FILTERED_LIBS[] = { - "libstdc++", - "libc++", - "libm", - "libgcc_s", - "libpthread", - "libunwind", - "libunwind-x86_64", - "libunwind-x86", - "libunwind-ptrace", - "libdw", - "libdl", - "librt", - "liblzma", - "libelf", - "libbz2", - "libz", - "libelf", - "libc", - "ld" -}; - -static bool MC_is_simgrid_lib(const char* libname) -{ - return !strcmp(libname, "libsimgrid"); -} - -static bool MC_is_filtered_lib(const char* libname) -{ - const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*); - size_t i; - for (i=0; i!=n; ++i) - if (strcmp(libname, FILTERED_LIBS[i])==0) - return true; - return false; -} - -struct s_mc_memory_map_re { - regex_t so_re; - regex_t version_re; -}; - -static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) { - const char* map_basename = basename((char*) pathname); - - regmatch_t match; - if(regexec(&res->so_re, map_basename, 1, &match, 0)) - return NULL; - - char* libname = strndup(map_basename, match.rm_so); - - // Strip the version suffix: - if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) { - char* temp = libname; - libname = strndup(temp, match.rm_so); - free(temp); - } - - return libname; -} - /** @brief Finds the range of the different memory segments and binary paths */ -static void MC_process_init_memory_map_info(mc_process_t process) +void Process::init_memory_map_info() { XBT_DEBUG("Get debug information ..."); - process->maestro_stack_start = NULL; - process->maestro_stack_end = NULL; - process->object_infos = NULL; - process->object_infos_size = 0; - process->binary_info = NULL; - process->libsimgrid_info = NULL; + this->maestro_stack_start = NULL; + this->maestro_stack_end = NULL; + this->object_infos = NULL; + this->object_infos_size = 0; + this->binary_info = NULL; + this->libsimgrid_info = NULL; struct s_mc_memory_map_re res; if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0)) xbt_die(".so regexp did not compile"); - memory_map_t maps = process->memory_map; + memory_map_t maps = this->memory_map; const char* current_name = NULL; @@ -294,8 +356,8 @@ static void MC_process_init_memory_map_info(mc_process_t process) // [stack], [vvar], [vsyscall], [vdso] ... if (pathname[0] == '[') { if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) { - process->maestro_stack_start = reg->start_addr; - process->maestro_stack_end = reg->end_addr; + this->maestro_stack_start = reg->start_addr; + this->maestro_stack_end = reg->end_addr; } current_name = NULL; continue; @@ -321,96 +383,96 @@ static void MC_process_init_memory_map_info(mc_process_t process) } mc_object_info_t info = - MC_find_object_info(process->memory_map, pathname, is_executable); - process->object_infos = (mc_object_info_t*) realloc(process->object_infos, - (process->object_infos_size+1) * sizeof(mc_object_info_t*)); - process->object_infos[process->object_infos_size] = info; - process->object_infos_size++; + MC_find_object_info(this->memory_map, pathname, is_executable); + this->object_infos = (mc_object_info_t*) realloc(this->object_infos, + (this->object_infos_size+1) * sizeof(mc_object_info_t*)); + this->object_infos[this->object_infos_size] = info; + this->object_infos_size++; if (is_executable) - process->binary_info = info; + this->binary_info = info; else if (libname && MC_is_simgrid_lib(libname)) - process->libsimgrid_info = info; + this->libsimgrid_info = info; free(libname); } regfree(&res.so_re); regfree(&res.version_re); - // Resolve time (including accress differents objects): - for (size_t i=0; i!=process->object_infos_size; ++i) - MC_post_process_object_info(process, process->object_infos[i]); + // Resolve time (including accross differents objects): + for (size_t i=0; i!=this->object_infos_size; ++i) + MC_post_process_object_info(this, this->object_infos[i]); - xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start"); - xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end"); + xbt_assert(this->maestro_stack_start, "Did not find maestro_stack_start"); + xbt_assert(this->maestro_stack_end, "Did not find maestro_stack_end"); XBT_DEBUG("Get debug information done !"); } -mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr) +mc_object_info_t Process::find_object_info(remote_ptr addr) const { size_t i; - for (i = 0; i != process->object_infos_size; ++i) { - if (addr >= (void *) process->object_infos[i]->start - && addr <= (void *) process->object_infos[i]->end) { - return process->object_infos[i]; + for (i = 0; i != this->object_infos_size; ++i) { + if (addr.address() >= (std::uint64_t)this->object_infos[i]->start + && addr.address() <= (std::uint64_t)this->object_infos[i]->end) { + return this->object_infos[i]; } } return NULL; } -mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr) +mc_object_info_t Process::find_object_info_exec(remote_ptr addr) const { size_t i; - for (i = 0; i != process->object_infos_size; ++i) { - if (addr >= (void *) process->object_infos[i]->start_exec - && addr <= (void *) process->object_infos[i]->end_exec) { - return process->object_infos[i]; + for (i = 0; i != this->object_infos_size; ++i) { + if (addr.address() >= (std::uint64_t)this->object_infos[i]->start_exec + && addr.address() <= (std::uint64_t)this->object_infos[i]->end_exec) { + return this->object_infos[i]; } } return NULL; } -mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr) +mc_object_info_t Process::find_object_info_rw(remote_ptr addr) const { size_t i; - for (i = 0; i != process->object_infos_size; ++i) { - if (addr >= (void *) process->object_infos[i]->start_rw - && addr <= (void *) process->object_infos[i]->end_rw) { - return process->object_infos[i]; + for (i = 0; i != this->object_infos_size; ++i) { + if (addr.address() >= (std::uint64_t)this->object_infos[i]->start_rw + && addr.address() <= (std::uint64_t)this->object_infos[i]->end_rw) { + return this->object_infos[i]; } } return NULL; } -// Functions, variables… - -dw_frame_t MC_process_find_function(mc_process_t process, const void *ip) +dw_frame_t Process::find_function(remote_ptr ip) const { - mc_object_info_t info = MC_process_find_object_info_exec(process, ip); + mc_object_info_t info = this->find_object_info_exec(ip); if (info == NULL) return NULL; else - return MC_file_object_info_find_function(info, ip); + return MC_file_object_info_find_function(info, (void*) ip.address()); } -dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name) +/** Find (one occurence of) the named variable definition + */ +dw_variable_t Process::find_variable(const char* name) const { - const size_t n = process->object_infos_size; + const size_t n = this->object_infos_size; size_t i; // First lookup the variable in the executable shared object. // A global variable used directly by the executable code from a library // is reinstanciated in the executable memory .data/.bss. // We need to look up the variable in the execvutable first. - if (process->binary_info) { - mc_object_info_t info = process->binary_info; + if (this->binary_info) { + mc_object_info_t info = this->binary_info; dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name); if (var) return var; } for (i=0; i!=n; ++i) { - mc_object_info_t info =process->object_infos[i]; + mc_object_info_t info = this->object_infos[i]; dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name); if (var) return var; @@ -419,9 +481,9 @@ dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* return NULL; } -void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size) +void Process::read_variable(const char* name, void* target, size_t size) const { - dw_variable_t var = MC_process_find_variable_by_name(process, name); + dw_variable_t var = this->find_variable(name); if (!var->address) xbt_die("No simple location for this variable"); if (!var->type->full_type) @@ -429,23 +491,22 @@ void MC_process_read_variable(mc_process_t process, const char* name, void* targ if ((size_t) var->type->full_type->byte_size != size) xbt_die("Unexpected size for %s (expected %zi, was %zi)", name, size, (size_t) var->type->full_type->byte_size); - MC_process_read(process, simgrid::mc::AddressSpace::Normal, target, var->address, size, - simgrid::mc::ProcessIndexAny); + this->read_bytes(target, size, remote(var->address)); } -char* MC_process_read_string(mc_process_t process, void* address) +char* Process::read_string(remote_ptr address) const { if (!address) return NULL; - if (process->is_self()) - return strdup((char*) address); + if (this->is_self()) + return strdup((char*) address.address()); off_t len = 128; char* res = (char*) malloc(len); off_t off = 0; while (1) { - ssize_t c = pread(process->memory_file, res + off, len - off, (off_t) address + off); + ssize_t c = pread(this->memory_file, res + off, len - off, (off_t) address.address() + off); if (c == -1) { if (errno == EINTR) continue; @@ -467,80 +528,12 @@ char* MC_process_read_string(mc_process_t process, void* address) } } -// ***** Memory access - -int MC_process_vm_open(pid_t pid, int flags) -{ - const size_t buffer_size = 30; - char buffer[buffer_size]; - int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid); - if (res < 0 || (size_t) res >= buffer_size) { - errno = ENAMETOOLONG; - return -1; - } - return open(buffer, flags); -} - -static void MC_process_open_memory_file(mc_process_t process) -{ - if (process->is_self() || process->memory_file >= 0) - return; - - int fd = MC_process_vm_open(process->pid, O_RDWR); - if (fd<0) - xbt_die("Could not open file for process virtual address space"); - process->memory_file = fd; -} - -static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset) -{ - char* buffer = (char*) buf; - ssize_t real_count = count; - while (count) { - ssize_t res = pread(fd, buffer, count, offset); - if (res > 0) { - count -= res; - buffer += res; - offset += res; - } else if (res==0) { - return -1; - } else if (errno != EINTR) { - return -1; - } - } - return real_count; -} - -static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset) -{ - const char* buffer = (const char*) buf; - ssize_t real_count = count; - while (count) { - ssize_t res = pwrite(fd, buffer, count, offset); - if (res > 0) { - count -= res; - buffer += res; - offset += res; - } else if (res==0) { - return -1; - } else if (errno != EINTR) { - return -1; - } - } - return real_count; -} - -} - -namespace simgrid { -namespace mc { - const void *Process::read_bytes(void* buffer, std::size_t size, remote_ptr address, int process_index, - AddressSpace::ReadMode mode) + AddressSpace::ReadMode mode) const { if (process_index != simgrid::mc::ProcessIndexDisabled) { - mc_object_info_t info = MC_process_find_object_info_rw(this, (void*)address.address()); + mc_object_info_t info = this->find_object_info_rw((void*)address.address()); // Segment overlap is not handled. if (MC_object_info_is_privatized(info)) { if (process_index < 0) @@ -566,6 +559,38 @@ const void *Process::read_bytes(void* buffer, std::size_t size, } } +/** Write data to a process memory + * + * @param process the process + * @param local local memory address (source) + * @param remote target process memory address (target) + * @param len data size + */ +void Process::write_bytes(const void* buffer, size_t len, remote_ptr address) +{ + if (this->is_self()) { + memcpy((void*)address.address(), buffer, len); + } else { + if (pwrite_whole(this->memory_file, buffer, len, address.address()) < 0) + xbt_die("Write to process %lli failed", (long long) this->pid); + } +} + +void Process::clear_bytes(remote_ptr address, size_t len) +{ + if (this->is_self()) { + memset((void*)address.address(), 0, len); + } else { + pthread_once(&zero_buffer_flag, MC_zero_buffer_init); + while (len) { + size_t s = len > zero_buffer_size ? zero_buffer_size : len; + this->write_bytes(zero_buffer, s, address); + address = remote((char*) address.address() + s); + len -= s; + } + } +} + } } @@ -575,63 +600,23 @@ const void* MC_process_read_dynar_element(mc_process_t process, void* local, const void* remote_dynar, size_t i, size_t len) { s_xbt_dynar_t d; - MC_process_read_simple(process, &d, remote_dynar, sizeof(d)); + process->read_bytes(&d, sizeof(d), remote(remote_dynar)); if (i >= d.used) xbt_die("Out of bound index %zi/%lu", i, d.used); if (len != d.elmsize) xbt_die("Bad size in MC_process_read_dynar_element"); - MC_process_read_simple(process, local, xbt_dynar_get_ptr(&d, i), len); + process->read_bytes(local, len, remote(xbt_dynar_get_ptr(&d, i))); return local; } -void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len) -{ - if (process->is_self()) { - memcpy(remote, local, len); - } else { - if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0) - xbt_die("Write to process %lli failed", (long long) process->pid); - } -} - unsigned long MC_process_read_dynar_length(mc_process_t process, const void* remote_dynar) { if (!remote_dynar) return 0; unsigned long res; - MC_process_read_simple(process, &res, - &((xbt_dynar_t)remote_dynar)->used, sizeof(res)); + process->read_bytes(&res, sizeof(res), + remote(&((xbt_dynar_t)remote_dynar)->used)); return res; } -static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT; -static const void* zero_buffer; -static const size_t zero_buffer_size = 10 * 4096; - -static void MC_zero_buffer_init(void) -{ - int fd = open("/dev/zero", O_RDONLY); - if (fd<0) - xbt_die("Could not open /dev/zero"); - zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0); - if (zero_buffer == MAP_FAILED) - xbt_die("Could not map the zero buffer"); - close(fd); -} - -void MC_process_clear_memory(mc_process_t process, void* remote, size_t len) -{ - if (process->is_self()) { - memset(remote, 0, len); - } else { - pthread_once(&zero_buffer_flag, MC_zero_buffer_init); - while (len) { - size_t s = len > zero_buffer_size ? zero_buffer_size : len; - MC_process_write(process, zero_buffer, remote, s); - remote = (char*) remote + s; - len -= s; - } - } -} - } diff --git a/src/mc/mc_process.h b/src/mc/mc_process.h index 743fb2f4a8..c21e951b7b 100644 --- a/src/mc/mc_process.h +++ b/src/mc/mc_process.h @@ -53,9 +53,31 @@ class Process : public AddressSpace { public: Process(pid_t pid, int sockfd); ~Process(); + + bool is_self() const + { + return this->process_flags & MC_PROCESS_SELF_FLAG; + } + + // Read memory: const void* read_bytes(void* buffer, std::size_t size, remote_ptr address, int process_index = ProcessIndexAny, - ReadMode mode = Normal) override; + ReadMode mode = Normal) const override; + void read_variable(const char* name, void* target, size_t size) const; + char* read_string(remote_ptr address) const; + + // Write memory: + void write_bytes(const void* buffer, size_t len, remote_ptr address); + void clear_bytes(remote_ptr address, size_t len); + + // Debug information: + mc_object_info_t find_object_info(remote_ptr addr) const; + mc_object_info_t find_object_info_exec(remote_ptr addr) const; + mc_object_info_t find_object_info_rw(remote_ptr addr) const; + dw_frame_t find_function(remote_ptr ip) const; + dw_variable_t find_variable(const char* name) const; + + // Heap access: xbt_mheap_t get_heap() { if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)) @@ -68,11 +90,9 @@ public: this->refresh_malloc_info(); return this->heap_info; } - bool is_self() - { - return this->process_flags & MC_PROCESS_SELF_FLAG; - } + private: + void init_memory_map_info(); void refresh_heap(); void refresh_malloc_info(); public: // to be private @@ -147,66 +167,20 @@ public: // to be private xbt_dynar_t checkpoint_ignore; }; -} -} - -SG_BEGIN_DECL() - /** Open a FD to a remote process memory (`/dev/$pid/mem`) */ -int MC_process_vm_open(pid_t pid, int flags); +int open_vm(pid_t pid, int flags); -/* Process memory access: */ - -static inline -const void* MC_process_read(mc_process_t process, - simgrid::mc::AddressSpace::ReadMode mode, - void* local, const void* remote, size_t len, - int process_index) -{ - return process->read_bytes(local, len, (std::uint64_t)remote, process_index, mode); } - -// Simplified versions/wrappers (whould be moved in mc_address_space): -static inline const void* MC_process_read_simple(mc_process_t process, - void* local, const void* remote, size_t len) -{ - return process->read_bytes(local, len, (std::uint64_t)remote); } +SG_BEGIN_DECL() + XBT_INTERNAL const void* MC_process_read_dynar_element(mc_process_t process, void* local, const void* remote_dynar, size_t i, size_t len); XBT_INTERNAL unsigned long MC_process_read_dynar_length(mc_process_t process, const void* remote_dynar); -/** Write data to a process memory - * - * @param process the process - * @param local local memory address (source) - * @param remote target process memory address (target) - * @param len data size - */ -XBT_INTERNAL void MC_process_write(mc_process_t process, - const void* local, void* remote, size_t len); - -XBT_INTERNAL void MC_process_clear_memory(mc_process_t process, - void* remote, size_t len); - -/* Functions, variables of the process: */ - -XBT_INTERNAL mc_object_info_t MC_process_find_object_info(mc_process_t process, const void* addr); -XBT_INTERNAL mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void* addr); -XBT_INTERNAL mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void* addr); - -XBT_INTERNAL dw_frame_t MC_process_find_function(mc_process_t process, const void* ip); - -XBT_INTERNAL void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size); -XBT_INTERNAL char* MC_process_read_string(mc_process_t, void* address); - -/** Find (one occurence of) the named variable definition - */ -XBT_INTERNAL dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name); - XBT_INTERNAL void MC_invalidate_cache(void); SG_END_DECL() diff --git a/src/mc/mc_request.cpp b/src/mc/mc_request.cpp index 4d0ebf46c1..e28f11983f 100644 --- a/src/mc/mc_request.cpp +++ b/src/mc/mc_request.cpp @@ -11,6 +11,8 @@ #include "mc_private.h" #include "mc_smx.h" +using simgrid::mc::remote; + extern "C" { XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_request, mc, @@ -275,8 +277,8 @@ char *MC_request_to_string(smx_simcall_t req, int value, e_mc_request_type_t req // size_t size = size_pointer ? *size_pointer : 0; size_t size = 0; if (remote_size) - MC_process_read_simple(&mc_model_checker->process(), &size, - remote_size, sizeof(size)); + mc_model_checker->process().read_bytes(&size, sizeof(size), + remote(remote_size)); type = "iRecv"; char* p = pointer_to_string(simcall_comm_irecv__get__dst_buff(req)); @@ -311,8 +313,8 @@ char *MC_request_to_string(smx_simcall_t req, int value, e_mc_request_type_t req s_smx_synchro_t synchro; smx_synchro_t act; if (use_remote_comm) { - MC_process_read_simple(&mc_model_checker->process(), &synchro, - remote_act, sizeof(synchro)); + mc_model_checker->process().read_bytes(&synchro, + sizeof(synchro), remote(remote_act)); act = &synchro; } else act = remote_act; @@ -336,8 +338,8 @@ char *MC_request_to_string(smx_simcall_t req, int value, e_mc_request_type_t req s_smx_synchro_t synchro; smx_synchro_t act; if (use_remote_comm) { - MC_process_read_simple(&mc_model_checker->process(), &synchro, - remote_act, sizeof(synchro)); + mc_model_checker->process().read_bytes(&synchro, + sizeof(synchro), remote(remote_act)); act = &synchro; } else act = remote_act; @@ -368,8 +370,8 @@ char *MC_request_to_string(smx_simcall_t req, int value, e_mc_request_type_t req case SIMCALL_COMM_WAITANY: { type = "WaitAny"; s_xbt_dynar_t comms; - MC_process_read_simple(&mc_model_checker->process(), - &comms, simcall_comm_waitany__get__comms(req), sizeof(comms)); + mc_model_checker->process().read_bytes( + &comms, sizeof(comms), remote(simcall_comm_waitany__get__comms(req))); if (!xbt_dynar_is_empty(&comms)) { smx_synchro_t remote_sync; MC_process_read_dynar_element(&mc_model_checker->process(), @@ -402,11 +404,11 @@ char *MC_request_to_string(smx_simcall_t req, int value, e_mc_request_type_t req type = "Mutex LOCK"; s_smx_mutex_t mutex; - MC_process_read_simple(&mc_model_checker->process(), &mutex, - simcall_mutex_lock__get__mutex(req), sizeof(mutex)); + mc_model_checker->process().read_bytes(&mutex, sizeof(mutex), + remote(simcall_mutex_lock__get__mutex(req))); s_xbt_swag_t mutex_sleeping; - MC_process_read_simple(&mc_model_checker->process(), &mutex_sleeping, - mutex.sleeping, sizeof(mutex_sleeping)); + mc_model_checker->process().read_bytes(&mutex_sleeping, sizeof(mutex_sleeping), + remote(mutex.sleeping)); args = bprintf("locked = %d, owner = %d, sleeping = %d", mutex.locked, @@ -458,14 +460,14 @@ unsigned int MC_request_testany_fail(smx_simcall_t req) // Read the dynar: s_xbt_dynar_t comms; - MC_process_read_simple(&mc_model_checker->process(), - &comms, simcall_comm_testany__get__comms(req), sizeof(comms)); + mc_model_checker->process().read_bytes( + &comms, sizeof(comms), remote(simcall_comm_testany__get__comms(req))); // Read ther dynar buffer: size_t buffer_size = comms.elmsize * comms.used; char buffer[buffer_size]; - MC_process_read_simple(&mc_model_checker->process(), - buffer, comms.data, buffer_size); + mc_model_checker->process().read_bytes( + buffer, buffer_size, remote(comms.data)); // Iterate over the elements: assert(comms.elmsize == sizeof(smx_synchro_t)); @@ -478,8 +480,8 @@ unsigned int MC_request_testany_fail(smx_simcall_t req) // Dereference the pointer: s_smx_synchro_t action; - MC_process_read_simple(&mc_model_checker->process(), - &action, remote_action, sizeof(action)); + mc_model_checker->process().read_bytes( + &action, sizeof(action), remote(remote_action)); // Finally so something useful about it: if (action.comm.src_proc && action.comm.dst_proc) @@ -518,8 +520,8 @@ int MC_request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx) } s_smx_synchro_t synchro; - MC_process_read_simple(&mc_model_checker->process(), - &synchro, remote_act, sizeof(synchro)); + mc_model_checker->process().read_bytes( + &synchro, sizeof(synchro), remote(remote_act)); return synchro.comm.src_proc && synchro.comm.dst_proc; } @@ -564,8 +566,8 @@ char *MC_request_get_dot_output(smx_simcall_t req, int value) } else { smx_synchro_t remote_act = simcall_comm_wait__get__comm(req); s_smx_synchro_t synchro; - MC_process_read_simple(&mc_model_checker->process(), &synchro, - remote_act, sizeof(synchro)); + mc_model_checker->process().read_bytes(&synchro, + sizeof(synchro), remote(remote_act)); smx_process_t src_proc = MC_smx_resolve_process(synchro.comm.src_proc); smx_process_t dst_proc = MC_smx_resolve_process(synchro.comm.dst_proc); @@ -587,8 +589,8 @@ char *MC_request_get_dot_output(smx_simcall_t req, int value) case SIMCALL_COMM_TEST: { smx_synchro_t remote_act = simcall_comm_test__get__comm(req); s_smx_synchro_t synchro; - MC_process_read_simple(&mc_model_checker->process(), &synchro, - remote_act, sizeof(synchro)); + mc_model_checker->process().read_bytes(&synchro, + sizeof(synchro), remote(remote_act)); if (synchro.comm.src_proc == NULL || synchro.comm.dst_proc == NULL) { if (issuer->smx_host) label = diff --git a/src/mc/mc_server.cpp b/src/mc/mc_server.cpp index 7613b16fb8..ef4dae32ff 100644 --- a/src/mc/mc_server.cpp +++ b/src/mc/mc_server.cpp @@ -22,6 +22,8 @@ #include "mc_ignore.h" #include "mcer_ignore.h" +using simgrid::mc::remote; + extern "C" { XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_server, mc, "MC server logic"); @@ -42,8 +44,7 @@ static int mc_symbol_pointer_callback_evaluate(void* p) { struct mc_symbol_pointer_callback* callback = (struct mc_symbol_pointer_callback*) p; int value; - MC_process_read(callback->process, simgrid::mc::AddressSpace::Normal, - &value, callback->value, sizeof(value), simgrid::mc::ProcessIndexAny); + callback->process->read_bytes(&value, sizeof(value), remote(callback->value)); return value; } diff --git a/src/mc/mc_smx.cpp b/src/mc/mc_smx.cpp index 6260b0cabb..8372875bd8 100644 --- a/src/mc/mc_smx.cpp +++ b/src/mc/mc_smx.cpp @@ -13,6 +13,8 @@ #include "mc_smx.h" #include "ModelChecker.hpp" +using simgrid::mc::remote; + extern "C" { static @@ -60,8 +62,7 @@ static void MC_process_refresh_simix_process_list( { // swag = REMOTE(*simix_global->process_list) s_xbt_swag_t swag; - MC_process_read(process, simgrid::mc::AddressSpace::Normal, &swag, remote_swag, sizeof(swag), - simgrid::mc::ProcessIndexAny); + process->read_bytes(&swag, sizeof(swag), remote(remote_swag)); smx_process_t p; xbt_dynar_reset(target); @@ -74,8 +75,7 @@ static void MC_process_refresh_simix_process_list( info.address = p; info.name = NULL; info.hostname = NULL; - MC_process_read(process, simgrid::mc::AddressSpace::Normal, - &info.copy, p, sizeof(info.copy), simgrid::mc::ProcessIndexAny); + process->read_bytes(&info.copy, sizeof(info.copy), remote(p)); xbt_dynar_push(target, &info); // Lookup next process address: @@ -95,13 +95,12 @@ void MC_process_smx_refresh(mc_process_t process) // simix_global_p = REMOTE(simix_global); smx_global_t simix_global_p; - MC_process_read_variable(process, "simix_global", &simix_global_p, sizeof(simix_global_p)); + process->read_variable("simix_global", &simix_global_p, sizeof(simix_global_p)); // simix_global = REMOTE(*simix_global) s_smx_global_t simix_global; - MC_process_read(process, simgrid::mc::AddressSpace::Normal, &simix_global, - simix_global_p, sizeof(simix_global), - simgrid::mc::ProcessIndexAny); + process->read_bytes(&simix_global, sizeof(simix_global), + remote(simix_global_p)); MC_process_refresh_simix_process_list( process, process->smx_process_infos, simix_global.process_list); @@ -189,10 +188,10 @@ const char* MC_smx_process_get_host_name(smx_process_t p) if (!info->hostname) { // Read the hostname from the MCed process: - MC_process_read_simple(process, &host_copy, p->smx_host, sizeof(host_copy)); + process->read_bytes(&host_copy, sizeof(host_copy), remote(p->smx_host)); int len = host_copy.key_len + 1; char hostname[len]; - MC_process_read_simple(process, hostname, host_copy.key, len); + process->read_bytes(hostname, len, remote(host_copy.key)); info->hostname = mc_model_checker->get_host_name(hostname); } return info->hostname; @@ -208,7 +207,7 @@ const char* MC_smx_process_get_name(smx_process_t p) mc_smx_process_info_t info = MC_smx_process_get_info(p); if (!info->name) { - info->name = MC_process_read_string(process, p->name); + info->name = process->read_string(p->name); } return info->name; } @@ -219,7 +218,7 @@ int MC_smpi_process_count(void) return smpi_process_count(); else { int res; - MC_process_read_variable(&mc_model_checker->process(), "process_count", + mc_model_checker->process().read_variable("process_count", &res, sizeof(res)); return res; } @@ -228,7 +227,7 @@ int MC_smpi_process_count(void) unsigned long MC_smx_get_maxpid(void) { unsigned long maxpid; - MC_process_read_variable(&mc_model_checker->process(), "simix_process_maxpid", + mc_model_checker->process().read_variable("simix_process_maxpid", &maxpid, sizeof(maxpid)); return maxpid; } diff --git a/src/mc/mc_snapshot.cpp b/src/mc/mc_snapshot.cpp index 57e229d3c4..270a6aedcb 100644 --- a/src/mc/mc_snapshot.cpp +++ b/src/mc/mc_snapshot.cpp @@ -23,7 +23,8 @@ extern "C" { * @param Snapshot region in the snapshot this pointer belongs to * (or NULL if it does not belong to any snapshot region) * */ -mc_mem_region_t mc_get_snapshot_region(const void* addr, mc_snapshot_t snapshot, int process_index) +mc_mem_region_t mc_get_snapshot_region( + const void* addr, const s_mc_snapshot_t* snapshot, int process_index) { size_t n = snapshot->snapshot_regions_count; for (size_t i = 0; i != n; ++i) { @@ -184,7 +185,7 @@ Snapshot::~Snapshot() const void* Snapshot::read_bytes(void* buffer, std::size_t size, remote_ptr address, int process_index, - AddressSpace::ReadMode mode) + AddressSpace::ReadMode mode) const { mc_mem_region_t region = mc_get_snapshot_region((void*)address.address(), this, process_index); if (region) { @@ -197,7 +198,7 @@ const void* Snapshot::read_bytes(void* buffer, std::size_t size, } } else - return MC_process_read(this->process, mode, buffer, (void*)address.address(), size, process_index); + return this->read_bytes(buffer, size, address, process_index, mode); } } diff --git a/src/mc/mc_snapshot.h b/src/mc/mc_snapshot.h index c9da67deb0..135cf26b5c 100644 --- a/src/mc/mc_snapshot.h +++ b/src/mc/mc_snapshot.h @@ -120,7 +120,7 @@ void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region) } XBT_INTERNAL mc_mem_region_t mc_get_snapshot_region( - const void* addr, mc_snapshot_t snapshot, int process_index); + const void* addr, const s_mc_snapshot_t *snapshot, int process_index); /** \brief Translate a pointer from process address space to snapshot address space * @@ -210,7 +210,7 @@ public: ~Snapshot(); const void* read_bytes(void* buffer, std::size_t size, remote_ptr address, int process_index = ProcessIndexAny, - ReadMode mode = Normal) override; + ReadMode mode = Normal) const override; public: // To be private mc_process_t process; int num_state; diff --git a/src/mc/mc_state.cpp b/src/mc/mc_state.cpp index 140452916f..f3b35cdf6e 100644 --- a/src/mc/mc_state.cpp +++ b/src/mc/mc_state.cpp @@ -14,6 +14,8 @@ #include "mc_comm_pattern.h" #include "mc_smx.h" +using simgrid::mc::remote; + extern "C" { XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc, @@ -127,16 +129,16 @@ void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req, case SIMCALL_COMM_WAIT: state->internal_req = *req; - MC_process_read_simple(&mc_model_checker->process(), &state->internal_comm , - simcall_comm_wait__get__comm(req), sizeof(state->internal_comm)); + mc_model_checker->process().read_bytes(&state->internal_comm , + sizeof(state->internal_comm), remote(simcall_comm_wait__get__comm(req))); simcall_comm_wait__set__comm(&state->executed_req, &state->internal_comm); simcall_comm_wait__set__comm(&state->internal_req, &state->internal_comm); break; case SIMCALL_COMM_TEST: state->internal_req = *req; - MC_process_read_simple(&mc_model_checker->process(), &state->internal_comm, - simcall_comm_test__get__comm(req), sizeof(state->internal_comm)); + mc_model_checker->process().read_bytes(&state->internal_comm, + sizeof(state->internal_comm), remote(simcall_comm_test__get__comm(req))); simcall_comm_test__set__comm(&state->executed_req, &state->internal_comm); simcall_comm_test__set__comm(&state->internal_req, &state->internal_comm); break; @@ -236,8 +238,8 @@ static inline smx_simcall_t MC_state_get_request_for_process( case SIMCALL_COMM_WAIT: { smx_synchro_t remote_act = simcall_comm_wait__get__comm(&process->simcall); s_smx_synchro_t act; - MC_process_read_simple(&mc_model_checker->process(), - &act, remote_act, sizeof(act)); + mc_model_checker->process().read_bytes( + &act, sizeof(act), remote(remote_act)); if (act.comm.src_proc && act.comm.dst_proc) { *value = 0; } else { diff --git a/src/mc/mc_unw.cpp b/src/mc/mc_unw.cpp index cb213124e7..f02a89fe3f 100644 --- a/src/mc/mc_unw.cpp +++ b/src/mc/mc_unw.cpp @@ -164,7 +164,7 @@ static int get_proc_name(unw_addr_space_t as, void* arg) { mc_unw_context_t context = (mc_unw_context_t) arg; - dw_frame_t frame = MC_process_find_function(context->process, (void*) addr); + dw_frame_t frame = context->process->find_function(remote(addr)); if (!frame) return - UNW_ENOINFO; *offp = (unw_word_t) frame->low_pc - addr; diff --git a/src/mc/mc_unw_vmread.cpp b/src/mc/mc_unw_vmread.cpp index fa1c2d2aa1..cf505c7fa3 100644 --- a/src/mc/mc_unw_vmread.cpp +++ b/src/mc/mc_unw_vmread.cpp @@ -74,7 +74,7 @@ static int access_mem(const unw_addr_space_t as, size_t count = size; off_t off = (off_t) addr; char* buf = (char*) valp; - int fd = MC_process_vm_open(pid, O_RDONLY); + int fd = simgrid::mc::open_vm(pid, O_RDONLY); if (fd < 0) return - UNW_EINVAL; while (1) {