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 #ifndef SRC_TRACED_PROBES_FTRACE_FTRACE_PRINT_FILTER_H_ 18 #define SRC_TRACED_PROBES_FTRACE_FTRACE_PRINT_FILTER_H_ 19 20 #include <optional> 21 #include <string> 22 #include <vector> 23 24 #include "src/traced/probes/ftrace/proto_translation_table.h" 25 26 namespace perfetto { 27 28 struct Event; 29 30 namespace protos { 31 namespace gen { 32 class FtraceConfig_PrintFilter; 33 } // namespace gen 34 } // namespace protos 35 36 class FtracePrintFilter { 37 public: 38 // Builds a filter from a proto config. 39 explicit FtracePrintFilter(const protos::gen::FtraceConfig::PrintFilter&); 40 41 // Returns true if a string is allowed by this filter, false otherwise. 42 // The string begins at `start` and terminates after `size` bytes, or at the 43 // first '\0' byte, whichever comes first. 44 bool IsAllowed(const char* start, size_t size) const; 45 46 private: 47 struct Rule { 48 enum class Type { 49 kPrefixMatch, 50 kAtraceMessage, 51 }; 52 Type type; 53 std::string before_pid_part; 54 std::string prefix; 55 bool allow; 56 }; 57 58 static bool RuleMatches(const Rule&, const char* start, size_t size); 59 60 std::vector<Rule> rules_; 61 }; 62 63 class FtracePrintFilterConfig { 64 public: 65 static std::optional<FtracePrintFilterConfig> Create( 66 const protos::gen::FtraceConfig_PrintFilter&, 67 ProtoTranslationTable* table); 68 event_id()69 uint32_t event_id() const { return event_id_; } 70 71 // Returns true if the "ftrace/print" event (encoded from `start` to `end`) 72 // should be allowed. 73 // 74 // If the event should be allowed, or **if there was a problem parsing it** 75 // returns true. If the event should be disallowed (i.e. ignored), returns 76 // false. 77 bool IsEventInteresting(const uint8_t* start, const uint8_t* end) const; 78 79 private: 80 explicit FtracePrintFilterConfig(FtracePrintFilter filter); 81 FtracePrintFilter filter_; 82 uint32_t event_id_; 83 uint16_t event_size_; 84 uint16_t buf_field_offset_; 85 }; 86 87 } // namespace perfetto 88 89 #endif // SRC_TRACED_PROBES_FTRACE_FTRACE_PRINT_FILTER_H_ 90