2019-10-05 19:25:31 +00:00
|
|
|
#if defined(__ELF__) && !defined(__FreeBSD__)
|
2019-08-21 00:48:34 +00:00
|
|
|
|
2019-07-29 01:08:52 +00:00
|
|
|
#include <Common/SymbolIndex.h>
|
2020-06-20 09:07:05 +00:00
|
|
|
#include <Common/hex.h>
|
2019-07-29 01:08:52 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include <link.h>
|
|
|
|
|
2019-07-29 12:48:19 +00:00
|
|
|
//#include <iostream>
|
|
|
|
#include <filesystem>
|
2019-07-29 01:08:52 +00:00
|
|
|
|
2019-08-22 02:31:49 +00:00
|
|
|
/**
|
|
|
|
|
|
|
|
ELF object can contain three different places with symbol names and addresses:
|
|
|
|
|
|
|
|
1. Symbol table in section headers. It is used for static linking and usually left in executable.
|
|
|
|
It is not loaded in memory and they are not necessary for program to run.
|
|
|
|
It does not relate to debug info and present regardless to -g flag.
|
|
|
|
You can use strip to get rid of this symbol table.
|
|
|
|
If you have this symbol table in your binary, you can manually read it and get symbol names, even for symbols from anonymous namespaces.
|
|
|
|
|
|
|
|
2. Hashes in program headers such as DT_HASH and DT_GNU_HASH.
|
|
|
|
It is necessary for dynamic object (.so libraries and any dynamically linked executable that depend on .so libraries)
|
|
|
|
because it is used for dynamic linking that happens in runtime and performed by dynamic loader.
|
|
|
|
Only exported symbols will be presented in that hash tables. Symbols from anonymous namespaces are not.
|
|
|
|
This part of executable binary is loaded in memory and accessible via 'dl_iterate_phdr', 'dladdr' and 'backtrace_symbols' functions from libc.
|
|
|
|
ClickHouse versions prior to 19.13 has used just these symbol names to symbolize stack traces
|
|
|
|
and stack traces may be incomplete due to lack of symbols with internal linkage.
|
|
|
|
But because ClickHouse is linked with most of the symbols exported (-rdynamic flag) it can still provide good enough stack traces.
|
|
|
|
|
|
|
|
3. DWARF debug info. It contains the most detailed information about symbols and everything else.
|
|
|
|
It allows to get source file names and line numbers from addresses. Only available if you use -g option for compiler.
|
|
|
|
It is also used by default for ClickHouse builds, but because of its weight (about two gigabytes)
|
2020-08-08 00:47:03 +00:00
|
|
|
it is split to separate binary and provided in clickhouse-common-static-dbg package.
|
2019-08-22 02:31:49 +00:00
|
|
|
This separate binary is placed in /usr/lib/debug/usr/bin/clickhouse and is loaded automatically by tools like gdb, addr2line.
|
2020-08-08 00:47:03 +00:00
|
|
|
When you build ClickHouse by yourself, debug info is not split and present in a single huge binary.
|
2019-08-22 02:31:49 +00:00
|
|
|
|
|
|
|
What ClickHouse is using to provide good stack traces?
|
|
|
|
|
|
|
|
In versions prior to 19.13, only "program headers" (2) was used.
|
|
|
|
|
|
|
|
In version 19.13, ClickHouse will read program headers (2) and cache them,
|
|
|
|
also it will read itself as ELF binary and extract symbol tables from section headers (1)
|
|
|
|
to also symbolize functions that are not exported for dynamic linking.
|
|
|
|
And finally, it will read DWARF info (3) if available to display file names and line numbers.
|
|
|
|
|
|
|
|
What detail can you obtain depending on your binary?
|
|
|
|
|
|
|
|
If you have debug info (you build ClickHouse by yourself or install clickhouse-common-static-dbg package), you will get source file names and line numbers.
|
|
|
|
Otherwise you will get only symbol names. If your binary contains symbol table in section headers (the default, unless stripped), you will get all symbol names.
|
|
|
|
Otherwise you will get only exported symbols from program headers.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-06-20 19:05:03 +00:00
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma clang diagnostic ignored "-Wreserved-id-macro"
|
|
|
|
# pragma clang diagnostic ignored "-Wunused-macros"
|
|
|
|
#endif
|
|
|
|
|
2020-09-18 23:04:36 +00:00
|
|
|
#define __msan_unpoison_string(X) // NOLINT
|
2021-02-23 04:34:58 +00:00
|
|
|
#if defined(ch_has_feature)
|
|
|
|
# if ch_has_feature(memory_sanitizer)
|
|
|
|
# undef __msan_unpoison_string
|
|
|
|
# include <sanitizer/msan_interface.h>
|
|
|
|
# endif
|
2020-06-20 19:05:03 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-29 01:08:52 +00:00
|
|
|
|
2019-07-29 22:26:44 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-07-29 01:08:52 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-07-29 12:48:19 +00:00
|
|
|
/// Notes: "PHDR" is "Program Headers".
|
2019-07-30 16:12:53 +00:00
|
|
|
/// To look at program headers, run:
|
|
|
|
/// readelf -l ./clickhouse-server
|
|
|
|
/// To look at section headers, run:
|
|
|
|
/// readelf -S ./clickhouse-server
|
2019-07-29 12:48:19 +00:00
|
|
|
/// Also look at: https://wiki.osdev.org/ELF
|
|
|
|
/// Also look at: man elf
|
|
|
|
/// http://www.linker-aliens.org/blogs/ali/entry/inside_elf_symbol_tables/
|
|
|
|
/// https://stackoverflow.com/questions/32088140/multiple-string-tables-in-elf-object
|
|
|
|
|
|
|
|
|
2019-07-29 01:08:52 +00:00
|
|
|
/// Based on the code of musl-libc and the answer of Kanalpiroge on
|
|
|
|
/// https://stackoverflow.com/questions/15779185/list-all-the-functions-symbols-on-the-fly-in-c-code-on-a-linux-architecture
|
2019-07-30 16:12:53 +00:00
|
|
|
/// It does not extract all the symbols (but only public - exported and used for dynamic linking),
|
|
|
|
/// but will work if we cannot find or parse ELF files.
|
2019-07-29 18:38:04 +00:00
|
|
|
void collectSymbolsFromProgramHeaders(dl_phdr_info * info,
|
2019-07-29 22:26:44 +00:00
|
|
|
std::vector<SymbolIndex::Symbol> & symbols)
|
2019-07-29 01:08:52 +00:00
|
|
|
{
|
|
|
|
/* Iterate over all headers of the current shared lib
|
2019-07-30 16:12:53 +00:00
|
|
|
* (first call is for the executable itself)
|
|
|
|
*/
|
2019-07-29 01:08:52 +00:00
|
|
|
for (size_t header_index = 0; header_index < info->dlpi_phnum; ++header_index)
|
|
|
|
{
|
|
|
|
/* Further processing is only needed if the dynamic section is reached
|
|
|
|
*/
|
|
|
|
if (info->dlpi_phdr[header_index].p_type != PT_DYNAMIC)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Get a pointer to the first entry of the dynamic section.
|
|
|
|
* It's address is the shared lib's address + the virtual address
|
|
|
|
*/
|
2019-07-29 12:48:19 +00:00
|
|
|
const ElfW(Dyn) * dyn_begin = reinterpret_cast<const ElfW(Dyn) *>(info->dlpi_addr + info->dlpi_phdr[header_index].p_vaddr);
|
2019-07-29 01:08:52 +00:00
|
|
|
|
|
|
|
/// For unknown reason, addresses are sometimes relative sometimes absolute.
|
|
|
|
auto correct_address = [](ElfW(Addr) base, ElfW(Addr) ptr)
|
|
|
|
{
|
|
|
|
return ptr > base ? ptr : base + ptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Iterate over all entries of the dynamic section until the
|
|
|
|
* end of the symbol table is reached. This is indicated by
|
|
|
|
* an entry with d_tag == DT_NULL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
size_t sym_cnt = 0;
|
2020-04-22 05:39:31 +00:00
|
|
|
for (const auto * it = dyn_begin; it->d_tag != DT_NULL; ++it)
|
2019-07-29 01:08:52 +00:00
|
|
|
{
|
2019-08-22 04:34:06 +00:00
|
|
|
// TODO: this branch leads to invalid address of the hash table. Need further investigation.
|
|
|
|
// if (it->d_tag == DT_HASH)
|
|
|
|
// {
|
|
|
|
// const ElfW(Word) * hash = reinterpret_cast<const ElfW(Word) *>(correct_address(info->dlpi_addr, it->d_un.d_ptr));
|
|
|
|
// sym_cnt = hash[1];
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
if (it->d_tag == DT_GNU_HASH)
|
2019-07-29 01:08:52 +00:00
|
|
|
{
|
|
|
|
/// This code based on Musl-libc.
|
|
|
|
|
|
|
|
const uint32_t * buckets = nullptr;
|
|
|
|
const uint32_t * hashval = nullptr;
|
|
|
|
|
2019-07-29 12:48:19 +00:00
|
|
|
const ElfW(Word) * hash = reinterpret_cast<const ElfW(Word) *>(correct_address(info->dlpi_addr, it->d_un.d_ptr));
|
2019-07-29 01:08:52 +00:00
|
|
|
|
|
|
|
buckets = hash + 4 + (hash[2] * sizeof(size_t) / 4);
|
|
|
|
|
|
|
|
for (ElfW(Word) i = 0; i < hash[0]; ++i)
|
|
|
|
if (buckets[i] > sym_cnt)
|
|
|
|
sym_cnt = buckets[i];
|
|
|
|
|
|
|
|
if (sym_cnt)
|
|
|
|
{
|
|
|
|
sym_cnt -= hash[1];
|
|
|
|
hashval = buckets + hash[0] + sym_cnt;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
++sym_cnt;
|
|
|
|
}
|
|
|
|
while (!(*hashval++ & 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sym_cnt)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char * strtab = nullptr;
|
2020-04-22 05:39:31 +00:00
|
|
|
for (const auto * it = dyn_begin; it->d_tag != DT_NULL; ++it)
|
2019-07-29 01:08:52 +00:00
|
|
|
{
|
|
|
|
if (it->d_tag == DT_STRTAB)
|
|
|
|
{
|
|
|
|
strtab = reinterpret_cast<const char *>(correct_address(info->dlpi_addr, it->d_un.d_ptr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strtab)
|
|
|
|
continue;
|
|
|
|
|
2020-04-22 05:39:31 +00:00
|
|
|
for (const auto * it = dyn_begin; it->d_tag != DT_NULL; ++it)
|
2019-07-29 01:08:52 +00:00
|
|
|
{
|
|
|
|
if (it->d_tag == DT_SYMTAB)
|
|
|
|
{
|
|
|
|
/* Get the pointer to the first entry of the symbol table */
|
2019-07-29 12:48:19 +00:00
|
|
|
const ElfW(Sym) * elf_sym = reinterpret_cast<const ElfW(Sym) *>(correct_address(info->dlpi_addr, it->d_un.d_ptr));
|
2019-07-29 01:08:52 +00:00
|
|
|
|
|
|
|
/* Iterate over the symbol table */
|
2020-03-18 13:11:38 +00:00
|
|
|
for (ElfW(Word) sym_index = 0; sym_index < ElfW(Word)(sym_cnt); ++sym_index)
|
2019-07-29 01:08:52 +00:00
|
|
|
{
|
2019-07-29 12:48:19 +00:00
|
|
|
/// We are not interested in empty symbols.
|
|
|
|
if (!elf_sym[sym_index].st_size)
|
|
|
|
continue;
|
|
|
|
|
2019-07-29 01:08:52 +00:00
|
|
|
/* Get the name of the sym_index-th symbol.
|
|
|
|
* This is located at the address of st_name relative to the beginning of the string table.
|
|
|
|
*/
|
|
|
|
const char * sym_name = &strtab[elf_sym[sym_index].st_name];
|
|
|
|
|
|
|
|
if (!sym_name)
|
|
|
|
continue;
|
|
|
|
|
2019-07-29 22:26:44 +00:00
|
|
|
SymbolIndex::Symbol symbol;
|
2019-07-29 01:08:52 +00:00
|
|
|
symbol.address_begin = reinterpret_cast<const void *>(info->dlpi_addr + elf_sym[sym_index].st_value);
|
|
|
|
symbol.address_end = reinterpret_cast<const void *>(info->dlpi_addr + elf_sym[sym_index].st_value + elf_sym[sym_index].st_size);
|
2019-07-29 22:26:44 +00:00
|
|
|
symbol.name = sym_name;
|
2020-03-18 03:27:32 +00:00
|
|
|
symbols.push_back(symbol);
|
2019-07-29 01:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 12:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-14 03:10:35 +00:00
|
|
|
String getBuildIDFromProgramHeaders(dl_phdr_info * info)
|
|
|
|
{
|
|
|
|
for (size_t header_index = 0; header_index < info->dlpi_phnum; ++header_index)
|
|
|
|
{
|
|
|
|
const ElfPhdr & phdr = info->dlpi_phdr[header_index];
|
|
|
|
if (phdr.p_type != PT_NOTE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return Elf::getBuildID(reinterpret_cast<const char *>(info->dlpi_addr + phdr.p_vaddr), phdr.p_memsz);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-29 12:48:19 +00:00
|
|
|
void collectSymbolsFromELFSymbolTable(
|
|
|
|
dl_phdr_info * info,
|
2019-07-29 22:26:44 +00:00
|
|
|
const Elf & elf,
|
|
|
|
const Elf::Section & symbol_table,
|
|
|
|
const Elf::Section & string_table,
|
|
|
|
std::vector<SymbolIndex::Symbol> & symbols)
|
2019-07-29 12:48:19 +00:00
|
|
|
{
|
|
|
|
/// Iterate symbol table.
|
2019-07-29 17:14:53 +00:00
|
|
|
const ElfSym * symbol_table_entry = reinterpret_cast<const ElfSym *>(symbol_table.begin());
|
|
|
|
const ElfSym * symbol_table_end = reinterpret_cast<const ElfSym *>(symbol_table.end());
|
2019-07-29 12:48:19 +00:00
|
|
|
|
2019-07-29 17:14:53 +00:00
|
|
|
const char * strings = string_table.begin();
|
2019-07-29 12:48:19 +00:00
|
|
|
|
|
|
|
for (; symbol_table_entry < symbol_table_end; ++symbol_table_entry)
|
|
|
|
{
|
|
|
|
if (!symbol_table_entry->st_name
|
|
|
|
|| !symbol_table_entry->st_value
|
|
|
|
|| !symbol_table_entry->st_size
|
2019-07-29 17:14:53 +00:00
|
|
|
|| strings + symbol_table_entry->st_name >= elf.end())
|
2019-07-29 12:48:19 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/// Find the name in strings table.
|
|
|
|
const char * symbol_name = strings + symbol_table_entry->st_name;
|
|
|
|
|
2019-07-29 22:26:44 +00:00
|
|
|
if (!symbol_name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SymbolIndex::Symbol symbol;
|
2019-07-29 12:48:19 +00:00
|
|
|
symbol.address_begin = reinterpret_cast<const void *>(info->dlpi_addr + symbol_table_entry->st_value);
|
|
|
|
symbol.address_end = reinterpret_cast<const void *>(info->dlpi_addr + symbol_table_entry->st_value + symbol_table_entry->st_size);
|
2019-07-29 22:26:44 +00:00
|
|
|
symbol.name = symbol_name;
|
2020-03-18 03:27:32 +00:00
|
|
|
symbols.push_back(symbol);
|
2019-07-29 12:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-29 17:14:53 +00:00
|
|
|
bool searchAndCollectSymbolsFromELFSymbolTable(
|
2019-07-29 12:48:19 +00:00
|
|
|
dl_phdr_info * info,
|
2019-07-29 22:26:44 +00:00
|
|
|
const Elf & elf,
|
2019-07-29 17:14:53 +00:00
|
|
|
unsigned section_header_type,
|
2019-07-29 12:48:19 +00:00
|
|
|
const char * string_table_name,
|
2019-07-29 22:26:44 +00:00
|
|
|
std::vector<SymbolIndex::Symbol> & symbols)
|
2019-07-29 12:48:19 +00:00
|
|
|
{
|
2019-07-29 22:26:44 +00:00
|
|
|
std::optional<Elf::Section> symbol_table;
|
|
|
|
std::optional<Elf::Section> string_table;
|
2019-07-29 12:48:19 +00:00
|
|
|
|
2019-07-29 22:26:44 +00:00
|
|
|
if (!elf.iterateSections([&](const Elf::Section & section, size_t)
|
2019-07-29 17:14:53 +00:00
|
|
|
{
|
|
|
|
if (section.header.sh_type == section_header_type)
|
|
|
|
symbol_table.emplace(section);
|
|
|
|
else if (section.header.sh_type == SHT_STRTAB && 0 == strcmp(section.name(), string_table_name))
|
|
|
|
string_table.emplace(section);
|
2019-07-29 12:48:19 +00:00
|
|
|
|
2020-03-08 21:18:53 +00:00
|
|
|
return (symbol_table && string_table);
|
2019-07-29 17:14:53 +00:00
|
|
|
}))
|
|
|
|
{
|
2019-07-29 12:48:19 +00:00
|
|
|
return false;
|
2019-07-29 17:14:53 +00:00
|
|
|
}
|
2019-07-29 12:48:19 +00:00
|
|
|
|
2019-07-29 17:14:53 +00:00
|
|
|
collectSymbolsFromELFSymbolTable(info, elf, *symbol_table, *string_table, symbols);
|
2019-07-29 12:48:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
void collectSymbolsFromELF(dl_phdr_info * info,
|
2019-07-29 22:26:44 +00:00
|
|
|
std::vector<SymbolIndex::Symbol> & symbols,
|
2020-06-20 09:07:05 +00:00
|
|
|
std::vector<SymbolIndex::Object> & objects,
|
|
|
|
String & build_id)
|
2019-07-29 12:48:19 +00:00
|
|
|
{
|
2020-06-20 19:05:03 +00:00
|
|
|
/// MSan does not know that the program segments in memory are initialized.
|
|
|
|
__msan_unpoison_string(info->dlpi_name);
|
|
|
|
|
2019-07-29 12:48:19 +00:00
|
|
|
std::string object_name = info->dlpi_name;
|
|
|
|
|
2020-06-20 09:07:05 +00:00
|
|
|
String our_build_id = getBuildIDFromProgramHeaders(info);
|
|
|
|
|
2020-11-30 14:30:55 +00:00
|
|
|
/// If the name is empty and there is a non-empty build-id - it's main executable.
|
|
|
|
/// Find a elf file for the main executable and set the build-id.
|
2019-07-29 12:48:19 +00:00
|
|
|
if (object_name.empty())
|
2020-06-20 09:07:05 +00:00
|
|
|
{
|
2019-07-29 12:48:19 +00:00
|
|
|
object_name = "/proc/self/exe";
|
2020-11-30 14:30:55 +00:00
|
|
|
if (build_id.empty())
|
|
|
|
build_id = our_build_id;
|
2020-06-20 09:07:05 +00:00
|
|
|
}
|
2019-07-29 12:48:19 +00:00
|
|
|
|
|
|
|
std::error_code ec;
|
2019-07-30 16:12:53 +00:00
|
|
|
std::filesystem::path canonical_path = std::filesystem::canonical(object_name, ec);
|
2019-07-29 12:48:19 +00:00
|
|
|
|
|
|
|
if (ec)
|
|
|
|
return;
|
|
|
|
|
2020-08-08 00:47:03 +00:00
|
|
|
/// Debug info and symbol table sections may be split to separate binary.
|
2020-11-30 14:30:55 +00:00
|
|
|
std::filesystem::path local_debug_info_path = canonical_path.parent_path() / canonical_path.stem();
|
|
|
|
local_debug_info_path += ".debug";
|
2019-08-01 03:45:33 +00:00
|
|
|
std::filesystem::path debug_info_path = std::filesystem::path("/usr/lib/debug") / canonical_path.relative_path();
|
2019-07-30 16:12:53 +00:00
|
|
|
|
2020-11-30 14:30:55 +00:00
|
|
|
if (std::filesystem::exists(local_debug_info_path))
|
|
|
|
object_name = local_debug_info_path;
|
|
|
|
else if (std::filesystem::exists(debug_info_path))
|
|
|
|
object_name = debug_info_path;
|
2021-01-24 02:28:43 +00:00
|
|
|
else if (build_id.size() >= 2)
|
2021-01-23 19:03:54 +00:00
|
|
|
{
|
|
|
|
// Check if there is a .debug file in .build-id folder. For example:
|
|
|
|
// /usr/lib/debug/.build-id/e4/0526a12e9a8f3819a18694f6b798f10c624d5c.debug
|
|
|
|
String build_id_hex;
|
|
|
|
build_id_hex.resize(build_id.size() * 2);
|
|
|
|
|
|
|
|
char * pos = build_id_hex.data();
|
|
|
|
for (auto c : build_id)
|
|
|
|
{
|
|
|
|
writeHexByteLowercase(c, pos);
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::filesystem::path build_id_debug_info_path(
|
|
|
|
fmt::format("/usr/lib/debug/.build-id/{}/{}.debug", build_id_hex.substr(0, 2), build_id_hex.substr(2)));
|
|
|
|
if (std::filesystem::exists(build_id_debug_info_path))
|
|
|
|
object_name = build_id_debug_info_path;
|
|
|
|
else
|
|
|
|
object_name = canonical_path;
|
|
|
|
}
|
2020-11-30 14:30:55 +00:00
|
|
|
else
|
|
|
|
object_name = canonical_path;
|
2019-07-30 16:12:53 +00:00
|
|
|
|
2020-06-14 03:10:35 +00:00
|
|
|
/// But we have to compare Build ID to check that debug info corresponds to the same executable.
|
|
|
|
|
2019-07-29 22:26:44 +00:00
|
|
|
SymbolIndex::Object object;
|
|
|
|
object.elf = std::make_unique<Elf>(object_name);
|
2020-06-14 03:10:35 +00:00
|
|
|
|
|
|
|
String file_build_id = object.elf->getBuildID();
|
|
|
|
|
|
|
|
if (our_build_id != file_build_id)
|
|
|
|
{
|
|
|
|
/// If debug info doesn't correspond to our binary, fallback to the info in our binary.
|
|
|
|
if (object_name != canonical_path)
|
|
|
|
{
|
|
|
|
object_name = canonical_path;
|
|
|
|
object.elf = std::make_unique<Elf>(object_name);
|
|
|
|
|
|
|
|
/// But it can still be outdated, for example, if executable file was deleted from filesystem and replaced by another file.
|
|
|
|
file_build_id = object.elf->getBuildID();
|
|
|
|
if (our_build_id != file_build_id)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
object.address_begin = reinterpret_cast<const void *>(info->dlpi_addr);
|
2019-07-29 22:26:44 +00:00
|
|
|
object.address_end = reinterpret_cast<const void *>(info->dlpi_addr + object.elf->size());
|
2019-07-29 18:38:04 +00:00
|
|
|
object.name = object_name;
|
|
|
|
objects.push_back(std::move(object));
|
|
|
|
|
2019-07-29 22:26:44 +00:00
|
|
|
searchAndCollectSymbolsFromELFSymbolTable(info, *objects.back().elf, SHT_SYMTAB, ".strtab", symbols);
|
|
|
|
|
|
|
|
/// Unneeded because they were parsed from "program headers" of loaded objects.
|
|
|
|
//searchAndCollectSymbolsFromELFSymbolTable(info, *objects.back().elf, SHT_DYNSYM, ".dynstr", symbols);
|
2019-07-29 12:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Callback for dl_iterate_phdr.
|
|
|
|
* Is called by dl_iterate_phdr for every loaded shared lib until something
|
|
|
|
* else than 0 is returned by one call of this function.
|
|
|
|
*/
|
2019-07-29 18:38:04 +00:00
|
|
|
int collectSymbols(dl_phdr_info * info, size_t, void * data_ptr)
|
2019-07-29 12:48:19 +00:00
|
|
|
{
|
2019-07-29 22:26:44 +00:00
|
|
|
SymbolIndex::Data & data = *reinterpret_cast<SymbolIndex::Data *>(data_ptr);
|
2019-07-29 12:48:19 +00:00
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
collectSymbolsFromProgramHeaders(info, data.symbols);
|
2020-06-20 09:07:05 +00:00
|
|
|
collectSymbolsFromELF(info, data.symbols, data.objects, data.build_id);
|
2019-07-29 01:08:52 +00:00
|
|
|
|
|
|
|
/* Continue iterations */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
template <typename T>
|
|
|
|
const T * find(const void * address, const std::vector<T> & vec)
|
2019-07-29 01:08:52 +00:00
|
|
|
{
|
|
|
|
/// First range that has left boundary greater than address.
|
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
auto it = std::lower_bound(vec.begin(), vec.end(), address,
|
|
|
|
[](const T & symbol, const void * addr) { return symbol.address_begin <= addr; });
|
2019-07-29 01:08:52 +00:00
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
if (it == vec.begin())
|
2019-07-29 01:08:52 +00:00
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
--it; /// Last range that has left boundary less or equals than address.
|
|
|
|
|
|
|
|
if (address >= it->address_begin && address < it->address_end)
|
|
|
|
return &*it;
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-07-29 18:38:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
void SymbolIndex::update()
|
|
|
|
{
|
|
|
|
dl_iterate_phdr(collectSymbols, &data.symbols);
|
2019-07-30 16:12:53 +00:00
|
|
|
|
|
|
|
std::sort(data.objects.begin(), data.objects.end(), [](const Object & a, const Object & b) { return a.address_begin < b.address_begin; });
|
2019-07-29 18:38:04 +00:00
|
|
|
std::sort(data.symbols.begin(), data.symbols.end(), [](const Symbol & a, const Symbol & b) { return a.address_begin < b.address_begin; });
|
2019-07-30 16:12:53 +00:00
|
|
|
|
|
|
|
/// We found symbols both from loaded program headers and from ELF symbol tables.
|
|
|
|
data.symbols.erase(std::unique(data.symbols.begin(), data.symbols.end(), [](const Symbol & a, const Symbol & b)
|
|
|
|
{
|
|
|
|
return a.address_begin == b.address_begin && a.address_end == b.address_end;
|
|
|
|
}), data.symbols.end());
|
2019-07-29 18:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SymbolIndex::Symbol * SymbolIndex::findSymbol(const void * address) const
|
|
|
|
{
|
|
|
|
return find(address, data.symbols);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SymbolIndex::Object * SymbolIndex::findObject(const void * address) const
|
|
|
|
{
|
|
|
|
return find(address, data.objects);
|
|
|
|
}
|
|
|
|
|
2020-06-20 09:07:05 +00:00
|
|
|
String SymbolIndex::getBuildIDHex() const
|
|
|
|
{
|
|
|
|
String build_id_binary = getBuildID();
|
|
|
|
String build_id_hex;
|
|
|
|
build_id_hex.resize(build_id_binary.size() * 2);
|
|
|
|
|
|
|
|
char * pos = build_id_hex.data();
|
|
|
|
for (auto c : build_id_binary)
|
|
|
|
{
|
|
|
|
writeHexByteUppercase(c, pos);
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return build_id_hex;
|
|
|
|
}
|
|
|
|
|
2021-08-28 03:43:43 +00:00
|
|
|
MultiVersion<SymbolIndex> & SymbolIndex::instanceImpl()
|
2019-08-22 03:24:05 +00:00
|
|
|
{
|
2020-11-30 14:30:55 +00:00
|
|
|
static MultiVersion<SymbolIndex> instance(std::unique_ptr<SymbolIndex>(new SymbolIndex));
|
2021-08-28 03:43:43 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiVersion<SymbolIndex>::Version SymbolIndex::instance()
|
|
|
|
{
|
|
|
|
return instanceImpl().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolIndex::reload()
|
|
|
|
{
|
|
|
|
instanceImpl().set(std::unique_ptr<SymbolIndex>(new SymbolIndex));
|
|
|
|
/// Also drop stacktrace cache.
|
|
|
|
StackTrace::dropCache();
|
2019-08-22 03:24:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
}
|
2019-08-21 00:48:34 +00:00
|
|
|
|
|
|
|
#endif
|