Coverage Report

Created: 2026-03-12 00:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/common/symbol_index.h
Line
Count
Source
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
27
#include <boost/noncopyable.hpp>
28
#include <string>
29
#include <unordered_map>
30
#include <vector>
31
32
#include "common/elf.h"
33
#include "common/multi_version.h"
34
35
namespace doris {
36
37
/** Allow to quickly find symbol name from address.
38
  * Used as a replacement for "dladdr" function which is extremely slow.
39
  * It works better than "dladdr" because it also allows to search private symbols, that are not participated in shared linking.
40
  */
41
class SymbolIndex : private boost::noncopyable {
42
protected:
43
1
    SymbolIndex() { update(); }
44
45
public:
46
    static MultiVersion<SymbolIndex>::Version instance();
47
    static void reload();
48
49
    struct Symbol {
50
        const void* address_begin = nullptr;
51
        const void* address_end = nullptr;
52
        const char* name = nullptr;
53
    };
54
55
    struct Object {
56
        const void* address_begin = nullptr;
57
        const void* address_end = nullptr;
58
        std::string name;
59
        std::shared_ptr<Elf> elf;
60
    };
61
62
    /// Address in virtual memory should be passed. These addresses include offset where the object is loaded in memory.
63
    const Symbol* findSymbol(const void* address) const;
64
    const Object* findObject(const void* address) const;
65
66
0
    const std::vector<Symbol>& symbols() const { return data.symbols; }
67
0
    const std::vector<Object>& objects() const { return data.objects; }
68
69
0
    std::string_view getResource(std::string name) const {
70
0
        if (auto it = data.resources.find(name); it != data.resources.end()) {
71
0
            return it->second.data();
72
0
        }
73
0
        return {};
74
0
    }
75
76
    /// The BuildID that is generated by compiler.
77
0
    std::string getBuildID() const { return data.build_id; }
78
    std::string getBuildIDHex() const;
79
80
    struct ResourcesBlob {
81
        /// Symbol can be presented in multiple shared objects,
82
        /// base_address will be used to compare only symbols from the same SO.
83
        ElfW(Addr) base_address = 0;
84
        /// Just a human name of the SO.
85
        std::string_view object_name;
86
        /// Data blob.
87
        std::string_view start;
88
        std::string_view end;
89
90
0
        [[nodiscard]] std::string_view data() const {
91
0
            assert(end.data() >= start.data());
92
0
            return std::string_view {start.data(), static_cast<size_t>(end.data() - start.data())};
93
0
        }
94
    };
95
    using Resources = std::unordered_map<std::string_view /* symbol name */, ResourcesBlob>;
96
97
    struct Data {
98
        std::vector<Symbol> symbols;
99
        std::vector<Object> objects;
100
        std::string build_id;
101
102
        /// Resources (embedded binary data) are located by symbols in form of _binary_name_start and _binary_name_end.
103
        Resources resources;
104
    };
105
106
private:
107
    Data data;
108
109
    void update();
110
    static MultiVersion<SymbolIndex>& instanceImpl();
111
};
112
113
} // namespace doris
114
115
#endif