• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 SIMPLE_PERF_THREAD_TREE_H_
18 #define SIMPLE_PERF_THREAD_TREE_H_
19 
20 #include <stdint.h>
21 
22 #include <limits>
23 #include <map>
24 #include <memory>
25 #include <unordered_map>
26 
27 #include "dso.h"
28 
29 namespace simpleperf {
30 
31 struct Record;
32 
33 constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]";
34 constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon";
35 
36 namespace map_flags {
37 constexpr uint32_t PROT_JIT_SYMFILE_MAP = 0x4000;
38 }  // namespace map_flags
39 
40 struct MapEntry {
41   uint64_t start_addr;
42   uint64_t len;
43   uint64_t pgoff;
44   Dso* dso;
45   bool in_kernel;
46   uint32_t flags;
47 
48   MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff, Dso* dso, bool in_kernel,
49            uint32_t flags = 0)
start_addrMapEntry50       : start_addr(start_addr),
51         len(len),
52         pgoff(pgoff),
53         dso(dso),
54         in_kernel(in_kernel),
55         flags(flags) {}
MapEntryMapEntry56   MapEntry() {}
57 
get_end_addrMapEntry58   uint64_t get_end_addr() const { return start_addr + len; }
59 
ContainsMapEntry60   uint64_t Contains(uint64_t addr) const { return addr >= start_addr && addr < get_end_addr(); }
61 
GetVaddrInFileMapEntry62   uint64_t GetVaddrInFile(uint64_t addr) const {
63     if (Contains(addr)) {
64       return dso->IpToVaddrInFile(addr, start_addr, pgoff);
65     }
66     return 0;
67   }
68 };
69 
70 struct MapSet {
71   std::map<uint64_t, const MapEntry*> maps;  // Map from start_addr to a MapEntry.
72   uint64_t version = 0u;                     // incremented each time changing maps
73 
74   const MapEntry* FindMapByAddr(uint64_t addr) const;
75 };
76 
77 struct ThreadEntry {
78   int pid;
79   int tid;
80   const char* comm;              // It always refers to the latest comm.
81   std::shared_ptr<MapSet> maps;  // maps is shared by threads in the same process.
82 };
83 
84 struct FileFeature;
85 
86 // ThreadTree contains thread information (in ThreadEntry) and mmap information
87 // (in MapEntry) of the monitored threads. It also has interface to access
88 // symbols in executable binaries mapped in the monitored threads.
89 class ThreadTree {
90  public:
ThreadTree()91   ThreadTree()
92       : show_ip_for_unknown_symbol_(false),
93         show_mark_for_unknown_symbol_(false),
94         unknown_symbol_("unknown", 0, std::numeric_limits<unsigned long long>::max()) {
95     unknown_dso_ = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown");
96     unknown_map_ =
97         MapEntry(0, std::numeric_limits<unsigned long long>::max(), 0, unknown_dso_.get(), false);
98     // We can't dump comm for pid 0 from /proc, so add it's name here.
99     SetThreadName(0, 0, "swapper");
100   }
~ThreadTree()101   virtual ~ThreadTree() {}
102 
103   void SetThreadName(int pid, int tid, const std::string& comm);
104   void ForkThread(int pid, int tid, int ppid, int ptid);
105   virtual ThreadEntry* FindThread(int tid) const;
106   ThreadEntry* FindThreadOrNew(int pid, int tid);
107   void ExitThread(int pid, int tid);
108   void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, const std::string& filename);
GetKernelMaps()109   const MapSet& GetKernelMaps() { return kernel_maps_; }
110   void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
111                     const std::string& filename, uint32_t flags = 0);
112 
113   // Add process symbols that do not correspond to any real dso.
114   // For example, these might be symbols generated by a JIT.
115   void AddSymbolsForProcess(int pid, std::vector<Symbol>* symbols);
116 
117   const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel);
118   // Find map for an ip address when we don't know whether it is in kernel.
119   const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip);
120   const Symbol* FindSymbol(const MapEntry* map, uint64_t ip, uint64_t* pvaddr_in_file,
121                            Dso** pdso = nullptr);
122   const Symbol* FindKernelSymbol(uint64_t ip);
IsUnknownDso(const Dso * dso)123   bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); }
UnknownSymbol()124   const Symbol* UnknownSymbol() const { return &unknown_symbol_; }
125 
ShowIpForUnknownSymbol()126   void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; }
ShowMarkForUnknownSymbol()127   void ShowMarkForUnknownSymbol() {
128     show_mark_for_unknown_symbol_ = true;
129     unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX);
130   }
131   // Clear thread and map information, but keep loaded dso information. It saves
132   // the time to reload dso information.
133   void ClearThreadAndMap();
134   void AddDsoInfo(FileFeature& file);
135   void AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset);
136 
137   // Update thread tree with information provided by record.
138   void Update(const Record& record);
139 
140   std::vector<Dso*> GetAllDsos() const;
141 
142  private:
143   ThreadEntry* CreateThread(int pid, int tid);
144   Dso* FindKernelDsoOrNew();
145   Dso* FindKernelModuleDsoOrNew(const std::string& filename, uint64_t memory_start,
146                                 uint64_t memory_end);
147   Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0,
148                         DsoType dso_type = DSO_ELF_FILE);
149   const MapEntry* AllocateMap(const MapEntry& entry);
150   void InsertMap(MapSet& maps, const MapEntry& entry);
151 
152   // Add thread maps to cover symbols in dso.
153   void AddThreadMapsForDsoSymbols(ThreadEntry* thread, Dso* dso);
154 
155   std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
156   std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
157 
158   MapSet kernel_maps_;
159   std::vector<std::unique_ptr<MapEntry>> map_storage_;
160   MapEntry unknown_map_;
161 
162   std::unique_ptr<Dso> kernel_dso_;
163   std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_;
164   std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_;
165   std::unique_ptr<Dso> unknown_dso_;
166   bool show_ip_for_unknown_symbol_;
167   bool show_mark_for_unknown_symbol_;
168   Symbol unknown_symbol_;
169 };
170 
171 }  // namespace simpleperf
172 
173 #endif  // SIMPLE_PERF_THREAD_TREE_H_
174