• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 #include "kernel_symbols_processor.h"
16 
17 #include "string_help.h"
18 #include "string_to_numerical.h"
19 
20 namespace SysTuning {
21 namespace TraceStreamer {
KernelSymbolsProcessor(TraceDataCache * dataCache,const TraceStreamerFilters * filters)22 KernelSymbolsProcessor::KernelSymbolsProcessor(TraceDataCache *dataCache, const TraceStreamerFilters *filters)
23     : traceDataCache_(dataCache), streamFilters_(filters)
24 {
25     if (!streamFilters_) {
26         TS_LOGE("streamFilters_ should not be null");
27         return;
28     }
29 }
30 
~KernelSymbolsProcessor()31 KernelSymbolsProcessor::~KernelSymbolsProcessor()
32 {
33     TS_LOGI("KernelSymbolsProcessor destroy!");
34 }
35 
IsValidKernelSymbol(const KernelSymbol & symbol)36 bool KernelSymbolsProcessor::IsValidKernelSymbol(const KernelSymbol &symbol)
37 {
38     if (symbol.addr == 0 || symbol.name.empty()) {
39         return false;
40     }
41     if (symbol.name[0] == '$') {
42         return false;
43     }
44     // eg: ffffffc010b8daa4 t bio_complete
45     if (symbol.type != 't' && symbol.type != 'T') {
46         return false;
47     }
48     return true;
49 }
50 
HandleKallSyms(const std::string & kallsyms)51 bool KernelSymbolsProcessor::HandleKallSyms(const std::string &kallsyms)
52 {
53     TS_CHECK_TRUE(!kallsyms.empty(), false, "kallsyms is empty!");
54     std::stringstream symsStream(kallsyms);
55     std::string line;
56     KernelSymbol symbol;
57     std::string addrStr;
58     std::stringstream strStream;
59     while (std::getline(symsStream, line)) {
60         strStream.clear();
61         strStream.str(line);
62         if (strStream >> addrStr >> symbol.type >> symbol.name) {
63             symbol.addr = base::StrToInt<uint64_t>(addrStr, base::INTEGER_RADIX_TYPE_HEX).value();
64         }
65         if (symbol.addr == 0) {
66             continue;
67         }
68         if (base::EndWith(symbol.name, ".cfi")) {
69             symbol.name = symbol.name.substr(0, symbol.name.size() - (sizeof(".cfi") - 1));
70         }
71         if (IsValidKernelSymbol(symbol)) {
72             traceDataCache_->GetSymbolsData()->UpdateSymbol(symbol.addr, traceDataCache_->GetDataIndex(symbol.name));
73         }
74     }
75     TS_LOGI("kernel symbolsData size = %" PRIu64 "", traceDataCache_->GetSymbolsData()->Size());
76     return true;
77 }
78 } // namespace TraceStreamer
79 } // namespace SysTuning
80