ClickHouse/dbms/src/Common/tests/symbol_index.cpp

51 lines
1.3 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>
2019-07-29 01:08:52 +00:00
#include <common/demangle.h>
#include <iostream>
#include <dlfcn.h>
void f() {}
using namespace DB;
int main(int argc, char ** argv)
{
if (argc < 2)
{
std::cerr << "Usage: ./symbol_index address\n";
return 1;
}
SymbolIndex symbol_index;
2019-07-29 18:38:04 +00:00
for (const auto & elem : symbol_index.objects())
std::cout << elem.name << ": " << elem.address_begin << " ... " << elem.address_end << "\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";
Elf elf("/proc/self/exe");
Dwarf dwarf(elf);
Dwarf::LocationInfo location;
if (dwarf.findAddress(uintptr_t(address), location, Dwarf::LocationInfoMode::FULL))
std::cerr << location.file.toString() << ":" << location.line << "\n";
else
std::cerr << "Dwarf: Not found\n";
2019-07-29 01:08:52 +00:00
return 0;
}