#include "getMappedArea.h" #include #if defined(OS_LINUX) #include #include #include #include namespace DB { namespace ErrorCodes { extern const int LOGICAL_ERROR; } namespace { uintptr_t readAddressHex(DB::ReadBuffer & in) { uintptr_t res = 0; while (!in.eof()) { if (isHexDigit(*in.position())) { res *= 16; res += unhex(*in.position()); ++in.position(); } else break; } return res; } } std::pair getMappedArea(void * ptr) { using namespace DB; uintptr_t uintptr = reinterpret_cast(ptr); ReadBufferFromFile in("/proc/self/maps"); while (!in.eof()) { uintptr_t begin = readAddressHex(in); assertChar('-', in); uintptr_t end = readAddressHex(in); skipToNextLineOrEOF(in); if (begin <= uintptr && uintptr < end) return {reinterpret_cast(begin), end - begin}; } throw Exception("Cannot find mapped area for pointer", ErrorCodes::LOGICAL_ERROR); } } #else namespace DB { namespace ErrorCodes { extern const int NOT_IMPLEMENTED; } std::pair getMappedArea(void *) { throw Exception("The function getMappedArea is implemented only for Linux", ErrorCodes::NOT_IMPLEMENTED); } } #endif