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