1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 size_t copySize = format.size; 33 if (sizeof(T) < format.size) { 34 copySize = sizeof(T); 35 } 36 ReadData(start, end, &retval, copySize); 37 return retval; 38 } 39 40 template <typename T> ParseVectorIntField(const std::vector<FieldFormat> & fields,size_t id,uint8_t data[],size_t size)41 static std::vector<T> ParseVectorIntField(const std::vector<FieldFormat>& fields, 42 size_t id, uint8_t data[], size_t size) 43 { 44 static_assert(std::is_integral<T>::value, "Integral type T required."); 45 if (fields.size() <= id) { 46 return {}; 47 } 48 49 FieldFormat format = fields[id]; 50 std::vector<T> retvalVec = {}; 51 size_t retvalSize = sizeof(unsigned long); 52 size_t count = format.size / retvalSize; 53 for (size_t i = 0; i < count; i++) { 54 auto start = data + format.offset + i * retvalSize; 55 auto end = start + retvalSize; 56 T retval = {}; 57 ReadData(start, end, &retval, retvalSize); 58 retvalVec.push_back(retval); 59 } 60 return retvalVec; 61 } 62 63 template <typename T> ParseIntField(const std::vector<FieldFormat> & fields,size_t id,uint8_t data[],size_t size)64 static T ParseIntField(const std::vector<FieldFormat>& fields, size_t id, uint8_t data[], size_t size) 65 { 66 static_assert(std::is_integral<T>::value, "Integral type T required."); 67 if (fields.size() > id) { 68 return ParseIntField<T>(fields[id], data, size); 69 } 70 return {}; 71 } 72 73 static std::string ParseStrField(const FieldFormat& format, uint8_t data[], size_t size); 74 ParseStrField(const std::vector<FieldFormat> & fields,size_t id,uint8_t data[],size_t size)75 static std::string ParseStrField(const std::vector<FieldFormat>& fields, size_t id, uint8_t data[], size_t size) 76 { 77 if (fields.size() > id) { 78 return ParseStrField(fields[id], data, size); 79 } 80 return ""; 81 } 82 83 private: 84 static bool ReadData(const uint8_t start[], const uint8_t end[], VoidPtr out, size_t size); 85 }; 86 FTRACE_NS_END 87 #endif 88