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 #include "thread_tree.h"
18 
19 #include <inttypes.h>
20 
21 #include <limits>
22 
23 #include <android-base/logging.h>
24 #include <android-base/stringprintf.h>
25 #include <android-base/strings.h>
26 
27 #include "perf_event.h"
28 #include "record.h"
29 #include "record_file.h"
30 #include "utils.h"
31 
32 namespace simpleperf {
33 namespace {
34 
35 // Real map file path depends on where the process can create files.
36 // For example, app can create files only in its data directory.
37 // Use normalized name inherited from pid instead.
GetSymbolMapDsoName(int pid)38 std::string GetSymbolMapDsoName(int pid) {
39   return android::base::StringPrintf("perf-%d.map", pid);
40 }
41 
42 }  // namespace
43 
SetThreadName(int pid,int tid,const std::string & comm)44 void ThreadTree::SetThreadName(int pid, int tid, const std::string& comm) {
45   ThreadEntry* thread = FindThreadOrNew(pid, tid);
46   if (comm != thread->comm) {
47     thread_comm_storage_.push_back(std::unique_ptr<std::string>(new std::string(comm)));
48     thread->comm = thread_comm_storage_.back()->c_str();
49   }
50 }
51 
ForkThread(int pid,int tid,int ppid,int ptid)52 void ThreadTree::ForkThread(int pid, int tid, int ppid, int ptid) {
53   ThreadEntry* parent = FindThreadOrNew(ppid, ptid);
54   ThreadEntry* child = FindThreadOrNew(pid, tid);
55   child->comm = parent->comm;
56   if (pid != ppid) {
57     // Copy maps from parent process.
58     if (child->maps->maps.empty()) {
59       *child->maps = *parent->maps;
60     } else {
61       CHECK_NE(child->maps, parent->maps);
62       for (auto& pair : parent->maps->maps) {
63         InsertMap(*child->maps, *pair.second);
64       }
65     }
66   }
67 }
68 
FindThread(int tid) const69 ThreadEntry* ThreadTree::FindThread(int tid) const {
70   if (auto it = thread_tree_.find(tid); it != thread_tree_.end()) {
71     return it->second.get();
72   }
73   return nullptr;
74 }
75 
FindThreadOrNew(int pid,int tid)76 ThreadEntry* ThreadTree::FindThreadOrNew(int pid, int tid) {
77   auto it = thread_tree_.find(tid);
78   if (it != thread_tree_.end() && pid == it->second.get()->pid) {
79     return it->second.get();
80   }
81   if (it != thread_tree_.end()) {
82     ExitThread(it->second.get()->pid, tid);
83   }
84   return CreateThread(pid, tid);
85 }
86 
CreateThread(int pid,int tid)87 ThreadEntry* ThreadTree::CreateThread(int pid, int tid) {
88   const char* comm;
89   std::shared_ptr<MapSet> maps;
90   if (pid == tid) {
91     comm = "unknown";
92     maps.reset(new MapSet);
93   } else {
94     // Share maps among threads in the same thread group.
95     ThreadEntry* process = FindThreadOrNew(pid, pid);
96     comm = process->comm;
97     maps = process->maps;
98   }
99   ThreadEntry* thread = new ThreadEntry{
100       pid,
101       tid,
102       comm,
103       maps,
104   };
105   auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread)));
106   CHECK(pair.second);
107   if (pid == tid) {
108     // If there is a symbol map dso for the process, add maps for the symbols.
109     auto name = GetSymbolMapDsoName(pid);
110     auto it = user_dso_tree_.find(name);
111     if (it != user_dso_tree_.end()) {
112       AddThreadMapsForDsoSymbols(thread, it->second.get());
113     }
114   }
115   return thread;
116 }
117 
ExitThread(int pid,int tid)118 void ThreadTree::ExitThread(int pid, int tid) {
119   auto it = thread_tree_.find(tid);
120   if (it != thread_tree_.end() && pid == it->second.get()->pid) {
121     thread_tree_.erase(it);
122   }
123 }
124 
AddKernelMap(uint64_t start_addr,uint64_t len,uint64_t pgoff,const std::string & filename)125 void ThreadTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
126                               const std::string& filename) {
127   // kernel map len can be 0 when record command is not run in supervisor mode.
128   if (len == 0) {
129     return;
130   }
131   Dso* dso;
132   if (android::base::StartsWith(filename, DEFAULT_KERNEL_MMAP_NAME)) {
133     dso = FindKernelDsoOrNew();
134   } else {
135     dso = FindKernelModuleDsoOrNew(filename, start_addr, start_addr + len);
136   }
137   InsertMap(kernel_maps_, MapEntry(start_addr, len, pgoff, dso, true));
138 }
139 
FindKernelDsoOrNew()140 Dso* ThreadTree::FindKernelDsoOrNew() {
141   if (!kernel_dso_) {
142     kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
143   }
144   return kernel_dso_.get();
145 }
146 
FindKernelModuleDsoOrNew(const std::string & filename,uint64_t memory_start,uint64_t memory_end)147 Dso* ThreadTree::FindKernelModuleDsoOrNew(const std::string& filename, uint64_t memory_start,
148                                           uint64_t memory_end) {
149   auto it = module_dso_tree_.find(filename);
150   if (it == module_dso_tree_.end()) {
151     module_dso_tree_[filename] =
152         Dso::CreateKernelModuleDso(filename, memory_start, memory_end, FindKernelDsoOrNew());
153     it = module_dso_tree_.find(filename);
154   }
155   return it->second.get();
156 }
157 
AddThreadMap(int pid,int tid,uint64_t start_addr,uint64_t len,uint64_t pgoff,const std::string & filename,uint32_t flags)158 void ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
159                               const std::string& filename, uint32_t flags) {
160   ThreadEntry* thread = FindThreadOrNew(pid, tid);
161   Dso* dso = FindUserDsoOrNew(filename, start_addr);
162   InsertMap(*thread->maps, MapEntry(start_addr, len, pgoff, dso, false, flags));
163 }
164 
AddThreadMapsForDsoSymbols(ThreadEntry * thread,Dso * dso)165 void ThreadTree::AddThreadMapsForDsoSymbols(ThreadEntry* thread, Dso* dso) {
166   const uint64_t page_size = GetPageSize();
167 
168   auto maps = thread->maps;
169 
170   uint64_t map_start = 0;
171   uint64_t map_end = 0;
172 
173   // Dso symbols are sorted by address. Walk and calculate containing pages.
174   for (const auto& sym : dso->GetSymbols()) {
175     uint64_t sym_map_start = AlignDown(sym.addr, page_size);
176     uint64_t sym_map_end = Align(sym.addr + sym.len, page_size);
177 
178     if (map_end < sym_map_start) {
179       if (map_start < map_end) {
180         InsertMap(*maps, MapEntry(map_start, map_end - map_start, map_start, dso, false, 0));
181       }
182       map_start = sym_map_start;
183     }
184     if (map_end < sym_map_end) {
185       map_end = sym_map_end;
186     }
187   }
188 
189   if (map_start < map_end) {
190     InsertMap(*maps, MapEntry(map_start, map_end - map_start, map_start, dso, false, 0));
191   }
192 }
193 
FindUserDsoOrNew(const std::string & filename,uint64_t start_addr,DsoType dso_type)194 Dso* ThreadTree::FindUserDsoOrNew(const std::string& filename, uint64_t start_addr,
195                                   DsoType dso_type) {
196   auto it = user_dso_tree_.find(filename);
197   if (it == user_dso_tree_.end()) {
198     bool force_64bit = start_addr > UINT_MAX;
199     std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, filename, force_64bit);
200     auto pair = user_dso_tree_.insert(std::make_pair(filename, std::move(dso)));
201     CHECK(pair.second);
202     it = pair.first;
203   }
204   return it->second.get();
205 }
206 
AddSymbolsForProcess(int pid,std::vector<Symbol> * symbols)207 void ThreadTree::AddSymbolsForProcess(int pid, std::vector<Symbol>* symbols) {
208   auto name = GetSymbolMapDsoName(pid);
209 
210   auto dso = FindUserDsoOrNew(name, 0, DSO_SYMBOL_MAP_FILE);
211   dso->SetSymbols(symbols);
212 
213   auto thread = FindThreadOrNew(pid, pid);
214   AddThreadMapsForDsoSymbols(thread, dso);
215 }
216 
AllocateMap(const MapEntry & entry)217 const MapEntry* ThreadTree::AllocateMap(const MapEntry& entry) {
218   map_storage_.emplace_back(new MapEntry(entry));
219   return map_storage_.back().get();
220 }
221 
RemoveFirstPartOfMapEntry(const MapEntry * entry,uint64_t new_start_addr)222 static MapEntry RemoveFirstPartOfMapEntry(const MapEntry* entry, uint64_t new_start_addr) {
223   MapEntry result = *entry;
224   result.start_addr = new_start_addr;
225   result.len -= result.start_addr - entry->start_addr;
226   result.pgoff += result.start_addr - entry->start_addr;
227   return result;
228 }
229 
RemoveSecondPartOfMapEntry(const MapEntry * entry,uint64_t new_len)230 static MapEntry RemoveSecondPartOfMapEntry(const MapEntry* entry, uint64_t new_len) {
231   MapEntry result = *entry;
232   result.len = new_len;
233   return result;
234 }
235 
236 // Insert a new map entry in a MapSet. If some existing map entries overlap the new map entry,
237 // then remove the overlapped parts.
InsertMap(MapSet & maps,const MapEntry & entry)238 void ThreadTree::InsertMap(MapSet& maps, const MapEntry& entry) {
239   std::map<uint64_t, const MapEntry*>& map = maps.maps;
240   auto it = map.lower_bound(entry.start_addr);
241   // Remove overlapped entry with start_addr < entry.start_addr.
242   if (it != map.begin()) {
243     auto it2 = it;
244     --it2;
245     if (it2->second->get_end_addr() > entry.get_end_addr()) {
246       map.emplace(entry.get_end_addr(),
247                   AllocateMap(RemoveFirstPartOfMapEntry(it2->second, entry.get_end_addr())));
248     }
249     if (it2->second->get_end_addr() > entry.start_addr) {
250       it2->second =
251           AllocateMap(RemoveSecondPartOfMapEntry(it2->second, entry.start_addr - it2->first));
252     }
253   }
254   // Remove overlapped entries with start_addr >= entry.start_addr.
255   while (it != map.end() && it->second->get_end_addr() <= entry.get_end_addr()) {
256     it = map.erase(it);
257   }
258   if (it != map.end() && it->second->start_addr < entry.get_end_addr()) {
259     map.emplace(entry.get_end_addr(),
260                 AllocateMap(RemoveFirstPartOfMapEntry(it->second, entry.get_end_addr())));
261     map.erase(it);
262   }
263   // Insert the new entry.
264   map.emplace(entry.start_addr, AllocateMap(entry));
265   maps.version++;
266 }
267 
FindMapByAddr(uint64_t addr) const268 const MapEntry* MapSet::FindMapByAddr(uint64_t addr) const {
269   auto it = maps.upper_bound(addr);
270   if (it != maps.begin()) {
271     --it;
272     if (it->second->get_end_addr() > addr) {
273       return it->second;
274     }
275   }
276   return nullptr;
277 }
278 
FindMap(const ThreadEntry * thread,uint64_t ip,bool in_kernel)279 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) {
280   const MapEntry* result = nullptr;
281   if (!in_kernel) {
282     result = thread->maps->FindMapByAddr(ip);
283   } else {
284     result = kernel_maps_.FindMapByAddr(ip);
285   }
286   return result != nullptr ? result : &unknown_map_;
287 }
288 
FindMap(const ThreadEntry * thread,uint64_t ip)289 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip) {
290   const MapEntry* result = thread->maps->FindMapByAddr(ip);
291   if (result != nullptr) {
292     return result;
293   }
294   result = kernel_maps_.FindMapByAddr(ip);
295   return result != nullptr ? result : &unknown_map_;
296 }
297 
FindSymbol(const MapEntry * map,uint64_t ip,uint64_t * pvaddr_in_file,Dso ** pdso)298 const Symbol* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip, uint64_t* pvaddr_in_file,
299                                      Dso** pdso) {
300   uint64_t vaddr_in_file = 0;
301   const Symbol* symbol = nullptr;
302   Dso* dso = map->dso;
303   if (map->flags & map_flags::PROT_JIT_SYMFILE_MAP) {
304     vaddr_in_file = ip;
305   } else {
306     vaddr_in_file = dso->IpToVaddrInFile(ip, map->start_addr, map->pgoff);
307   }
308   symbol = dso->FindSymbol(vaddr_in_file);
309   if (symbol == nullptr && dso->type() == DSO_KERNEL_MODULE) {
310     // If the ip address hits the vmlinux, or hits a kernel module, but we can't find its symbol
311     // in the kernel module file, then find its symbol in /proc/kallsyms or vmlinux.
312     vaddr_in_file = ip;
313     dso = FindKernelDsoOrNew();
314     symbol = dso->FindSymbol(vaddr_in_file);
315   }
316 
317   if (symbol == nullptr) {
318     if (show_ip_for_unknown_symbol_) {
319       std::string name = android::base::StringPrintf("%s%s[+%" PRIx64 "]",
320                                                      (show_mark_for_unknown_symbol_ ? "*" : ""),
321                                                      dso->FileName().c_str(), vaddr_in_file);
322       dso->AddUnknownSymbol(vaddr_in_file, name);
323       symbol = dso->FindSymbol(vaddr_in_file);
324       CHECK(symbol != nullptr);
325     } else {
326       symbol = &unknown_symbol_;
327     }
328   }
329   if (pvaddr_in_file != nullptr) {
330     *pvaddr_in_file = vaddr_in_file;
331   }
332   if (pdso != nullptr) {
333     *pdso = dso;
334   }
335   return symbol;
336 }
337 
FindKernelSymbol(uint64_t ip)338 const Symbol* ThreadTree::FindKernelSymbol(uint64_t ip) {
339   const MapEntry* map = FindMap(nullptr, ip, true);
340   return FindSymbol(map, ip, nullptr);
341 }
342 
ClearThreadAndMap()343 void ThreadTree::ClearThreadAndMap() {
344   thread_tree_.clear();
345   thread_comm_storage_.clear();
346   kernel_maps_.maps.clear();
347   map_storage_.clear();
348 }
349 
AddDsoInfo(FileFeature & file)350 void ThreadTree::AddDsoInfo(FileFeature& file) {
351   DsoType dso_type = file.type;
352   Dso* dso = nullptr;
353   if (dso_type == DSO_KERNEL) {
354     dso = FindKernelDsoOrNew();
355   } else if (dso_type == DSO_KERNEL_MODULE) {
356     dso = FindKernelModuleDsoOrNew(file.path, 0, 0);
357   } else {
358     dso = FindUserDsoOrNew(file.path, 0, dso_type);
359   }
360   dso->SetMinExecutableVaddr(file.min_vaddr, file.file_offset_of_min_vaddr);
361   dso->SetSymbols(&file.symbols);
362   for (uint64_t offset : file.dex_file_offsets) {
363     dso->AddDexFileOffset(offset);
364   }
365 }
366 
AddDexFileOffset(const std::string & file_path,uint64_t dex_file_offset)367 void ThreadTree::AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset) {
368   Dso* dso = FindUserDsoOrNew(file_path, 0, DSO_DEX_FILE);
369   dso->AddDexFileOffset(dex_file_offset);
370 }
371 
Update(const Record & record)372 void ThreadTree::Update(const Record& record) {
373   if (record.type() == PERF_RECORD_MMAP) {
374     const MmapRecord& r = *static_cast<const MmapRecord*>(&record);
375     if (r.InKernel()) {
376       AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
377     } else {
378       AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len, r.data->pgoff, r.filename);
379     }
380   } else if (record.type() == PERF_RECORD_MMAP2) {
381     const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record);
382     if (r.InKernel()) {
383       AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
384     } else {
385       std::string filename =
386           (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename;
387       AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len, r.data->pgoff, filename,
388                    r.data->prot);
389     }
390   } else if (record.type() == PERF_RECORD_COMM) {
391     const CommRecord& r = *static_cast<const CommRecord*>(&record);
392     SetThreadName(r.data->pid, r.data->tid, r.comm);
393   } else if (record.type() == PERF_RECORD_FORK) {
394     const ForkRecord& r = *static_cast<const ForkRecord*>(&record);
395     ForkThread(r.data->pid, r.data->tid, r.data->ppid, r.data->ptid);
396   } else if (record.type() == PERF_RECORD_EXIT) {
397     const ExitRecord& r = *static_cast<const ExitRecord*>(&record);
398     ExitThread(r.data->pid, r.data->tid);
399   } else if (record.type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
400     const auto& r = *static_cast<const KernelSymbolRecord*>(&record);
401     Dso::SetKallsyms(std::move(r.kallsyms));
402   }
403 }
404 
GetAllDsos() const405 std::vector<Dso*> ThreadTree::GetAllDsos() const {
406   std::vector<Dso*> result;
407   if (kernel_dso_) {
408     result.push_back(kernel_dso_.get());
409   }
410   for (auto& p : module_dso_tree_) {
411     result.push_back(p.second.get());
412   }
413   for (auto& p : user_dso_tree_) {
414     result.push_back(p.second.get());
415   }
416   result.push_back(unknown_dso_.get());
417   return result;
418 }
419 
420 }  // namespace simpleperf
421