blob: 616bafe424b0d56eb32989f8d7967c5c139cb2a6 [file] [log] [blame]
Yabin Cui142acc82020-10-14 10:24:38 -07001/*
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#include "ProbeEvents.h"
18
19#include <inttypes.h>
20
21#include <memory>
Yabin Cui142acc82020-10-14 10:24:38 -070022#include <string>
23
24#include <android-base/file.h>
25#include <android-base/logging.h>
26#include <android-base/parseint.h>
27#include <android-base/stringprintf.h>
28#include <android-base/strings.h>
29#include <android-base/unique_fd.h>
30
Yabin Cuif00f4fc2022-11-23 15:15:30 -080031#include "RegEx.h"
Yabin Cui142acc82020-10-14 10:24:38 -070032#include "environment.h"
Yabin Cui68534b92020-10-19 14:50:54 -070033#include "event_type.h"
Yabin Cui142acc82020-10-14 10:24:38 -070034#include "utils.h"
35
Yabin Cuifaa7b922021-01-11 17:35:57 -080036namespace simpleperf {
37
Yabin Cui142acc82020-10-14 10:24:38 -070038using android::base::ParseInt;
39using android::base::ParseUint;
40using android::base::Split;
41using android::base::StringPrintf;
42using android::base::unique_fd;
43using android::base::WriteStringToFd;
Yabin Cui142acc82020-10-14 10:24:38 -070044
Yabin Cui68534b92020-10-19 14:50:54 -070045static const std::string kKprobeEventPrefix = "kprobes:";
46
Yabin Cui142acc82020-10-14 10:24:38 -070047bool ProbeEvents::ParseKprobeEventName(const std::string& kprobe_cmd, ProbeEvent* event) {
48 // kprobe_cmd is in formats described in <kernel>/Documentation/trace/kprobetrace.rst:
49 // p[:[GRP/]EVENT] [MOD:]SYM[+offs]|MEMADDR [FETCHARGS]
50 // r[MAXACTIVE][:[GRP/]EVENT] [MOD:]SYM[+offs] [FETCHARGS]
51 std::vector<std::string> args = Split(kprobe_cmd, " ");
52 if (args.size() < 2) {
53 return false;
54 }
55
56 // Parse given name.
57 event->group_name = "kprobes";
Yabin Cuif00f4fc2022-11-23 15:15:30 -080058 auto name_reg = RegEx::Create(R"(:([a-zA-Z_][\w_]*/)?([a-zA-Z_][\w_]*))");
59 auto match = name_reg->SearchAll(args[0]);
60 if (match->IsValid()) {
61 if (match->GetField(1).length() > 0) {
62 event->group_name = match->GetField(1);
Yabin Cui142acc82020-10-14 10:24:38 -070063 event->group_name.pop_back();
64 }
Yabin Cuif00f4fc2022-11-23 15:15:30 -080065 event->event_name = match->GetField(2);
Yabin Cui142acc82020-10-14 10:24:38 -070066 return true;
67 }
68
69 // Generate name from MEMADDR.
70 char probe_type = args[0][0];
71 uint64_t kaddr;
72 if (ParseUint(args[1], &kaddr)) {
73 event->event_name = StringPrintf("%c_0x%" PRIx64, probe_type, kaddr);
74 return true;
75 }
76
77 // Generate name from [MOD:]SYM[+offs].
78 std::string symbol;
79 int64_t offset;
80 size_t split_pos = args[1].find_first_of("+-");
81 if (split_pos == std::string::npos) {
82 symbol = args[1];
83 offset = 0;
84 } else {
85 symbol = args[1].substr(0, split_pos);
86 if (!ParseInt(args[1].substr(split_pos), &offset) || offset < 0) {
87 return false;
88 }
89 }
90 std::string s = StringPrintf("%c_%s_%" PRId64, probe_type, symbol.c_str(), offset);
Yabin Cuif00f4fc2022-11-23 15:15:30 -080091 event->event_name = RegEx::Create(R"(\.|:)")->Replace(s, "_").value();
Yabin Cui142acc82020-10-14 10:24:38 -070092 return true;
93}
94
95bool ProbeEvents::IsKprobeSupported() {
96 if (!kprobe_control_path_.has_value()) {
97 kprobe_control_path_ = "";
98 if (const char* tracefs_dir = GetTraceFsDir(); tracefs_dir != nullptr) {
99 std::string path = std::string(tracefs_dir) + "/kprobe_events";
100 if (IsRegularFile(path)) {
101 kprobe_control_path_ = std::move(path);
102 }
103 }
104 }
105 return !kprobe_control_path_.value().empty();
106}
107
108bool ProbeEvents::AddKprobe(const std::string& kprobe_cmd) {
109 ProbeEvent event;
110 if (!ParseKprobeEventName(kprobe_cmd, &event)) {
111 LOG(ERROR) << "invalid kprobe cmd: " << kprobe_cmd;
112 return false;
113 }
114 if (!WriteKprobeCmd(kprobe_cmd)) {
115 return false;
116 }
117 kprobe_events_.emplace_back(std::move(event));
118 return true;
119}
120
Yabin Cui68534b92020-10-19 14:50:54 -0700121bool ProbeEvents::IsProbeEvent(const std::string& event_name) {
122 return android::base::StartsWith(event_name, kKprobeEventPrefix);
123}
124
125bool ProbeEvents::CreateProbeEventIfNotExist(const std::string& event_name) {
126 if (EventTypeManager::Instance().FindType(event_name) != nullptr) {
127 return true;
128 }
129 std::string function_name = event_name.substr(kKprobeEventPrefix.size());
130 return AddKprobe(StringPrintf("p:%s %s", function_name.c_str(), function_name.c_str()));
131}
132
Yabin Cui142acc82020-10-14 10:24:38 -0700133void ProbeEvents::Clear() {
134 for (const auto& kprobe_event : kprobe_events_) {
135 if (!WriteKprobeCmd("-:" + kprobe_event.group_name + "/" + kprobe_event.event_name)) {
136 LOG(WARNING) << "failed to delete kprobe event " << kprobe_event.group_name << ":"
137 << kprobe_event.event_name;
138 }
Yabin Cui68534b92020-10-19 14:50:54 -0700139 EventTypeManager::Instance().RemoveProbeType(kprobe_event.group_name + ":" +
140 kprobe_event.event_name);
Yabin Cui142acc82020-10-14 10:24:38 -0700141 }
142 kprobe_events_.clear();
143}
144
145bool ProbeEvents::WriteKprobeCmd(const std::string& kprobe_cmd) {
146 if (!IsKprobeSupported()) {
147 LOG(ERROR) << "kprobe events isn't supported by the kernel.";
148 return false;
149 }
150 const std::string& path = kprobe_control_path_.value();
151 unique_fd fd(open(path.c_str(), O_APPEND | O_WRONLY | O_CLOEXEC));
152 if (!fd.ok()) {
153 PLOG(ERROR) << "failed to open " << path;
154 return false;
155 }
156 if (!WriteStringToFd(kprobe_cmd, fd)) {
157 PLOG(ERROR) << "failed to write '" << kprobe_cmd << "' to " << path;
158 return false;
159 }
Yabin Cui142acc82020-10-14 10:24:38 -0700160 return true;
161}
162
163} // namespace simpleperf