Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Cross-process support for MC_ignore
[simgrid.git] / src / mc / mc_process.c
index ada6195..207efa9 100644 (file)
 #include <regex.h>
 #include <sys/mman.h> // PROT_*
 
+#include <pthread.h>
+
 #include <libgen.h>
 
 #include "mc_process.h"
 #include "mc_object_info.h"
+#include "mc_address_space.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
                                 "MC process information");
@@ -21,9 +24,13 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
 static void MC_process_init_memory_map_info(mc_process_t process);
 static void MC_process_open_memory_file(mc_process_t process);
 
+static s_mc_address_space_class_t mc_process_class = {
+  .read = (void*) &MC_process_read
+};
 
 void MC_process_init(mc_process_t process, pid_t pid)
 {
+  process->address_space.address_space_class = &mc_process_class;
   process->process_flags = MC_PROCESS_NO_FLAG;
   process->pid = pid;
   if (pid==getpid())
@@ -42,12 +49,14 @@ void MC_process_init(mc_process_t process, pid_t pid)
     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, &process->heap_address,
-    std_heap_var->address, sizeof(struct mdesc*));
+  MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
+    &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
+    MC_PROCESS_INDEX_DISABLED);
 }
 
 void MC_process_clear(mc_process_t process)
 {
+  process->address_space.address_space_class = NULL;
   process->process_flags = MC_PROCESS_NO_FLAG;
   process->pid = 0;
 
@@ -87,8 +96,10 @@ void MC_process_refresh_heap(mc_process_t process)
     process->heap = malloc(sizeof(struct mdesc));
     mmalloc_set_current_heap(oldheap);
   }
-  MC_process_read(process, process->heap,
-    process->heap_address, sizeof(struct mdesc));
+  MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
+    process->heap, process->heap_address, sizeof(struct mdesc),
+    MC_PROCESS_INDEX_DISABLED
+    );
 }
 
 void MC_process_refresh_malloc_info(mc_process_t process)
@@ -105,8 +116,10 @@ void MC_process_refresh_malloc_info(mc_process_t process)
     malloc_info_bytesize);
   mmalloc_set_current_heap(oldheap);
 
-  MC_process_read(process, process->heap_info,
-    process->heap->heapinfo, malloc_info_bytesize);
+  MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
+    process->heap_info,
+    process->heap->heapinfo, malloc_info_bytesize,
+    MC_PROCESS_INDEX_DISABLED);
 }
 
 #define SO_RE "\\.so[\\.0-9]*$"
@@ -258,12 +271,36 @@ static void MC_process_init_memory_map_info(mc_process_t process)
   XBT_INFO("Get debug information done !");
 }
 
-mc_object_info_t MC_process_find_object_info(mc_process_t process, void *ip)
+mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
+{
+  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];
+    }
+  }
+  return NULL;
+}
+
+mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
+{
+  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];
+    }
+  }
+  return NULL;
+}
+
+mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
 {
   size_t i;
   for (i = 0; i != process->object_infos_size; ++i) {
-    if (ip >= (void *) process->object_infos[i]->start_exec
-        && ip <= (void *) process->object_infos[i]->end_exec) {
+    if (addr >= (void *) process->object_infos[i]->start_rw
+        && addr <= (void *) process->object_infos[i]->end_rw) {
       return process->object_infos[i];
     }
   }
@@ -272,9 +309,9 @@ mc_object_info_t MC_process_find_object_info(mc_process_t process, void *ip)
 
 // Functions, variables…
 
-dw_frame_t MC_process_find_function(mc_process_t process, void *ip)
+dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
 {
-  mc_object_info_t info = MC_process_find_object_info(process, ip);
+  mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
   if (info == NULL)
     return NULL;
   else
@@ -354,13 +391,33 @@ static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
   return real_count;
 }
 
-void MC_process_read(mc_process_t process, void* local, const void* remote, size_t len)
+const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
+  void* local, const void* remote, size_t len,
+  int process_index)
 {
+  if (process_index != MC_PROCESS_INDEX_DISABLED) {
+    mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
+    // Segment overlap is not handled.
+    if (MC_object_info_is_privatized(info)) {
+      if (process_index < 0)
+        xbt_die("Missing process index");
+      // Address translation in the privaization segment:
+      size_t offset = (const char*) remote - info->start_rw;
+      remote = (const char*) remote - offset;
+    }
+  }
+
   if (MC_process_is_self(process)) {
-    memcpy(local, remote, len);
+    if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
+      return remote;
+    else {
+      memcpy(local, remote, len);
+      return local;
+    }
   } else {
     if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
       xbt_die("Read from process %lli failed", (long long) process->pid);
+    return local;
   }
 }
 
@@ -373,3 +430,33 @@ void MC_process_write(mc_process_t process, const void* local, void* remote, siz
       xbt_die("Write to process %lli failed", (long long) process->pid);
   }
 }
+
+static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
+static const void* zero_buffer;
+static const int 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 (MC_process_is_self(process)) {
+    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;
+    }
+  }
+}