aboutsummaryrefslogtreecommitdiff
path: root/src/traced/probes/ftrace/format_parser/format_parser.h
blob: a8a5cdb260d0a819ba1b63a13cd806a6d69972fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SRC_TRACED_PROBES_FTRACE_FORMAT_PARSER_FORMAT_PARSER_H_
#define SRC_TRACED_PROBES_FTRACE_FORMAT_PARSER_FORMAT_PARSER_H_

#include <stdint.h>
#include <string>

#include <iosfwd>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

namespace perfetto {

struct FtraceEvent {
  struct Field {
    std::string type_and_name;
    uint16_t offset;
    uint16_t size;
    bool is_signed;

    bool operator==(const Field& other) const {
      return std::tie(type_and_name, offset, size, is_signed) ==
             std::tie(other.type_and_name, other.offset, other.size,
                      other.is_signed);
    }
  };

  // When making changes / additions to these structs, remember that
  // proto_translation_table.cc has some fallback code for 'page_header' and
  // the 'print' event that fill this struct.
  std::string name;
  uint32_t id;
  std::vector<Field> common_fields;
  std::vector<Field> fields;
};

std::string GetNameFromTypeAndName(const std::string& type_and_name);

// Allow gtest to pretty print FtraceEvent::Field.
::std::ostream& operator<<(::std::ostream& os, const FtraceEvent::Field&);
void PrintTo(const FtraceEvent::Field& args, ::std::ostream* os);

// Parses only the body (i.e. contents of format) of an ftrace event format
// file, e.g.
//
//   field:unsigned short common_type;  offset:0;  size:2;  signed:0;
//   field:unsigned char common_flags;  offset:2;  size:1;  signed:0;
//   field:unsigned char common_preempt_count;  offset:3;  size:1;  signed:0;
//   field:int common_pid;  offset:4;  size:4;  signed:1;
//
//   field:dev_t dev;  offset:8;  size:4;  signed:0;
//   field:ino_t ino;  offset:12;  size:4;  signed:0;
//   field:ino_t dir;  offset:16;  size:4;  signed:0;
//   field:__u16 mode;  offset:20;  size:2;  signed:0;
bool ParseFtraceEventBody(std::string input,
                          std::vector<FtraceEvent::Field>* common_fields,
                          std::vector<FtraceEvent::Field>* fields,
                          bool disable_logging_for_testing = false);
// Parses ftrace event format file. This includes the headers specifying
// name and ID of the event, e.g.
//
// name: ext4_allocate_inode
// ID: 309
// format:
//   field:unsigned short common_type;  offset:0;  size:2;  signed:0;
//   field:unsigned char common_flags;  offset:2;  size:1;  signed:0;
//   field:unsigned char common_preempt_count;  offset:3;  size:1;  signed:0;
//   field:int common_pid;  offset:4;  size:4;  signed:1;
//
//   field:dev_t dev;  offset:8;  size:4;  signed:0;
//   field:ino_t ino;  offset:12;  size:4;  signed:0;
//   field:ino_t dir;  offset:16;  size:4;  signed:0;
//   field:__u16 mode;  offset:20;  size:2;  signed:0;
bool ParseFtraceEvent(std::string input, FtraceEvent* output = nullptr);

}  // namespace perfetto

#endif  // SRC_TRACED_PROBES_FTRACE_FORMAT_PARSER_FORMAT_PARSER_H_