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