• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum class ProbeEventType {
28   kKprobe,
29   kUprobe,
30 };
31 
32 struct ProbeEvent {
33   ProbeEventType type;
34   std::string group_name;
35   std::string event_name;
36 };
37 
38 // Add kprobe events in /sys/kernel/debug/tracing/kprobe_events, and
39 // delete them in ProbeEvents::clear().
40 class ProbeEvents {
41  public:
ProbeEvents(EventSelectionSet & event_selection_set)42   ProbeEvents(EventSelectionSet& event_selection_set) : event_selection_set_(event_selection_set) {}
43   ~ProbeEvents();
44 
45   static bool ParseProbeEventName(ProbeEventType type, const std::string& kprobe_cmd,
46                                   ProbeEvent* event);
47   bool IsProbeSupported(ProbeEventType type);
48 
49   // Accept kprobe cmd as in <linux_kernel>/Documentation/trace/kprobetrace.rst
50   // or uprobe cmd as in <linux_kernel>/Documentation/trace/uprobetracer.rst.
51   bool AddProbe(ProbeEventType type, const std::string& probe_cmd);
52   // If not exist, add a kprobe tracepoint at the function entry.
53   bool CreateProbeEventIfNotExist(const std::string& event_name);
54 
55  private:
56   bool IsKprobeEvent(const std::string& event_name);
IsEmpty()57   bool IsEmpty() const { return probe_events_.empty(); }
58   void Clear();
59   bool WriteProbeCmd(ProbeEventType type, const std::string& probe_cmd);
60   std::optional<std::string>& GetProbeControlPath(ProbeEventType type);
61 
62   EventSelectionSet& event_selection_set_;
63   std::vector<ProbeEvent> probe_events_;
64   std::optional<std::string> kprobe_control_path_;
65   std::optional<std::string> uprobe_control_path_;
66 };
67 
68 }  // namespace simpleperf
69