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 #ifndef FTRACE_PARSE_HELPERS_H 16 #define FTRACE_PARSE_HELPERS_H 17 #include <string> 18 #include <vector> 19 #include "ftrace_common_type.h" 20 21 FTRACE_NS_BEGIN 22 using VoidPtr = std::unique_ptr<void>::pointer; 23 24 class FtraceFieldParser { 25 public: ParseIntField(const FieldFormat & format,uint8_t data[],size_t size)26 template <typename T> static T ParseIntField(const FieldFormat& format, uint8_t data[], size_t size) 27 { 28 static_assert(std::is_integral<T>::value, "Integral type T required."); 29 T retval = {}; 30 auto end = data + size; 31 auto start = data + format.offset; 32 ReadData(start, end, &retval, format.size); 33 return retval; 34 } 35 36 template <typename T> ParseIntField(const std::vector<FieldFormat> & fields,size_t id,uint8_t data[],size_t size)37 static T ParseIntField(const std::vector<FieldFormat>& fields, size_t id, uint8_t data[], size_t size) 38 { 39 static_assert(std::is_integral<T>::value, "Integral type T required."); 40 if (fields.size() > id) { 41 return ParseIntField<T>(fields[id], data, size); 42 } 43 return {}; 44 } 45 46 static std::string ParseStrField(const FieldFormat& format, uint8_t data[], size_t size); 47 ParseStrField(const std::vector<FieldFormat> & fields,size_t id,uint8_t data[],size_t size)48 static std::string ParseStrField(const std::vector<FieldFormat>& fields, size_t id, uint8_t data[], size_t size) 49 { 50 if (fields.size() > id) { 51 return ParseStrField(fields[id], data, size); 52 } 53 return ""; 54 } 55 56 private: 57 static bool ReadData(const uint8_t start[], const uint8_t end[], VoidPtr out, size_t size); 58 }; 59 FTRACE_NS_END 60 #endif 61