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 "perfetto/ftrace_reader/ftrace_config.h" 18 19 #include "perfetto/base/logging.h" 20 21 namespace perfetto { 22 namespace { 23 24 bool IsGoodPunctuation(char c) { 25 return c == '_' || c == '.'; 26 } 27 28 bool IsValid(const std::string& str) { 29 for (size_t i = 0; i < str.size(); i++) { 30 if (!isalnum(str[i]) && !IsGoodPunctuation(str[i])) 31 return false; 32 } 33 return true; 34 } 35 36 } // namespace 37 38 std::set<std::string> FtraceEventsAsSet(const FtraceConfig& config) { 39 std::set<std::string> events; 40 for (const std::string& event : config.ftrace_events()) 41 events.insert(event); 42 return events; 43 } 44 45 FtraceConfig CreateFtraceConfig(std::set<std::string> names) { 46 FtraceConfig config; 47 for (const std::string& name : names) 48 *config.add_ftrace_events() = name; 49 return config; 50 } 51 52 bool RequiresAtrace(const FtraceConfig& config) { 53 return !config.atrace_categories().empty() || !config.atrace_apps().empty(); 54 } 55 56 bool ValidConfig(const FtraceConfig& config) { 57 for (const std::string& event_name : config.ftrace_events()) { 58 if (!IsValid(event_name)) { 59 PERFETTO_ELOG("Bad event name '%s'", event_name.c_str()); 60 return false; 61 } 62 } 63 for (const std::string& category : config.atrace_categories()) { 64 if (!IsValid(category)) { 65 PERFETTO_ELOG("Bad category name '%s'", category.c_str()); 66 return false; 67 } 68 } 69 for (const std::string& app : config.atrace_apps()) { 70 if (!IsValid(app)) { 71 PERFETTO_ELOG("Bad app '%s'", app.c_str()); 72 return false; 73 } 74 } 75 return true; 76 } 77 78 } // namespace perfetto 79