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