1 /* 2 * Copyright (c) 2025 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 16 #ifndef KERNEL_SNAPSHOT_PARSER_H 17 #define KERNEL_SNAPSHOT_PARSER_H 18 19 #include <list> 20 21 #include "i_kernel_snapshot_parser.h" 22 #include "kernel_snapshot_trie.h" 23 24 namespace OHOS { 25 namespace HiviewDFX { 26 27 struct SnapshotCell { 28 SnapshotSection sectionKey; 29 const std::vector<std::string> &lines; 30 size_t start; 31 size_t end; 32 }; 33 34 class KernelSnapshotParser : public IKernelSnapshotParser { 35 public: 36 KernelSnapshotParser(); 37 std::vector<CrashMap> ParseAll(const std::string &cont) override; 38 private: 39 void SplitByNewLine(const std::string &str, std::vector<std::string> &lines); 40 std::vector<CrashMap> ParseSnapshot(std::vector<std::string> &snapshotLines); 41 void ParseSameSeqSnapshot(const std::vector<std::string> &lines, std::vector<CrashMap>& crashMaps); 42 CrashMap ParseSnapshotUnit(const std::vector<std::string> &lines, size_t &index); 43 44 bool PreProcessLine(std::string &line) const; 45 std::unordered_map<std::string, std::string> ConvertThreadInfoToPairs(const std::string &line); 46 47 bool ProcessTransStart(const std::vector<std::string>& lines, size_t& index, 48 std::string keyword, CrashMap& output); 49 50 void ParseTransStart(const SnapshotCell& cell, CrashMap &output); 51 void ParseThreadInfo(const SnapshotCell& cell, CrashMap &output); 52 void ParseStackBacktrace(const SnapshotCell& cell, CrashMap &output); 53 void ParseProcessRealName(const SnapshotCell& cell, CrashMap& output); 54 void ParseDefaultAction(const SnapshotCell& cell, CrashMap &output); 55 56 void ProcessSnapshotSection(const SnapshotCell& cell, CrashMap &output); 57 58 void InitializeParseTable(); 59 void InitializeKeywordTrie(); 60 using ParseFunction = void (KernelSnapshotParser::*)(const SnapshotCell&, CrashMap&); 61 std::unordered_map<SnapshotSection, ParseFunction> parseTable_; 62 KernelSnapshotTrie snapshotTrie_; 63 }; 64 } // namespace HiviewDFX 65 } // namespace OHOS 66 #endif 67