blob: 289872224a7b191f9743f45d1693442bd0013837 [file] [log] [blame]
Yabin Cui60a0ea92015-07-22 20:30:43 -07001/*
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
Yabin Cui15475e62016-07-14 13:26:19 -070019#include <inttypes.h>
20
Yabin Cuic8485602015-08-20 15:04:39 -070021#include <limits>
22
Elliott Hughes66dd09e2015-12-04 14:00:57 -080023#include <android-base/logging.h>
Yabin Cui15475e62016-07-14 13:26:19 -070024#include <android-base/stringprintf.h>
Yabin Cuic13ff892020-11-10 13:11:01 -080025#include <android-base/strings.h>
Yabin Cuic8485602015-08-20 15:04:39 -070026
Yabin Cui73d80782015-07-23 21:39:57 -070027#include "perf_event.h"
28#include "record.h"
Yabin Cui8d005de2020-10-21 13:48:53 -070029#include "record_file.h"
Evgeny Eltsin91dbae02020-08-27 15:46:09 +020030#include "utils.h"
Yabin Cui60a0ea92015-07-22 20:30:43 -070031
Yabin Cui040f7b42016-04-13 21:28:54 -070032namespace simpleperf {
Evgeny Eltsin3775b142020-08-28 13:00:04 +020033namespace {
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.
38std::string GetSymbolMapDsoName(int pid) {
39 return android::base::StringPrintf("perf-%d.map", pid);
40}
41
42} // namespace
43
Yabin Cuiaa0dd192016-12-15 11:24:03 -080044void ThreadTree::SetThreadName(int pid, int tid, const std::string& comm) {
45 ThreadEntry* thread = FindThreadOrNew(pid, tid);
46 if (comm != thread->comm) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020047 thread_comm_storage_.push_back(std::unique_ptr<std::string>(new std::string(comm)));
Yabin Cuiaa0dd192016-12-15 11:24:03 -080048 thread->comm = thread_comm_storage_.back()->c_str();
Yabin Cui003b2452016-09-29 15:32:45 -070049 }
Yabin Cui60a0ea92015-07-22 20:30:43 -070050}
51
52void 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;
Yabin Cuiaa0dd192016-12-15 11:24:03 -080056 if (pid != ppid) {
57 // Copy maps from parent process.
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070058 if (child->maps->maps.empty()) {
59 *child->maps = *parent->maps;
60 } else {
Yabin Cui847f3fd2019-05-02 12:58:05 -070061 CHECK_NE(child->maps, parent->maps);
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070062 for (auto& pair : parent->maps->maps) {
63 InsertMap(*child->maps, *pair.second);
64 }
65 }
Yabin Cuiaa0dd192016-12-15 11:24:03 -080066 }
Yabin Cui60a0ea92015-07-22 20:30:43 -070067}
68
Yabin Cuia89a3742021-02-11 13:14:54 -080069ThreadEntry* ThreadTree::FindThread(int tid) const {
Yabin Cuifc9da9b2019-08-08 18:15:14 -070070 if (auto it = thread_tree_.find(tid); it != thread_tree_.end()) {
71 return it->second.get();
72 }
73 return nullptr;
74}
75
Yabin Cui60a0ea92015-07-22 20:30:43 -070076ThreadEntry* ThreadTree::FindThreadOrNew(int pid, int tid) {
77 auto it = thread_tree_.find(tid);
Yabin Cui847f3fd2019-05-02 12:58:05 -070078 if (it != thread_tree_.end() && pid == it->second.get()->pid) {
79 return it->second.get();
Yabin Cui60a0ea92015-07-22 20:30:43 -070080 }
Yabin Cui847f3fd2019-05-02 12:58:05 -070081 if (it != thread_tree_.end()) {
82 ExitThread(it->second.get()->pid, tid);
83 }
84 return CreateThread(pid, tid);
Yabin Cui60a0ea92015-07-22 20:30:43 -070085}
86
Yabin Cuiaa0dd192016-12-15 11:24:03 -080087ThreadEntry* ThreadTree::CreateThread(int pid, int tid) {
Yabin Cui847f3fd2019-05-02 12:58:05 -070088 const char* comm;
89 std::shared_ptr<MapSet> maps;
Yabin Cuiaa0dd192016-12-15 11:24:03 -080090 if (pid == tid) {
Yabin Cui847f3fd2019-05-02 12:58:05 -070091 comm = "unknown";
92 maps.reset(new MapSet);
Yabin Cuiaa0dd192016-12-15 11:24:03 -080093 } else {
94 // Share maps among threads in the same thread group.
95 ThreadEntry* process = FindThreadOrNew(pid, pid);
Yabin Cui847f3fd2019-05-02 12:58:05 -070096 comm = process->comm;
Yabin Cuiaa0dd192016-12-15 11:24:03 -080097 maps = process->maps;
98 }
99 ThreadEntry* thread = new ThreadEntry{
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200100 pid,
101 tid,
102 comm,
103 maps,
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800104 };
105 auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread)));
106 CHECK(pair.second);
Evgeny Eltsin3775b142020-08-28 13:00:04 +0200107 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 }
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800115 return thread;
116}
117
Yabin Cui847f3fd2019-05-02 12:58:05 -0700118void 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
Yabin Cui9970a232016-06-29 12:18:11 -0700125void ThreadTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700126 const std::string& filename) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700127 // kernel map len can be 0 when record command is not run in supervisor mode.
128 if (len == 0) {
129 return;
130 }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800131 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 }
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700137 InsertMap(kernel_maps_, MapEntry(start_addr, len, pgoff, dso, true));
Yabin Cui60a0ea92015-07-22 20:30:43 -0700138}
139
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800140Dso* ThreadTree::FindKernelDsoOrNew() {
141 if (!kernel_dso_) {
142 kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700143 }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800144 return kernel_dso_.get();
145}
146
147Dso* ThreadTree::FindKernelModuleDsoOrNew(const std::string& filename, uint64_t memory_start,
148 uint64_t memory_end) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700149 auto it = module_dso_tree_.find(filename);
150 if (it == module_dso_tree_.end()) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800151 module_dso_tree_[filename] =
152 Dso::CreateKernelModuleDso(filename, memory_start, memory_end, FindKernelDsoOrNew());
Yabin Cui60a0ea92015-07-22 20:30:43 -0700153 it = module_dso_tree_.find(filename);
154 }
155 return it->second.get();
156}
157
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200158void ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
159 const std::string& filename, uint32_t flags) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700160 ThreadEntry* thread = FindThreadOrNew(pid, tid);
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700161 Dso* dso = FindUserDsoOrNew(filename, start_addr);
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700162 InsertMap(*thread->maps, MapEntry(start_addr, len, pgoff, dso, false, flags));
Yabin Cui60a0ea92015-07-22 20:30:43 -0700163}
164
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200165void 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
Yabin Cui516a87c2018-03-26 17:34:00 -0700194Dso* ThreadTree::FindUserDsoOrNew(const std::string& filename, uint64_t start_addr,
195 DsoType dso_type) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700196 auto it = user_dso_tree_.find(filename);
197 if (it == user_dso_tree_.end()) {
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700198 bool force_64bit = start_addr > UINT_MAX;
Yabin Cui516a87c2018-03-26 17:34:00 -0700199 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;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700203 }
204 return it->second.get();
205}
206
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200207void 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
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700217const MapEntry* ThreadTree::AllocateMap(const MapEntry& entry) {
218 map_storage_.emplace_back(new MapEntry(entry));
219 return map_storage_.back().get();
Yabin Cui547c60e2015-10-12 16:56:05 -0700220}
221
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700222static 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}
Yabin Cui547c60e2015-10-12 16:56:05 -0700229
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700230static 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.
238void 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));
Yabin Cui547c60e2015-10-12 16:56:05 -0700252 }
253 }
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700254 // 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++;
Yabin Cui547c60e2015-10-12 16:56:05 -0700266}
267
Yabin Cui418ba0d2020-03-24 11:53:39 -0700268const MapEntry* MapSet::FindMapByAddr(uint64_t addr) const {
269 auto it = maps.upper_bound(addr);
270 if (it != maps.begin()) {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700271 --it;
272 if (it->second->get_end_addr() > addr) {
273 return it->second;
274 }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700275 }
276 return nullptr;
277}
278
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700279const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) {
280 const MapEntry* result = nullptr;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700281 if (!in_kernel) {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700282 result = thread->maps->FindMapByAddr(ip);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700283 } else {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700284 result = kernel_maps_.FindMapByAddr(ip);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700285 }
286 return result != nullptr ? result : &unknown_map_;
287}
288
Yabin Cuib64a8632016-05-24 18:23:33 -0700289const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip) {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700290 const MapEntry* result = thread->maps->FindMapByAddr(ip);
Yabin Cuib64a8632016-05-24 18:23:33 -0700291 if (result != nullptr) {
292 return result;
293 }
Yabin Cui418ba0d2020-03-24 11:53:39 -0700294 result = kernel_maps_.FindMapByAddr(ip);
Yabin Cuib64a8632016-05-24 18:23:33 -0700295 return result != nullptr ? result : &unknown_map_;
296}
297
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200298const Symbol* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip, uint64_t* pvaddr_in_file,
299 Dso** pdso) {
Yabin Cuidb2c4932019-02-07 15:06:42 -0800300 uint64_t vaddr_in_file = 0;
Yabin Cuib068c102017-11-01 17:53:10 -0700301 const Symbol* symbol = nullptr;
Yabin Cui15475e62016-07-14 13:26:19 -0700302 Dso* dso = map->dso;
Yabin Cuidb2c4932019-02-07 15:06:42 -0800303 if (map->flags & map_flags::PROT_JIT_SYMFILE_MAP) {
304 vaddr_in_file = ip;
Yabin Cuib068c102017-11-01 17:53:10 -0700305 } else {
Yabin Cuidb2c4932019-02-07 15:06:42 -0800306 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;
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800313 dso = FindKernelDsoOrNew();
Yabin Cuidb2c4932019-02-07 15:06:42 -0800314 symbol = dso->FindSymbol(vaddr_in_file);
Yabin Cuib4212972016-05-25 14:08:05 -0700315 }
Yabin Cuib068c102017-11-01 17:53:10 -0700316
Yabin Cui60a0ea92015-07-22 20:30:43 -0700317 if (symbol == nullptr) {
Yabin Cui15475e62016-07-14 13:26:19 -0700318 if (show_ip_for_unknown_symbol_) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200319 std::string name = android::base::StringPrintf("%s%s[+%" PRIx64 "]",
320 (show_mark_for_unknown_symbol_ ? "*" : ""),
321 dso->FileName().c_str(), vaddr_in_file);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700322 dso->AddUnknownSymbol(vaddr_in_file, name);
Yabin Cui15475e62016-07-14 13:26:19 -0700323 symbol = dso->FindSymbol(vaddr_in_file);
324 CHECK(symbol != nullptr);
325 } else {
326 symbol = &unknown_symbol_;
327 }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700328 }
Yabin Cui9970a232016-06-29 12:18:11 -0700329 if (pvaddr_in_file != nullptr) {
330 *pvaddr_in_file = vaddr_in_file;
331 }
Yabin Cui16501ff2016-10-19 15:06:29 -0700332 if (pdso != nullptr) {
333 *pdso = dso;
334 }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700335 return symbol;
336}
Yabin Cui73d80782015-07-23 21:39:57 -0700337
Yabin Cui6965d422016-06-15 11:41:42 -0700338const Symbol* ThreadTree::FindKernelSymbol(uint64_t ip) {
339 const MapEntry* map = FindMap(nullptr, ip, true);
Yabin Cui9970a232016-06-29 12:18:11 -0700340 return FindSymbol(map, ip, nullptr);
Yabin Cui6965d422016-06-15 11:41:42 -0700341}
342
Yabin Cui767dd172016-06-02 21:02:43 -0700343void ThreadTree::ClearThreadAndMap() {
Yabin Cuib7f481f2015-10-23 19:48:42 -0700344 thread_tree_.clear();
345 thread_comm_storage_.clear();
Christopher Ferris15933b62018-02-22 19:06:42 -0800346 kernel_maps_.maps.clear();
Yabin Cuib7f481f2015-10-23 19:48:42 -0700347 map_storage_.clear();
Yabin Cuib7f481f2015-10-23 19:48:42 -0700348}
349
Yabin Cui8d005de2020-10-21 13:48:53 -0700350void ThreadTree::AddDsoInfo(FileFeature& file) {
351 DsoType dso_type = file.type;
Yabin Cuic5b4a312016-10-24 13:38:38 -0700352 Dso* dso = nullptr;
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800353 if (dso_type == DSO_KERNEL) {
354 dso = FindKernelDsoOrNew();
355 } else if (dso_type == DSO_KERNEL_MODULE) {
356 dso = FindKernelModuleDsoOrNew(file.path, 0, 0);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700357 } else {
Yabin Cui8d005de2020-10-21 13:48:53 -0700358 dso = FindUserDsoOrNew(file.path, 0, dso_type);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700359 }
Yabin Cui8d005de2020-10-21 13:48:53 -0700360 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) {
Yabin Cuidd401b32018-04-11 11:17:06 -0700363 dso->AddDexFileOffset(offset);
Yabin Cui516a87c2018-03-26 17:34:00 -0700364 }
365}
366
367void ThreadTree::AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset) {
368 Dso* dso = FindUserDsoOrNew(file_path, 0, DSO_DEX_FILE);
Yabin Cuidd401b32018-04-11 11:17:06 -0700369 dso->AddDexFileOffset(dex_file_offset);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700370}
371
Yabin Cui767dd172016-06-02 21:02:43 -0700372void ThreadTree::Update(const Record& record) {
Yabin Cuib4212972016-05-25 14:08:05 -0700373 if (record.type() == PERF_RECORD_MMAP) {
Yabin Cuif8258892015-08-03 11:01:22 -0700374 const MmapRecord& r = *static_cast<const MmapRecord*>(&record);
Yabin Cuib4212972016-05-25 14:08:05 -0700375 if (r.InKernel()) {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700376 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
Yabin Cuif8258892015-08-03 11:01:22 -0700377 } else {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700378 AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len, r.data->pgoff, r.filename);
Yabin Cui73d80782015-07-23 21:39:57 -0700379 }
Yabin Cuib4212972016-05-25 14:08:05 -0700380 } else if (record.type() == PERF_RECORD_MMAP2) {
Yabin Cuif8258892015-08-03 11:01:22 -0700381 const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record);
Yabin Cuib4212972016-05-25 14:08:05 -0700382 if (r.InKernel()) {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700383 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
Yabin Cuif8258892015-08-03 11:01:22 -0700384 } else {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200385 std::string filename =
386 (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename;
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700387 AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len, r.data->pgoff, filename,
388 r.data->prot);
Yabin Cuif8258892015-08-03 11:01:22 -0700389 }
Yabin Cuib4212972016-05-25 14:08:05 -0700390 } else if (record.type() == PERF_RECORD_COMM) {
Yabin Cuif8258892015-08-03 11:01:22 -0700391 const CommRecord& r = *static_cast<const CommRecord*>(&record);
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800392 SetThreadName(r.data->pid, r.data->tid, r.comm);
Yabin Cuib4212972016-05-25 14:08:05 -0700393 } else if (record.type() == PERF_RECORD_FORK) {
Yabin Cuif8258892015-08-03 11:01:22 -0700394 const ForkRecord& r = *static_cast<const ForkRecord*>(&record);
Yabin Cui190a8482016-08-04 10:22:17 -0700395 ForkThread(r.data->pid, r.data->tid, r.data->ppid, r.data->ptid);
Yabin Cui847f3fd2019-05-02 12:58:05 -0700396 } else if (record.type() == PERF_RECORD_EXIT) {
397 const ExitRecord& r = *static_cast<const ExitRecord*>(&record);
398 ExitThread(r.data->pid, r.data->tid);
Yabin Cuib4212972016-05-25 14:08:05 -0700399 } else if (record.type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
400 const auto& r = *static_cast<const KernelSymbolRecord*>(&record);
Yabin Cui1761a272016-06-23 17:11:14 -0700401 Dso::SetKallsyms(std::move(r.kallsyms));
Yabin Cui73d80782015-07-23 21:39:57 -0700402 }
403}
Yabin Cui767dd172016-06-02 21:02:43 -0700404
Yabin Cui78fddd12016-10-24 14:09:26 -0700405std::vector<Dso*> ThreadTree::GetAllDsos() const {
406 std::vector<Dso*> result;
Yabin Cui7078c672020-11-10 16:24:12 -0800407 if (kernel_dso_) {
408 result.push_back(kernel_dso_.get());
409 }
Yabin Cui78fddd12016-10-24 14:09:26 -0700410 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 }
Yabin Cui98c75842017-04-28 13:43:08 -0700416 result.push_back(unknown_dso_.get());
Yabin Cui78fddd12016-10-24 14:09:26 -0700417 return result;
418}
419
Yabin Cui767dd172016-06-02 21:02:43 -0700420} // namespace simpleperf