Coverage Report

Created: 2024-11-21 12:22

/root/doris/be/src/util/bfd_parser.cpp
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
18
#include "util/bfd_parser.h"
19
20
#include <stdio.h>
21
#include <stdlib.h>
22
23
#include <algorithm>
24
#include <memory>
25
#include <ostream>
26
27
#include "common/logging.h"
28
29
namespace doris {
30
31
struct BfdFindCtx {
32
    BfdFindCtx(bfd_symbol** syms_, bfd_vma pc_)
33
            : found(false),
34
              syms(syms_),
35
              pc(pc_),
36
              file_name(nullptr),
37
              func_name(nullptr),
38
0
              lineno(0) {}
39
40
    bool found;
41
    bfd_symbol** syms;
42
    bfd_vma pc;
43
    const char* file_name;
44
    const char* func_name;
45
    unsigned int lineno;
46
};
47
48
std::mutex BfdParser::_bfd_mutex;
49
bool BfdParser::_is_bfd_inited = false;
50
51
0
static void find_addr_in_section(bfd* abfd, asection* sec, void* arg) {
52
0
    BfdFindCtx* ctx = (BfdFindCtx*)arg;
53
0
    if (ctx->found) {
54
0
        return;
55
0
    }
56
0
#ifdef bfd_get_section_flags
57
0
    if ((bfd_get_section_flags(abfd, sec) & SEC_ALLOC) == 0) {
58
0
        return;
59
0
    }
60
#else
61
    if ((bfd_section_flags(sec) & SEC_ALLOC) == 0) {
62
        return;
63
    }
64
#endif
65
0
#ifdef bfd_get_section_vma
66
0
    auto vma = bfd_get_section_vma(abfd, sec);
67
#else
68
    auto vma = bfd_section_vma(sec);
69
#endif
70
0
    if (ctx->pc < vma) {
71
0
        return;
72
0
    }
73
0
#ifdef bfd_get_section_size
74
0
    auto size = bfd_get_section_size(sec);
75
#else
76
    auto size = bfd_section_size(sec);
77
#endif
78
0
    if (ctx->pc >= vma + size) {
79
0
        return;
80
0
    }
81
0
    ctx->found = bfd_find_nearest_line(abfd, sec, ctx->syms, ctx->pc - vma, &ctx->file_name,
82
0
                                       &ctx->func_name, &ctx->lineno);
83
0
}
84
85
0
static void section_print(bfd* bfd, asection* sec, void* arg) {
86
0
    std::string* str = (std::string*)arg;
87
0
    str->append(sec->name);
88
0
    str->push_back('\n');
89
0
}
90
91
0
void BfdParser::init_bfd() {
92
0
    if (_is_bfd_inited) {
93
0
        return;
94
0
    }
95
0
    std::lock_guard<std::mutex> lock(_bfd_mutex);
96
0
    bfd_init();
97
0
    if (!bfd_set_default_target("elf64-x86-64")) {
98
0
        LOG(ERROR) << "set default target to elf64-x86-64 failed.";
99
0
    }
100
0
    _is_bfd_inited = true;
101
0
}
102
103
0
BfdParser* BfdParser::create() {
104
0
    FILE* file = fopen("/proc/self/cmdline", "r");
105
0
    if (file == nullptr) {
106
0
        return nullptr;
107
0
    }
108
109
0
    char prog_name[1024];
110
111
0
    if (fscanf(file, "%1023s ", prog_name) != 1) {
112
0
        fclose(file);
113
0
        return nullptr;
114
0
    }
115
116
0
    fclose(file);
117
0
    std::unique_ptr<BfdParser> parser(new BfdParser(prog_name));
118
0
    if (parser->parse()) {
119
0
        return nullptr;
120
0
    }
121
0
    return parser.release();
122
0
}
123
124
0
BfdParser* BfdParser::create(const std::string& prog_name) {
125
0
    std::unique_ptr<BfdParser> parser(new BfdParser(prog_name));
126
0
    if (parser->parse()) {
127
0
        return nullptr;
128
0
    }
129
0
    return parser.release();
130
0
}
131
132
0
void BfdParser::list_targets(std::vector<std::string>* out) {
133
0
    if (!_is_bfd_inited) {
134
0
        init_bfd();
135
0
    }
136
137
0
    const char** targets = bfd_target_list();
138
0
    const char** p = targets;
139
0
    while ((*p) != nullptr) {
140
0
        out->emplace_back(*p++);
141
0
    }
142
0
    free(targets);
143
0
}
144
145
BfdParser::BfdParser(const std::string& file_name)
146
0
        : _file_name(file_name), _abfd(nullptr), _syms(nullptr), _num_symbols(0), _symbol_size(0) {
147
0
    if (!_is_bfd_inited) {
148
0
        init_bfd();
149
0
    }
150
0
}
151
152
0
BfdParser::~BfdParser() {
153
0
    if (_syms != nullptr) {
154
0
        free(_syms);
155
0
    }
156
0
    if (_abfd != nullptr) {
157
0
        bfd_close(_abfd);
158
0
    }
159
0
}
160
161
0
void BfdParser::list_sections(std::string* ss) {
162
0
    std::lock_guard<std::mutex> lock(_mutex);
163
0
    bfd_map_over_sections(_abfd, section_print, (void*)ss);
164
0
}
165
166
0
static void list_matching_formats(char** p, std::string* message) {
167
0
    if (!p || !*p) {
168
0
        return;
169
0
    }
170
0
    message->append(": Matching formats: ");
171
0
    while (*p) {
172
0
        message->append(*p++);
173
0
    }
174
0
    message->push_back('\n');
175
0
}
176
177
0
int BfdParser::open_bfd() {
178
0
    _abfd = bfd_openr(_file_name.c_str(), nullptr);
179
0
    if (_abfd == nullptr) {
180
0
        LOG(WARNING) << "bfd_openr failed because errmsg=" << bfd_errmsg(bfd_get_error());
181
0
        return -1;
182
0
    }
183
0
    if (bfd_check_format(_abfd, bfd_archive)) {
184
0
        LOG(WARNING) << "bfd_check_format for archive failed because errmsg="
185
0
                     << bfd_errmsg(bfd_get_error());
186
0
        return -1;
187
0
    }
188
189
0
    char** matches = nullptr;
190
0
    if (!bfd_check_format_matches(_abfd, bfd_object, &matches)) {
191
0
        if (bfd_get_error() == bfd_error_file_ambiguously_recognized) {
192
0
            std::string message = _file_name;
193
0
            list_matching_formats(matches, &message);
194
0
            free(matches);
195
0
            LOG(WARNING) << "bfd_check_format_matches failed because errmsg="
196
0
                         << bfd_errmsg(bfd_get_error()) << " and " << message;
197
0
        } else {
198
0
            LOG(WARNING) << "bfd_check_format_matches failed because errmsg="
199
0
                         << bfd_errmsg(bfd_get_error());
200
0
        }
201
0
        return -1;
202
0
    }
203
204
0
    return 0;
205
0
}
206
207
0
int BfdParser::load_symbols() {
208
0
    if ((bfd_get_file_flags(_abfd) & HAS_SYMS) == 0) {
209
        // No need to load symbols;
210
0
        return 0;
211
0
    }
212
0
    _num_symbols = bfd_read_minisymbols(_abfd, FALSE, (void**)&_syms, &_symbol_size);
213
0
    if (_num_symbols == 0) {
214
0
        _num_symbols =
215
0
                bfd_read_minisymbols(_abfd, TRUE /* dynamic */, (void**)&_syms, &_symbol_size);
216
0
    }
217
0
    if (_num_symbols == 0) {
218
0
        LOG(WARNING) << "Load symbols failed because errmsg=" << bfd_errmsg(bfd_get_error());
219
0
        return -1;
220
0
    }
221
0
    return 0;
222
0
}
223
224
0
int BfdParser::parse() {
225
0
    int ret = open_bfd();
226
0
    if (ret != 0) {
227
0
        LOG(WARNING) << "open bfd failed.";
228
0
        return ret;
229
0
    }
230
231
0
    ret = load_symbols();
232
0
    if (ret != 0) {
233
0
        LOG(WARNING) << "Load symbols failed.";
234
0
        return ret;
235
0
    }
236
237
0
    return 0;
238
0
}
239
240
int BfdParser::decode_address(const char* str, const char** end, std::string* file_name,
241
0
                              std::string* func_name, unsigned int* lineno) {
242
0
    bfd_vma pc = bfd_scan_vma(str, end, 16);
243
0
    BfdFindCtx ctx(_syms, pc);
244
245
0
    std::lock_guard<std::mutex> lock(_mutex);
246
0
    bfd_map_over_sections(_abfd, find_addr_in_section, (void*)&ctx);
247
0
    if (!ctx.found) {
248
0
        file_name->append("??");
249
0
        func_name->append("??");
250
0
        return -1;
251
0
    }
252
    // demangle function
253
0
    if (ctx.func_name != nullptr) {
254
0
#define DMGL_PARAMS (1 << 0)
255
0
#define DMGL_ANSI (1 << 1)
256
0
        char* demangled_name = bfd_demangle(_abfd, ctx.func_name, DMGL_ANSI | DMGL_PARAMS);
257
0
        if (demangled_name != nullptr) {
258
0
            func_name->append(demangled_name);
259
0
            free(demangled_name);
260
0
        } else {
261
0
            func_name->append(ctx.func_name);
262
0
        }
263
0
    } else {
264
0
        func_name->append("??");
265
0
    }
266
    // file_name
267
0
    if (ctx.file_name != nullptr) {
268
0
        file_name->append(ctx.file_name);
269
0
    } else {
270
0
        file_name->append("??");
271
0
    }
272
0
    *lineno = ctx.lineno;
273
0
    return 0;
274
0
}
275
276
} // namespace doris