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 bool GetEventFormat(uint32_t id, EventFormat& format); 47 48 int GetHeaderPageCommitSize(void); 49 bool ParseHeaderPageFormat(const std::string& formatDesc); 50 bool ParseEventFormat(const std::string& formatDesc, EventFormat& format); 51 bool ParseFieldFormat(const std::string& fieldLine, EventFormat& format); 52 bool ParseFieldType(const std::string& type, FieldFormat& field); 53 void PrintFieldInfo(const FieldFormat& info); 54 static void ParseProtoType(FieldFormat& field); 55 56 bool ParsePageHeader(); 57 58 // parse different page types 59 bool ParsePaddingData(const FtraceEventHeader& eventHeader); 60 bool ParseTimeExtend(const FtraceEventHeader& eventHeader); 61 bool ParseTimeStamp(const FtraceEventHeader& eventHeader); 62 bool ParseDataRecord(const FtraceEventHeader& eventHeader, FtraceCpuDetailMsg& cpuDetailMsg); 63 64 bool ParseFtraceEvent(FtraceEvent& ftraceEvent, uint8_t data[], size_t dataSize, const EventFormat& format); 65 bool ParseFtraceCommonFields(FtraceEvent& ftraceEvent, uint8_t data[], size_t dataSize, const EventFormat& format); 66 67 private: 68 DISALLOW_COPY_AND_MOVE(FtraceParser); 69 bool debugOn_ = false; 70 std::regex fixedCharArrayRegex_; 71 std::regex flexDataLocArrayRegex_; 72 std::unordered_map<uint32_t, EventFormat> eventDict_ = {}; 73 PageHeaderFormat pageHeaderFormat_ = {}; 74 std::string savedTgidPath_ = ""; 75 std::string savedCmdlines_ = ""; 76 77 uint8_t* cur_ = nullptr; 78 uint8_t* page_ = nullptr; // page start 79 uint8_t* endOfData_ = nullptr; // end of event data 80 uint8_t* endOfPage_ = nullptr; // end of full page 81 uint64_t timestamp_ = 0; 82 PageHeader pageHeader_ = {}; 83 84 std::unordered_map<int32_t, int32_t> tgidDict_ = {}; 85 std::unordered_map<int32_t, std::string> commDict_ = {}; 86 }; 87 FTRACE_NS_END 88 #endif 89