2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "thread_tree.h"
23 #include <android-base/logging.h>
24 #include <android-base/stringprintf.h>
26 #include "perf_event.h"
29 namespace simpleperf {
31 bool MapComparator::operator()(const MapEntry* map1,
32 const MapEntry* map2) const {
33 if (map1->start_addr != map2->start_addr) {
34 return map1->start_addr < map2->start_addr;
36 // Compare map->len instead of map->get_end_addr() here. Because we set map's
37 // len to std::numeric_limits<uint64_t>::max() in FindMapByAddr(), which makes
38 // map->get_end_addr() overflow.
39 if (map1->len != map2->len) {
40 return map1->len < map2->len;
42 if (map1->time != map2->time) {
43 return map1->time < map2->time;
48 void ThreadTree::SetThreadName(int pid, int tid, const std::string& comm) {
49 ThreadEntry* thread = FindThreadOrNew(pid, tid);
50 if (comm != thread->comm) {
51 thread_comm_storage_.push_back(
52 std::unique_ptr<std::string>(new std::string(comm)));
53 thread->comm = thread_comm_storage_.back()->c_str();
57 void ThreadTree::ForkThread(int pid, int tid, int ppid, int ptid) {
58 ThreadEntry* parent = FindThreadOrNew(ppid, ptid);
59 ThreadEntry* child = FindThreadOrNew(pid, tid);
60 child->comm = parent->comm;
62 // Copy maps from parent process.
63 *child->maps = *parent->maps;
67 ThreadEntry* ThreadTree::FindThreadOrNew(int pid, int tid) {
68 auto it = thread_tree_.find(tid);
69 if (it == thread_tree_.end()) {
70 return CreateThread(pid, tid);
72 if (pid != it->second.get()->pid) {
74 LOG(DEBUG) << "unexpected (pid, tid) pair: expected ("
75 << it->second.get()->pid << ", " << tid << "), actual (" << pid
76 << ", " << tid << ")";
79 return it->second.get();
82 ThreadEntry* ThreadTree::CreateThread(int pid, int tid) {
83 MapSet* maps = nullptr;
86 map_set_storage_.push_back(std::unique_ptr<MapSet>(maps));
88 // Share maps among threads in the same thread group.
89 ThreadEntry* process = FindThreadOrNew(pid, pid);
92 ThreadEntry* thread = new ThreadEntry{
97 auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread)));
102 void ThreadTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
103 uint64_t time, const std::string& filename) {
104 // kernel map len can be 0 when record command is not run in supervisor mode.
108 Dso* dso = FindKernelDsoOrNew(filename);
110 AllocateMap(MapEntry(start_addr, len, pgoff, time, dso, true));
111 FixOverlappedMap(&kernel_maps_, map);
112 auto pair = kernel_maps_.maps.insert(map);
116 Dso* ThreadTree::FindKernelDsoOrNew(const std::string& filename) {
117 if (filename == DEFAULT_KERNEL_MMAP_NAME ||
118 filename == DEFAULT_KERNEL_MMAP_NAME_PERF) {
119 return kernel_dso_.get();
121 auto it = module_dso_tree_.find(filename);
122 if (it == module_dso_tree_.end()) {
123 module_dso_tree_[filename] = Dso::CreateDso(DSO_KERNEL_MODULE, filename);
124 it = module_dso_tree_.find(filename);
126 return it->second.get();
129 void ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr,
130 uint64_t len, uint64_t pgoff, uint64_t time,
131 const std::string& filename) {
132 ThreadEntry* thread = FindThreadOrNew(pid, tid);
133 Dso* dso = FindUserDsoOrNew(filename, start_addr);
135 AllocateMap(MapEntry(start_addr, len, pgoff, time, dso, false));
136 FixOverlappedMap(thread->maps, map);
137 auto pair = thread->maps->maps.insert(map);
139 thread->maps->version++;
142 Dso* ThreadTree::FindUserDsoOrNew(const std::string& filename, uint64_t start_addr) {
143 auto it = user_dso_tree_.find(filename);
144 if (it == user_dso_tree_.end()) {
145 bool force_64bit = start_addr > UINT_MAX;
146 user_dso_tree_[filename] = Dso::CreateDso(DSO_ELF_FILE, filename, force_64bit);
147 it = user_dso_tree_.find(filename);
149 return it->second.get();
152 MapEntry* ThreadTree::AllocateMap(const MapEntry& value) {
153 MapEntry* map = new MapEntry(value);
154 map_storage_.push_back(std::unique_ptr<MapEntry>(map));
158 void ThreadTree::FixOverlappedMap(MapSet* maps, const MapEntry* map) {
159 for (auto it = maps->maps.begin(); it != maps->maps.end();) {
160 if ((*it)->start_addr >= map->get_end_addr()) {
161 // No more overlapped maps.
164 if ((*it)->get_end_addr() <= map->start_addr) {
168 if (old->start_addr < map->start_addr) {
169 MapEntry* before = AllocateMap(
170 MapEntry(old->start_addr, map->start_addr - old->start_addr,
171 old->pgoff, old->time, old->dso, old->in_kernel));
172 maps->maps.insert(before);
174 if (old->get_end_addr() > map->get_end_addr()) {
175 MapEntry* after = AllocateMap(MapEntry(
176 map->get_end_addr(), old->get_end_addr() - map->get_end_addr(),
177 map->get_end_addr() - old->start_addr + old->pgoff, old->time,
178 old->dso, old->in_kernel));
179 maps->maps.insert(after);
182 it = maps->maps.erase(it);
187 static bool IsAddrInMap(uint64_t addr, const MapEntry* map) {
188 return (addr >= map->start_addr && addr < map->get_end_addr());
191 static MapEntry* FindMapByAddr(const MapSet& maps, uint64_t addr) {
192 // Construct a map_entry which is strictly after the searched map_entry, based
194 MapEntry find_map(addr, std::numeric_limits<uint64_t>::max(), 0,
195 std::numeric_limits<uint64_t>::max(), nullptr, false);
196 auto it = maps.maps.upper_bound(&find_map);
197 if (it != maps.maps.begin() && IsAddrInMap(addr, *--it)) {
203 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip,
205 MapEntry* result = nullptr;
207 result = FindMapByAddr(*thread->maps, ip);
209 result = FindMapByAddr(kernel_maps_, ip);
211 return result != nullptr ? result : &unknown_map_;
214 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip) {
215 MapEntry* result = FindMapByAddr(*thread->maps, ip);
216 if (result != nullptr) {
219 result = FindMapByAddr(kernel_maps_, ip);
220 return result != nullptr ? result : &unknown_map_;
223 const Symbol* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip,
224 uint64_t* pvaddr_in_file, Dso** pdso) {
225 uint64_t vaddr_in_file;
226 const Symbol* symbol = nullptr;
228 if (!map->in_kernel) {
229 // Find symbol in user space shared libraries.
230 vaddr_in_file = ip - map->start_addr + map->dso->MinVirtualAddress();
231 symbol = dso->FindSymbol(vaddr_in_file);
233 if (dso != kernel_dso_.get()) {
234 // Find symbol in kernel modules.
235 vaddr_in_file = ip - map->start_addr + map->dso->MinVirtualAddress();
236 symbol = dso->FindSymbol(vaddr_in_file);
238 if (symbol == nullptr) {
239 // If the ip address hits the vmlinux, or hits a kernel module, but we can't find its symbol
240 // in the kernel module file, then find its symbol in /proc/kallsyms or vmlinux.
242 dso = kernel_dso_.get();
243 symbol = dso->FindSymbol(vaddr_in_file);
247 if (symbol == nullptr) {
248 if (show_ip_for_unknown_symbol_) {
249 std::string name = android::base::StringPrintf(
250 "%s%s[+%" PRIx64 "]", (show_mark_for_unknown_symbol_ ? "*" : ""),
251 dso->FileName().c_str(), vaddr_in_file);
252 dso->AddUnknownSymbol(vaddr_in_file, name);
253 symbol = dso->FindSymbol(vaddr_in_file);
254 CHECK(symbol != nullptr);
256 symbol = &unknown_symbol_;
259 if (pvaddr_in_file != nullptr) {
260 *pvaddr_in_file = vaddr_in_file;
262 if (pdso != nullptr) {
268 const Symbol* ThreadTree::FindKernelSymbol(uint64_t ip) {
269 const MapEntry* map = FindMap(nullptr, ip, true);
270 return FindSymbol(map, ip, nullptr);
273 void ThreadTree::ClearThreadAndMap() {
274 thread_tree_.clear();
275 thread_comm_storage_.clear();
276 map_set_storage_.clear();
277 kernel_maps_.maps.clear();
278 map_storage_.clear();
281 void ThreadTree::AddDsoInfo(const std::string& file_path, uint32_t file_type,
282 uint64_t min_vaddr, std::vector<Symbol>* symbols) {
283 DsoType dso_type = static_cast<DsoType>(file_type);
285 if (dso_type == DSO_KERNEL || dso_type == DSO_KERNEL_MODULE) {
286 dso = FindKernelDsoOrNew(file_path);
288 dso = FindUserDsoOrNew(file_path);
290 dso->SetMinVirtualAddress(min_vaddr);
291 dso->SetSymbols(symbols);
294 void ThreadTree::Update(const Record& record) {
295 if (record.type() == PERF_RECORD_MMAP) {
296 const MmapRecord& r = *static_cast<const MmapRecord*>(&record);
298 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff,
299 r.sample_id.time_data.time, r.filename);
301 AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len,
302 r.data->pgoff, r.sample_id.time_data.time, r.filename);
304 } else if (record.type() == PERF_RECORD_MMAP2) {
305 const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record);
307 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff,
308 r.sample_id.time_data.time, r.filename);
310 std::string filename = (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP)
313 AddThreadMap(r.data->pid, r.data->tid, r.data->addr, r.data->len,
314 r.data->pgoff, r.sample_id.time_data.time, filename);
316 } else if (record.type() == PERF_RECORD_COMM) {
317 const CommRecord& r = *static_cast<const CommRecord*>(&record);
318 SetThreadName(r.data->pid, r.data->tid, r.comm);
319 } else if (record.type() == PERF_RECORD_FORK) {
320 const ForkRecord& r = *static_cast<const ForkRecord*>(&record);
321 ForkThread(r.data->pid, r.data->tid, r.data->ppid, r.data->ptid);
322 } else if (record.type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
323 const auto& r = *static_cast<const KernelSymbolRecord*>(&record);
324 Dso::SetKallsyms(std::move(r.kallsyms));
328 std::vector<Dso*> ThreadTree::GetAllDsos() const {
329 std::vector<Dso*> result;
330 result.push_back(kernel_dso_.get());
331 for (auto& p : module_dso_tree_) {
332 result.push_back(p.second.get());
334 for (auto& p : user_dso_tree_) {
335 result.push_back(p.second.get());
337 result.push_back(unknown_dso_.get());
341 std::vector<const ThreadEntry*> ThreadTree::GetAllThreads() const {
342 std::vector<const ThreadEntry*> threads;
343 for (auto& pair : thread_tree_) {
344 threads.push_back(pair.second.get());
349 } // namespace simpleperf