2022-01-25 16:47:43 +00:00
|
|
|
#pragma once
|
2022-06-10 08:22:31 +00:00
|
|
|
#if defined(__ELF__) && !defined(OS_FREEBSD)
|
2022-01-25 16:20:19 +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/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <IO/WriteBufferFromArena.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Access/Common/AccessFlags.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ResultT, Dwarf::LocationInfoMode locationInfoMode>
|
|
|
|
class FunctionAddressToLineBase : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 1)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} needs exactly one argument; passed {}.",
|
|
|
|
getName(), arguments.size());
|
2022-01-25 16:20:19 +00:00
|
|
|
|
|
|
|
const auto & type = arguments[0].type;
|
|
|
|
|
|
|
|
if (!WhichDataType(type.get()).isUInt64())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "The only argument for function {} must be UInt64. "
|
|
|
|
"Found {} instead.", getName(), type->getName());
|
2022-01-25 16:20:19 +00:00
|
|
|
|
|
|
|
return getDataType();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
|
|
|
{
|
|
|
|
const ColumnPtr & column = arguments[0].column;
|
|
|
|
const ColumnUInt64 * column_concrete = checkAndGetColumn<ColumnUInt64>(column.get());
|
|
|
|
|
|
|
|
if (!column_concrete)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}",
|
|
|
|
column->getName(), getName());
|
2022-01-25 16:20:19 +00:00
|
|
|
|
|
|
|
const typename ColumnVector<UInt64>::Container & data = column_concrete->getData();
|
|
|
|
return getResultColumn(data, input_rows_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual DataTypePtr getDataType() const = 0;
|
|
|
|
virtual ColumnPtr getResultColumn(const typename ColumnVector<UInt64>::Container & data, size_t input_rows_count) const = 0;
|
|
|
|
virtual void
|
|
|
|
setResult(ResultT & result, const Dwarf::LocationInfo & location, const std::vector<Dwarf::SymbolizedFrame> & frames) const = 0;
|
|
|
|
|
|
|
|
struct Cache
|
|
|
|
{
|
|
|
|
std::mutex mutex;
|
|
|
|
Arena arena;
|
|
|
|
using Map = HashMap<uintptr_t, ResultT>;
|
|
|
|
Map map;
|
|
|
|
std::unordered_map<std::string, Dwarf> dwarfs;
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable Cache cache;
|
|
|
|
|
|
|
|
ResultT impl(uintptr_t addr) const
|
|
|
|
{
|
2023-07-09 07:20:03 +00:00
|
|
|
const SymbolIndex & symbol_index = SymbolIndex::instance();
|
2022-01-25 16:20:19 +00:00
|
|
|
|
|
|
|
if (const auto * object = symbol_index.findObject(reinterpret_cast<const void *>(addr)))
|
|
|
|
{
|
|
|
|
auto dwarf_it = cache.dwarfs.try_emplace(object->name, object->elf).first;
|
|
|
|
if (!std::filesystem::exists(object->name))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Dwarf::LocationInfo location;
|
|
|
|
std::vector<Dwarf::SymbolizedFrame> frames; // NOTE: not used in FAST mode.
|
|
|
|
ResultT result;
|
|
|
|
if (dwarf_it->second.findAddress(addr - uintptr_t(object->address_begin), location, locationInfoMode, frames))
|
|
|
|
{
|
|
|
|
setResult(result, location, frames);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return {object->name};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultT implCached(uintptr_t addr) const
|
|
|
|
{
|
|
|
|
typename Cache::Map::LookupResult it;
|
|
|
|
bool inserted;
|
|
|
|
std::lock_guard lock(cache.mutex);
|
|
|
|
cache.map.emplace(addr, it, inserted);
|
|
|
|
if (inserted)
|
|
|
|
it->getMapped() = impl(addr);
|
|
|
|
return it->getMapped();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|