X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/9104957deccc59e0e804215d5db498fabfd40d29..ab5814bd4ea6117ecf9ac2df328333e89eb45f5f:/src/xbt/memory_map.cpp diff --git a/src/xbt/memory_map.cpp b/src/xbt/memory_map.cpp index 248d12d2b9..2d54b6aee8 100644 --- a/src/xbt/memory_map.cpp +++ b/src/xbt/memory_map.cpp @@ -1,8 +1,9 @@ -/* Copyright (c) 2008-2020. 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 #include #include #include @@ -50,8 +51,21 @@ #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 @@ -74,7 +88,7 @@ std::vector get_memory_map(pid_t pid) /* * Darwin do not give us the number of mappings, so we read entries until - * we get an KERN_INVALID_ADDRESS return. + * we get a KERN_INVALID_ADDRESS return. */ mach_vm_address_t address = VM_MIN_ADDRESS; while (true) { @@ -91,21 +105,51 @@ std::vector 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(); } @@ -146,11 +190,9 @@ std::vector get_memory_map(pid_t pid) if (dladdr(reinterpret_cast(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; @@ -160,9 +202,9 @@ std::vector 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(0, 0); + fp.rdbuf()->pubsetbuf(nullptr, 0); fp.open(path); if (not fp) { std::perror("open failed"); @@ -181,7 +223,7 @@ std::vector get_memory_map(pid_t pid) /* Tokenize the line using spaces as delimiters and store each token in lfields array. We expect 5 tokens for 6 fields */ char* saveptr = nullptr; // for strtok_r() - char* lfields[6]; + std::array lfields; lfields[0] = strtok_r(line, " ", &saveptr); int i; @@ -208,21 +250,17 @@ std::vector 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++){ @@ -258,32 +296,26 @@ std::vector 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]) @@ -291,7 +323,7 @@ std::vector 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)); } @@ -386,5 +418,4 @@ std::vector get_memory_map(pid_t pid) return ret; } -} -} +} // namespace simgrid::xbt