ClickHouse/dbms/Common/tests/symbol_index.cpp

68 lines
1.9 KiB
C++
Raw Normal View History

2019-07-29 01:08:52 +00:00
#include <Common/SymbolIndex.h>
2019-07-29 18:38:04 +00:00
#include <Common/Elf.h>
#include <Common/Dwarf.h>
#include <Core/Defines.h>
2019-07-29 01:08:52 +00:00
#include <common/demangle.h>
#include <iostream>
#include <dlfcn.h>
2019-12-15 06:34:43 +00:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
static NO_INLINE const void * getAddress()
{
return __builtin_return_address(0);
}
2019-12-15 06:34:43 +00:00
#pragma GCC diagnostic pop
2019-07-29 01:08:52 +00:00
int main(int argc, char ** argv)
{
#if defined(__ELF__) && !defined(__FreeBSD__)
2019-09-20 17:34:59 +00:00
using namespace DB;
2019-07-29 01:08:52 +00:00
if (argc < 2)
{
std::cerr << "Usage: ./symbol_index address\n";
return 1;
}
2019-07-29 22:26:44 +00:00
const SymbolIndex & symbol_index = SymbolIndex::instance();
2019-07-29 01:08:52 +00:00
2019-07-29 22:26:44 +00:00
for (const auto & elem : symbol_index.symbols())
2019-07-29 18:38:04 +00:00
std::cout << elem.name << ": " << elem.address_begin << " ... " << elem.address_end << "\n";
2019-07-30 16:18:06 +00:00
std::cout << "\n";
2019-07-29 01:08:52 +00:00
const void * address = reinterpret_cast<void*>(std::stoull(argv[1], nullptr, 16));
2019-07-29 18:38:04 +00:00
auto symbol = symbol_index.findSymbol(address);
2019-07-29 01:08:52 +00:00
if (symbol)
std::cerr << symbol->name << ": " << symbol->address_begin << " ... " << symbol->address_end << "\n";
else
2019-07-29 18:38:04 +00:00
std::cerr << "SymbolIndex: Not found\n";
2019-07-29 01:08:52 +00:00
Dl_info info;
if (dladdr(address, &info) && info.dli_sname)
std::cerr << demangle(info.dli_sname) << ": " << info.dli_saddr << "\n";
else
2019-07-29 18:38:04 +00:00
std::cerr << "dladdr: Not found\n";
auto object = symbol_index.findObject(getAddress());
Dwarf dwarf(*object->elf);
2019-07-29 18:38:04 +00:00
Dwarf::LocationInfo location;
2019-12-24 20:07:44 +00:00
if (dwarf.findAddress(uintptr_t(address) - uintptr_t(info.dli_fbase), location, Dwarf::LocationInfoMode::FAST))
2019-07-29 18:38:04 +00:00
std::cerr << location.file.toString() << ":" << location.line << "\n";
else
std::cerr << "Dwarf: Not found\n";
2019-07-29 01:08:52 +00:00
2019-07-30 16:18:06 +00:00
std::cerr << "\n";
2019-07-29 22:26:44 +00:00
std::cerr << StackTrace().toString() << "\n";
2019-08-21 00:48:34 +00:00
#else
(void)argc;
(void)argv;
std::cerr << "This test does not make sense for non-ELF objects.\n";
#endif
2019-07-29 22:26:44 +00:00
2019-07-29 01:08:52 +00:00
return 0;
}