blob: cb6a862c9f80d2727b33c8460d6c7e9ae73fecdb [file] [log] [blame]
Yabin Cui4f41df62016-06-01 17:29:06 -07001/*
2 * Copyright (C) 2016 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 "tracing.h"
18
Yabin Cuia9cfcde2020-06-29 14:04:44 -070019#include <stdlib.h>
Yabin Cui4f41df62016-06-01 17:29:06 -070020#include <string.h>
21
22#include <map>
Yabin Cuia9cfcde2020-06-29 14:04:44 -070023#include <optional>
Yabin Cui15dd5f72020-08-24 14:21:55 -070024#include <regex>
Yabin Cui4f41df62016-06-01 17:29:06 -070025#include <string>
26#include <vector>
27
28#include <android-base/file.h>
29#include <android-base/logging.h>
Yabin Cui8cd92332018-03-21 14:34:29 -070030#include <android-base/parseint.h>
Yabin Cui4f41df62016-06-01 17:29:06 -070031#include <android-base/stringprintf.h>
32#include <android-base/strings.h>
33
Yabin Cuia22efeb2020-01-29 13:27:33 -080034#include "environment.h"
Yabin Cui4f41df62016-06-01 17:29:06 -070035#include "perf_event.h"
36#include "utils.h"
37
Yabin Cui15dd5f72020-08-24 14:21:55 -070038using android::base::Split;
39using android::base::StartsWith;
40
Yabin Cuifaa7b922021-01-11 17:35:57 -080041namespace simpleperf {
42
Yabin Cui15dd5f72020-08-24 14:21:55 -070043template <>
44void MoveFromBinaryFormat(std::string& data, const char*& p) {
45 data.clear();
46 while (*p != '\0') {
47 data.push_back(*p++);
48 }
49 p++;
50}
51
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020052const char TRACING_INFO_MAGIC[10] = {23, 8, 68, 't', 'r', 'a', 'c', 'i', 'n', 'g'};
Yabin Cui4f41df62016-06-01 17:29:06 -070053
54template <class T>
55void AppendData(std::vector<char>& data, const T& s) {
56 const char* p = reinterpret_cast<const char*>(&s);
57 data.insert(data.end(), p, p + sizeof(T));
58}
59
60static void AppendData(std::vector<char>& data, const char* s) {
61 data.insert(data.end(), s, s + strlen(s) + 1);
62}
63
64template <>
65void AppendData(std::vector<char>& data, const std::string& s) {
66 data.insert(data.end(), s.c_str(), s.c_str() + s.size() + 1);
67}
68
Yabin Cui4f41df62016-06-01 17:29:06 -070069static void AppendFile(std::vector<char>& data, const std::string& file,
70 uint32_t file_size_bytes = 8) {
71 if (file_size_bytes == 8) {
72 uint64_t file_size = file.size();
73 AppendData(data, file_size);
74 } else if (file_size_bytes == 4) {
75 uint32_t file_size = file.size();
76 AppendData(data, file_size);
77 }
78 data.insert(data.end(), file.begin(), file.end());
79}
80
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020081static void DetachFile(const char*& p, std::string& file, uint32_t file_size_bytes = 8) {
Yabin Cui4f41df62016-06-01 17:29:06 -070082 uint64_t file_size = ConvertBytesToValue(p, file_size_bytes);
83 p += file_size_bytes;
84 file.clear();
85 file.insert(file.end(), p, p + file_size);
86 p += file_size;
87}
88
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020089static bool ReadTraceFsFile(const std::string& path, std::string* content,
90 bool report_error = true) {
Yabin Cuia9cfcde2020-06-29 14:04:44 -070091 const char* tracefs_dir = GetTraceFsDir();
92 if (tracefs_dir == nullptr) {
93 if (report_error) {
94 LOG(ERROR) << "tracefs doesn't exist";
95 }
96 return false;
97 }
98 std::string full_path = tracefs_dir + path;
99 if (!android::base::ReadFileToString(full_path, content)) {
100 if (report_error) {
101 PLOG(ERROR) << "failed to read " << full_path;
102 }
103 return false;
104 }
105 return true;
106}
107
Yabin Cui4f41df62016-06-01 17:29:06 -0700108struct TraceType {
109 std::string system;
110 std::string name;
111};
112
113class TracingFile {
114 public:
115 TracingFile();
116 bool RecordHeaderFiles();
117 void RecordFtraceFiles(const std::vector<TraceType>& trace_types);
118 bool RecordEventFiles(const std::vector<TraceType>& trace_types);
119 bool RecordKallsymsFile();
120 bool RecordPrintkFormatsFile();
121 std::vector<char> BinaryFormat() const;
122 void LoadFromBinary(const std::vector<char>& data);
123 void Dump(size_t indent) const;
124 std::vector<TracingFormat> LoadTracingFormatsFromEventFiles() const;
125 const std::string& GetKallsymsFile() const { return kallsyms_file; }
126 uint32_t GetPageSize() const { return page_size; }
127
128 private:
129 char magic[10];
130 std::string version;
131 char endian;
132 uint8_t size_of_long;
133 uint32_t page_size;
134 std::string header_page_file;
135 std::string header_event_file;
136
137 std::vector<std::string> ftrace_format_files;
138 // pair of system, format_file_data.
139 std::vector<std::pair<std::string, std::string>> event_format_files;
140
141 std::string kallsyms_file;
142 std::string printk_formats_file;
143};
144
145TracingFile::TracingFile() {
146 memcpy(magic, TRACING_INFO_MAGIC, sizeof(TRACING_INFO_MAGIC));
147 version = "0.5";
148 endian = 0;
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200149 size_of_long = static_cast<int>(sizeof(long)); // NOLINT(google-runtime-int)
Yabin Cuifaa7b922021-01-11 17:35:57 -0800150 page_size = static_cast<uint32_t>(simpleperf::GetPageSize());
Yabin Cui4f41df62016-06-01 17:29:06 -0700151}
152
153bool TracingFile::RecordHeaderFiles() {
Yabin Cuia22efeb2020-01-29 13:27:33 -0800154 return ReadTraceFsFile("/events/header_page", &header_page_file) &&
155 ReadTraceFsFile("/events/header_event", &header_event_file);
Yabin Cui4f41df62016-06-01 17:29:06 -0700156}
157
158void TracingFile::RecordFtraceFiles(const std::vector<TraceType>& trace_types) {
159 for (const auto& type : trace_types) {
Yabin Cui4f41df62016-06-01 17:29:06 -0700160 std::string format_data;
Yabin Cuia22efeb2020-01-29 13:27:33 -0800161 if (ReadTraceFsFile("/events/ftrace/" + type.name + "/format", &format_data, false)) {
162 ftrace_format_files.emplace_back(std::move(format_data));
Yabin Cui4f41df62016-06-01 17:29:06 -0700163 }
164 }
165}
166
167bool TracingFile::RecordEventFiles(const std::vector<TraceType>& trace_types) {
168 for (const auto& type : trace_types) {
Yabin Cui4f41df62016-06-01 17:29:06 -0700169 std::string format_data;
Yabin Cuia22efeb2020-01-29 13:27:33 -0800170 if (!ReadTraceFsFile("/events/" + type.system + "/" + type.name + "/format", &format_data)) {
Yabin Cui4f41df62016-06-01 17:29:06 -0700171 return false;
172 }
Yabin Cuia22efeb2020-01-29 13:27:33 -0800173 event_format_files.emplace_back(type.system, std::move(format_data));
Yabin Cui4f41df62016-06-01 17:29:06 -0700174 }
175 return true;
176}
177
178bool TracingFile::RecordPrintkFormatsFile() {
Yabin Cuia22efeb2020-01-29 13:27:33 -0800179 return ReadTraceFsFile("/printk_formats", &printk_formats_file);
Yabin Cui4f41df62016-06-01 17:29:06 -0700180}
181
182std::vector<char> TracingFile::BinaryFormat() const {
183 std::vector<char> ret;
184 ret.insert(ret.end(), magic, magic + sizeof(magic));
185 AppendData(ret, version);
186 ret.push_back(endian);
187 AppendData(ret, size_of_long);
188 AppendData(ret, page_size);
189 AppendData(ret, "header_page");
190 AppendFile(ret, header_page_file);
191 AppendData(ret, "header_event");
192 AppendFile(ret, header_event_file);
193 int count = static_cast<int>(ftrace_format_files.size());
194 AppendData(ret, count);
195 for (const auto& format : ftrace_format_files) {
196 AppendFile(ret, format);
197 }
198 count = static_cast<int>(event_format_files.size());
199 AppendData(ret, count);
200 for (const auto& pair : event_format_files) {
201 AppendData(ret, pair.first);
202 AppendData(ret, 1);
203 AppendFile(ret, pair.second);
204 }
205 AppendFile(ret, kallsyms_file, 4);
206 AppendFile(ret, printk_formats_file, 4);
207 return ret;
208}
209
210void TracingFile::LoadFromBinary(const std::vector<char>& data) {
211 const char* p = data.data();
212 const char* end = data.data() + data.size();
213 CHECK(memcmp(p, magic, sizeof(magic)) == 0);
214 p += sizeof(magic);
215 MoveFromBinaryFormat(version, p);
216 MoveFromBinaryFormat(endian, p);
217 MoveFromBinaryFormat(size_of_long, p);
218 MoveFromBinaryFormat(page_size, p);
219 std::string filename;
220 MoveFromBinaryFormat(filename, p);
221 CHECK_EQ(filename, "header_page");
222 DetachFile(p, header_page_file);
223 MoveFromBinaryFormat(filename, p);
224 CHECK_EQ(filename, "header_event");
225 DetachFile(p, header_event_file);
226 uint32_t count;
227 MoveFromBinaryFormat(count, p);
228 ftrace_format_files.resize(count);
229 for (uint32_t i = 0; i < count; ++i) {
230 DetachFile(p, ftrace_format_files[i]);
231 }
232 MoveFromBinaryFormat(count, p);
233 event_format_files.clear();
234 for (uint32_t i = 0; i < count; ++i) {
235 std::string system;
236 MoveFromBinaryFormat(system, p);
237 uint32_t count_in_system;
238 MoveFromBinaryFormat(count_in_system, p);
239 for (uint32_t i = 0; i < count_in_system; ++i) {
240 std::string format;
241 DetachFile(p, format);
242 event_format_files.push_back(std::make_pair(system, std::move(format)));
243 }
244 }
245 DetachFile(p, kallsyms_file, 4);
246 DetachFile(p, printk_formats_file, 4);
247 CHECK_EQ(p, end);
248}
249
250void TracingFile::Dump(size_t indent) const {
251 PrintIndented(indent, "tracing data:\n");
252 PrintIndented(indent + 1, "magic: ");
253 for (size_t i = 0; i < 3u; ++i) {
254 printf("0x%x ", magic[i]);
255 }
256 for (size_t i = 3; i < sizeof(magic); ++i) {
257 printf("%c", magic[i]);
258 }
259 printf("\n");
260 PrintIndented(indent + 1, "version: %s\n", version.c_str());
261 PrintIndented(indent + 1, "endian: %d\n", endian);
262 PrintIndented(indent + 1, "header_page:\n%s\n\n", header_page_file.c_str());
263 PrintIndented(indent + 1, "header_event:\n%s\n\n", header_event_file.c_str());
264 for (size_t i = 0; i < ftrace_format_files.size(); ++i) {
265 PrintIndented(indent + 1, "ftrace format file %zu/%zu:\n%s\n\n", i + 1,
266 ftrace_format_files.size(), ftrace_format_files[i].c_str());
267 }
268 for (size_t i = 0; i < event_format_files.size(); ++i) {
269 PrintIndented(indent + 1, "event format file %zu/%zu %s:\n%s\n\n", i + 1,
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200270 event_format_files.size(), event_format_files[i].first.c_str(),
Yabin Cui4f41df62016-06-01 17:29:06 -0700271 event_format_files[i].second.c_str());
272 }
273 PrintIndented(indent + 1, "kallsyms:\n%s\n\n", kallsyms_file.c_str());
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200274 PrintIndented(indent + 1, "printk_formats:\n%s\n\n", printk_formats_file.c_str());
Yabin Cui4f41df62016-06-01 17:29:06 -0700275}
276
277enum class FormatParsingState {
278 READ_NAME,
279 READ_ID,
280 READ_FIELDS,
281 READ_PRINTFMT,
282};
283
284// Parse lines like: field:char comm[16]; offset:8; size:16; signed:1;
285static TracingField ParseTracingField(const std::string& s) {
286 TracingField field;
Yabin Cui4f41df62016-06-01 17:29:06 -0700287 std::string name;
288 std::string value;
Yabin Cui15dd5f72020-08-24 14:21:55 -0700289 std::regex re(R"((\w+):(.+?);)");
290
291 std::sregex_iterator match_it(s.begin(), s.end(), re);
292 std::sregex_iterator match_end;
293 while (match_it != match_end) {
294 std::smatch match = *match_it++;
295 std::string name = match.str(1);
296 std::string value = match.str(2);
297
298 if (name == "field") {
299 std::string last_value_part = Split(value, " \t").back();
300
301 if (StartsWith(value, "__data_loc char[]")) {
302 // Parse value like "__data_loc char[] name".
303 field.name = last_value_part;
304 field.elem_count = 1;
305 field.is_dynamic = true;
306 } else if (auto left_bracket_pos = last_value_part.find('[');
307 left_bracket_pos != std::string::npos) {
308 // Parse value with brackets like "char comm[16]".
309 field.name = last_value_part.substr(0, left_bracket_pos);
310 field.elem_count = 1;
311 if (size_t right_bracket_pos = last_value_part.find(']', left_bracket_pos);
312 right_bracket_pos != std::string::npos) {
313 size_t len = right_bracket_pos - left_bracket_pos - 1;
314 size_t elem_count;
315 // Array size may not be a number, like field:u32 rates[IEEE80211_NUM_BANDS].
316 if (android::base::ParseUint(last_value_part.substr(left_bracket_pos + 1, len),
317 &elem_count)) {
318 field.elem_count = elem_count;
Yabin Cui8cd92332018-03-21 14:34:29 -0700319 }
Yabin Cui4f41df62016-06-01 17:29:06 -0700320 }
Yabin Cui15dd5f72020-08-24 14:21:55 -0700321 } else {
322 // Parse value like "int common_pid".
323 field.name = last_value_part;
324 field.elem_count = 1;
Yabin Cui4f41df62016-06-01 17:29:06 -0700325 }
Yabin Cui15dd5f72020-08-24 14:21:55 -0700326 } else if (name == "offset") {
327 field.offset = static_cast<size_t>(strtoull(value.c_str(), nullptr, 10));
328 } else if (name == "size") {
329 size_t size = static_cast<size_t>(strtoull(value.c_str(), nullptr, 10));
330 CHECK_EQ(size % field.elem_count, 0u);
331 field.elem_size = size / field.elem_count;
332 } else if (name == "signed") {
333 int is_signed = static_cast<int>(strtoull(value.c_str(), nullptr, 10));
334 field.is_signed = (is_signed == 1);
Yabin Cui4f41df62016-06-01 17:29:06 -0700335 }
336 }
337 return field;
338}
339
Yabin Cui15dd5f72020-08-24 14:21:55 -0700340TracingFormat ParseTracingFormat(const std::string& data) {
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700341 TracingFormat format;
Yabin Cui15dd5f72020-08-24 14:21:55 -0700342 std::vector<std::string> strs = Split(data, "\n");
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700343 FormatParsingState state = FormatParsingState::READ_NAME;
344 for (const auto& s : strs) {
345 if (state == FormatParsingState::READ_NAME) {
346 if (size_t pos = s.find("name:"); pos != std::string::npos) {
347 format.name = android::base::Trim(s.substr(pos + strlen("name:")));
348 state = FormatParsingState::READ_ID;
349 }
350 } else if (state == FormatParsingState::READ_ID) {
351 if (size_t pos = s.find("ID:"); pos != std::string::npos) {
352 format.id = strtoull(s.substr(pos + strlen("ID:")).c_str(), nullptr, 10);
353 state = FormatParsingState::READ_FIELDS;
354 }
355 } else if (state == FormatParsingState::READ_FIELDS) {
356 if (size_t pos = s.find("field:"); pos != std::string::npos) {
357 TracingField field = ParseTracingField(s);
358 format.fields.push_back(field);
359 }
360 }
361 }
362 return format;
363}
364
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200365std::vector<TracingFormat> TracingFile::LoadTracingFormatsFromEventFiles() const {
Yabin Cui4f41df62016-06-01 17:29:06 -0700366 std::vector<TracingFormat> formats;
367 for (const auto& pair : event_format_files) {
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700368 TracingFormat format = ParseTracingFormat(pair.second);
Yabin Cui4f41df62016-06-01 17:29:06 -0700369 format.system_name = pair.first;
Yabin Cui4f41df62016-06-01 17:29:06 -0700370 formats.push_back(format);
371 }
372 return formats;
373}
374
375Tracing::Tracing(const std::vector<char>& data) {
376 tracing_file_ = new TracingFile;
377 tracing_file_->LoadFromBinary(data);
378}
379
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200380Tracing::~Tracing() {
381 delete tracing_file_;
382}
Yabin Cui4f41df62016-06-01 17:29:06 -0700383
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200384void Tracing::Dump(size_t indent) {
385 tracing_file_->Dump(indent);
386}
Yabin Cui4f41df62016-06-01 17:29:06 -0700387
388TracingFormat Tracing::GetTracingFormatHavingId(uint64_t trace_event_id) {
389 if (tracing_formats_.empty()) {
390 tracing_formats_ = tracing_file_->LoadTracingFormatsFromEventFiles();
391 }
392 for (const auto& format : tracing_formats_) {
393 if (format.id == trace_event_id) {
394 return format;
395 }
396 }
397 LOG(FATAL) << "no tracing format for id " << trace_event_id;
398 return TracingFormat();
399}
400
401std::string Tracing::GetTracingEventNameHavingId(uint64_t trace_event_id) {
402 if (tracing_formats_.empty()) {
403 tracing_formats_ = tracing_file_->LoadTracingFormatsFromEventFiles();
404 }
405 for (const auto& format : tracing_formats_) {
406 if (format.id == trace_event_id) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200407 return android::base::StringPrintf("%s:%s", format.system_name.c_str(), format.name.c_str());
Yabin Cui4f41df62016-06-01 17:29:06 -0700408 }
409 }
410 return "";
411}
412
413const std::string& Tracing::GetKallsyms() const {
414 return tracing_file_->GetKallsymsFile();
415}
416
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200417uint32_t Tracing::GetPageSize() const {
418 return tracing_file_->GetPageSize();
419}
Yabin Cui4f41df62016-06-01 17:29:06 -0700420
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200421bool GetTracingData(const std::vector<const EventType*>& event_types, std::vector<char>* data) {
Yabin Cui4f41df62016-06-01 17:29:06 -0700422 data->clear();
423 std::vector<TraceType> trace_types;
424 for (const auto& type : event_types) {
Yabin Cui516a87c2018-03-26 17:34:00 -0700425 CHECK_EQ(static_cast<uint32_t>(PERF_TYPE_TRACEPOINT), type->type);
Yabin Cui877751b2016-06-13 18:03:47 -0700426 size_t pos = type->name.find(':');
427 TraceType trace_type;
428 trace_type.system = type->name.substr(0, pos);
429 trace_type.name = type->name.substr(pos + 1);
430 trace_types.push_back(trace_type);
Yabin Cui4f41df62016-06-01 17:29:06 -0700431 }
432 TracingFile tracing_file;
433 if (!tracing_file.RecordHeaderFiles()) {
434 return false;
435 }
436 tracing_file.RecordFtraceFiles(trace_types);
437 if (!tracing_file.RecordEventFiles(trace_types)) {
438 return false;
439 }
440 // Don't record /proc/kallsyms here, as it will be contained in
441 // KernelSymbolRecord.
442 if (!tracing_file.RecordPrintkFormatsFile()) {
443 return false;
444 }
445 *data = tracing_file.BinaryFormat();
446 return true;
447}
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700448
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700449namespace {
450
451// Briefly check if the filter format is acceptable by the kernel, which is described in
Yabin Cui84c55ff2020-07-09 14:14:22 -0700452// Documentation/trace/events.rst in the kernel. Also adjust quotes in string operands.
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700453//
454// filter := predicate_expr [logical_operator predicate_expr]*
455// predicate_expr := predicate | '!' predicate_expr | '(' filter ')'
456// predicate := field_name relational_operator value
457//
458// logical_operator := '&&' | '||'
459// relational_operator := numeric_operator | string_operator
460// numeric_operator := '==' | '!=' | '<' | '<=' | '>' | '>=' | '&'
461// string_operator := '==' | '!=' | '~'
462// value := int or string
Yabin Cui84c55ff2020-07-09 14:14:22 -0700463struct FilterFormatAdjuster {
464 FilterFormatAdjuster(bool use_quote) : use_quote(use_quote) {}
465
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700466 bool MatchFilter(const char*& p) {
467 bool ok = MatchPredicateExpr(p);
468 while (ok && *p != '\0') {
469 RemoveSpace(p);
470 if (strncmp(p, "||", 2) == 0 || strncmp(p, "&&", 2) == 0) {
Yabin Cui84c55ff2020-07-09 14:14:22 -0700471 CopyBytes(p, 2);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700472 ok = MatchPredicateExpr(p);
473 } else {
474 break;
475 }
476 }
477 RemoveSpace(p);
478 return ok;
479 }
480
481 void RemoveSpace(const char*& p) {
Yabin Cui84c55ff2020-07-09 14:14:22 -0700482 size_t i = 0;
483 while (isspace(p[i])) {
484 i++;
485 }
486 if (i > 0) {
487 CopyBytes(p, i);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700488 }
489 }
490
491 bool MatchPredicateExpr(const char*& p) {
492 RemoveSpace(p);
493 if (*p == '!') {
Yabin Cui84c55ff2020-07-09 14:14:22 -0700494 CopyBytes(p, 1);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700495 return MatchPredicateExpr(p);
496 }
497 if (*p == '(') {
Yabin Cui84c55ff2020-07-09 14:14:22 -0700498 CopyBytes(p, 1);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700499 bool ok = MatchFilter(p);
500 if (!ok) {
501 return false;
502 }
503 RemoveSpace(p);
504 if (*p != ')') {
505 return false;
506 }
Yabin Cui84c55ff2020-07-09 14:14:22 -0700507 CopyBytes(p, 1);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700508 return true;
509 }
510 return MatchPredicate(p);
511 }
512
513 bool MatchPredicate(const char*& p) {
514 return MatchFieldName(p) && MatchRelationalOperator(p) && MatchValue(p);
515 }
516
517 bool MatchFieldName(const char*& p) {
518 RemoveSpace(p);
519 std::string name;
Yabin Cui84c55ff2020-07-09 14:14:22 -0700520 for (size_t i = 0; isalnum(p[i]) || p[i] == '_'; i++) {
521 name.push_back(p[i]);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700522 }
Yabin Cui84c55ff2020-07-09 14:14:22 -0700523 CopyBytes(p, name.size());
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700524 if (name.empty()) {
525 return false;
526 }
527 used_fields.emplace(std::move(name));
528 return true;
529 }
530
531 bool MatchRelationalOperator(const char*& p) {
532 RemoveSpace(p);
533 // "==", "!=", "<", "<=", ">", ">=", "&", "~"
534 if (*p == '=' || *p == '!' || *p == '<' || *p == '>') {
535 if (p[1] == '=') {
Yabin Cui84c55ff2020-07-09 14:14:22 -0700536 CopyBytes(p, 2);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700537 return true;
538 }
539 }
540 if (*p == '<' || *p == '>' || *p == '&' || *p == '~') {
Yabin Cui84c55ff2020-07-09 14:14:22 -0700541 CopyBytes(p, 1);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700542 return true;
543 }
544 return false;
545 }
546
547 bool MatchValue(const char*& p) {
548 RemoveSpace(p);
Yabin Cui84c55ff2020-07-09 14:14:22 -0700549 // Match a string with quotes.
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700550 if (*p == '\'' || *p == '"') {
Yabin Cui84c55ff2020-07-09 14:14:22 -0700551 char quote = *p;
552 size_t len = 1;
553 while (p[len] != quote && p[len] != '\0') {
554 len++;
555 }
556 if (p[len] != quote) {
557 return false;
558 }
559 len++;
560 if (use_quote) {
561 CopyBytes(p, len);
562 } else {
563 p++;
564 CopyBytes(p, len - 2);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700565 p++;
566 }
Yabin Cui84c55ff2020-07-09 14:14:22 -0700567 return true;
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700568 }
569 // Match an int value.
570 char* end;
571 errno = 0;
572 if (*p == '-') {
573 strtoll(p, &end, 0);
574 } else {
575 strtoull(p, &end, 0);
576 }
Yabin Cui84c55ff2020-07-09 14:14:22 -0700577 if (errno == 0 && end != p) {
578 CopyBytes(p, end - p);
579 return true;
580 }
581 // Match a string without quotes, stopping at ), &&, || or space.
582 size_t len = 0;
583 while (p[len] != '\0' && strchr(")&| \t", p[len]) == nullptr) {
584 len++;
585 }
586 if (len == 0) {
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700587 return false;
588 }
Yabin Cui84c55ff2020-07-09 14:14:22 -0700589 if (use_quote) {
590 adjusted_filter += '"';
591 }
592 CopyBytes(p, len);
593 if (use_quote) {
594 adjusted_filter += '"';
595 }
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700596 return true;
597 }
598
Yabin Cui84c55ff2020-07-09 14:14:22 -0700599 void CopyBytes(const char*& p, size_t len) {
600 adjusted_filter.append(p, len);
601 p += len;
602 }
603
604 const bool use_quote;
605 std::string adjusted_filter;
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700606 FieldNameSet used_fields;
607};
608
609} // namespace
610
Yabin Cui84c55ff2020-07-09 14:14:22 -0700611std::optional<std::string> AdjustTracepointFilter(const std::string& filter, bool use_quote,
612 FieldNameSet* used_fields) {
613 FilterFormatAdjuster adjuster(use_quote);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700614 const char* p = filter.c_str();
Yabin Cui84c55ff2020-07-09 14:14:22 -0700615 if (!adjuster.MatchFilter(p) || *p != '\0') {
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700616 LOG(ERROR) << "format error in filter \"" << filter << "\" starting from \"" << p << "\"";
Yabin Cui84c55ff2020-07-09 14:14:22 -0700617 return std::nullopt;
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700618 }
Yabin Cui84c55ff2020-07-09 14:14:22 -0700619 *used_fields = std::move(adjuster.used_fields);
620 return std::move(adjuster.adjusted_filter);
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700621}
622
623std::optional<FieldNameSet> GetFieldNamesForTracepointEvent(const EventType& event) {
Yabin Cui15dd5f72020-08-24 14:21:55 -0700624 std::vector<std::string> strs = Split(event.name, ":");
Yabin Cuia9cfcde2020-06-29 14:04:44 -0700625 if (strs.size() != 2) {
626 return {};
627 }
628 std::string data;
629 if (!ReadTraceFsFile("/events/" + strs[0] + "/" + strs[1] + "/format", &data, false)) {
630 return {};
631 }
632 TracingFormat format = ParseTracingFormat(data);
633 FieldNameSet names;
634 for (auto& field : format.fields) {
635 names.emplace(std::move(field.name));
636 }
637 return names;
638}
Yabin Cui15dd5f72020-08-24 14:21:55 -0700639
640} // namespace simpleperf