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 "printk_formats_processor.h"
16
17 #include <sstream>
18 #include "string_help.h"
19 #include "string_to_numerical.h"
20
21 namespace SysTuning {
22 namespace TraceStreamer {
GetInstance()23 PrintkFormatsProcessor &PrintkFormatsProcessor::GetInstance()
24 {
25 static PrintkFormatsProcessor instance;
26 return instance;
27 }
28
PrintkFormatsProcessor()29 PrintkFormatsProcessor::PrintkFormatsProcessor() {}
30
~PrintkFormatsProcessor()31 PrintkFormatsProcessor::~PrintkFormatsProcessor() {}
32
GetSymbol(uint64_t addr)33 std::string PrintkFormatsProcessor::GetSymbol(uint64_t addr)
34 {
35 auto iter = printkFormatsDict_.find(addr);
36 if (iter != printkFormatsDict_.end()) {
37 return iter->second;
38 }
39 auto addrStr = "0x" + base::number(addr, base::INTEGER_RADIX_TYPE_HEX);
40 TS_LOGD("can't find %s(addr) sym!", addrStr.data());
41 return addrStr;
42 }
43
HandlePrintkSyms(const std::string & printkFormats)44 bool PrintkFormatsProcessor::HandlePrintkSyms(const std::string &printkFormats)
45 {
46 std::stringstream prinktkFormatStream(printkFormats);
47 std::string curLine;
48 uint64_t addr = 0;
49 std::string curSymbol = "";
50 while (std::getline(prinktkFormatStream, curLine)) {
51 auto pos = curLine.find(':');
52 if (pos == std::string::npos) {
53 continue;
54 }
55 std::string addrInfo = base::Strip(curLine.substr(0, pos));
56 addr = base::StrToInt<uint64_t>(addrInfo, base::INTEGER_RADIX_TYPE_HEX).value();
57 curSymbol = base::Strip(curLine.substr(pos + 1));
58 if (curSymbol.back() == '"') {
59 curSymbol.pop_back();
60 }
61 if (curSymbol.front() == '"') {
62 curSymbol = curSymbol.substr(1);
63 }
64 printkFormatsDict_[addr] = curSymbol;
65 }
66 TS_LOGI("printkFormatsDict_ size = %zu", printkFormatsDict_.size());
67 return printkFormatsDict_.size() > 0;
68 }
Clear()69 void PrintkFormatsProcessor::Clear()
70 {
71 printkFormatsDict_.clear();
72 }
73 } // namespace TraceStreamer
74 } // namespace SysTuning
75