Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The creation of the pimpl needs no simcall
[simgrid.git] / src / mc / mc_unw_vmread.cpp
index 55e3481..3f37e2a 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015. The SimGrid Team.
+/* Copyright (c) 2015-2019. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -7,12 +7,12 @@
 #include <sys/types.h>
 #include <sys/uio.h>
 
+#include <fcntl.h>
 #include <libunwind.h>
 #include <libunwind-ptrace.h>
 
-#include "mc_unw.h"
-
-extern "C" {
+#include "src/mc/mc_unw.hpp"
+#include "src/mc/remote/RemoteClient.hpp"
 
 /** \file
  *  Libunwind namespace implementation using process_vm_readv.
@@ -22,13 +22,13 @@ extern "C" {
 
 /** Partial structure of libunwind-ptrace context in order to get the PID
  *
- *  The context type for libunwind-race is an opaque type. We need to get the
- *  PID which is the first field. This is a hack which might break if the
- *  libunwind-ptrace structure changes.
+ *  HACK, The context type for libunwind-race is an opaque type.
+ *  We need to get the PID which is the first field. This is a hack
+ *  which might break if the libunwind-ptrace structure changes.
  */
 struct _UPT_info {
   pid_t pid;
-  // Other things;
+  // Other things...
 };
 
 /** Get the PID of a `libunwind-ptrace` context
@@ -36,7 +36,7 @@ struct _UPT_info {
 static inline
 pid_t _UPT_getpid(void* arg)
 {
-  struct _UPT_info* info = (_UPT_info*) arg;
+  _UPT_info* info = static_cast<_UPT_info*>(arg);
   return info->pid;
 }
 
@@ -48,17 +48,16 @@ static int access_mem(const unw_addr_space_t as,
 {
   if (write)
     return - UNW_EINVAL;
-  ssize_t s;
   pid_t pid = _UPT_getpid(arg);
   size_t size = sizeof(unw_word_t);
 
-#ifdef HAVE_PROCESS_VM_READV
+#if HAVE_PROCESS_VM_READV
   // process_vm_read implementation.
   // This is only available since Linux 3.2.
 
   struct iovec local = { valp, size };
   struct iovec remote = { (void*) addr, size };
-  s = process_vm_readv(pid, &local, 1, &remote, 1, 0);
+  ssize_t s = process_vm_readv(pid, &local, 1, &remote, 1, 0);
   if (s >= 0) {
     if ((size_t) s != size)
       return - UNW_EINVAL;
@@ -75,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) {
@@ -101,16 +100,42 @@ static int access_mem(const unw_addr_space_t as,
   return _UPT_access_mem(as, addr, valp, write, arg);
 }
 
-unw_accessors_t mc_unw_vmread_accessors =
-  {
-    .find_proc_info             = &_UPT_find_proc_info,
-    .put_unwind_info            = &_UPT_put_unwind_info,
-    .get_dyn_info_list_addr     = &_UPT_get_dyn_info_list_addr,
-    .access_mem                 = &access_mem,
-    .access_reg                 = &_UPT_access_reg,
-    .access_fpreg               = &_UPT_access_fpreg,
-    .resume                     = &_UPT_resume,
-    .get_proc_name              = &_UPT_get_proc_name
-  };
+namespace simgrid {
+namespace unw {
+
+/** Virtual table for our `libunwind-process_vm_readv` implementation.
+ *
+ *  This implementation reuse most the code of `libunwind-ptrace` but
+ *  does not use ptrace() to read the target process memory by
+ *  `process_vm_readv()` or `/dev/${pid}/mem` if possible.
+ *
+ *  Does not support any MC-specific behaviour (privatization, snapshots)
+ *  and `ucontext_t`.
+ *
+ *  It works with `void*` contexts allocated with `_UPT_create(pid)`.
+ */
+// TODO, we could get rid of this if we properly stop the model-checked
+// process before reading the memory.
+static unw_accessors_t accessors = {
+  &_UPT_find_proc_info,
+  &_UPT_put_unwind_info,
+  &_UPT_get_dyn_info_list_addr,
+  &access_mem,
+  &_UPT_access_reg,
+  &_UPT_access_fpreg,
+  &_UPT_resume,
+  &_UPT_get_proc_name
+};
+
+unw_addr_space_t create_addr_space()
+{
+  return unw_create_addr_space(&accessors, BYTE_ORDER);
+}
+
+void* create_context(unw_addr_space_t /*as*/, pid_t pid)
+{
+  return _UPT_create(pid);
+}
 
 }
+}