-/* Copyright (c) 2008-2019. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2008-2021. 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 <array>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "memory_map.hpp"
+// 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 {
namespace xbt {
/*
* 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) {
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;
/* to be returned. */
std::string path = std::string("/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");
/* 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<char*, 6> lfields;
lfields[0] = strtok_r(line, " ", &saveptr);
int i;
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++){
/* 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])
/* 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));
}