1 /*
2  * Copyright (C) 2017 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 #ifndef SRC_TRACED_PROBES_FTRACE_FORMAT_PARSER_FORMAT_PARSER_H_
18 #define SRC_TRACED_PROBES_FTRACE_FORMAT_PARSER_FORMAT_PARSER_H_
19 
20 #include <stdint.h>
21 #include <string>
22 
23 #include <iosfwd>
24 #include <iostream>
25 #include <string>
26 #include <tuple>
27 #include <vector>
28 
29 namespace perfetto {
30 
31 struct FtraceEvent {
32   struct Field {
33     std::string type_and_name;
34     uint16_t offset;
35     uint16_t size;
36     bool is_signed;
37 
38     bool operator==(const Field& other) const {
39       return std::tie(type_and_name, offset, size, is_signed) ==
40              std::tie(other.type_and_name, other.offset, other.size,
41                       other.is_signed);
42     }
43   };
44 
45   // When making changes / additions to these structs, remember that
46   // proto_translation_table.cc has some fallback code for 'page_header' and
47   // the 'print' event that fill this struct.
48   std::string name;
49   uint32_t id;
50   std::vector<Field> common_fields;
51   std::vector<Field> fields;
52 };
53 
54 std::string GetNameFromTypeAndName(const std::string& type_and_name);
55 
56 // Allow gtest to pretty print FtraceEvent::Field.
57 ::std::ostream& operator<<(::std::ostream& os, const FtraceEvent::Field&);
58 void PrintTo(const FtraceEvent::Field& args, ::std::ostream* os);
59 
60 // Parses only the body (i.e. contents of format) of an ftrace event format
61 // file, e.g.
62 //
63 //   field:unsigned short common_type;  offset:0;  size:2;  signed:0;
64 //   field:unsigned char common_flags;  offset:2;  size:1;  signed:0;
65 //   field:unsigned char common_preempt_count;  offset:3;  size:1;  signed:0;
66 //   field:int common_pid;  offset:4;  size:4;  signed:1;
67 //
68 //   field:dev_t dev;  offset:8;  size:4;  signed:0;
69 //   field:ino_t ino;  offset:12;  size:4;  signed:0;
70 //   field:ino_t dir;  offset:16;  size:4;  signed:0;
71 //   field:__u16 mode;  offset:20;  size:2;  signed:0;
72 bool ParseFtraceEventBody(std::string input,
73                           std::vector<FtraceEvent::Field>* common_fields,
74                           std::vector<FtraceEvent::Field>* fields,
75                           bool disable_logging_for_testing = false);
76 // Parses ftrace event format file. This includes the headers specifying
77 // name and ID of the event, e.g.
78 //
79 // name: ext4_allocate_inode
80 // ID: 309
81 // format:
82 //   field:unsigned short common_type;  offset:0;  size:2;  signed:0;
83 //   field:unsigned char common_flags;  offset:2;  size:1;  signed:0;
84 //   field:unsigned char common_preempt_count;  offset:3;  size:1;  signed:0;
85 //   field:int common_pid;  offset:4;  size:4;  signed:1;
86 //
87 //   field:dev_t dev;  offset:8;  size:4;  signed:0;
88 //   field:ino_t ino;  offset:12;  size:4;  signed:0;
89 //   field:ino_t dir;  offset:16;  size:4;  signed:0;
90 //   field:__u16 mode;  offset:20;  size:2;  signed:0;
91 bool ParseFtraceEvent(std::string input, FtraceEvent* output = nullptr);
92 
93 }  // namespace perfetto
94 
95 #endif  // SRC_TRACED_PROBES_FTRACE_FORMAT_PARSER_FORMAT_PARSER_H_
96