1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. 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 "printk_formats_parser.h"
16
17 #include <sstream>
18 #include "file_utils.h"
19 #include "ftrace_fs_ops.h"
20 #include "logging.h"
21 #include "string_utils.h"
22
23 namespace {
24 constexpr int HEX_BASE = 16;
25 }
26
27 FTRACE_NS_BEGIN
GetInstance()28 PrintkFormatsParser& PrintkFormatsParser::GetInstance()
29 {
30 static PrintkFormatsParser instance;
31 return instance;
32 }
33
PrintkFormatsParser()34 PrintkFormatsParser::PrintkFormatsParser()
35 {
36 PROFILER_LOG_INFO(LOG_CORE, "PrintkFormatsParser create!");
37 }
38
~PrintkFormatsParser()39 PrintkFormatsParser::~PrintkFormatsParser()
40 {
41 PROFILER_LOG_INFO(LOG_CORE, "PrintkFormatsParser destroy!");
42 }
43
GetSymbol(uint64_t addr)44 std::string PrintkFormatsParser::GetSymbol(uint64_t addr)
45 {
46 auto it = printkFormats_.find(addr);
47 if (it != printkFormats_.end()) {
48 return it->second;
49 }
50 return "NULL";
51 }
52
Parse(const std::string & printkFormats)53 bool PrintkFormatsParser::Parse(const std::string& printkFormats)
54 {
55 std::stringstream sin(printkFormats);
56 std::string line;
57 uint64_t addr = 0;
58 char seperator = ':';
59 std::string symbol = "";
60 while (std::getline(sin, line)) {
61 auto pos = line.find(seperator);
62 if (pos != std::string::npos) {
63 std::string addrStr = StringUtils::Strip(line.substr(0, pos));
64 addr = static_cast<uint64_t>(strtoull(addrStr.c_str(), nullptr, HEX_BASE));
65
66 symbol = StringUtils::Strip(line.substr(pos + 1));
67 if (symbol.back() == '"') {
68 symbol.pop_back();
69 }
70 if (symbol.front() == '"') {
71 symbol = symbol.substr(1);
72 }
73 printkFormats_[addr] = symbol;
74 }
75 }
76 return printkFormats_.size() > 0;
77 }
78 FTRACE_NS_END