2019-07-29 01:08:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-06-10 08:22:31 +00:00
|
|
|
#if defined(__ELF__) && !defined(OS_FREEBSD)
|
2019-08-21 00:48:34 +00:00
|
|
|
|
2019-07-29 01:08:52 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2021-12-21 13:41:53 +00:00
|
|
|
#include <unordered_map>
|
2019-07-29 22:26:44 +00:00
|
|
|
#include <Common/Elf.h>
|
2019-08-22 03:24:05 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2019-07-29 01:08:52 +00:00
|
|
|
|
2023-07-23 03:25:14 +00:00
|
|
|
|
2019-07-29 01:08:52 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Allow to quickly find symbol name from address.
|
|
|
|
* Used as a replacement for "dladdr" function which is extremely slow.
|
2019-07-29 22:26:44 +00:00
|
|
|
* It works better than "dladdr" because it also allows to search private symbols, that are not participated in shared linking.
|
2019-07-29 01:08:52 +00:00
|
|
|
*/
|
2019-08-22 03:24:05 +00:00
|
|
|
class SymbolIndex : private boost::noncopyable
|
2019-07-29 01:08:52 +00:00
|
|
|
{
|
2019-07-29 22:26:44 +00:00
|
|
|
protected:
|
2023-07-09 07:20:03 +00:00
|
|
|
SymbolIndex() { load(); }
|
2019-07-29 22:26:44 +00:00
|
|
|
|
2019-07-29 01:08:52 +00:00
|
|
|
public:
|
2023-07-09 07:20:03 +00:00
|
|
|
static const SymbolIndex & instance();
|
2019-08-22 03:24:05 +00:00
|
|
|
|
2019-07-29 01:08:52 +00:00
|
|
|
struct Symbol
|
|
|
|
{
|
|
|
|
const void * address_begin;
|
|
|
|
const void * address_end;
|
2019-07-29 22:26:44 +00:00
|
|
|
const char * name;
|
2019-07-29 18:38:04 +00:00
|
|
|
};
|
2019-07-29 01:08:52 +00:00
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
struct Object
|
|
|
|
{
|
|
|
|
const void * address_begin;
|
|
|
|
const void * address_end;
|
|
|
|
std::string name;
|
2021-01-20 13:03:25 +00:00
|
|
|
std::shared_ptr<Elf> elf;
|
2019-07-29 01:08:52 +00:00
|
|
|
};
|
|
|
|
|
2019-12-24 20:07:44 +00:00
|
|
|
/// Address in virtual memory should be passed. These addresses include offset where the object is loaded in memory.
|
2019-07-29 18:38:04 +00:00
|
|
|
const Symbol * findSymbol(const void * address) const;
|
|
|
|
const Object * findObject(const void * address) const;
|
2019-07-29 01:08:52 +00:00
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
const std::vector<Symbol> & symbols() const { return data.symbols; }
|
|
|
|
const std::vector<Object> & objects() const { return data.objects; }
|
2019-07-29 01:08:52 +00:00
|
|
|
|
2020-06-20 09:07:05 +00:00
|
|
|
/// The BuildID that is generated by compiler.
|
|
|
|
String getBuildID() const { return data.build_id; }
|
|
|
|
String getBuildIDHex() const;
|
|
|
|
|
2019-07-29 18:38:04 +00:00
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
std::vector<Symbol> symbols;
|
|
|
|
std::vector<Object> objects;
|
2020-06-20 09:07:05 +00:00
|
|
|
String build_id;
|
2019-07-29 18:38:04 +00:00
|
|
|
};
|
2019-07-29 01:08:52 +00:00
|
|
|
private:
|
2019-07-29 18:38:04 +00:00
|
|
|
Data data;
|
2019-07-29 22:26:44 +00:00
|
|
|
|
2023-07-09 07:20:03 +00:00
|
|
|
void load();
|
2019-07-29 01:08:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2019-08-21 00:48:34 +00:00
|
|
|
|
|
|
|
#endif
|