1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. 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_FIELD_PROCESSOR_H 16 #define FTRACE_FIELD_PROCESSOR_H 17 #include <string> 18 #include <vector> 19 #include "ftrace_common_type.h" 20 #include "log.h" 21 22 namespace SysTuning { 23 namespace TraceStreamer { 24 using namespace OHOS::Profiler::Plugins; 25 class FtraceFieldProcessor { 26 public: 27 template <typename T> HandleIntField(const FieldFormat & format,uint8_t data[],size_t size)28 static T HandleIntField(const FieldFormat &format, uint8_t data[], size_t size) 29 { 30 static_assert(std::is_integral<T>::value, "T must be Integral type."); 31 T curValue = {}; 32 auto endPos = data + size; 33 auto startPos = data + format.offset; 34 HandleTypeData(startPos, endPos, &curValue, format.size); 35 return curValue; 36 } 37 38 template <typename T> HandleVectorIntField(const std::vector<FieldFormat> & fields,size_t id,uint8_t data[],size_t size)39 static std::vector<T> HandleVectorIntField(const std::vector<FieldFormat> &fields, 40 size_t id, 41 uint8_t data[], 42 size_t size) 43 { 44 static_assert(std::is_integral<T>::value, "T must be Integral type."); 45 TS_CHECK_TRUE_RET(fields.size() > id, {}); 46 FieldFormat format = fields[id]; 47 std::vector<T> curValueVec = {}; 48 size_t curValueSize = sizeof(unsigned long); 49 size_t valueNum = format.size / curValueSize; 50 for (size_t index = 0; index < valueNum; index++) { 51 auto start = data + format.offset + index * curValueSize; 52 auto end = start + curValueSize; 53 T retval = {}; 54 HandleTypeData(start, end, &retval, curValueSize); 55 curValueVec.push_back(retval); 56 } 57 return curValueVec; 58 } 59 60 template <typename T> HandleIntField(const std::vector<FieldFormat> & fields,size_t id,uint8_t data[],size_t size)61 static T HandleIntField(const std::vector<FieldFormat> &fields, size_t id, uint8_t data[], size_t size) 62 { 63 static_assert(std::is_integral<T>::value, "T must be Integral type."); 64 TS_CHECK_TRUE_RET(fields.size() > id, {}); 65 return HandleIntField<T>(fields[id], data, size); 66 } 67 68 static std::string HandleStrField(const FieldFormat &format, uint8_t data[], size_t size); 69 HandleStrField(const std::vector<FieldFormat> & fields,size_t id,uint8_t data[],size_t size)70 static std::string HandleStrField(const std::vector<FieldFormat> &fields, size_t id, uint8_t data[], size_t size) 71 { 72 TS_CHECK_TRUE_RET(fields.size() > id, ""); 73 return HandleStrField(fields[id], data, size); 74 } 75 76 private: 77 static bool HandleTypeData(const uint8_t *startPos, const uint8_t *endPos, void *out, size_t size); 78 }; 79 } // namespace TraceStreamer 80 } // namespace SysTuning 81 #endif // FTRACE_FIELD_PROCESSOR_H 82