Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add powerset_iterator to simgrid::xbt
[simgrid.git] / src / xbt / memory_map.cpp
index c81bcfb..2d54b6a 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2008-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "memory_map.hpp"
 
-namespace simgrid {
-namespace xbt {
+// abort with a message if `expr' is false
+#define CHECK(expr)                                                                                                    \
+  if (not(expr)) {                                                                                                     \
+    fprintf(stderr, "CHECK FAILED: %s:%d: %s\n", __FILE__, __LINE__, #expr);                                           \
+    abort();                                                                                                           \
+  } else                                                                                                               \
+    ((void)0)
+
+#define DEBUG_PRINT(...)                                                                                               \
+  if (false) {                                                                                                         \
+    fprintf(stderr, __VA_ARGS__);                                                                                      \
+  } else                                                                                                               \
+    ((void)0)
+
+namespace simgrid::xbt {
 
 /**
  * \todo This function contains many cases that do not allow for a
@@ -92,21 +105,51 @@ std::vector<VmMap> get_memory_map(pid_t pid)
     mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT;
 #endif
 
-    kr =
-      mach_vm_region(
-          map,
-          &address,
-          &size,
-          flavor,
-          (vm_region_info_t)&info,
-          &info_count,
-          &object);
+    kr = mach_vm_region(map, &address, &size, flavor, (vm_region_info_t)&info, &info_count, &object);
     if (kr == KERN_INVALID_ADDRESS) {
       break;
-    }
-    else if (kr != KERN_SUCCESS) {
+
+    } else if (kr != KERN_SUCCESS) {
+      const char* name = nullptr;
+      switch (kr) { // https://github.com/apple/darwin-xnu/blob/main/bsd/kern/stackshot.c#L42
+        case KERN_SUCCESS:
+          name = "kr=KERN_SUCCESS";
+          break;
+        case KERN_RESOURCE_SHORTAGE:
+          name = "kr=KERN_RESOURCE_SHORTAGE (ENOMEM)";
+          break;
+        case KERN_INSUFFICIENT_BUFFER_SIZE:
+          name = "kr=KERN_INSUFFICIENT_BUFFER_SIZE (ENOSPC)";
+          break;
+        case KERN_NO_SPACE:
+          name = "kr=KERN_NO_SPACE (ENOSPC)";
+          break;
+        case KERN_NO_ACCESS:
+          name = "kr=KERN_NO_ACCESS (EPERM)";
+          break;
+        case KERN_MEMORY_PRESENT:
+          name = "kr=KERN_MEMORY_PRESENT (EEXIST)";
+          break;
+        case KERN_NOT_SUPPORTED:
+          name = "kr=KERN_NOT_SUPPORTED (ENOTSUP)";
+          break;
+        case KERN_NOT_IN_SET:
+          name = "kr=KERN_NOT_IN_SET (ENOENT)";
+          break;
+        case KERN_ABORTED:
+          name = "kr=KERN_ABORTED (EINTR)";
+          break;
+        case KERN_FAILURE:
+          name = "kr=KERN_FAILURE (EBUSY)";
+          break;
+        case KERN_OPERATION_TIMED_OUT:
+          name = "kr=KERN_OPERATION_TIMED_OUT (ETIMEDOUT)";
+          break;
+        default:
+          name = "kr=default case (EINVAL)";
+      }
       std::perror("mach_vm_region failed");
-      std::fprintf(stderr, "Cannot request authorization for kernel information access\n");
+      std::fprintf(stderr, "Cannot request authorization for kernel information access (kr=%d ; %s)\n", (int)kr, name);
       abort();
     }
 
@@ -147,11 +190,9 @@ std::vector<VmMap> get_memory_map(pid_t pid)
     if (dladdr(reinterpret_cast<void*>(address), &dlinfo))
       memreg.pathname = dlinfo.dli_fname;
 
-#if 0 /* debug */
-    std::fprintf(stderr, "Region: %016" PRIx64 "-%016" PRIx64 " | %c%c%c | %s\n", memreg.start_addr, memreg.end_addr,
-              (memreg.prot & PROT_READ) ? 'r' : '-', (memreg.prot & PROT_WRITE) ? 'w' : '-',
-              (memreg.prot & PROT_EXEC) ? 'x' : '-', memreg.pathname.c_str());
-#endif
+    DEBUG_PRINT("Region: %016" PRIx64 "-%016" PRIx64 " | %c%c%c | %s\n", memreg.start_addr, memreg.end_addr,
+                (memreg.prot & PROT_READ) ? 'r' : '-', (memreg.prot & PROT_WRITE) ? 'w' : '-',
+                (memreg.prot & PROT_EXEC) ? 'x' : '-', memreg.pathname.c_str());
 
     ret.push_back(std::move(memreg));
     address += size;
@@ -161,7 +202,7 @@ std::vector<VmMap> get_memory_map(pid_t pid)
 #elif defined __linux__
   /* Open the actual process's proc maps file and create the memory_map_t */
   /* to be returned. */
-  std::string path = std::string("/proc/") + std::to_string(pid) + "/maps";
+  std::string path = "/proc/" + std::to_string(pid) + "/maps";
   std::ifstream fp;
   fp.rdbuf()->pubsetbuf(nullptr, 0);
   fp.open(path);
@@ -209,21 +250,17 @@ std::vector<VmMap> get_memory_map(pid_t pid)
     char *endptr;
     memreg.start_addr = std::strtoull(tok, &endptr, 16);
     /* Make sure that the entire string was an hex number */
-    if (*endptr != '\0')
-      abort();
+    CHECK(*endptr == '\0');
 
     tok = strtok_r(nullptr, "-", &saveptr);
-    if (tok == nullptr)
-      abort();
+    CHECK(tok != nullptr);
 
     memreg.end_addr = std::strtoull(tok, &endptr, 16);
     /* Make sure that the entire string was an hex number */
-    if (*endptr != '\0')
-      abort();
+    CHECK(*endptr == '\0');
 
     /* Get the permissions flags */
-    if (std::strlen(lfields[1]) < 4)
-      abort();
+    CHECK(std::strlen(lfields[1]) >= 4);
 
     memreg.prot = 0;
     for (i = 0; i < 3; i++){
@@ -259,32 +296,26 @@ std::vector<VmMap> get_memory_map(pid_t pid)
     /* Get the offset value */
     memreg.offset = std::strtoull(lfields[2], &endptr, 16);
     /* Make sure that the entire string was an hex number */
-    if (*endptr != '\0')
-      abort();
+    CHECK(*endptr == '\0');
 
     /* Get the device major:minor bytes */
     tok = strtok_r(lfields[3], ":", &saveptr);
-    if (tok == nullptr)
-      abort();
+    CHECK(tok != nullptr);
 
     memreg.dev_major = (char) strtoul(tok, &endptr, 16);
     /* Make sure that the entire string was an hex number */
-    if (*endptr != '\0')
-      abort();
+    CHECK(*endptr == '\0');
 
     tok = strtok_r(nullptr, ":", &saveptr);
-    if (tok == nullptr)
-      abort();
+    CHECK(tok != nullptr);
 
     memreg.dev_minor = (char) std::strtoul(tok, &endptr, 16);
     /* Make sure that the entire string was an hex number */
-    if (*endptr != '\0')
-      abort();
+    CHECK(*endptr == '\0');
 
     /* Get the inode number and make sure that the entire string was a long int */
     memreg.inode = strtoul(lfields[4], &endptr, 10);
-    if (*endptr != '\0')
-      abort();
+    CHECK(*endptr == '\0');
 
     /* And finally get the pathname */
     if (lfields[5])
@@ -292,7 +323,7 @@ std::vector<VmMap> get_memory_map(pid_t pid)
 
     /* Create space for a new map region in the region's array and copy the */
     /* parsed stuff from the temporal memreg variable */
-    // std::fprintf(stderr, "Found region for %s\n", not memreg.pathname.empty() ? memreg.pathname.c_str() : "(null)");
+    DEBUG_PRINT("Found region for \"%s\"\n", memreg.pathname.c_str());
 
     ret.push_back(std::move(memreg));
   }
@@ -387,5 +418,4 @@ std::vector<VmMap> get_memory_map(pid_t pid)
   return ret;
 }
 
-}
-}
+} // namespace simgrid::xbt