blob: a6d2e81ebf5ffec422428c7ff12e99c999869269 [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
Yabin Cui70fa40a2022-12-14 15:36:31 -080052bool ThreadTree::ForkThread(int pid, int tid, int ppid, int ptid) {
53 // Check thread ID.
Yabin Cui34fac912022-12-12 10:22:21 -080054 if (tid == ptid) {
Yabin Cui70fa40a2022-12-14 15:36:31 -080055 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;
Yabin Cui34fac912022-12-12 10:22:21 -080060 }
Yabin Cui60a0ea92015-07-22 20:30:43 -070061 ThreadEntry* parent = FindThreadOrNew(ppid, ptid);
62 ThreadEntry* child = FindThreadOrNew(pid, tid);
63 child->comm = parent->comm;
Yabin Cuiaa0dd192016-12-15 11:24:03 -080064 if (pid != ppid) {
65 // Copy maps from parent process.
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070066 if (child->maps->maps.empty()) {
67 *child->maps = *parent->maps;
68 } else {
Yabin Cui847f3fd2019-05-02 12:58:05 -070069 CHECK_NE(child->maps, parent->maps);
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070070 for (auto& pair : parent->maps->maps) {
71 InsertMap(*child->maps, *pair.second);
72 }
73 }
Yabin Cuiaa0dd192016-12-15 11:24:03 -080074 }
Yabin Cui70fa40a2022-12-14 15:36:31 -080075 return true;
Yabin Cui60a0ea92015-07-22 20:30:43 -070076}
77
Yabin Cuia89a3742021-02-11 13:14:54 -080078ThreadEntry* ThreadTree::FindThread(int tid) const {
Yabin Cuifc9da9b2019-08-08 18:15:14 -070079 if (auto it = thread_tree_.find(tid); it != thread_tree_.end()) {
80 return it->second.get();
81 }
82 return nullptr;
83}
84
Yabin Cui60a0ea92015-07-22 20:30:43 -070085ThreadEntry* ThreadTree::FindThreadOrNew(int pid, int tid) {
86 auto it = thread_tree_.find(tid);
Yabin Cui847f3fd2019-05-02 12:58:05 -070087 if (it != thread_tree_.end() && pid == it->second.get()->pid) {
88 return it->second.get();
Yabin Cui60a0ea92015-07-22 20:30:43 -070089 }
Yabin Cui847f3fd2019-05-02 12:58:05 -070090 if (it != thread_tree_.end()) {
91 ExitThread(it->second.get()->pid, tid);
92 }
93 return CreateThread(pid, tid);
Yabin Cui60a0ea92015-07-22 20:30:43 -070094}
95
Yabin Cuiaa0dd192016-12-15 11:24:03 -080096ThreadEntry* ThreadTree::CreateThread(int pid, int tid) {
Yabin Cui847f3fd2019-05-02 12:58:05 -070097 const char* comm;
98 std::shared_ptr<MapSet> maps;
Yabin Cuiaa0dd192016-12-15 11:24:03 -080099 if (pid == tid) {
Yabin Cui847f3fd2019-05-02 12:58:05 -0700100 comm = "unknown";
101 maps.reset(new MapSet);
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800102 } else {
103 // Share maps among threads in the same thread group.
104 ThreadEntry* process = FindThreadOrNew(pid, pid);
Yabin Cui847f3fd2019-05-02 12:58:05 -0700105 comm = process->comm;
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800106 maps = process->maps;
107 }
108 ThreadEntry* thread = new ThreadEntry{
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200109 pid,
110 tid,
111 comm,
112 maps,
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800113 };
114 auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread)));
115 CHECK(pair.second);
Evgeny Eltsin3775b142020-08-28 13:00:04 +0200116 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 }
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800124 return thread;
125}
126
Yabin Cui847f3fd2019-05-02 12:58:05 -0700127void 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
Yabin Cui9970a232016-06-29 12:18:11 -0700134void ThreadTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700135 const std::string& filename) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700136 // kernel map len can be 0 when record command is not run in supervisor mode.
137 if (len == 0) {
138 return;
139 }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800140 Dso* dso;
141 if (android::base::StartsWith(filename, DEFAULT_KERNEL_MMAP_NAME)) {
142 dso = FindKernelDsoOrNew();
143 } else {
144 dso = FindKernelModuleDsoOrNew(filename, start_addr, start_addr + len);
145 }
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700146 InsertMap(kernel_maps_, MapEntry(start_addr, len, pgoff, dso, true));
Yabin Cui60a0ea92015-07-22 20:30:43 -0700147}
148
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800149Dso* ThreadTree::FindKernelDsoOrNew() {
150 if (!kernel_dso_) {
151 kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700152 }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800153 return kernel_dso_.get();
154}
155
156Dso* ThreadTree::FindKernelModuleDsoOrNew(const std::string& filename, uint64_t memory_start,
157 uint64_t memory_end) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700158 auto it = module_dso_tree_.find(filename);
159 if (it == module_dso_tree_.end()) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800160 module_dso_tree_[filename] =
161 Dso::CreateKernelModuleDso(filename, memory_start, memory_end, FindKernelDsoOrNew());
Yabin Cui60a0ea92015-07-22 20:30:43 -0700162 it = module_dso_tree_.find(filename);
163 }
164 return it->second.get();
165}
166
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200167void ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
168 const std::string& filename, uint32_t flags) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700169 ThreadEntry* thread = FindThreadOrNew(pid, tid);
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700170 Dso* dso = FindUserDsoOrNew(filename, start_addr);
Yabin Cui90a547e2022-12-07 16:29:13 -0800171 CHECK(dso != nullptr);
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700172 InsertMap(*thread->maps, MapEntry(start_addr, len, pgoff, dso, false, flags));
Yabin Cui60a0ea92015-07-22 20:30:43 -0700173}
174
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200175void ThreadTree::AddThreadMapsForDsoSymbols(ThreadEntry* thread, Dso* dso) {
176 const uint64_t page_size = GetPageSize();
177
178 auto maps = thread->maps;
179
180 uint64_t map_start = 0;
181 uint64_t map_end = 0;
182
183 // Dso symbols are sorted by address. Walk and calculate containing pages.
184 for (const auto& sym : dso->GetSymbols()) {
185 uint64_t sym_map_start = AlignDown(sym.addr, page_size);
186 uint64_t sym_map_end = Align(sym.addr + sym.len, page_size);
187
188 if (map_end < sym_map_start) {
189 if (map_start < map_end) {
190 InsertMap(*maps, MapEntry(map_start, map_end - map_start, map_start, dso, false, 0));
191 }
192 map_start = sym_map_start;
193 }
194 if (map_end < sym_map_end) {
195 map_end = sym_map_end;
196 }
197 }
198
199 if (map_start < map_end) {
200 InsertMap(*maps, MapEntry(map_start, map_end - map_start, map_start, dso, false, 0));
201 }
202}
203
Yabin Cui516a87c2018-03-26 17:34:00 -0700204Dso* ThreadTree::FindUserDsoOrNew(const std::string& filename, uint64_t start_addr,
205 DsoType dso_type) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700206 auto it = user_dso_tree_.find(filename);
207 if (it == user_dso_tree_.end()) {
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700208 bool force_64bit = start_addr > UINT_MAX;
Yabin Cui516a87c2018-03-26 17:34:00 -0700209 std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, filename, force_64bit);
Yabin Cui90a547e2022-12-07 16:29:13 -0800210 if (!dso) {
211 return nullptr;
212 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700213 auto pair = user_dso_tree_.insert(std::make_pair(filename, std::move(dso)));
214 CHECK(pair.second);
215 it = pair.first;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700216 }
217 return it->second.get();
218}
219
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200220void ThreadTree::AddSymbolsForProcess(int pid, std::vector<Symbol>* symbols) {
221 auto name = GetSymbolMapDsoName(pid);
222
223 auto dso = FindUserDsoOrNew(name, 0, DSO_SYMBOL_MAP_FILE);
224 dso->SetSymbols(symbols);
225
226 auto thread = FindThreadOrNew(pid, pid);
227 AddThreadMapsForDsoSymbols(thread, dso);
228}
229
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700230const MapEntry* ThreadTree::AllocateMap(const MapEntry& entry) {
231 map_storage_.emplace_back(new MapEntry(entry));
232 return map_storage_.back().get();
Yabin Cui547c60e2015-10-12 16:56:05 -0700233}
234
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700235static MapEntry RemoveFirstPartOfMapEntry(const MapEntry* entry, uint64_t new_start_addr) {
236 MapEntry result = *entry;
237 result.start_addr = new_start_addr;
238 result.len -= result.start_addr - entry->start_addr;
239 result.pgoff += result.start_addr - entry->start_addr;
240 return result;
241}
Yabin Cui547c60e2015-10-12 16:56:05 -0700242
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700243static MapEntry RemoveSecondPartOfMapEntry(const MapEntry* entry, uint64_t new_len) {
244 MapEntry result = *entry;
245 result.len = new_len;
246 return result;
247}
248
249// Insert a new map entry in a MapSet. If some existing map entries overlap the new map entry,
250// then remove the overlapped parts.
251void ThreadTree::InsertMap(MapSet& maps, const MapEntry& entry) {
252 std::map<uint64_t, const MapEntry*>& map = maps.maps;
253 auto it = map.lower_bound(entry.start_addr);
254 // Remove overlapped entry with start_addr < entry.start_addr.
255 if (it != map.begin()) {
256 auto it2 = it;
257 --it2;
258 if (it2->second->get_end_addr() > entry.get_end_addr()) {
259 map.emplace(entry.get_end_addr(),
260 AllocateMap(RemoveFirstPartOfMapEntry(it2->second, entry.get_end_addr())));
261 }
262 if (it2->second->get_end_addr() > entry.start_addr) {
263 it2->second =
264 AllocateMap(RemoveSecondPartOfMapEntry(it2->second, entry.start_addr - it2->first));
Yabin Cui547c60e2015-10-12 16:56:05 -0700265 }
266 }
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700267 // Remove overlapped entries with start_addr >= entry.start_addr.
268 while (it != map.end() && it->second->get_end_addr() <= entry.get_end_addr()) {
269 it = map.erase(it);
270 }
271 if (it != map.end() && it->second->start_addr < entry.get_end_addr()) {
272 map.emplace(entry.get_end_addr(),
273 AllocateMap(RemoveFirstPartOfMapEntry(it->second, entry.get_end_addr())));
274 map.erase(it);
275 }
276 // Insert the new entry.
277 map.emplace(entry.start_addr, AllocateMap(entry));
278 maps.version++;
Yabin Cui547c60e2015-10-12 16:56:05 -0700279}
280
Yabin Cui418ba0d2020-03-24 11:53:39 -0700281const MapEntry* MapSet::FindMapByAddr(uint64_t addr) const {
282 auto it = maps.upper_bound(addr);
283 if (it != maps.begin()) {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700284 --it;
285 if (it->second->get_end_addr() > addr) {
286 return it->second;
287 }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700288 }
289 return nullptr;
290}
291
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700292const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) {
293 const MapEntry* result = nullptr;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700294 if (!in_kernel) {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700295 result = thread->maps->FindMapByAddr(ip);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700296 } else {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700297 result = kernel_maps_.FindMapByAddr(ip);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700298 }
299 return result != nullptr ? result : &unknown_map_;
300}
301
Yabin Cuib64a8632016-05-24 18:23:33 -0700302const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip) {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700303 const MapEntry* result = thread->maps->FindMapByAddr(ip);
Yabin Cuib64a8632016-05-24 18:23:33 -0700304 if (result != nullptr) {
305 return result;
306 }
Yabin Cui418ba0d2020-03-24 11:53:39 -0700307 result = kernel_maps_.FindMapByAddr(ip);
Yabin Cuib64a8632016-05-24 18:23:33 -0700308 return result != nullptr ? result : &unknown_map_;
309}
310
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200311const Symbol* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip, uint64_t* pvaddr_in_file,
312 Dso** pdso) {
Yabin Cuidb2c4932019-02-07 15:06:42 -0800313 uint64_t vaddr_in_file = 0;
Yabin Cuib068c102017-11-01 17:53:10 -0700314 const Symbol* symbol = nullptr;
Yabin Cui15475e62016-07-14 13:26:19 -0700315 Dso* dso = map->dso;
Yabin Cuidb2c4932019-02-07 15:06:42 -0800316 if (map->flags & map_flags::PROT_JIT_SYMFILE_MAP) {
317 vaddr_in_file = ip;
Yabin Cuib068c102017-11-01 17:53:10 -0700318 } else {
Yabin Cuidb2c4932019-02-07 15:06:42 -0800319 vaddr_in_file = dso->IpToVaddrInFile(ip, map->start_addr, map->pgoff);
320 }
321 symbol = dso->FindSymbol(vaddr_in_file);
322 if (symbol == nullptr && dso->type() == DSO_KERNEL_MODULE) {
323 // If the ip address hits the vmlinux, or hits a kernel module, but we can't find its symbol
324 // in the kernel module file, then find its symbol in /proc/kallsyms or vmlinux.
325 vaddr_in_file = ip;
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800326 dso = FindKernelDsoOrNew();
Yabin Cuidb2c4932019-02-07 15:06:42 -0800327 symbol = dso->FindSymbol(vaddr_in_file);
Yabin Cuib4212972016-05-25 14:08:05 -0700328 }
Yabin Cuib068c102017-11-01 17:53:10 -0700329
Yabin Cui60a0ea92015-07-22 20:30:43 -0700330 if (symbol == nullptr) {
Yabin Cui15475e62016-07-14 13:26:19 -0700331 if (show_ip_for_unknown_symbol_) {
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200332 std::string name = android::base::StringPrintf("%s%s[+%" PRIx64 "]",
333 (show_mark_for_unknown_symbol_ ? "*" : ""),
334 dso->FileName().c_str(), vaddr_in_file);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700335 dso->AddUnknownSymbol(vaddr_in_file, name);
Yabin Cui15475e62016-07-14 13:26:19 -0700336 symbol = dso->FindSymbol(vaddr_in_file);
337 CHECK(symbol != nullptr);
338 } else {
339 symbol = &unknown_symbol_;
340 }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700341 }
Yabin Cui9970a232016-06-29 12:18:11 -0700342 if (pvaddr_in_file != nullptr) {
343 *pvaddr_in_file = vaddr_in_file;
344 }
Yabin Cui16501ff2016-10-19 15:06:29 -0700345 if (pdso != nullptr) {
346 *pdso = dso;
347 }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700348 return symbol;
349}
Yabin Cui73d80782015-07-23 21:39:57 -0700350
Yabin Cui6965d422016-06-15 11:41:42 -0700351const Symbol* ThreadTree::FindKernelSymbol(uint64_t ip) {
352 const MapEntry* map = FindMap(nullptr, ip, true);
Yabin Cui9970a232016-06-29 12:18:11 -0700353 return FindSymbol(map, ip, nullptr);
Yabin Cui6965d422016-06-15 11:41:42 -0700354}
355
Yabin Cui767dd172016-06-02 21:02:43 -0700356void ThreadTree::ClearThreadAndMap() {
Yabin Cuib7f481f2015-10-23 19:48:42 -0700357 thread_tree_.clear();
358 thread_comm_storage_.clear();
Christopher Ferris15933b62018-02-22 19:06:42 -0800359 kernel_maps_.maps.clear();
Yabin Cuib7f481f2015-10-23 19:48:42 -0700360 map_storage_.clear();
Yabin Cuib7f481f2015-10-23 19:48:42 -0700361}
362
Yabin Cui90a547e2022-12-07 16:29:13 -0800363bool ThreadTree::AddDsoInfo(FileFeature& file) {
Yabin Cui8d005de2020-10-21 13:48:53 -0700364 DsoType dso_type = file.type;
Yabin Cuic5b4a312016-10-24 13:38:38 -0700365 Dso* dso = nullptr;
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800366 if (dso_type == DSO_KERNEL) {
367 dso = FindKernelDsoOrNew();
368 } else if (dso_type == DSO_KERNEL_MODULE) {
369 dso = FindKernelModuleDsoOrNew(file.path, 0, 0);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700370 } else {
Yabin Cui8d005de2020-10-21 13:48:53 -0700371 dso = FindUserDsoOrNew(file.path, 0, dso_type);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700372 }
Yabin Cui90a547e2022-12-07 16:29:13 -0800373 if (!dso) {
374 return false;
375 }
Yabin Cui8d005de2020-10-21 13:48:53 -0700376 dso->SetMinExecutableVaddr(file.min_vaddr, file.file_offset_of_min_vaddr);
377 dso->SetSymbols(&file.symbols);
378 for (uint64_t offset : file.dex_file_offsets) {
Yabin Cuidd401b32018-04-11 11:17:06 -0700379 dso->AddDexFileOffset(offset);
Yabin Cui516a87c2018-03-26 17:34:00 -0700380 }
Yabin Cui90a547e2022-12-07 16:29:13 -0800381 return true;
Yabin Cui516a87c2018-03-26 17:34:00 -0700382}
383
384void ThreadTree::AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset) {
385 Dso* dso = FindUserDsoOrNew(file_path, 0, DSO_DEX_FILE);
Yabin Cuidd401b32018-04-11 11:17:06 -0700386 dso->AddDexFileOffset(dex_file_offset);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700387}
388
Yabin Cui767dd172016-06-02 21:02:43 -0700389void ThreadTree::Update(const Record& record) {
Yabin Cuib4212972016-05-25 14:08:05 -0700390 if (record.type() == PERF_RECORD_MMAP) {
Yabin Cuif8258892015-08-03 11:01:22 -0700391 const MmapRecord& r = *static_cast<const MmapRecord*>(&record);
Yabin Cuib4212972016-05-25 14:08:05 -0700392 if (r.InKernel()) {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700393 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
Yabin Cuif8258892015-08-03 11:01:22 -0700394 } else {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700395 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 -0700396 }
Yabin Cuib4212972016-05-25 14:08:05 -0700397 } else if (record.type() == PERF_RECORD_MMAP2) {
Yabin Cuif8258892015-08-03 11:01:22 -0700398 const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record);
Yabin Cuib4212972016-05-25 14:08:05 -0700399 if (r.InKernel()) {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700400 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
Yabin Cuif8258892015-08-03 11:01:22 -0700401 } else {
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200402 std::string filename =
403 (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename;
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700404 AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len, r.data->pgoff, filename,
405 r.data->prot);
Yabin Cuif8258892015-08-03 11:01:22 -0700406 }
Yabin Cuib4212972016-05-25 14:08:05 -0700407 } else if (record.type() == PERF_RECORD_COMM) {
Yabin Cuif8258892015-08-03 11:01:22 -0700408 const CommRecord& r = *static_cast<const CommRecord*>(&record);
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800409 SetThreadName(r.data->pid, r.data->tid, r.comm);
Yabin Cuib4212972016-05-25 14:08:05 -0700410 } else if (record.type() == PERF_RECORD_FORK) {
Yabin Cuif8258892015-08-03 11:01:22 -0700411 const ForkRecord& r = *static_cast<const ForkRecord*>(&record);
Yabin Cui190a8482016-08-04 10:22:17 -0700412 ForkThread(r.data->pid, r.data->tid, r.data->ppid, r.data->ptid);
Yabin Cui847f3fd2019-05-02 12:58:05 -0700413 } else if (record.type() == PERF_RECORD_EXIT) {
Yabin Cui5a1b6262023-05-01 09:53:34 -0700414 if (!disable_thread_exit_records_) {
415 const ExitRecord& r = *static_cast<const ExitRecord*>(&record);
416 ExitThread(r.data->pid, r.data->tid);
417 }
Yabin Cuib4212972016-05-25 14:08:05 -0700418 } else if (record.type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
419 const auto& r = *static_cast<const KernelSymbolRecord*>(&record);
Yabin Cuiebbda482022-08-25 14:43:12 -0700420 Dso::SetKallsyms(std::string(r.kallsyms, r.kallsyms_size));
Yabin Cui73d80782015-07-23 21:39:57 -0700421 }
422}
Yabin Cui767dd172016-06-02 21:02:43 -0700423
Yabin Cui78fddd12016-10-24 14:09:26 -0700424std::vector<Dso*> ThreadTree::GetAllDsos() const {
425 std::vector<Dso*> result;
Yabin Cui7078c672020-11-10 16:24:12 -0800426 if (kernel_dso_) {
427 result.push_back(kernel_dso_.get());
428 }
Yabin Cui78fddd12016-10-24 14:09:26 -0700429 for (auto& p : module_dso_tree_) {
430 result.push_back(p.second.get());
431 }
432 for (auto& p : user_dso_tree_) {
433 result.push_back(p.second.get());
434 }
Yabin Cui98c75842017-04-28 13:43:08 -0700435 result.push_back(unknown_dso_.get());
Yabin Cui78fddd12016-10-24 14:09:26 -0700436 return result;
437}
438
Yabin Cui767dd172016-06-02 21:02:43 -0700439} // namespace simpleperf