106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci * 1506f6ba60Sopenharmony_ci * Description: KernelSymbolsParser class implements 1606f6ba60Sopenharmony_ci */ 1706f6ba60Sopenharmony_ci#include "kernel_symbols_parser.h" 1806f6ba60Sopenharmony_ci#include "file_utils.h" 1906f6ba60Sopenharmony_ci#include "logging.h" 2006f6ba60Sopenharmony_ci#include "string_utils.h" 2106f6ba60Sopenharmony_ci 2206f6ba60Sopenharmony_ci#include <algorithm> // for std::sort 2306f6ba60Sopenharmony_ci#include <sstream> // for std::stringstream 2406f6ba60Sopenharmony_ci 2506f6ba60Sopenharmony_cinamespace { 2606f6ba60Sopenharmony_ciconstexpr int ADDR_VALUE_BASE = 16; 2706f6ba60Sopenharmony_ciconstexpr int MAX_BUFFER_SIZE = 10 * 1000; 2806f6ba60Sopenharmony_ci} // namespace 2906f6ba60Sopenharmony_ci 3006f6ba60Sopenharmony_ciFTRACE_NS_BEGIN 3106f6ba60Sopenharmony_ciKernelSymbolsParser::KernelSymbolsParser() 3206f6ba60Sopenharmony_ci{ 3306f6ba60Sopenharmony_ci PROFILER_LOG_INFO(LOG_CORE, "KernelSymbolsParser create!"); 3406f6ba60Sopenharmony_ci} 3506f6ba60Sopenharmony_ci 3606f6ba60Sopenharmony_ciKernelSymbolsParser::~KernelSymbolsParser() 3706f6ba60Sopenharmony_ci{ 3806f6ba60Sopenharmony_ci PROFILER_LOG_INFO(LOG_CORE, "KernelSymbolsParser destroy!"); 3906f6ba60Sopenharmony_ci} 4006f6ba60Sopenharmony_ci 4106f6ba60Sopenharmony_civoid KernelSymbolsParser::Accept(const std::function<void(const KernelSymbol&)>& visitor) 4206f6ba60Sopenharmony_ci{ 4306f6ba60Sopenharmony_ci std::for_each(kernelSymbols_.begin(), kernelSymbols_.end(), visitor); 4406f6ba60Sopenharmony_ci} 4506f6ba60Sopenharmony_ci 4606f6ba60Sopenharmony_cibool KernelSymbolsParser::IsValidTextSymbol(const KernelSymbol& a) 4706f6ba60Sopenharmony_ci{ 4806f6ba60Sopenharmony_ci if (a.addr == 0 || a.name.empty()) { 4906f6ba60Sopenharmony_ci return false; 5006f6ba60Sopenharmony_ci } 5106f6ba60Sopenharmony_ci if (a.name[0] == '$') { 5206f6ba60Sopenharmony_ci return false; 5306f6ba60Sopenharmony_ci } 5406f6ba60Sopenharmony_ci if (a.type != 't' && a.type != 'T') { 5506f6ba60Sopenharmony_ci return false; 5606f6ba60Sopenharmony_ci } 5706f6ba60Sopenharmony_ci return true; 5806f6ba60Sopenharmony_ci} 5906f6ba60Sopenharmony_ci 6006f6ba60Sopenharmony_cibool KernelSymbolsParser::CompareSymbolInfo(const KernelSymbol& a, const KernelSymbol& b) 6106f6ba60Sopenharmony_ci{ 6206f6ba60Sopenharmony_ci if (a.addr != b.addr) { 6306f6ba60Sopenharmony_ci return a.addr < b.addr; 6406f6ba60Sopenharmony_ci } 6506f6ba60Sopenharmony_ci if (a.name != b.name) { 6606f6ba60Sopenharmony_ci return a.name < b.name; 6706f6ba60Sopenharmony_ci } 6806f6ba60Sopenharmony_ci return a.type < b.type; 6906f6ba60Sopenharmony_ci} 7006f6ba60Sopenharmony_ci 7106f6ba60Sopenharmony_cibool KernelSymbolsParser::Parse(const std::string& kallsyms) 7206f6ba60Sopenharmony_ci{ 7306f6ba60Sopenharmony_ci CHECK_TRUE(!kallsyms.empty(), false, "kallsyms is empty!"); 7406f6ba60Sopenharmony_ci std::stringstream sin(kallsyms); 7506f6ba60Sopenharmony_ci std::string line; 7606f6ba60Sopenharmony_ci KernelSymbol info; 7706f6ba60Sopenharmony_ci std::string addrStr; 7806f6ba60Sopenharmony_ci std::stringstream ss; 7906f6ba60Sopenharmony_ci int count = 0; 8006f6ba60Sopenharmony_ci while (std::getline(sin, line)) { 8106f6ba60Sopenharmony_ci // one of following format: 8206f6ba60Sopenharmony_ci // c0109b3c T arm_elf_read_implies_exec 8306f6ba60Sopenharmony_ci // bf004fd8 t media_mem_init [hi_osal] 8406f6ba60Sopenharmony_ci ss.clear(); 8506f6ba60Sopenharmony_ci ss.str(line); 8606f6ba60Sopenharmony_ci if (ss >> addrStr >> info.type >> info.name) { 8706f6ba60Sopenharmony_ci info.addr = strtoull(addrStr.c_str(), nullptr, ADDR_VALUE_BASE); 8806f6ba60Sopenharmony_ci } 8906f6ba60Sopenharmony_ci if (info.addr == 0) { 9006f6ba60Sopenharmony_ci continue; 9106f6ba60Sopenharmony_ci } 9206f6ba60Sopenharmony_ci if (StringUtils::EndsWith(info.name, ".cfi")) { 9306f6ba60Sopenharmony_ci info.name = info.name.substr(0, info.name.size() - (sizeof(".cfi") - 1)); 9406f6ba60Sopenharmony_ci } 9506f6ba60Sopenharmony_ci if (IsValidTextSymbol(info)) { 9606f6ba60Sopenharmony_ci if (count % MAX_BUFFER_SIZE == 0) { 9706f6ba60Sopenharmony_ci kernelSymbols_.resize(kernelSymbols_.size() + MAX_BUFFER_SIZE); 9806f6ba60Sopenharmony_ci } 9906f6ba60Sopenharmony_ci kernelSymbols_[count] = info; 10006f6ba60Sopenharmony_ci count++; 10106f6ba60Sopenharmony_ci } 10206f6ba60Sopenharmony_ci } 10306f6ba60Sopenharmony_ci 10406f6ba60Sopenharmony_ci kernelSymbols_.resize(count); 10506f6ba60Sopenharmony_ci std::sort(kernelSymbols_.begin(), kernelSymbols_.end(), CompareSymbolInfo); 10606f6ba60Sopenharmony_ci return kernelSymbols_.size(); 10706f6ba60Sopenharmony_ci} 10806f6ba60Sopenharmony_ciFTRACE_NS_END 109