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 * Description: FtraceParser class define 16 */ 17 #ifndef FTRACE_EVENT_CONTAINER_H 18 #define FTRACE_EVENT_CONTAINER_H 19 #include <memory> 20 #include <regex> 21 #include <string> 22 #include <vector> 23 #include "ftrace_common_type.h" 24 #include "ftrace_field_parser.h" 25 #include "printk_formats_parser.h" 26 #include "sub_event_parser.h" 27 28 FTRACE_NS_BEGIN 29 class FtraceParser { 30 public: 31 FtraceParser(); 32 ~FtraceParser(); 33 34 bool Init(); 35 bool SetupEvent(const std::string& type, const std::string& name); 36 37 bool ParsePerCpuStatus(PerCpuStats& stats, const std::string& perCpuStats); 38 bool ParsePage(FtraceCpuDetailMsg& cpuDetailMsg, uint8_t page[], size_t size); 39 40 bool ParseSavedTgid(const std::string& savedTgid); 41 bool ParseSavedCmdlines(const std::string& savedCmdlines); 42 43 void SetDebugOn(bool value); 44 45 private: 46 int GetHeaderPageCommitSize(void); 47 bool ParseHeaderPageFormat(const std::string& formatDesc); 48 bool ParseEventFormat(const std::string& formatDesc, EventFormat& format); 49 bool ParseFieldFormat(const std::string& fieldLine, EventFormat& format); 50 bool ParseFieldType(const std::string& type, FieldFormat& field); 51 void PrintFieldInfo(const FieldFormat& info); 52 static void ParseProtoType(FieldFormat& field); 53 54 bool ParsePageHeader(); 55 56 // parse different page types 57 bool ParsePaddingData(const FtraceEventHeader& eventHeader); 58 bool ParseTimeExtend(const FtraceEventHeader& eventHeader); 59 bool ParseTimeStamp(const FtraceEventHeader& eventHeader); 60 bool ParseDataRecord(const FtraceEventHeader& eventHeader, FtraceCpuDetailMsg& cpuDetailMsg); 61 62 bool ParseFtraceEvent(FtraceEvent& ftraceEvent, uint8_t data[], 63 size_t dataSize, const SubEventParser::ParseEventCtx* parseEventCtx); 64 bool ParseFtraceCommonFields(FtraceEvent& ftraceEvent, uint8_t data[], size_t dataSize, const EventFormat& format); 65 66 private: 67 DISALLOW_COPY_AND_MOVE(FtraceParser); 68 bool debugOn_ = false; 69 std::regex fixedCharArrayRegex_; 70 std::regex flexDataLocArrayRegex_; 71 PageHeaderFormat pageHeaderFormat_ = {}; 72 std::string savedTgidPath_ = ""; 73 std::string savedCmdlines_ = ""; 74 75 uint8_t* cur_ = nullptr; 76 uint8_t* page_ = nullptr; // page start 77 uint8_t* endOfData_ = nullptr; // end of event data 78 uint8_t* endOfPage_ = nullptr; // end of full page 79 uint64_t timestamp_ = 0; 80 PageHeader pageHeader_ = {}; 81 82 std::unordered_map<int32_t, int32_t> tgidDict_ = {}; 83 std::unordered_map<int32_t, std::string> commDict_ = {}; 84 }; 85 FTRACE_NS_END 86 #endif 87