Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 25 | class EventSelectionSet; |
| 26 | |
Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 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: |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 36 | ProbeEvents(EventSelectionSet& event_selection_set) : event_selection_set_(event_selection_set) {} |
| 37 | ~ProbeEvents(); |
Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 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); |
Yabin Cui | 68534b9 | 2020-10-19 14:50:54 -0700 | [diff] [blame] | 44 | // If not exist, add a kprobe tracepoint at the function entry. |
| 45 | bool CreateProbeEventIfNotExist(const std::string& event_name); |
Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 46 | |
| 47 | private: |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 48 | bool IsProbeEvent(const std::string& event_name); |
| 49 | bool IsEmpty() const { return kprobe_events_.empty(); } |
| 50 | void Clear(); |
Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 51 | bool WriteKprobeCmd(const std::string& kprobe_cmd); |
| 52 | |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 53 | EventSelectionSet& event_selection_set_; |
Yabin Cui | 142acc8 | 2020-10-14 10:24:38 -0700 | [diff] [blame] | 54 | std::vector<ProbeEvent> kprobe_events_; |
| 55 | std::optional<std::string> kprobe_control_path_; |
| 56 | }; |
| 57 | |
| 58 | } // namespace simpleperf |