xref: /aosp_15_r20/external/perfetto/src/traced/probes/ftrace/ftrace_print_filter.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2022 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/ftrace_print_filter.h"
18 
19 #include <string.h>
20 
21 #include "protos/perfetto/config/ftrace/ftrace_config.gen.h"
22 #include "src/traced/probes/ftrace/event_info_constants.h"
23 
24 namespace perfetto {
25 namespace {
26 using ::perfetto::protos::gen::FtraceConfig;
27 
PrefixMatches(const std::string & prefix,const char * start,size_t size)28 bool PrefixMatches(const std::string& prefix, const char* start, size_t size) {
29   if (prefix.size() > size) {
30     return false;
31   }
32   return strncmp(prefix.c_str(), start, prefix.size()) == 0;
33 }
34 
AtraceMessageMatches(const std::string & before_pid_part,const std::string & after_pid_prefix,const char * start,size_t size)35 bool AtraceMessageMatches(const std::string& before_pid_part,
36                           const std::string& after_pid_prefix,
37                           const char* start,
38                           size_t size) {
39   base::StringView s(start, size);
40   if (!s.StartsWith(base::StringView(before_pid_part))) {
41     return false;
42   }
43   s = s.substr(before_pid_part.size());
44 
45   if (!s.StartsWith("|")) {
46     return false;
47   }
48   s = s.substr(1);
49 
50   size_t skip_pid_count = 0;
51   for (;; skip_pid_count++) {
52     if (skip_pid_count == s.size()) {
53       return false;
54     }
55     if (s.at(skip_pid_count) == '|') {
56       break;
57     }
58     if (!isdigit(s.at(skip_pid_count))) {
59       return false;
60     }
61   }
62   skip_pid_count++;
63   s = s.substr(skip_pid_count);
64 
65   return PrefixMatches(after_pid_prefix, s.data(), s.size());
66 }
67 
68 }  // namespace
69 
70 // static
RuleMatches(const Rule & rule,const char * start,size_t size)71 bool FtracePrintFilter::RuleMatches(const Rule& rule,
72                                     const char* start,
73                                     size_t size) {
74   switch (rule.type) {
75     case Rule::Type::kAtraceMessage:
76       return AtraceMessageMatches(rule.before_pid_part, rule.prefix, start,
77                                   size);
78     case Rule::Type::kPrefixMatch:
79       break;
80   }
81   return PrefixMatches(rule.prefix, start, size);
82 }
83 
FtracePrintFilter(const FtraceConfig::PrintFilter & conf)84 FtracePrintFilter::FtracePrintFilter(const FtraceConfig::PrintFilter& conf) {
85   rules_.reserve(conf.rules().size());
86   for (const FtraceConfig::PrintFilter::Rule& conf_rule : conf.rules()) {
87     Rule rule;
88     rule.allow = conf_rule.allow();
89     if (conf_rule.has_atrace_msg()) {
90       rule.type = Rule::Type::kAtraceMessage;
91       rule.before_pid_part = conf_rule.atrace_msg().type();
92       rule.prefix = conf_rule.atrace_msg().prefix();
93     } else {
94       rule.type = Rule::Type::kPrefixMatch;
95       rule.prefix = conf_rule.prefix();
96     }
97     rules_.push_back(std::move(rule));
98   }
99 }
100 
IsAllowed(const char * start,size_t size) const101 bool FtracePrintFilter::IsAllowed(const char* start, size_t size) const {
102   for (const Rule& rule : rules_) {
103     if (RuleMatches(rule, start, size)) {
104       return rule.allow;
105     }
106   }
107   return true;
108 }
109 
110 // static
Create(const protos::gen::FtraceConfig::PrintFilter & config,ProtoTranslationTable * table)111 std::optional<FtracePrintFilterConfig> FtracePrintFilterConfig::Create(
112     const protos::gen::FtraceConfig::PrintFilter& config,
113     ProtoTranslationTable* table) {
114   const Event* print_event = table->GetEvent(GroupAndName("ftrace", "print"));
115   if (!print_event) {
116     return std::nullopt;
117   }
118   const Field* buf_field = nullptr;
119   for (const Field& field : print_event->fields) {
120     if (strcmp(field.ftrace_name, "buf") == 0) {
121       buf_field = &field;
122       break;
123     }
124   }
125   if (!buf_field) {
126     return std::nullopt;
127   }
128 
129   if (buf_field->strategy != kCStringToString) {
130     return std::nullopt;
131   }
132   FtracePrintFilterConfig ret{FtracePrintFilter{config}};
133   ret.event_id_ = print_event->ftrace_event_id;
134   ret.event_size_ = print_event->size;
135   ret.buf_field_offset_ = buf_field->ftrace_offset;
136   return std::move(ret);
137 }
138 
FtracePrintFilterConfig(FtracePrintFilter filter)139 FtracePrintFilterConfig::FtracePrintFilterConfig(FtracePrintFilter filter)
140     : filter_(filter) {}
141 
IsEventInteresting(const uint8_t * start,const uint8_t * end) const142 bool FtracePrintFilterConfig::IsEventInteresting(const uint8_t* start,
143                                                  const uint8_t* end) const {
144   PERFETTO_DCHECK(start < end);
145   const size_t length = static_cast<size_t>(end - start);
146 
147   // If the end of the buffer is before the end of the event, give up.
148   if (event_size_ >= length) {
149     PERFETTO_DFATAL("Buffer overflowed.");
150     return true;
151   }
152 
153   const uint8_t* field_start = start + buf_field_offset_;
154   return filter_.IsAllowed(reinterpret_cast<const char*>(field_start),
155                            static_cast<size_t>(end - field_start));
156 }
157 
158 }  // namespace perfetto
159