1 /* 2 * Copyright (C) 2020 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 #pragma once 18 19 #include <optional> 20 #include <string> 21 #include <vector> 22 23 namespace simpleperf { 24 25 class EventSelectionSet; 26 27 struct ProbeEvent { 28 std::string group_name; 29 std::string event_name; 30 }; 31 32 // Add kprobe events in /sys/kernel/debug/tracing/kprobe_events, and 33 // delete them in ProbeEvents::clear(). 34 class ProbeEvents { 35 public: ProbeEvents(EventSelectionSet & event_selection_set)36 ProbeEvents(EventSelectionSet& event_selection_set) : event_selection_set_(event_selection_set) {} 37 ~ProbeEvents(); 38 39 static bool ParseKprobeEventName(const std::string& kprobe_cmd, ProbeEvent* event); 40 bool IsKprobeSupported(); 41 42 // Accept kprobe cmd as in <linux_kernel>/Documentation/trace/kprobetrace.rst. 43 bool AddKprobe(const std::string& kprobe_cmd); 44 // If not exist, add a kprobe tracepoint at the function entry. 45 bool CreateProbeEventIfNotExist(const std::string& event_name); 46 47 private: 48 bool IsProbeEvent(const std::string& event_name); IsEmpty()49 bool IsEmpty() const { return kprobe_events_.empty(); } 50 void Clear(); 51 bool WriteKprobeCmd(const std::string& kprobe_cmd); 52 53 EventSelectionSet& event_selection_set_; 54 std::vector<ProbeEvent> kprobe_events_; 55 std::optional<std::string> kprobe_control_path_; 56 }; 57 58 } // namespace simpleperf 59