1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: KernelSymbolsParser class implements
16 */
17 #include "kernel_symbols_parser.h"
18 #include "file_utils.h"
19 #include "logging.h"
20 #include "string_utils.h"
21
22 #include <algorithm> // for std::sort
23 #include <sstream> // for std::stringstream
24
25 namespace {
26 constexpr int ADDR_VALUE_BASE = 16;
27 } // namespace
28
29 FTRACE_NS_BEGIN
KernelSymbolsParser()30 KernelSymbolsParser::KernelSymbolsParser()
31 {
32 HILOG_INFO(LOG_CORE, "KernelSymbolsParser create!");
33 }
34
~KernelSymbolsParser()35 KernelSymbolsParser::~KernelSymbolsParser()
36 {
37 HILOG_INFO(LOG_CORE, "KernelSymbolsParser destroy!");
38 }
39
Accept(const std::function<void (const KernelSymbol &)> & visitor)40 void KernelSymbolsParser::Accept(const std::function<void(const KernelSymbol&)>& visitor)
41 {
42 std::for_each(kernelSymbols_.begin(), kernelSymbols_.end(), visitor);
43 }
44
IsValidTextSymbol(const KernelSymbol & a)45 bool KernelSymbolsParser::IsValidTextSymbol(const KernelSymbol& a)
46 {
47 if (a.addr == 0 || a.name.empty()) {
48 return false;
49 }
50 if (a.name[0] == '$') {
51 return false;
52 }
53 if (a.type != 't' && a.type != 'T') {
54 return false;
55 }
56 return true;
57 }
58
CompareSymbolInfo(const KernelSymbol & a,const KernelSymbol & b)59 bool KernelSymbolsParser::CompareSymbolInfo(const KernelSymbol& a, const KernelSymbol& b)
60 {
61 if (a.addr != b.addr) {
62 return a.addr < b.addr;
63 }
64 if (a.name != b.name) {
65 return a.name < b.name;
66 }
67 return a.type < b.type;
68 }
69
Parse(const std::string & kallsyms)70 bool KernelSymbolsParser::Parse(const std::string& kallsyms)
71 {
72 std::string line;
73 std::stringstream sin(kallsyms);
74 while (std::getline(sin, line)) {
75 // one of following format:
76 // c0109b3c T arm_elf_read_implies_exec
77 // bf004fd8 t media_mem_init [hi_osal]
78 std::stringstream ss(line);
79 KernelSymbol info;
80 std::string addrStr;
81 if (ss >> addrStr >> info.type >> info.name) {
82 info.addr = strtoull(addrStr.c_str(), nullptr, ADDR_VALUE_BASE);
83 }
84 if (info.addr == 0) {
85 continue;
86 }
87 if (StringUtils::EndsWith(info.name, ".cfi")) {
88 info.name = info.name.substr(0, info.name.size() - (sizeof(".cfi") - 1));
89 }
90 if (IsValidTextSymbol(info)) {
91 kernelSymbols_.emplace_back(std::move(info));
92 }
93 }
94 std::sort(kernelSymbols_.begin(), kernelSymbols_.end(), CompareSymbolInfo);
95 kernelSymbols_.shrink_to_fit();
96 return kernelSymbols_.size();
97 }
98 FTRACE_NS_END
99