Coverage Report

Created: 2024-11-21 11:46

/root/doris/be/src/common/symbol_index.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/SymbolIndex.h
19
// and modified by Doris
20
21
#pragma once
22
23
#if defined(__ELF__) && !defined(__FreeBSD__)
24
25
#include <assert.h>
26
#include <common/elf.h>
27
#include <common/multi_version.h>
28
29
#include <boost/noncopyable.hpp>
30
#include <string>
31
#include <unordered_map>
32
#include <vector>
33
34
namespace doris {
35
36
/** Allow to quickly find symbol name from address.
37
  * Used as a replacement for "dladdr" function which is extremely slow.
38
  * It works better than "dladdr" because it also allows to search private symbols, that are not participated in shared linking.
39
  */
40
class SymbolIndex : private boost::noncopyable {
41
protected:
42
1
    SymbolIndex() { update(); }
43
44
public:
45
    static MultiVersion<SymbolIndex>::Version instance();
46
    static void reload();
47
48
    struct Symbol {
49
        const void* address_begin = nullptr;
50
        const void* address_end = nullptr;
51
        const char* name = nullptr;
52
    };
53
54
    struct Object {
55
        const void* address_begin = nullptr;
56
        const void* address_end = nullptr;
57
        std::string name;
58
        std::shared_ptr<Elf> elf;
59
    };
60
61
    /// Address in virtual memory should be passed. These addresses include offset where the object is loaded in memory.
62
    const Symbol* findSymbol(const void* address) const;
63
    const Object* findObject(const void* address) const;
64
65
0
    const std::vector<Symbol>& symbols() const { return data.symbols; }
66
0
    const std::vector<Object>& objects() const { return data.objects; }
67
68
0
    std::string_view getResource(std::string name) const {
69
0
        if (auto it = data.resources.find(name); it != data.resources.end()) {
70
0
            return it->second.data();
71
0
        }
72
0
        return {};
73
0
    }
74
75
    /// The BuildID that is generated by compiler.
76
0
    std::string getBuildID() const { return data.build_id; }
77
    std::string getBuildIDHex() const;
78
79
    struct ResourcesBlob {
80
        /// Symbol can be presented in multiple shared objects,
81
        /// base_address will be used to compare only symbols from the same SO.
82
        ElfW(Addr) base_address = 0;
83
        /// Just a human name of the SO.
84
        std::string_view object_name;
85
        /// Data blob.
86
        std::string_view start;
87
        std::string_view end;
88
89
0
        [[nodiscard]] std::string_view data() const {
90
0
            assert(end.data() >= start.data());
91
0
            return std::string_view {start.data(), static_cast<size_t>(end.data() - start.data())};
92
0
        }
93
    };
94
    using Resources = std::unordered_map<std::string_view /* symbol name */, ResourcesBlob>;
95
96
    struct Data {
97
        std::vector<Symbol> symbols;
98
        std::vector<Object> objects;
99
        std::string build_id;
100
101
        /// Resources (embedded binary data) are located by symbols in form of _binary_name_start and _binary_name_end.
102
        Resources resources;
103
    };
104
105
private:
106
    Data data;
107
108
    void update();
109
    static MultiVersion<SymbolIndex>& instanceImpl();
110
};
111
112
} // namespace doris
113
114
#endif