xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/etm/target_memory.h (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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_ETM_TARGET_MEMORY_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_ETM_TARGET_MEMORY_H_
19 
20 #include <cstdint>
21 #include <memory>
22 #include <optional>
23 
24 #include "perfetto/ext/base/flat_hash_map.h"
25 #include "src/trace_processor/importers/etm/virtual_address_space.h"
26 #include "src/trace_processor/storage/trace_storage.h"
27 #include "src/trace_processor/types/destructible.h"
28 
29 namespace perfetto::trace_processor {
30 class AddressRange;
31 class TraceProcessorContext;
32 namespace etm {
33 
34 class MappingVersion;
35 
36 // This class represents tracks the memory contents for all processes.
37 // It can answer queries in the form: At timestamp t, what was the mapping at
38 // address x for the thread tid.
39 class TargetMemory : public Destructible {
40  public:
IsKernelAddress(uint64_t address)41   static bool IsKernelAddress(uint64_t address) {
42     return address & (1ull << 63);
43   }
44 
45   static void InitStorage(TraceProcessorContext* context);
Get(TraceStorage * storage)46   static const TargetMemory* Get(TraceStorage* storage) {
47     PERFETTO_DCHECK(storage->etm_target_memory() != nullptr);
48     return static_cast<const TargetMemory*>(storage->etm_target_memory());
49   }
50 
51   ~TargetMemory() override;
52 
storage()53   TraceStorage* storage() const { return storage_; }
54 
55   const MappingVersion* FindMapping(int64_t ts,
56                                     uint32_t tid,
57                                     uint64_t address) const;
58   const MappingVersion* FindMapping(int64_t ts,
59                                     uint32_t tid,
60                                     const AddressRange& range) const;
61 
62  private:
63   explicit TargetMemory(TraceProcessorContext* context);
64   VirtualAddressSpace* FindUserSpaceForTid(uint32_t tid) const;
65   std::optional<UniquePid> FindUpidForTid(uint32_t tid) const;
66 
67   TraceStorage* const storage_;
68   // Kernel memory is shared by all processes.
69   VirtualAddressSpace kernel_memory_;
70 
71   base::FlatHashMap<UniquePid, VirtualAddressSpace> user_memory_;
72 
73   // Cache for quick tid -> upid lookups.
74   // TODO(carlscab): This should probably live in `ProcessTracker`
75   mutable base::FlatHashMap<uint32_t, VirtualAddressSpace*> tid_to_space_;
76 };
77 
78 }  // namespace etm
79 }  // namespace perfetto::trace_processor
80 
81 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_ETM_TARGET_MEMORY_H_
82