thread_tree.cpp revision 66dd09e8e2407082ce93bf0784de641298131912
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 2760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuibool MapComparator::operator()(const MapEntry* map1, const MapEntry* map2) const { 2860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (map1->start_addr != map2->start_addr) { 2960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return map1->start_addr < map2->start_addr; 3060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 31b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui // Compare map->len instead of map->get_end_addr() here. Because we set map's len 32b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui // to std::numeric_limits<uint64_t>::max() in FindMapByAddr(), which makes 33b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui // map->get_end_addr() overflow. 34b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui if (map1->len != map2->len) { 35b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui return map1->len < map2->len; 3660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 3760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (map1->time != map2->time) { 3860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return map1->time < map2->time; 3960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 4060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return false; 4160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 4260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 4360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuivoid ThreadTree::AddThread(int pid, int tid, const std::string& comm) { 4460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = thread_tree_.find(tid); 4560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it == thread_tree_.end()) { 4660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui ThreadEntry* thread = new ThreadEntry{ 4760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui pid, tid, 4860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui "unknown", // comm 4960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui std::set<MapEntry*, MapComparator>(), // maps 5060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui }; 5160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread))); 5260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui CHECK(pair.second); 5360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it = pair.first; 5460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 5560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui thread_comm_storage_.push_back(std::unique_ptr<std::string>(new std::string(comm))); 5660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it->second->comm = thread_comm_storage_.back()->c_str(); 5760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 5860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 5960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuivoid ThreadTree::ForkThread(int pid, int tid, int ppid, int ptid) { 6060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui ThreadEntry* parent = FindThreadOrNew(ppid, ptid); 6160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui ThreadEntry* child = FindThreadOrNew(pid, tid); 6260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui child->comm = parent->comm; 6360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui child->maps = parent->maps; 6460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 6560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 6660a0ea96c0fb9e807c899759256df5e20bd904bdYabin CuiThreadEntry* ThreadTree::FindThreadOrNew(int pid, int tid) { 6760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = thread_tree_.find(tid); 6860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it == thread_tree_.end()) { 6960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui AddThread(pid, tid, "unknown"); 7060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it = thread_tree_.find(tid); 7160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } else { 725f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui if (pid != it->second.get()->pid) { 735f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui // TODO: b/22185053. 745f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui LOG(DEBUG) << "unexpected (pid, tid) pair: expected (" << it->second.get()->pid << ", " << tid 755f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui << "), actual (" << pid << ", " << tid << ")"; 765f2a170a577258a2792bd768a8a776a562f4e6c2Yabin Cui } 7760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 7860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return it->second.get(); 7960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 8060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 8160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuivoid ThreadTree::AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time, 8260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui const std::string& filename) { 8360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui // kernel map len can be 0 when record command is not run in supervisor mode. 8460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (len == 0) { 8560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return; 8660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 87c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui Dso* dso = FindKernelDsoOrNew(filename); 88547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* map = AllocateMap(MapEntry(start_addr, len, pgoff, time, dso)); 89547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui FixOverlappedMap(&kernel_map_tree_, map); 9060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto pair = kernel_map_tree_.insert(map); 9160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui CHECK(pair.second); 9260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 9360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 94c84856093e8bf4350d30fc521dc0f1c800c5270bYabin CuiDso* ThreadTree::FindKernelDsoOrNew(const std::string& filename) { 9560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (filename == DEFAULT_KERNEL_MMAP_NAME) { 9660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (kernel_dso_ == nullptr) { 97c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui kernel_dso_ = Dso::CreateDso(DSO_KERNEL); 9860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 9960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return kernel_dso_.get(); 10060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 10160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = module_dso_tree_.find(filename); 10260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it == module_dso_tree_.end()) { 103c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui module_dso_tree_[filename] = Dso::CreateDso(DSO_KERNEL_MODULE, filename); 10460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it = module_dso_tree_.find(filename); 10560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 10660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return it->second.get(); 10760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 10860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 10960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuivoid ThreadTree::AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff, 11060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui uint64_t time, const std::string& filename) { 11160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui ThreadEntry* thread = FindThreadOrNew(pid, tid); 112c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui Dso* dso = FindUserDsoOrNew(filename); 113547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* map = AllocateMap(MapEntry(start_addr, len, pgoff, time, dso)); 114547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui FixOverlappedMap(&thread->maps, map); 11560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto pair = thread->maps.insert(map); 11660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui CHECK(pair.second); 11760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 11860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 119c84856093e8bf4350d30fc521dc0f1c800c5270bYabin CuiDso* ThreadTree::FindUserDsoOrNew(const std::string& filename) { 12060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = user_dso_tree_.find(filename); 12160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it == user_dso_tree_.end()) { 122c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cui user_dso_tree_[filename] = Dso::CreateDso(DSO_ELF_FILE, filename); 12360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui it = user_dso_tree_.find(filename); 12460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 12560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return it->second.get(); 12660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 12760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 128547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin CuiMapEntry* ThreadTree::AllocateMap(const MapEntry& value) { 129547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* map = new MapEntry(value); 130547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui map_storage_.push_back(std::unique_ptr<MapEntry>(map)); 131547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui return map; 132547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui} 133547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui 134547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cuivoid ThreadTree::FixOverlappedMap(std::set<MapEntry*, MapComparator>* map_set, const MapEntry* map) { 135547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui for (auto it = map_set->begin(); it != map_set->end();) { 136547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui if ((*it)->start_addr >= map->get_end_addr()) { 137547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui // No more overlapped maps. 138547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui break; 139547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 140547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui if ((*it)->get_end_addr() <= map->start_addr) { 141547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui ++it; 142547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } else { 143547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* old = *it; 144547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui if (old->start_addr < map->start_addr) { 145547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* before = AllocateMap(MapEntry(old->start_addr, map->start_addr - old->start_addr, 146547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui old->pgoff, old->time, old->dso)); 147547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui map_set->insert(before); 148547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 149547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui if (old->get_end_addr() > map->get_end_addr()) { 150547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry* after = AllocateMap( 151547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry(map->get_end_addr(), old->get_end_addr() - map->get_end_addr(), 152547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui map->get_end_addr() - old->start_addr + old->pgoff, old->time, old->dso)); 153547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui map_set->insert(after); 154547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 155547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui 156547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui it = map_set->erase(it); 157547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 158547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui } 159547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui} 160547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui 16160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuistatic bool IsAddrInMap(uint64_t addr, const MapEntry* map) { 162547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui return (addr >= map->start_addr && addr < map->get_end_addr()); 16360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 16460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 16560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuistatic MapEntry* FindMapByAddr(const std::set<MapEntry*, MapComparator>& maps, uint64_t addr) { 16660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui // Construct a map_entry which is strictly after the searched map_entry, based on MapComparator. 167547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui MapEntry find_map(addr, std::numeric_limits<uint64_t>::max(), 0, 168547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui std::numeric_limits<uint64_t>::max(), nullptr); 16960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui auto it = maps.upper_bound(&find_map); 17060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (it != maps.begin() && IsAddrInMap(addr, *--it)) { 17160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return *it; 17260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 17360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return nullptr; 17460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 17560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 17660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cuiconst MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel) { 17760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui MapEntry* result = nullptr; 17860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (!in_kernel) { 17960a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui result = FindMapByAddr(thread->maps, ip); 18060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } else { 18160a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui result = FindMapByAddr(kernel_map_tree_, ip); 18260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 18360a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return result != nullptr ? result : &unknown_map_; 18460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 18560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui 186c84856093e8bf4350d30fc521dc0f1c800c5270bYabin Cuiconst Symbol* ThreadTree::FindSymbol(const MapEntry* map, uint64_t ip) { 187547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui uint64_t vaddr_in_file; 18860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (map->dso == kernel_dso_.get()) { 189547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui vaddr_in_file = ip; 19060a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } else { 191547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui vaddr_in_file = ip - map->start_addr + map->dso->MinVirtualAddress(); 19260a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 193547c60e4dd29c5788d5948ad348acf33a55d6ed6Yabin Cui const Symbol* symbol = map->dso->FindSymbol(vaddr_in_file); 19460a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui if (symbol == nullptr) { 19560a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui symbol = &unknown_symbol_; 19660a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui } 19760a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui return symbol; 19860a0ea96c0fb9e807c899759256df5e20bd904bdYabin Cui} 19973d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui 200b7f481f59126456b0e708a76f40fa88224f3531bYabin Cuivoid ThreadTree::Clear() { 201b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui thread_tree_.clear(); 202b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui thread_comm_storage_.clear(); 203b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui kernel_map_tree_.clear(); 204b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui map_storage_.clear(); 205b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui kernel_dso_.reset(); 206b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui module_dso_tree_.clear(); 207b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui user_dso_tree_.clear(); 208b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui} 209b7f481f59126456b0e708a76f40fa88224f3531bYabin Cui 210f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cuivoid BuildThreadTree(const Record& record, ThreadTree* thread_tree) { 211f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui if (record.header.type == PERF_RECORD_MMAP) { 212f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui const MmapRecord& r = *static_cast<const MmapRecord*>(&record); 213f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui if ((r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL) { 214f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff, r.sample_id.time_data.time, 215f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui r.filename); 216f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else { 217f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff, 218f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui r.sample_id.time_data.time, r.filename); 219f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } 220f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else if (record.header.type == PERF_RECORD_MMAP2) { 221f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record); 222f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui if ((r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL) { 223f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff, r.sample_id.time_data.time, 224f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui r.filename); 225f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else { 226f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui std::string filename = 227f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename; 228f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff, 229f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui r.sample_id.time_data.time, filename); 23073d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui } 231f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else if (record.header.type == PERF_RECORD_COMM) { 232f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui const CommRecord& r = *static_cast<const CommRecord*>(&record); 233f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->AddThread(r.data.pid, r.data.tid, r.comm); 234f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui } else if (record.header.type == PERF_RECORD_FORK) { 235f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui const ForkRecord& r = *static_cast<const ForkRecord*>(&record); 236f8258896a364b6c996b6d6816b88849cc8d0a488Yabin Cui thread_tree->ForkThread(r.data.pid, r.data.tid, r.data.ppid, r.data.ptid); 23773d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui } 23873d8078e6e5ef92b2301386ce96260bd99efacbbYabin Cui} 239