160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui/* 260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * Copyright (C) 2015 The Android Open Source Project 360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * 460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * Licensed under the Apache License, Version 2.0 (the "License"); 560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * you may not use this file except in compliance with the License. 660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * You may obtain a copy of the License at 760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * 860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * http://www.apache.org/licenses/LICENSE-2.0 960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * 1060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * Unless required by applicable law or agreed to in writing, software 1160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * distributed under the License is distributed on an "AS IS" BASIS, 1260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * See the License for the specific language governing permissions and 1460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui * limitations under the License. 1560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui */ 1660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 1760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui#include "thread_tree.h" 1860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 19c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui#include <limits> 20c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui 2166dd09e8e2407082ce93bf0784de641298131912Elliott Hughes#include <android-base/logging.h> 22c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui 2360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui#include "environment.h" 2473d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui#include "perf_event.h" 2573d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui#include "record.h" 2660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 272362310b546b157eeee352e72b1585363002d662Yabin Cuinamespace simpleperf { 282362310b546b157eeee352e72b1585363002d662Yabin Cui 2960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuibool MapComparator::operator()(const MapEntry* map1, const MapEntry* map2) const { 3060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (map1->start_addr != map2->start_addr) { 3160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return map1->start_addr < map2->start_addr; 3260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 33b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui // Compare map->len instead of map->get_end_addr() here. Because we set map's len 34b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui // to std::numeric_limits<uint64_t>::max() in FindMapByAddr(), which makes 35b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui // map->get_end_addr() overflow. 36b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui if (map1->len != map2->len) { 37b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui return map1->len < map2->len; 3860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 3960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (map1->time != map2->time) { 4060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return map1->time < map2->time; 4160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 4260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return false; 4360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 4460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 4560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuivoid ThreadTree::AddThread(int pid, int tid, const std::string& comm) { 4660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = thread_tree_.find(tid); 4760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it == thread_tree_.end()) { 4860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui ThreadEntry* thread = new ThreadEntry{ 4960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui pid, tid, 5060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui "unknown", // comm 5160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui std::set<MapEntry*, MapComparator>(), // maps 5260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui }; 5360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread))); 5460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui CHECK(pair.second); 5560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it = pair.first; 5660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 5760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui thread_comm_storage_.push_back(std::unique_ptr<std::string>(new std::string(comm))); 5860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it->second->comm = thread_comm_storage_.back()->c_str(); 5960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 6060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 6160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuivoid ThreadTree::ForkThread(int pid, int tid, int ppid, int ptid) { 6260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui ThreadEntry* parent = FindThreadOrNew(ppid, ptid); 6360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui ThreadEntry* child = FindThreadOrNew(pid, tid); 6460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui child->comm = parent->comm; 6560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui child->maps = parent->maps; 6660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 6760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 6860a0ea96c0fb9e807c899759256df5e20bd904bdYabin CuiThreadEntry* ThreadTree::FindThreadOrNew(int pid, int tid) { 6960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = thread_tree_.find(tid); 7060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it == thread_tree_.end()) { 7160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui AddThread(pid, tid, "unknown"); 7260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it = thread_tree_.find(tid); 7360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } else { 745f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui if (pid != it->second.get()->pid) { 755f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui // TODO: b/22185053. 765f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui LOG(DEBUG) << "unexpected (pid, tid) pair: expected (" << it->second.get()->pid << ", " << tid 775f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui << "), actual (" << pid << ", " << tid << ")"; 785f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui } 7960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 8060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return it->second.get(); 8160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 8260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 8360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuivoid ThreadTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time, 8460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui const std::string& filename) { 8560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui // kernel map len can be 0 when record command is not run in supervisor mode. 8660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (len == 0) { 8760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return; 8860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 89c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui Dso* dso = FindKernelDsoOrNew(filename); 90547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* map = AllocateMap(MapEntry(start_addr, len, pgoff, time, dso)); 91547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui FixOverlappedMap(&kernel_map_tree_, map); 9260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto pair = kernel_map_tree_.insert(map); 9360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui CHECK(pair.second); 9460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 9560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 96c84856093e8bf4350d30fc521dc0f1c800c5270bYabin CuiDso* ThreadTree::FindKernelDsoOrNew(const std::string& filename) { 9760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (filename == DEFAULT_KERNEL_MMAP_NAME) { 9860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (kernel_dso_ == nullptr) { 99c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui kernel_dso_ = Dso::CreateDso(DSO_KERNEL); 10060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 10160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return kernel_dso_.get(); 10260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 10360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = module_dso_tree_.find(filename); 10460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it == module_dso_tree_.end()) { 105c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui module_dso_tree_[filename] = Dso::CreateDso(DSO_KERNEL_MODULE, filename); 10660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it = module_dso_tree_.find(filename); 10760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 10860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return it->second.get(); 10960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 11060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 11160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuivoid ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff, 11260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui uint64_t time, const std::string& filename) { 11360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui ThreadEntry* thread = FindThreadOrNew(pid, tid); 114c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui Dso* dso = FindUserDsoOrNew(filename); 115547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* map = AllocateMap(MapEntry(start_addr, len, pgoff, time, dso)); 116547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui FixOverlappedMap(&thread->maps, map); 11760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto pair = thread->maps.insert(map); 11860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui CHECK(pair.second); 11960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 12060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 121c84856093e8bf4350d30fc521dc0f1c800c5270bYabin CuiDso* ThreadTree::FindUserDsoOrNew(const std::string& filename) { 12260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = user_dso_tree_.find(filename); 12360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it == user_dso_tree_.end()) { 124c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui user_dso_tree_[filename] = Dso::CreateDso(DSO_ELF_FILE, filename); 12560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it = user_dso_tree_.find(filename); 12660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 12760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return it->second.get(); 12860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 12960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 130547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin CuiMapEntry* ThreadTree::AllocateMap(const MapEntry& value) { 131547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* map = new MapEntry(value); 132547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui map_storage_.push_back(std::unique_ptr<MapEntry>(map)); 133547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui return map; 134547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui} 135547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui 136547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cuivoid ThreadTree::FixOverlappedMap(std::set<MapEntry*, MapComparator>* map_set, const MapEntry* map) { 137547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui for (auto it = map_set->begin(); it != map_set->end();) { 138547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui if ((*it)->start_addr >= map->get_end_addr()) { 139547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui // No more overlapped maps. 140547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui break; 141547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 142547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui if ((*it)->get_end_addr() <= map->start_addr) { 143547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui ++it; 144547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } else { 145547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* old = *it; 146547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui if (old->start_addr < map->start_addr) { 147547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* before = AllocateMap(MapEntry(old->start_addr, map->start_addr - old->start_addr, 148547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui old->pgoff, old->time, old->dso)); 149547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui map_set->insert(before); 150547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 151547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui if (old->get_end_addr() > map->get_end_addr()) { 152547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* after = AllocateMap( 153547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry(map->get_end_addr(), old->get_end_addr() - map->get_end_addr(), 154547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui map->get_end_addr() - old->start_addr + old->pgoff, old->time, old->dso)); 155547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui map_set->insert(after); 156547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 157547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui 158547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui it = map_set->erase(it); 159547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 160547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 161547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui} 162547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui 16360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuistatic bool IsAddrInMap(uint64_t addr, const MapEntry* map) { 164547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui return (addr >= map->start_addr && addr < map->get_end_addr()); 16560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 16660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 16760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuistatic MapEntry* FindMapByAddr(const std::set<MapEntry*, MapComparator>& maps, uint64_t addr) { 16860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui // Construct a map_entry which is strictly after the searched map_entry, based on MapComparator. 169547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry find_map(addr, std::numeric_limits<uint64_t>::max(), 0, 170547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui std::numeric_limits<uint64_t>::max(), nullptr); 17160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = maps.upper_bound(&find_map); 17260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it != maps.begin() && IsAddrInMap(addr, *--it)) { 17360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return *it; 17460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 17560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return nullptr; 17660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 17760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 17860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuiconst MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) { 17960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui MapEntry* result = nullptr; 18060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (!in_kernel) { 18160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui result = FindMapByAddr(thread->maps, ip); 18260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } else { 18360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui result = FindMapByAddr(kernel_map_tree_, ip); 18460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 18560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return result != nullptr ? result : &unknown_map_; 18660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 18760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 188c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cuiconst Symbol* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip) { 189547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui uint64_t vaddr_in_file; 19060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (map->dso == kernel_dso_.get()) { 191547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui vaddr_in_file = ip; 19260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } else { 193547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui vaddr_in_file = ip - map->start_addr + map->dso->MinVirtualAddress(); 19460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 195547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui const Symbol* symbol = map->dso->FindSymbol(vaddr_in_file); 19660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (symbol == nullptr) { 19760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui symbol = &unknown_symbol_; 19860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 19960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return symbol; 20060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 20173d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui 202b7f481f59126456b0e708a76f40fa88224f3531bYabin Cuivoid ThreadTree::Clear() { 203b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui thread_tree_.clear(); 204b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui thread_comm_storage_.clear(); 205b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui kernel_map_tree_.clear(); 206b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui map_storage_.clear(); 207b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui kernel_dso_.reset(); 208b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui module_dso_tree_.clear(); 209b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui user_dso_tree_.clear(); 210b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui} 211b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui 2122362310b546b157eeee352e72b1585363002d662Yabin Cui} // namespace simpleperf 2132362310b546b157eeee352e72b1585363002d662Yabin Cui 214f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cuivoid BuildThreadTree(const Record& record, ThreadTree* thread_tree) { 215f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui if (record.header.type == PERF_RECORD_MMAP) { 216f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui const MmapRecord& r = *static_cast<const MmapRecord*>(&record); 217f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui if ((r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL) { 218f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff, r.sample_id.time_data.time, 219f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui r.filename); 220f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else { 221f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff, 222f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui r.sample_id.time_data.time, r.filename); 223f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } 224f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else if (record.header.type == PERF_RECORD_MMAP2) { 225f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record); 226f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui if ((r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL) { 227f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff, r.sample_id.time_data.time, 228f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui r.filename); 229f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else { 230f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui std::string filename = 231f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename; 232f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff, 233f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui r.sample_id.time_data.time, filename); 23473d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui } 235f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else if (record.header.type == PERF_RECORD_COMM) { 236f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui const CommRecord& r = *static_cast<const CommRecord*>(&record); 237f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddThread(r.data.pid, r.data.tid, r.comm); 238f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else if (record.header.type == PERF_RECORD_FORK) { 239f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui const ForkRecord& r = *static_cast<const ForkRecord*>(&record); 240f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->ForkThread(r.data.pid, r.data.tid, r.data.ppid, r.data.ptid); 24173d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui } 24273d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui} 243