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