1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "thread_tree.h"
18
19 #include <inttypes.h>
20
21 #include <limits>
22
23 #include <android-base/logging.h>
24 #include <android-base/stringprintf.h>
25
26 #include "perf_event.h"
27 #include "record.h"
28
29 namespace simpleperf {
30
SetThreadName(int pid,int tid,const std::string & comm)31 void ThreadTree::SetThreadName(int pid, int tid, const std::string& comm) {
32 ThreadEntry* thread = FindThreadOrNew(pid, tid);
33 if (comm != thread->comm) {
34 thread_comm_storage_.push_back(
35 std::unique_ptr<std::string>(new std::string(comm)));
36 thread->comm = thread_comm_storage_.back()->c_str();
37 }
38 }
39
ForkThread(int pid,int tid,int ppid,int ptid)40 void ThreadTree::ForkThread(int pid, int tid, int ppid, int ptid) {
41 ThreadEntry* parent = FindThreadOrNew(ppid, ptid);
42 ThreadEntry* child = FindThreadOrNew(pid, tid);
43 child->comm = parent->comm;
44 if (pid != ppid) {
45 // Copy maps from parent process.
46 if (child->maps->maps.empty()) {
47 *child->maps = *parent->maps;
48 } else {
49 for (auto& pair : parent->maps->maps) {
50 InsertMap(*child->maps, *pair.second);
51 }
52 }
53 }
54 }
55
FindThreadOrNew(int pid,int tid)56 ThreadEntry* ThreadTree::FindThreadOrNew(int pid, int tid) {
57 auto it = thread_tree_.find(tid);
58 if (it == thread_tree_.end()) {
59 return CreateThread(pid, tid);
60 } else {
61 if (pid != it->second.get()->pid) {
62 // TODO: b/22185053.
63 LOG(DEBUG) << "unexpected (pid, tid) pair: expected ("
64 << it->second.get()->pid << ", " << tid << "), actual (" << pid
65 << ", " << tid << ")";
66 }
67 }
68 return it->second.get();
69 }
70
CreateThread(int pid,int tid)71 ThreadEntry* ThreadTree::CreateThread(int pid, int tid) {
72 MapSet* maps = nullptr;
73 if (pid == tid) {
74 maps = new MapSet;
75 map_set_storage_.push_back(std::unique_ptr<MapSet>(maps));
76 } else {
77 // Share maps among threads in the same thread group.
78 ThreadEntry* process = FindThreadOrNew(pid, pid);
79 maps = process->maps;
80 }
81 ThreadEntry* thread = new ThreadEntry{
82 pid, tid,
83 "unknown",
84 maps,
85 };
86 auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread)));
87 CHECK(pair.second);
88 return thread;
89 }
90
AddKernelMap(uint64_t start_addr,uint64_t len,uint64_t pgoff,const std::string & filename)91 void ThreadTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
92 const std::string& filename) {
93 // kernel map len can be 0 when record command is not run in supervisor mode.
94 if (len == 0) {
95 return;
96 }
97 Dso* dso = FindKernelDsoOrNew(filename);
98 InsertMap(kernel_maps_, MapEntry(start_addr, len, pgoff, dso, true));
99 }
100
FindKernelDsoOrNew(const std::string & filename)101 Dso* ThreadTree::FindKernelDsoOrNew(const std::string& filename) {
102 if (filename == DEFAULT_KERNEL_MMAP_NAME ||
103 filename == DEFAULT_KERNEL_MMAP_NAME_PERF) {
104 return kernel_dso_.get();
105 }
106 auto it = module_dso_tree_.find(filename);
107 if (it == module_dso_tree_.end()) {
108 module_dso_tree_[filename] = Dso::CreateDso(DSO_KERNEL_MODULE, filename);
109 it = module_dso_tree_.find(filename);
110 }
111 return it->second.get();
112 }
113
AddThreadMap(int pid,int tid,uint64_t start_addr,uint64_t len,uint64_t pgoff,const std::string & filename,uint32_t flags)114 void ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len,
115 uint64_t pgoff, const std::string& filename, uint32_t flags) {
116 ThreadEntry* thread = FindThreadOrNew(pid, tid);
117 Dso* dso = FindUserDsoOrNew(filename, start_addr);
118 InsertMap(*thread->maps, MapEntry(start_addr, len, pgoff, dso, false, flags));
119 }
120
FindUserDsoOrNew(const std::string & filename,uint64_t start_addr,DsoType dso_type)121 Dso* ThreadTree::FindUserDsoOrNew(const std::string& filename, uint64_t start_addr,
122 DsoType dso_type) {
123 auto it = user_dso_tree_.find(filename);
124 if (it == user_dso_tree_.end()) {
125 bool force_64bit = start_addr > UINT_MAX;
126 std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, filename, force_64bit);
127 auto pair = user_dso_tree_.insert(std::make_pair(filename, std::move(dso)));
128 CHECK(pair.second);
129 it = pair.first;
130 }
131 return it->second.get();
132 }
133
AllocateMap(const MapEntry & entry)134 const MapEntry* ThreadTree::AllocateMap(const MapEntry& entry) {
135 map_storage_.emplace_back(new MapEntry(entry));
136 return map_storage_.back().get();
137 }
138
RemoveFirstPartOfMapEntry(const MapEntry * entry,uint64_t new_start_addr)139 static MapEntry RemoveFirstPartOfMapEntry(const MapEntry* entry, uint64_t new_start_addr) {
140 MapEntry result = *entry;
141 result.start_addr = new_start_addr;
142 result.len -= result.start_addr - entry->start_addr;
143 result.pgoff += result.start_addr - entry->start_addr;
144 return result;
145 }
146
RemoveSecondPartOfMapEntry(const MapEntry * entry,uint64_t new_len)147 static MapEntry RemoveSecondPartOfMapEntry(const MapEntry* entry, uint64_t new_len) {
148 MapEntry result = *entry;
149 result.len = new_len;
150 return result;
151 }
152
153 // Insert a new map entry in a MapSet. If some existing map entries overlap the new map entry,
154 // then remove the overlapped parts.
InsertMap(MapSet & maps,const MapEntry & entry)155 void ThreadTree::InsertMap(MapSet& maps, const MapEntry& entry) {
156 std::map<uint64_t, const MapEntry*>& map = maps.maps;
157 auto it = map.lower_bound(entry.start_addr);
158 // Remove overlapped entry with start_addr < entry.start_addr.
159 if (it != map.begin()) {
160 auto it2 = it;
161 --it2;
162 if (it2->second->get_end_addr() > entry.get_end_addr()) {
163 map.emplace(entry.get_end_addr(),
164 AllocateMap(RemoveFirstPartOfMapEntry(it2->second, entry.get_end_addr())));
165 }
166 if (it2->second->get_end_addr() > entry.start_addr) {
167 it2->second =
168 AllocateMap(RemoveSecondPartOfMapEntry(it2->second, entry.start_addr - it2->first));
169 }
170 }
171 // Remove overlapped entries with start_addr >= entry.start_addr.
172 while (it != map.end() && it->second->get_end_addr() <= entry.get_end_addr()) {
173 it = map.erase(it);
174 }
175 if (it != map.end() && it->second->start_addr < entry.get_end_addr()) {
176 map.emplace(entry.get_end_addr(),
177 AllocateMap(RemoveFirstPartOfMapEntry(it->second, entry.get_end_addr())));
178 map.erase(it);
179 }
180 // Insert the new entry.
181 map.emplace(entry.start_addr, AllocateMap(entry));
182 maps.version++;
183 }
184
FindMapByAddr(const MapSet & maps,uint64_t addr)185 static const MapEntry* FindMapByAddr(const MapSet& maps, uint64_t addr) {
186 auto it = maps.maps.upper_bound(addr);
187 if (it != maps.maps.begin()) {
188 --it;
189 if (it->second->get_end_addr() > addr) {
190 return it->second;
191 }
192 }
193 return nullptr;
194 }
195
FindMap(const ThreadEntry * thread,uint64_t ip,bool in_kernel)196 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) {
197 const MapEntry* result = nullptr;
198 if (!in_kernel) {
199 result = FindMapByAddr(*thread->maps, ip);
200 } else {
201 result = FindMapByAddr(kernel_maps_, ip);
202 }
203 return result != nullptr ? result : &unknown_map_;
204 }
205
FindMap(const ThreadEntry * thread,uint64_t ip)206 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip) {
207 const MapEntry* result = FindMapByAddr(*thread->maps, ip);
208 if (result != nullptr) {
209 return result;
210 }
211 result = FindMapByAddr(kernel_maps_, ip);
212 return result != nullptr ? result : &unknown_map_;
213 }
214
FindSymbol(const MapEntry * map,uint64_t ip,uint64_t * pvaddr_in_file,Dso ** pdso)215 const Symbol* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip,
216 uint64_t* pvaddr_in_file, Dso** pdso) {
217 uint64_t vaddr_in_file = 0;
218 const Symbol* symbol = nullptr;
219 Dso* dso = map->dso;
220 if (map->flags & map_flags::PROT_JIT_SYMFILE_MAP) {
221 vaddr_in_file = ip;
222 } else {
223 vaddr_in_file = dso->IpToVaddrInFile(ip, map->start_addr, map->pgoff);
224 }
225 symbol = dso->FindSymbol(vaddr_in_file);
226 if (symbol == nullptr && dso->type() == DSO_KERNEL_MODULE) {
227 // If the ip address hits the vmlinux, or hits a kernel module, but we can't find its symbol
228 // in the kernel module file, then find its symbol in /proc/kallsyms or vmlinux.
229 vaddr_in_file = ip;
230 dso = kernel_dso_.get();
231 symbol = dso->FindSymbol(vaddr_in_file);
232 }
233
234 if (symbol == nullptr) {
235 if (show_ip_for_unknown_symbol_) {
236 std::string name = android::base::StringPrintf(
237 "%s%s[+%" PRIx64 "]", (show_mark_for_unknown_symbol_ ? "*" : ""),
238 dso->FileName().c_str(), vaddr_in_file);
239 dso->AddUnknownSymbol(vaddr_in_file, name);
240 symbol = dso->FindSymbol(vaddr_in_file);
241 CHECK(symbol != nullptr);
242 } else {
243 symbol = &unknown_symbol_;
244 }
245 }
246 if (pvaddr_in_file != nullptr) {
247 *pvaddr_in_file = vaddr_in_file;
248 }
249 if (pdso != nullptr) {
250 *pdso = dso;
251 }
252 return symbol;
253 }
254
FindKernelSymbol(uint64_t ip)255 const Symbol* ThreadTree::FindKernelSymbol(uint64_t ip) {
256 const MapEntry* map = FindMap(nullptr, ip, true);
257 return FindSymbol(map, ip, nullptr);
258 }
259
ClearThreadAndMap()260 void ThreadTree::ClearThreadAndMap() {
261 thread_tree_.clear();
262 thread_comm_storage_.clear();
263 map_set_storage_.clear();
264 kernel_maps_.maps.clear();
265 map_storage_.clear();
266 }
267
AddDsoInfo(const std::string & file_path,uint32_t file_type,uint64_t min_vaddr,uint64_t file_offset_of_min_vaddr,std::vector<Symbol> * symbols,const std::vector<uint64_t> & dex_file_offsets)268 void ThreadTree::AddDsoInfo(const std::string& file_path, uint32_t file_type,
269 uint64_t min_vaddr, uint64_t file_offset_of_min_vaddr,
270 std::vector<Symbol>* symbols,
271 const std::vector<uint64_t>& dex_file_offsets) {
272 DsoType dso_type = static_cast<DsoType>(file_type);
273 Dso* dso = nullptr;
274 if (dso_type == DSO_KERNEL || dso_type == DSO_KERNEL_MODULE) {
275 dso = FindKernelDsoOrNew(file_path);
276 } else {
277 dso = FindUserDsoOrNew(file_path, 0, dso_type);
278 }
279 dso->SetMinExecutableVaddr(min_vaddr, file_offset_of_min_vaddr);
280 dso->SetSymbols(symbols);
281 for (uint64_t offset : dex_file_offsets) {
282 dso->AddDexFileOffset(offset);
283 }
284 }
285
AddDexFileOffset(const std::string & file_path,uint64_t dex_file_offset)286 void ThreadTree::AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset) {
287 Dso* dso = FindUserDsoOrNew(file_path, 0, DSO_DEX_FILE);
288 dso->AddDexFileOffset(dex_file_offset);
289 }
290
Update(const Record & record)291 void ThreadTree::Update(const Record& record) {
292 if (record.type() == PERF_RECORD_MMAP) {
293 const MmapRecord& r = *static_cast<const MmapRecord*>(&record);
294 if (r.InKernel()) {
295 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
296 } else {
297 AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len, r.data->pgoff, r.filename);
298 }
299 } else if (record.type() == PERF_RECORD_MMAP2) {
300 const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record);
301 if (r.InKernel()) {
302 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, r.filename);
303 } else {
304 std::string filename = (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP)
305 ? "[unknown]"
306 : r.filename;
307 AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len, r.data->pgoff, filename,
308 r.data->prot);
309 }
310 } else if (record.type() == PERF_RECORD_COMM) {
311 const CommRecord& r = *static_cast<const CommRecord*>(&record);
312 SetThreadName(r.data->pid, r.data->tid, r.comm);
313 } else if (record.type() == PERF_RECORD_FORK) {
314 const ForkRecord& r = *static_cast<const ForkRecord*>(&record);
315 ForkThread(r.data->pid, r.data->tid, r.data->ppid, r.data->ptid);
316 } else if (record.type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
317 const auto& r = *static_cast<const KernelSymbolRecord*>(&record);
318 Dso::SetKallsyms(std::move(r.kallsyms));
319 }
320 }
321
GetAllDsos() const322 std::vector<Dso*> ThreadTree::GetAllDsos() const {
323 std::vector<Dso*> result;
324 result.push_back(kernel_dso_.get());
325 for (auto& p : module_dso_tree_) {
326 result.push_back(p.second.get());
327 }
328 for (auto& p : user_dso_tree_) {
329 result.push_back(p.second.get());
330 }
331 result.push_back(unknown_dso_.get());
332 return result;
333 }
334
GetAllThreads() const335 std::vector<const ThreadEntry*> ThreadTree::GetAllThreads() const {
336 std::vector<const ThreadEntry*> threads;
337 for (auto& pair : thread_tree_) {
338 threads.push_back(pair.second.get());
339 }
340 return threads;
341 }
342
343 } // namespace simpleperf
344