2019-10-05 19:25:31 +00:00
|
|
|
#if defined(__ELF__) && !defined(__FreeBSD__)
|
2019-08-21 00:48:34 +00:00
|
|
|
|
2019-07-29 23:37:50 +00:00
|
|
|
#include <Common/Dwarf.h>
|
|
|
|
#include <Common/SymbolIndex.h>
|
|
|
|
#include <Common/HashTable/HashMap.h>
|
|
|
|
#include <Common/Arena.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2019-07-29 23:37:50 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <IO/WriteBufferFromArena.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2020-01-26 09:49:53 +00:00
|
|
|
#include <Access/AccessFlags.h>
|
2019-07-29 23:54:49 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2019-07-29 23:37:50 +00:00
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-07-29 23:37:50 +00:00
|
|
|
class FunctionAddressToLine : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "addressToLine";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2019-07-29 23:37:50 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
context->checkAccess(AccessType::addressToLine);
|
2019-07-29 23:37:50 +00:00
|
|
|
return std::make_shared<FunctionAddressToLine>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2019-07-29 23:37:50 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 1)
|
|
|
|
throw Exception("Function " + getName() + " needs exactly one argument; passed "
|
|
|
|
+ toString(arguments.size()) + ".", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
const auto & type = arguments[0].type;
|
|
|
|
|
|
|
|
if (!WhichDataType(type.get()).isUInt64())
|
|
|
|
throw Exception("The only argument for function " + getName() + " must be UInt64. Found "
|
|
|
|
+ type->getName() + " instead.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2019-07-29 23:37:50 +00:00
|
|
|
{
|
2020-10-17 15:22:42 +00:00
|
|
|
const ColumnPtr & column = arguments[0].column;
|
2019-07-29 23:37:50 +00:00
|
|
|
const ColumnUInt64 * column_concrete = checkAndGetColumn<ColumnUInt64>(column.get());
|
|
|
|
|
|
|
|
if (!column_concrete)
|
|
|
|
throw Exception("Illegal column " + column->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
const typename ColumnVector<UInt64>::Container & data = column_concrete->getData();
|
|
|
|
auto result_column = ColumnString::create();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
|
|
|
StringRef res_str = implCached(data[i]);
|
|
|
|
result_column->insertData(res_str.data, res_str.size);
|
|
|
|
}
|
|
|
|
|
2020-10-17 15:22:42 +00:00
|
|
|
return result_column;
|
2019-07-29 23:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-21 13:58:07 +00:00
|
|
|
struct Cache
|
|
|
|
{
|
|
|
|
std::mutex mutex;
|
|
|
|
Arena arena;
|
|
|
|
using Map = HashMap<uintptr_t, StringRef>;
|
|
|
|
Map map;
|
|
|
|
std::unordered_map<std::string, Dwarf> dwarfs;
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable Cache cache;
|
2019-07-29 23:37:50 +00:00
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
StringRef impl(uintptr_t addr) const
|
2019-07-29 23:37:50 +00:00
|
|
|
{
|
2020-11-30 14:30:55 +00:00
|
|
|
auto symbol_index_ptr = SymbolIndex::instance();
|
|
|
|
const SymbolIndex & symbol_index = *symbol_index_ptr;
|
2019-07-29 23:37:50 +00:00
|
|
|
|
2020-04-22 08:31:10 +00:00
|
|
|
if (const auto * object = symbol_index.findObject(reinterpret_cast<const void *>(addr)))
|
2019-07-29 23:37:50 +00:00
|
|
|
{
|
2021-01-20 13:03:25 +00:00
|
|
|
auto dwarf_it = cache.dwarfs.try_emplace(object->name, object->elf).first;
|
2019-07-29 23:37:50 +00:00
|
|
|
if (!std::filesystem::exists(object->name))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Dwarf::LocationInfo location;
|
2021-01-19 12:34:27 +00:00
|
|
|
std::vector<Dwarf::SymbolizedFrame> frames; // NOTE: not used in FAST mode.
|
|
|
|
if (dwarf_it->second.findAddress(addr - uintptr_t(object->address_begin), location, Dwarf::LocationInfoMode::FAST, frames))
|
2019-07-29 23:37:50 +00:00
|
|
|
{
|
|
|
|
const char * arena_begin = nullptr;
|
2020-07-21 13:58:07 +00:00
|
|
|
WriteBufferFromArena out(cache.arena, arena_begin);
|
2019-07-29 23:37:50 +00:00
|
|
|
|
|
|
|
writeString(location.file.toString(), out);
|
|
|
|
writeChar(':', out);
|
|
|
|
writeIntText(location.line, out);
|
|
|
|
|
2020-05-22 00:01:35 +00:00
|
|
|
return out.finish();
|
2019-07-29 23:37:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return object->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
StringRef implCached(uintptr_t addr) const
|
2019-07-29 23:37:50 +00:00
|
|
|
{
|
2020-07-21 13:58:07 +00:00
|
|
|
Cache::Map::LookupResult it;
|
2019-07-29 23:37:50 +00:00
|
|
|
bool inserted;
|
2020-07-21 13:58:07 +00:00
|
|
|
std::lock_guard lock(cache.mutex);
|
|
|
|
cache.map.emplace(addr, it, inserted);
|
2019-07-29 23:37:50 +00:00
|
|
|
if (inserted)
|
2019-10-29 15:16:51 +00:00
|
|
|
it->getMapped() = impl(addr);
|
|
|
|
return it->getMapped();
|
2019-07-29 23:37:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 23:37:50 +00:00
|
|
|
void registerFunctionAddressToLine(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionAddressToLine>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-08-21 00:48:34 +00:00
|
|
|
|
|
|
|
#endif
|