xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/perf/mmap_record.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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/trace_processor/importers/perf/mmap_record.h"
18 
19 #include <optional>
20 
21 #include "perfetto/base/status.h"
22 #include "src/trace_processor/importers/perf/reader.h"
23 #include "src/trace_processor/importers/perf/record.h"
24 
25 namespace perfetto::trace_processor::perf_importer {
26 
Parse(const Record & record)27 base::Status MmapRecord::Parse(const Record& record) {
28   Reader reader(record.payload.copy());
29   if (!reader.Read(*static_cast<CommonMmapRecordFields*>(this)) ||
30       !reader.ReadCString(filename)) {
31     return base::ErrStatus("Failed to parse MMAP record");
32   }
33   cpu_mode = record.GetCpuMode();
34   return base::OkStatus();
35 }
36 
Parse(const Record & record)37 base::Status Mmap2Record::Parse(const Record& record) {
38   Reader reader(record.payload.copy());
39   if (!reader.Read(*static_cast<BaseMmap2Record*>(this)) ||
40       !reader.ReadCString(filename)) {
41     return base::ErrStatus("Failed to parse MMAP record");
42   }
43 
44   has_build_id = record.mmap_has_build_id();
45 
46   if (has_build_id && build_id.build_id_size >
47                           BaseMmap2Record::BuildIdFields::kMaxBuildIdSize) {
48     return base::ErrStatus(
49         "Invalid build_id_size in MMAP2 record. Expected <= %zu but found "
50         "%" PRIu8,
51         BaseMmap2Record::BuildIdFields::kMaxBuildIdSize,
52         build_id.build_id_size);
53   }
54 
55   cpu_mode = record.GetCpuMode();
56 
57   return base::OkStatus();
58 }
59 
GetBuildId() const60 std::optional<BuildId> Mmap2Record::GetBuildId() const {
61   return has_build_id ? std::make_optional(BuildId::FromRaw(std::string(
62                             build_id.build_id_buf, build_id.build_id_size)))
63                       : std::nullopt;
64 }
65 
66 }  // namespace perfetto::trace_processor::perf_importer
67