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
|
|
|
|
2022-06-10 08:22:31 +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};
|
|
|
|
}
|
|
|
|
|
2023-01-23 21:13:58 +00:00
|
|
|
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
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "The function getMappedArea is implemented only for Linux");
|
2020-09-17 12:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|