X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/39c935d6d5ee86d153f6f7e6a10d723ae7c57f6f..b1a51bf03db0f4d86f3e89016affd30f091bd8db:/src/xbt/memory_map.cpp diff --git a/src/xbt/memory_map.cpp b/src/xbt/memory_map.cpp index c81bcfb0a1..fccfac8a5a 100644 --- a/src/xbt/memory_map.cpp +++ b/src/xbt/memory_map.cpp @@ -51,6 +51,20 @@ #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 { @@ -147,11 +161,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; @@ -209,21 +221,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++){ @@ -259,32 +267,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]) @@ -292,7 +294,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)); }