blob: 4a17ee05b57e0b4d403bc5e50f6c5b9f1fe6dc6a [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;
Tomislav Novakf8c8bcb2024-07-12 10:16:06 -0700141 if (android::base::StartsWith(filename, DEFAULT_KERNEL_MMAP_NAME) ||
142 android::base::StartsWith(filename, DEFAULT_KERNEL_BPF_MMAP_NAME)) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800143 dso = FindKernelDsoOrNew();
144 } else {
145 dso = FindKernelModuleDsoOrNew(filename, start_addr, start_addr + len);
146 }
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700147 InsertMap(kernel_maps_, MapEntry(start_addr, len, pgoff, dso, true));
Yabin Cui60a0ea92015-07-22 20:30:43 -0700148}
149
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800150Dso* ThreadTree::FindKernelDsoOrNew() {
151 if (!kernel_dso_) {
152 kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700153 }
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800154 return kernel_dso_.get();
155}
156
157Dso* ThreadTree::FindKernelModuleDsoOrNew(const std::string& filename, uint64_t memory_start,
158 uint64_t memory_end) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700159 auto it = module_dso_tree_.find(filename);
160 if (it == module_dso_tree_.end()) {
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800161 module_dso_tree_[filename] =
162 Dso::CreateKernelModuleDso(filename, memory_start, memory_end, FindKernelDsoOrNew());
Yabin Cui60a0ea92015-07-22 20:30:43 -0700163 it = module_dso_tree_.find(filename);
164 }
165 return it->second.get();
166}
167
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200168void ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
169 const std::string& filename, uint32_t flags) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700170 ThreadEntry* thread = FindThreadOrNew(pid, tid);
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700171 Dso* dso = FindUserDsoOrNew(filename, start_addr);
Yabin Cui90a547e2022-12-07 16:29:13 -0800172 CHECK(dso != nullptr);
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700173 InsertMap(*thread->maps, MapEntry(start_addr, len, pgoff, dso, false, flags));
Yabin Cui60a0ea92015-07-22 20:30:43 -0700174}
175
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200176void 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
Yabin Cui516a87c2018-03-26 17:34:00 -0700205Dso* ThreadTree::FindUserDsoOrNew(const std::string& filename, uint64_t start_addr,
206 DsoType dso_type) {
Yabin Cui60a0ea92015-07-22 20:30:43 -0700207 auto it = user_dso_tree_.find(filename);
208 if (it == user_dso_tree_.end()) {
Yabin Cui63a1c3d2017-05-19 12:57:44 -0700209 bool force_64bit = start_addr > UINT_MAX;
Yabin Cui516a87c2018-03-26 17:34:00 -0700210 std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, filename, force_64bit);
Yabin Cui90a547e2022-12-07 16:29:13 -0800211 if (!dso) {
212 return nullptr;
213 }
Yabin Cui516a87c2018-03-26 17:34:00 -0700214 auto pair = user_dso_tree_.insert(std::make_pair(filename, std::move(dso)));
215 CHECK(pair.second);
216 it = pair.first;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700217 }
218 return it->second.get();
219}
220
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200221void 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
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700231const MapEntry* ThreadTree::AllocateMap(const MapEntry& entry) {
232 map_storage_.emplace_back(new MapEntry(entry));
233 return map_storage_.back().get();
Yabin Cui547c60e2015-10-12 16:56:05 -0700234}
235
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700236static 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}
Yabin Cui547c60e2015-10-12 16:56:05 -0700243
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700244static 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.
252void 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));
Yabin Cui547c60e2015-10-12 16:56:05 -0700266 }
267 }
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700268 // 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++;
Yabin Cui547c60e2015-10-12 16:56:05 -0700280}
281
Yabin Cui418ba0d2020-03-24 11:53:39 -0700282const MapEntry* MapSet::FindMapByAddr(uint64_t addr) const {
283 auto it = maps.upper_bound(addr);
284 if (it != maps.begin()) {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700285 --it;
286 if (it->second->get_end_addr() > addr) {
287 return it->second;
288 }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700289 }
290 return nullptr;
291}
292
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700293const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) {
294 const MapEntry* result = nullptr;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700295 if (!in_kernel) {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700296 result = thread->maps->FindMapByAddr(ip);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700297 } else {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700298 result = kernel_maps_.FindMapByAddr(ip);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700299 }
300 return result != nullptr ? result : &unknown_map_;
301}
302
Yabin Cuib64a8632016-05-24 18:23:33 -0700303const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip) {
Yabin Cui418ba0d2020-03-24 11:53:39 -0700304 const MapEntry* result = thread->maps->FindMapByAddr(ip);
Yabin Cuib64a8632016-05-24 18:23:33 -0700305 if (result != nullptr) {
306 return result;
307 }
Yabin Cui418ba0d2020-03-24 11:53:39 -0700308 result = kernel_maps_.FindMapByAddr(ip);
Yabin Cuib64a8632016-05-24 18:23:33 -0700309 return result != nullptr ? result : &unknown_map_;
310}
311
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200312const Symbol* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip, uint64_t* pvaddr_in_file,
313 Dso** pdso) {
Yabin Cuidb2c4932019-02-07 15:06:42 -0800314 uint64_t vaddr_in_file = 0;
Yabin Cuib068c102017-11-01 17:53:10 -0700315 const Symbol* symbol = nullptr;
Yabin Cui15475e62016-07-14 13:26:19 -0700316 Dso* dso = map->dso;
Yabin Cuidb2c4932019-02-07 15:06:42 -0800317 if (map->flags & map_flags::PROT_JIT_SYMFILE_MAP) {
318 vaddr_in_file = ip;
Yabin Cuib068c102017-11-01 17:53:10 -0700319 } else {
Yabin Cuidb2c4932019-02-07 15:06:42 -0800320 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;
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800327 dso = FindKernelDsoOrNew();
Yabin Cuidb2c4932019-02-07 15:06:42 -0800328 symbol = dso->FindSymbol(vaddr_in_file);
Yabin Cuib4212972016-05-25 14:08:05 -0700329 }
Yabin Cuib068c102017-11-01 17:53:10 -0700330
Yabin Cui60a0ea92015-07-22 20:30:43 -0700331 if (symbol == nullptr) {
Yabin Cui15475e62016-07-14 13:26:19 -0700332 if (show_ip_for_unknown_symbol_) {
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200333 std::string name = android::base::StringPrintf("%s%s[+%" PRIx64 "]",
334 (show_mark_for_unknown_symbol_ ? "*" : ""),
335 dso->FileName().c_str(), vaddr_in_file);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700336 dso->AddUnknownSymbol(vaddr_in_file, name);
Yabin Cui15475e62016-07-14 13:26:19 -0700337 symbol = dso->FindSymbol(vaddr_in_file);
338 CHECK(symbol != nullptr);
339 } else {
340 symbol = &unknown_symbol_;
341 }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700342 }
Yabin Cui9970a232016-06-29 12:18:11 -0700343 if (pvaddr_in_file != nullptr) {
344 *pvaddr_in_file = vaddr_in_file;
345 }
Yabin Cui16501ff2016-10-19 15:06:29 -0700346 if (pdso != nullptr) {
347 *pdso = dso;
348 }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700349 return symbol;
350}
Yabin Cui73d80782015-07-23 21:39:57 -0700351
Yabin Cui6965d422016-06-15 11:41:42 -0700352const Symbol* ThreadTree::FindKernelSymbol(uint64_t ip) {
353 const MapEntry* map = FindMap(nullptr, ip, true);
Yabin Cui9970a232016-06-29 12:18:11 -0700354 return FindSymbol(map, ip, nullptr);
Yabin Cui6965d422016-06-15 11:41:42 -0700355}
356
Yabin Cui767dd172016-06-02 21:02:43 -0700357void ThreadTree::ClearThreadAndMap() {
Yabin Cuib7f481f2015-10-23 19:48:42 -0700358 thread_tree_.clear();
359 thread_comm_storage_.clear();
Christopher Ferris15933b62018-02-22 19:06:42 -0800360 kernel_maps_.maps.clear();
Yabin Cuib7f481f2015-10-23 19:48:42 -0700361 map_storage_.clear();
Yabin Cuib7f481f2015-10-23 19:48:42 -0700362}
363
Yabin Cui90a547e2022-12-07 16:29:13 -0800364bool ThreadTree::AddDsoInfo(FileFeature& file) {
Yabin Cui8d005de2020-10-21 13:48:53 -0700365 DsoType dso_type = file.type;
Yabin Cuic5b4a312016-10-24 13:38:38 -0700366 Dso* dso = nullptr;
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800367 if (dso_type == DSO_KERNEL) {
368 dso = FindKernelDsoOrNew();
369 } else if (dso_type == DSO_KERNEL_MODULE) {
370 dso = FindKernelModuleDsoOrNew(file.path, 0, 0);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700371 } else {
Yabin Cui8d005de2020-10-21 13:48:53 -0700372 dso = FindUserDsoOrNew(file.path, 0, dso_type);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700373 }
Yabin Cui90a547e2022-12-07 16:29:13 -0800374 if (!dso) {
375 return false;
376 }
Yabin Cui8d005de2020-10-21 13:48:53 -0700377 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) {
Yabin Cuidd401b32018-04-11 11:17:06 -0700380 dso->AddDexFileOffset(offset);
Yabin Cui516a87c2018-03-26 17:34:00 -0700381 }
Yabin Cui90a547e2022-12-07 16:29:13 -0800382 return true;
Yabin Cui516a87c2018-03-26 17:34:00 -0700383}
384
385void ThreadTree::AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset) {
386 Dso* dso = FindUserDsoOrNew(file_path, 0, DSO_DEX_FILE);
Yabin Cuidd401b32018-04-11 11:17:06 -0700387 dso->AddDexFileOffset(dex_file_offset);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700388}
389
Yabin Cui767dd172016-06-02 21:02:43 -0700390void ThreadTree::Update(const Record& record) {
Yabin Cuib4212972016-05-25 14:08:05 -0700391 if (record.type() == PERF_RECORD_MMAP) {
Yabin Cuif8258892015-08-03 11:01:22 -0700392 const MmapRecord& r = *static_cast<const MmapRecord*>(&record);
Yabin Cuib4212972016-05-25 14:08:05 -0700393 if (r.InKernel()) {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700394 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
Yabin Cuif8258892015-08-03 11:01:22 -0700395 } else {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700396 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 -0700397 }
Yabin Cuib4212972016-05-25 14:08:05 -0700398 } else if (record.type() == PERF_RECORD_MMAP2) {
Yabin Cuif8258892015-08-03 11:01:22 -0700399 const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record);
Yabin Cuib4212972016-05-25 14:08:05 -0700400 if (r.InKernel()) {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700401 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
Yabin Cuif8258892015-08-03 11:01:22 -0700402 } else {
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200403 std::string filename =
404 (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename;
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700405 AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len, r.data->pgoff, filename,
406 r.data->prot);
Yabin Cuif8258892015-08-03 11:01:22 -0700407 }
Yabin Cuib4212972016-05-25 14:08:05 -0700408 } else if (record.type() == PERF_RECORD_COMM) {
Yabin Cuif8258892015-08-03 11:01:22 -0700409 const CommRecord& r = *static_cast<const CommRecord*>(&record);
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800410 SetThreadName(r.data->pid, r.data->tid, r.comm);
Yabin Cuib4212972016-05-25 14:08:05 -0700411 } else if (record.type() == PERF_RECORD_FORK) {
Yabin Cuif8258892015-08-03 11:01:22 -0700412 const ForkRecord& r = *static_cast<const ForkRecord*>(&record);
Yabin Cui190a8482016-08-04 10:22:17 -0700413 ForkThread(r.data->pid, r.data->tid, r.data->ppid, r.data->ptid);
Yabin Cui847f3fd2019-05-02 12:58:05 -0700414 } else if (record.type() == PERF_RECORD_EXIT) {
Yabin Cui2b1cfec2023-05-01 09:53:34 -0700415 if (!disable_thread_exit_records_) {
416 const ExitRecord& r = *static_cast<const ExitRecord*>(&record);
417 ExitThread(r.data->pid, r.data->tid);
418 }
Yabin Cuib4212972016-05-25 14:08:05 -0700419 } else if (record.type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
420 const auto& r = *static_cast<const KernelSymbolRecord*>(&record);
Yabin Cuiebbda482022-08-25 14:43:12 -0700421 Dso::SetKallsyms(std::string(r.kallsyms, r.kallsyms_size));
Yabin Cui73d80782015-07-23 21:39:57 -0700422 }
423}
Yabin Cui767dd172016-06-02 21:02:43 -0700424
Yabin Cui78fddd12016-10-24 14:09:26 -0700425std::vector<Dso*> ThreadTree::GetAllDsos() const {
426 std::vector<Dso*> result;
Yabin Cui7078c672020-11-10 16:24:12 -0800427 if (kernel_dso_) {
428 result.push_back(kernel_dso_.get());
429 }
Yabin Cui78fddd12016-10-24 14:09:26 -0700430 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 }
Yabin Cui98c75842017-04-28 13:43:08 -0700436 result.push_back(unknown_dso_.get());
Yabin Cui78fddd12016-10-24 14:09:26 -0700437 return result;
438}
439
Yabin Cui767dd172016-06-02 21:02:43 -0700440} // namespace simpleperf