• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/etm/target_memory_reader.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 
22 #include "perfetto/base/logging.h"
23 #include "src/trace_processor/importers/common/address_range.h"
24 #include "src/trace_processor/importers/etm/mapping_version.h"
25 #include "src/trace_processor/importers/etm/opencsd.h"
26 #include "src/trace_processor/importers/etm/target_memory.h"
27 
28 namespace perfetto::trace_processor::etm {
29 namespace {
Read(const MappingVersion & mapping,const AddressRange & range,uint8_t * dest)30 uint32_t Read(const MappingVersion& mapping,
31               const AddressRange& range,
32               uint8_t* dest) {
33   if (!mapping.data()) {
34     return 0;
35   }
36   memcpy(dest, mapping.data() + (range.start() - mapping.start()),
37          range.size());
38   return static_cast<uint32_t>(range.size());
39 }
40 }  // namespace
41 
ReadTargetMemory(const ocsd_vaddr_t address,const uint8_t,const ocsd_mem_space_acc_t mem_space,uint32_t * num_bytes,uint8_t * dest)42 ocsd_err_t TargetMemoryReader::ReadTargetMemory(
43     const ocsd_vaddr_t address,
44     const uint8_t,
45     const ocsd_mem_space_acc_t mem_space,
46     uint32_t* num_bytes,
47     uint8_t* dest) {
48   if (mem_space != OCSD_MEM_SPACE_EL1N || *num_bytes == 0) {
49     *num_bytes = 0;
50     return OCSD_OK;
51   }
52 
53   auto range = AddressRange::FromStartAndSize(address, *num_bytes);
54 
55   if (!cached_mapping_ || !cached_mapping_->Contains(range)) {
56     PERFETTO_CHECK(tid_);
57     cached_mapping_ = memory_->FindMapping(ts_, *tid_, range);
58   }
59 
60   if (!cached_mapping_) {
61     *num_bytes = 0;
62     return OCSD_OK;
63   }
64 
65   *num_bytes = Read(*cached_mapping_, range, dest);
66   return OCSD_OK;
67 }
68 
InvalidateMemAccCache(uint8_t)69 void TargetMemoryReader::InvalidateMemAccCache(uint8_t) {
70   cached_mapping_ = nullptr;
71 }
72 
SetPeContext(const ocsd_pe_context & cxt)73 void TargetMemoryReader::SetPeContext(const ocsd_pe_context& cxt) {
74   PERFETTO_CHECK(cxt.ctxt_id_valid);
75   InvalidateMemAccCache(0);
76   tid_ = cxt.context_id;
77 }
78 
FindMapping(uint64_t address) const79 const MappingVersion* TargetMemoryReader::FindMapping(uint64_t address) const {
80   if (cached_mapping_ && cached_mapping_->Contains(address)) {
81     return cached_mapping_;
82   }
83   PERFETTO_CHECK(tid_);
84 
85   return memory_->FindMapping(ts_, *tid_, address);
86 }
87 
SetTs(int64_t ts)88 void TargetMemoryReader::SetTs(int64_t ts) {
89   ts_ = ts;
90   InvalidateMemAccCache(0);
91 }
92 
93 }  // namespace perfetto::trace_processor::etm
94