1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/traced/probes/ftrace/event_info_constants.h"
18 
19 namespace perfetto {
20 using protozero::proto_utils::ProtoSchemaType;
21 
22 namespace {
StaticField(const char * ftrace_name,uint32_t proto_field_id,ProtoSchemaType proto_field_type)23 Field StaticField(const char* ftrace_name,
24                   uint32_t proto_field_id,
25                   ProtoSchemaType proto_field_type) {
26   Field field{};
27   field.ftrace_name = ftrace_name;
28   field.proto_field_id = proto_field_id;
29   field.proto_field_type = proto_field_type;
30   return field;
31 }
32 }  // namespace
33 
GetStaticCommonFieldsInfo()34 std::vector<Field> GetStaticCommonFieldsInfo() {
35   std::vector<Field> fields;
36 
37   fields.push_back(StaticField("common_pid", 2, ProtoSchemaType::kInt32));
38 
39   return fields;
40 }
41 
SetTranslationStrategy(FtraceFieldType ftrace,ProtoSchemaType proto,TranslationStrategy * out)42 bool SetTranslationStrategy(FtraceFieldType ftrace,
43                             ProtoSchemaType proto,
44                             TranslationStrategy* out) {
45   if (ftrace == kFtraceCommonPid32 && proto == ProtoSchemaType::kInt32) {
46     *out = kCommonPid32ToInt32;
47   } else if (ftrace == kFtraceCommonPid32 && proto == ProtoSchemaType::kInt64) {
48     *out = kCommonPid32ToInt64;
49   } else if (ftrace == kFtraceInode32 && proto == ProtoSchemaType::kUint64) {
50     *out = kInode32ToUint64;
51   } else if (ftrace == kFtraceInode64 && proto == ProtoSchemaType::kUint64) {
52     *out = kInode64ToUint64;
53   } else if (ftrace == kFtracePid32 && proto == ProtoSchemaType::kInt32) {
54     *out = kPid32ToInt32;
55   } else if (ftrace == kFtracePid32 && proto == ProtoSchemaType::kInt64) {
56     *out = kPid32ToInt64;
57   } else if (ftrace == kFtraceDevId32 && proto == ProtoSchemaType::kUint64) {
58     *out = kDevId32ToUint64;
59   } else if (ftrace == kFtraceDevId64 && proto == ProtoSchemaType::kUint64) {
60     *out = kDevId64ToUint64;
61   } else if (ftrace == kFtraceUint8 && proto == ProtoSchemaType::kUint32) {
62     *out = kUint8ToUint32;
63   } else if (ftrace == kFtraceUint8 && proto == ProtoSchemaType::kUint64) {
64     *out = kUint8ToUint64;
65   } else if (ftrace == kFtraceUint16 && proto == ProtoSchemaType::kUint32) {
66     *out = kUint16ToUint32;
67   } else if (ftrace == kFtraceUint16 && proto == ProtoSchemaType::kUint64) {
68     *out = kUint16ToUint64;
69   } else if (ftrace == kFtraceUint32 && proto == ProtoSchemaType::kUint32) {
70     *out = kUint32ToUint32;
71   } else if (ftrace == kFtraceUint32 && proto == ProtoSchemaType::kUint64) {
72     *out = kUint32ToUint64;
73   } else if (ftrace == kFtraceUint64 && proto == ProtoSchemaType::kUint64) {
74     *out = kUint64ToUint64;
75   } else if (ftrace == kFtraceInt8 && proto == ProtoSchemaType::kInt32) {
76     *out = kInt8ToInt32;
77   } else if (ftrace == kFtraceInt8 && proto == ProtoSchemaType::kInt64) {
78     *out = kInt8ToInt64;
79   } else if (ftrace == kFtraceInt16 && proto == ProtoSchemaType::kInt32) {
80     *out = kInt16ToInt32;
81   } else if (ftrace == kFtraceInt16 && proto == ProtoSchemaType::kInt64) {
82     *out = kInt16ToInt64;
83   } else if (ftrace == kFtraceInt32 && proto == ProtoSchemaType::kInt32) {
84     *out = kInt32ToInt32;
85   } else if (ftrace == kFtraceInt32 && proto == ProtoSchemaType::kInt64) {
86     *out = kInt32ToInt64;
87   } else if (ftrace == kFtraceInt64 && proto == ProtoSchemaType::kInt64) {
88     *out = kInt64ToInt64;
89   } else if (ftrace == kFtraceFixedCString &&
90              proto == ProtoSchemaType::kString) {
91     *out = kFixedCStringToString;
92   } else if (ftrace == kFtraceCString && proto == ProtoSchemaType::kString) {
93     *out = kCStringToString;
94   } else if (ftrace == kFtraceStringPtr && proto == ProtoSchemaType::kString) {
95     *out = kStringPtrToString;
96   } else if (ftrace == kFtraceBool && proto == ProtoSchemaType::kUint32) {
97     *out = kBoolToUint32;
98   } else if (ftrace == kFtraceBool && proto == ProtoSchemaType::kUint64) {
99     *out = kBoolToUint64;
100   } else if (ftrace == kFtraceDataLoc && proto == ProtoSchemaType::kString) {
101     *out = kDataLocToString;
102   } else if (ftrace == kFtraceSymAddr64 && proto == ProtoSchemaType::kUint64) {
103     *out = kFtraceSymAddr64ToUint64;
104   } else {
105     PERFETTO_DLOG("No translation strategy for '%s' -> '%s'", ToString(ftrace),
106                   ProtoSchemaToString(proto));
107     return false;
108   }
109   return true;
110 }
111 
112 }  // namespace perfetto
113