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