ClickHouse/src/Common/getMappedArea.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
1.4 KiB
C++
Raw Normal View History

2020-09-17 18:15:19 +00:00
#include "getMappedArea.h"
2020-09-18 20:19:41 +00:00
#include <Common/Exception.h>
2020-09-17 18:15:19 +00:00
#if defined(OS_LINUX)
2020-09-17 12:53:52 +00:00
#include <Common/StringUtils/StringUtils.h>
#include <Common/hex.h>
#include <IO/ReadBufferFromFile.h>
#include <IO/ReadHelpers.h>
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<void *, size_t> getMappedArea(void * ptr)
{
using namespace DB;
uintptr_t uintptr = reinterpret_cast<uintptr_t>(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<void *>(begin), end - begin};
}
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot find mapped area for pointer");
2020-09-17 12:53:52 +00:00
}
}
#else
namespace DB
{
2020-09-18 18:54:54 +00:00
namespace ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}
std::pair<void *, size_t> getMappedArea(void *)
2020-09-17 12:53:52 +00:00
{
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "The function getMappedArea is implemented only for Linux");
2020-09-17 12:53:52 +00:00
}
}
#endif