• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 "src/traced/probes/ftrace/proto_translation_table.h"
18 
19 #include <regex.h>
20 #include <sys/utsname.h>
21 
22 #include <algorithm>
23 
24 #include "perfetto/ext/base/string_utils.h"
25 #include "perfetto/protozero/proto_utils.h"
26 #include "src/traced/probes/ftrace/event_info.h"
27 #include "src/traced/probes/ftrace/ftrace_procfs.h"
28 
29 #include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
30 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
31 #include "protos/perfetto/trace/ftrace/generic.pbzero.h"
32 
33 namespace perfetto {
34 
35 namespace {
36 
37 using protos::pbzero::GenericFtraceEvent;
38 using protozero::proto_utils::ProtoSchemaType;
39 
MakeFtracePageHeaderSpec(const std::vector<FtraceEvent::Field> & fields)40 ProtoTranslationTable::FtracePageHeaderSpec MakeFtracePageHeaderSpec(
41     const std::vector<FtraceEvent::Field>& fields) {
42   ProtoTranslationTable::FtracePageHeaderSpec spec;
43   for (const FtraceEvent::Field& field : fields) {
44     std::string name = GetNameFromTypeAndName(field.type_and_name);
45     if (name == "timestamp")
46       spec.timestamp = field;
47     else if (name == "commit")
48       spec.size = field;
49     else if (name == "overwrite")
50       spec.overwrite = field;
51     else if (name != "data")
52       PERFETTO_DFATAL("Invalid field in header spec: %s", name.c_str());
53   }
54   return spec;
55 }
56 
57 // Fallback used when the "header_page" is not readable.
58 // It uses a hard-coded header_page. The only caveat is that the size of the
59 // |commit| field depends on the kernel bit-ness. This function tries to infer
60 // that from the uname() and if that fails assumes that the kernel bitness
61 // matches the userspace bitness.
GuessFtracePageHeaderSpec()62 ProtoTranslationTable::FtracePageHeaderSpec GuessFtracePageHeaderSpec() {
63   ProtoTranslationTable::FtracePageHeaderSpec spec{};
64 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && __i386__
65   // local_t is arch-specific and models the largest size of an integer that is
66   // still atomic across bus transactions, exceptions and IRQ. On android x86
67   // this is always size 8
68   uint16_t commit_size = 8;
69 #else
70   uint16_t commit_size = sizeof(long);
71 
72   struct utsname sysinfo;
73   // If user space is 32-bit check the kernel to verify.
74   if (commit_size < 8 && uname(&sysinfo) == 0) {
75     // Arm returns armv# for its machine type. The first (and only currently)
76     // arm processor that supports 64bit is the armv8 series.
77     commit_size =
78         strstr(sysinfo.machine, "64") || strstr(sysinfo.machine, "armv8") ? 8
79                                                                           : 4;
80   }
81 #endif
82 
83   // header_page typically looks as follows on a 64-bit kernel:
84   // field: u64 timestamp; offset:0; size:8; signed:0;
85   // field: local_t commit; offset:8; size:8; signed:1;
86   // field: int overwrite; offset:8; size:1; signed:1;
87   // field: char data; offset:16; size:4080; signed:0;
88   //
89   // On a 32-bit kernel local_t is 32-bit wide and data starts @ offset 12.
90 
91   spec.timestamp = FtraceEvent::Field{"u64 timestamp", 0, 8, 0};
92   spec.size = FtraceEvent::Field{"local_t commit", 8, commit_size, 1};
93   spec.overwrite = FtraceEvent::Field{"int overwrite", 8, 1, 1};
94   return spec;
95 }
96 
BuildEventsDeque(const std::vector<Event> & events)97 const std::deque<Event> BuildEventsDeque(const std::vector<Event>& events) {
98   size_t largest_id = 0;
99   for (const Event& event : events) {
100     if (event.ftrace_event_id > largest_id)
101       largest_id = event.ftrace_event_id;
102   }
103   std::deque<Event> events_by_id;
104   events_by_id.resize(largest_id + 1);
105   for (const Event& event : events) {
106     events_by_id[event.ftrace_event_id] = event;
107   }
108   events_by_id.shrink_to_fit();
109   return events_by_id;
110 }
111 
112 // Merge the information from |ftrace_field| into |field| (mutating it).
113 // We should set the following fields: offset, size, ftrace field type and
114 // translation strategy.
MergeFieldInfo(const FtraceEvent::Field & ftrace_field,Field * field,const char * event_name_for_debug)115 bool MergeFieldInfo(const FtraceEvent::Field& ftrace_field,
116                     Field* field,
117                     const char* event_name_for_debug) {
118   PERFETTO_DCHECK(field->ftrace_name);
119   PERFETTO_DCHECK(field->proto_field_id);
120   PERFETTO_DCHECK(static_cast<int>(field->proto_field_type));
121   PERFETTO_DCHECK(!field->ftrace_offset);
122   PERFETTO_DCHECK(!field->ftrace_size);
123   PERFETTO_DCHECK(!field->ftrace_type);
124 
125   if (!InferFtraceType(ftrace_field.type_and_name, ftrace_field.size,
126                        ftrace_field.is_signed, &field->ftrace_type)) {
127     PERFETTO_FATAL(
128         "Failed to infer ftrace field type for \"%s.%s\" (type:\"%s\" "
129         "size:%d "
130         "signed:%d)",
131         event_name_for_debug, field->ftrace_name,
132         ftrace_field.type_and_name.c_str(), ftrace_field.size,
133         ftrace_field.is_signed);
134     return false;
135   }
136 
137   field->ftrace_offset = ftrace_field.offset;
138   field->ftrace_size = ftrace_field.size;
139 
140   if (!SetTranslationStrategy(field->ftrace_type, field->proto_field_type,
141                               &field->strategy)) {
142     PERFETTO_DLOG(
143         "Failed to find translation strategy for ftrace field \"%s.%s\" (%s -> "
144         "%s)",
145         event_name_for_debug, field->ftrace_name, ToString(field->ftrace_type),
146         protozero::proto_utils::ProtoSchemaToString(field->proto_field_type));
147     // TODO(hjd): Uncomment DCHECK when proto generation is fixed.
148     // PERFETTO_DFATAL("Failed to find translation strategy");
149     return false;
150   }
151 
152   return true;
153 }
154 
155 // For each field in |fields| find the matching field from |ftrace_fields| (by
156 // comparing ftrace_name) and copy the information from the FtraceEvent::Field
157 // into the Field (mutating it). If there is no matching field in
158 // |ftrace_fields| remove the Field from |fields|. Return the maximum observed
159 // 'field end' (offset + size).
MergeFields(const std::vector<FtraceEvent::Field> & ftrace_fields,std::vector<Field> * fields,const char * event_name_for_debug)160 uint16_t MergeFields(const std::vector<FtraceEvent::Field>& ftrace_fields,
161                      std::vector<Field>* fields,
162                      const char* event_name_for_debug) {
163   uint16_t fields_end = 0;
164 
165   // Loop over each Field in |fields| modifying it with information from the
166   // matching |ftrace_fields| field or removing it.
167   auto field = fields->begin();
168   while (field != fields->end()) {
169     bool success = false;
170     for (const FtraceEvent::Field& ftrace_field : ftrace_fields) {
171       if (GetNameFromTypeAndName(ftrace_field.type_and_name) !=
172           field->ftrace_name)
173         continue;
174 
175       success = MergeFieldInfo(ftrace_field, &*field, event_name_for_debug);
176 
177       uint16_t field_end = field->ftrace_offset + field->ftrace_size;
178       fields_end = std::max<uint16_t>(fields_end, field_end);
179 
180       break;
181     }
182     if (success) {
183       ++field;
184     } else {
185       field = fields->erase(field);
186     }
187   }
188   return fields_end;
189 }
190 
Contains(const std::string & haystack,const std::string & needle)191 bool Contains(const std::string& haystack, const std::string& needle) {
192   return haystack.find(needle) != std::string::npos;
193 }
194 
RegexError(int errcode,const regex_t * preg)195 std::string RegexError(int errcode, const regex_t* preg) {
196   char buf[64];
197   regerror(errcode, preg, buf, sizeof(buf));
198   return {buf, sizeof(buf)};
199 }
200 
Match(const char * string,const char * pattern)201 bool Match(const char* string, const char* pattern) {
202   regex_t re;
203   int ret = regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB);
204   if (ret != 0) {
205     PERFETTO_FATAL("regcomp: %s", RegexError(ret, &re).c_str());
206   }
207   ret = regexec(&re, string, 0, nullptr, 0);
208   regfree(&re);
209   return ret != REG_NOMATCH;
210 }
211 
212 // Set proto field type and id based on the ftrace type.
SetProtoType(FtraceFieldType ftrace_type,ProtoSchemaType * proto_type,uint32_t * proto_field_id)213 void SetProtoType(FtraceFieldType ftrace_type,
214                   ProtoSchemaType* proto_type,
215                   uint32_t* proto_field_id) {
216   switch (ftrace_type) {
217     case kFtraceCString:
218     case kFtraceFixedCString:
219     case kFtraceStringPtr:
220     case kFtraceDataLoc:
221       *proto_type = ProtoSchemaType::kString;
222       *proto_field_id = GenericFtraceEvent::Field::kStrValueFieldNumber;
223       break;
224     case kFtraceInt8:
225     case kFtraceInt16:
226     case kFtraceInt32:
227     case kFtracePid32:
228     case kFtraceCommonPid32:
229     case kFtraceInt64:
230       *proto_type = ProtoSchemaType::kInt64;
231       *proto_field_id = GenericFtraceEvent::Field::kIntValueFieldNumber;
232       break;
233     case kFtraceUint8:
234     case kFtraceUint16:
235     case kFtraceUint32:
236     case kFtraceBool:
237     case kFtraceDevId32:
238     case kFtraceDevId64:
239     case kFtraceUint64:
240     case kFtraceInode32:
241     case kFtraceInode64:
242     case kFtraceSymAddr64:
243       *proto_type = ProtoSchemaType::kUint64;
244       *proto_field_id = GenericFtraceEvent::Field::kUintValueFieldNumber;
245       break;
246     case kInvalidFtraceFieldType:
247       PERFETTO_FATAL("Unexpected ftrace field type");
248   }
249 }
250 
251 }  // namespace
252 
253 // This is similar but different from InferProtoType (see format_parser.cc).
254 // TODO(hjd): Fold FtraceEvent(::Field) into Event.
InferFtraceType(const std::string & type_and_name,size_t size,bool is_signed,FtraceFieldType * out)255 bool InferFtraceType(const std::string& type_and_name,
256                      size_t size,
257                      bool is_signed,
258                      FtraceFieldType* out) {
259   // Fixed length strings: e.g. "char foo[16]" we don't care about the number
260   // since we get the size as it's own field. Somewhat awkwardly these fields
261   // are both fixed size and null terminated meaning that we can't just drop
262   // them directly into the protobuf (since if the string is shorter than 15
263   // characters we want only the bit up to the null terminator).
264   if (Match(type_and_name.c_str(), R"(char [a-zA-Z_]+\[[0-9]+\])")) {
265     *out = kFtraceFixedCString;
266     return true;
267   }
268 
269   // String pointers: "__data_loc char[] foo" (as in
270   // 'cpufreq_interactive_boost').
271   // TODO(fmayer): Handle u32[], u8[], __u8[] as well.
272   if (Contains(type_and_name, "__data_loc char[] ")) {
273     if (size != 4) {
274       PERFETTO_ELOG("__data_loc with incorrect size: %s (%zd)",
275                     type_and_name.c_str(), size);
276       return false;
277     }
278     *out = kFtraceDataLoc;
279     return true;
280   }
281 
282   if (Contains(type_and_name, "char[] ")) {
283     *out = kFtraceStringPtr;
284     return true;
285   }
286   if (Contains(type_and_name, "char * ")) {
287     *out = kFtraceStringPtr;
288     return true;
289   }
290 
291   // Kernel addresses that need symbolization via kallsyms. Only 64-bit kernels
292   // are supported for now. 32-bit kernels seems to be going away.
293   if ((base::StartsWith(type_and_name, "void*") ||
294        base::StartsWith(type_and_name, "void *")) &&
295       size == 8) {
296     *out = kFtraceSymAddr64;
297     return true;
298   }
299 
300   // Variable length strings: "char foo" + size: 0 (as in 'print').
301   if (base::StartsWith(type_and_name, "char ") && size == 0) {
302     *out = kFtraceCString;
303     return true;
304   }
305 
306   if (base::StartsWith(type_and_name, "bool ")) {
307     *out = kFtraceBool;
308     return true;
309   }
310 
311   if (base::StartsWith(type_and_name, "ino_t ") ||
312       base::StartsWith(type_and_name, "i_ino ")) {
313     if (size == 4) {
314       *out = kFtraceInode32;
315       return true;
316     } else if (size == 8) {
317       *out = kFtraceInode64;
318       return true;
319     }
320   }
321 
322   if (base::StartsWith(type_and_name, "dev_t ")) {
323     if (size == 4) {
324       *out = kFtraceDevId32;
325       return true;
326     } else if (size == 8) {
327       *out = kFtraceDevId64;
328       return true;
329     }
330   }
331 
332   // Pids (as in 'sched_switch').
333   if (base::StartsWith(type_and_name, "pid_t ") && size == 4) {
334     *out = kFtracePid32;
335     return true;
336   }
337 
338   if (Contains(type_and_name, "common_pid") && size == 4) {
339     *out = kFtraceCommonPid32;
340     return true;
341   }
342 
343   // Ints of various sizes:
344   if (size == 1 && is_signed) {
345     *out = kFtraceInt8;
346     return true;
347   } else if (size == 1 && !is_signed) {
348     *out = kFtraceUint8;
349     return true;
350   } else if (size == 2 && is_signed) {
351     *out = kFtraceInt16;
352     return true;
353   } else if (size == 2 && !is_signed) {
354     *out = kFtraceUint16;
355     return true;
356   } else if (size == 4 && is_signed) {
357     *out = kFtraceInt32;
358     return true;
359   } else if (size == 4 && !is_signed) {
360     *out = kFtraceUint32;
361     return true;
362   } else if (size == 8 && is_signed) {
363     *out = kFtraceInt64;
364     return true;
365   } else if (size == 8 && !is_signed) {
366     *out = kFtraceUint64;
367     return true;
368   }
369 
370   PERFETTO_DLOG("Could not infer ftrace type for '%s'", type_and_name.c_str());
371   return false;
372 }
373 
374 // static
375 ProtoTranslationTable::FtracePageHeaderSpec
DefaultPageHeaderSpecForTesting()376 ProtoTranslationTable::DefaultPageHeaderSpecForTesting() {
377   std::string page_header =
378       R"(	field: u64 timestamp;	offset:0;	size:8;	signed:0;
379 	field: local_t commit;	offset:8;	size:8;	signed:1;
380 	field: int overwrite;	offset:8;	size:1;	signed:1;
381 	field: char data;	offset:16;	size:4080;	signed:0;)";
382   std::vector<FtraceEvent::Field> page_header_fields;
383   PERFETTO_CHECK(ParseFtraceEventBody(std::move(page_header), nullptr,
384                                       &page_header_fields));
385   return MakeFtracePageHeaderSpec(page_header_fields);
386 }
387 
388 // static
Create(const FtraceProcfs * ftrace_procfs,std::vector<Event> events,std::vector<Field> common_fields)389 std::unique_ptr<ProtoTranslationTable> ProtoTranslationTable::Create(
390     const FtraceProcfs* ftrace_procfs,
391     std::vector<Event> events,
392     std::vector<Field> common_fields) {
393   bool common_fields_processed = false;
394   uint16_t common_fields_end = 0;
395 
396   std::string page_header = ftrace_procfs->ReadPageHeaderFormat();
397   bool ftrace_header_parsed = false;
398   FtracePageHeaderSpec header_spec{};
399   if (!page_header.empty()) {
400     std::vector<FtraceEvent::Field> page_header_fields;
401     ftrace_header_parsed = ParseFtraceEventBody(std::move(page_header), nullptr,
402                                                 &page_header_fields);
403     header_spec = MakeFtracePageHeaderSpec(page_header_fields);
404   }
405 
406   if (!ftrace_header_parsed) {
407     PERFETTO_LOG("Failed to parse ftrace page header, using fallback layout");
408     header_spec = GuessFtracePageHeaderSpec();
409   }
410 
411   for (Event& event : events) {
412     if (event.proto_field_id ==
413         protos::pbzero::FtraceEvent::kGenericFieldNumber) {
414       continue;
415     }
416     PERFETTO_DCHECK(event.name);
417     PERFETTO_DCHECK(event.group);
418     PERFETTO_DCHECK(event.proto_field_id);
419     PERFETTO_DCHECK(!event.ftrace_event_id);
420 
421     std::string contents =
422         ftrace_procfs->ReadEventFormat(event.group, event.name);
423     FtraceEvent ftrace_event;
424     if (contents.empty() || !ParseFtraceEvent(contents, &ftrace_event)) {
425       if (!strcmp(event.group, "ftrace") && !strcmp(event.name, "print")) {
426         // On some "user" builds of Android <P the ftrace/print event is not
427         // selinux-allowed. Thankfully this event is an always-on built-in
428         // so we don't need to write to its 'enable' file. However we need to
429         // know its binary layout to decode it, so we hardcode it.
430         ftrace_event.id = 5;  // Seems quite stable across kernels.
431         ftrace_event.name = "print";
432         // The only field we care about is:
433         // field:char buf; offset:16; size:0; signed:0;
434         ftrace_event.fields.emplace_back(
435             FtraceEvent::Field{"char buf", 16, 0, 0});
436       } else {
437         continue;
438       }
439     }
440 
441     event.ftrace_event_id = ftrace_event.id;
442 
443     if (!common_fields_processed) {
444       common_fields_end =
445           MergeFields(ftrace_event.common_fields, &common_fields, event.name);
446       common_fields_processed = true;
447     }
448 
449     uint16_t fields_end =
450         MergeFields(ftrace_event.fields, &event.fields, event.name);
451 
452     event.size = std::max<uint16_t>(fields_end, common_fields_end);
453   }
454 
455   events.erase(std::remove_if(events.begin(), events.end(),
456                               [](const Event& event) {
457                                 return event.proto_field_id == 0 ||
458                                        event.ftrace_event_id == 0;
459                               }),
460                events.end());
461 
462   // Pre-parse certain scheduler events, and see if the compile-time assumptions
463   // about their format hold for this kernel.
464   CompactSchedEventFormat compact_sched = ValidateFormatForCompactSched(events);
465 
466   std::string text = ftrace_procfs->ReadPrintkFormats();
467   PrintkMap printk_formats = ParsePrintkFormats(text);
468 
469   auto table = std::unique_ptr<ProtoTranslationTable>(new ProtoTranslationTable(
470       ftrace_procfs, events, std::move(common_fields), header_spec,
471       compact_sched, std::move(printk_formats)));
472   return table;
473 }
474 
ProtoTranslationTable(const FtraceProcfs * ftrace_procfs,const std::vector<Event> & events,std::vector<Field> common_fields,FtracePageHeaderSpec ftrace_page_header_spec,CompactSchedEventFormat compact_sched_format,PrintkMap printk_formats)475 ProtoTranslationTable::ProtoTranslationTable(
476     const FtraceProcfs* ftrace_procfs,
477     const std::vector<Event>& events,
478     std::vector<Field> common_fields,
479     FtracePageHeaderSpec ftrace_page_header_spec,
480     CompactSchedEventFormat compact_sched_format,
481     PrintkMap printk_formats)
482     : ftrace_procfs_(ftrace_procfs),
483       events_(BuildEventsDeque(events)),
484       largest_id_(events_.size() - 1),
485       common_fields_(std::move(common_fields)),
486       ftrace_page_header_spec_(ftrace_page_header_spec),
487       compact_sched_format_(compact_sched_format),
488       printk_formats_(printk_formats) {
489   for (const Event& event : events) {
490     group_and_name_to_event_[GroupAndName(event.group, event.name)] =
491         &events_.at(event.ftrace_event_id);
492     name_to_events_[event.name].push_back(&events_.at(event.ftrace_event_id));
493     group_to_events_[event.group].push_back(&events_.at(event.ftrace_event_id));
494   }
495 }
496 
GetOrCreateEvent(const GroupAndName & group_and_name)497 const Event* ProtoTranslationTable::GetOrCreateEvent(
498     const GroupAndName& group_and_name) {
499   const Event* event = GetEvent(group_and_name);
500   if (event)
501     return event;
502   // The ftrace event does not already exist so a new one will be created
503   // by parsing the format file.
504   std::string contents = ftrace_procfs_->ReadEventFormat(group_and_name.group(),
505                                                          group_and_name.name());
506   if (contents.empty())
507     return nullptr;
508   FtraceEvent ftrace_event = {};
509   ParseFtraceEvent(contents, &ftrace_event);
510 
511   // Ensure events vector is large enough
512   if (ftrace_event.id > largest_id_) {
513     events_.resize(ftrace_event.id + 1);
514     largest_id_ = ftrace_event.id;
515   }
516 
517   // Set known event variables
518   Event* e = &events_.at(ftrace_event.id);
519   e->ftrace_event_id = ftrace_event.id;
520   e->proto_field_id = protos::pbzero::FtraceEvent::kGenericFieldNumber;
521   e->name = InternString(group_and_name.name());
522   e->group = InternString(group_and_name.group());
523 
524   // Calculate size of common fields.
525   for (const FtraceEvent::Field& ftrace_field : ftrace_event.common_fields) {
526     uint16_t field_end = ftrace_field.offset + ftrace_field.size;
527     e->size = std::max(field_end, e->size);
528   }
529 
530   // For every field in the ftrace event, make a field in the generic event.
531   for (const FtraceEvent::Field& ftrace_field : ftrace_event.fields)
532     e->size = std::max(CreateGenericEventField(ftrace_field, *e), e->size);
533 
534   group_and_name_to_event_[group_and_name] = &events_.at(e->ftrace_event_id);
535   name_to_events_[e->name].push_back(&events_.at(e->ftrace_event_id));
536   group_to_events_[e->group].push_back(&events_.at(e->ftrace_event_id));
537 
538   return e;
539 }
540 
InternString(const std::string & str)541 const char* ProtoTranslationTable::InternString(const std::string& str) {
542   auto it_and_inserted = interned_strings_.insert(str);
543   return it_and_inserted.first->c_str();
544 }
545 
CreateGenericEventField(const FtraceEvent::Field & ftrace_field,Event & event)546 uint16_t ProtoTranslationTable::CreateGenericEventField(
547     const FtraceEvent::Field& ftrace_field,
548     Event& event) {
549   uint16_t field_end = ftrace_field.offset + ftrace_field.size;
550   std::string field_name = GetNameFromTypeAndName(ftrace_field.type_and_name);
551   if (field_name.empty()) {
552     PERFETTO_DLOG("Field: %s could not be added to the generic event.",
553                   ftrace_field.type_and_name.c_str());
554     return field_end;
555   }
556   event.fields.emplace_back();
557   Field* field = &event.fields.back();
558   field->ftrace_name = InternString(field_name);
559   if (!InferFtraceType(ftrace_field.type_and_name, ftrace_field.size,
560                        ftrace_field.is_signed, &field->ftrace_type)) {
561     PERFETTO_DLOG(
562         "Failed to infer ftrace field type for \"%s.%s\" (type:\"%s\" "
563         "size:%d "
564         "signed:%d)",
565         event.name, field->ftrace_name, ftrace_field.type_and_name.c_str(),
566         ftrace_field.size, ftrace_field.is_signed);
567     event.fields.pop_back();
568     return field_end;
569   }
570   SetProtoType(field->ftrace_type, &field->proto_field_type,
571                &field->proto_field_id);
572   field->ftrace_offset = ftrace_field.offset;
573   field->ftrace_size = ftrace_field.size;
574   // Proto type is set based on ftrace type so all fields should have a
575   // translation strategy.
576   bool success = SetTranslationStrategy(
577       field->ftrace_type, field->proto_field_type, &field->strategy);
578   PERFETTO_DCHECK(success);
579   return field_end;
580 }
581 
582 EventFilter::EventFilter() = default;
583 EventFilter::~EventFilter() = default;
584 
AddEnabledEvent(size_t ftrace_event_id)585 void EventFilter::AddEnabledEvent(size_t ftrace_event_id) {
586   if (ftrace_event_id >= enabled_ids_.size())
587     enabled_ids_.resize(ftrace_event_id + 1);
588   enabled_ids_[ftrace_event_id] = true;
589 }
590 
DisableEvent(size_t ftrace_event_id)591 void EventFilter::DisableEvent(size_t ftrace_event_id) {
592   if (ftrace_event_id >= enabled_ids_.size())
593     return;
594   enabled_ids_[ftrace_event_id] = false;
595 }
596 
IsEventEnabled(size_t ftrace_event_id) const597 bool EventFilter::IsEventEnabled(size_t ftrace_event_id) const {
598   if (ftrace_event_id == 0 || ftrace_event_id >= enabled_ids_.size())
599     return false;
600   return enabled_ids_[ftrace_event_id];
601 }
602 
GetEnabledEvents() const603 std::set<size_t> EventFilter::GetEnabledEvents() const {
604   std::set<size_t> enabled;
605   for (size_t i = 0; i < enabled_ids_.size(); i++) {
606     if (enabled_ids_[i]) {
607       enabled.insert(i);
608     }
609   }
610   return enabled;
611 }
612 
EnableEventsFrom(const EventFilter & other)613 void EventFilter::EnableEventsFrom(const EventFilter& other) {
614   size_t max_length = std::max(enabled_ids_.size(), other.enabled_ids_.size());
615   enabled_ids_.resize(max_length);
616   for (size_t i = 0; i < other.enabled_ids_.size(); i++) {
617     if (other.enabled_ids_[i])
618       enabled_ids_[i] = true;
619   }
620 }
621 
622 ProtoTranslationTable::~ProtoTranslationTable() = default;
623 
624 }  // namespace perfetto
625