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 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; 35 } 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; 41 } 42 if (map1->time != map2->time) { 43 return map1->time < map2->time; 44 } 45 return false; 46 } 47 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(); 54 } 55 } 56 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; 61 if (pid != ppid) { 62 // Copy maps from parent process. 63 *child->maps = *parent->maps; 64 } 65 } 66 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); 71 } else { 72 if (pid != it->second.get()->pid) { 73 // TODO: b/22185053. 74 LOG(DEBUG) << "unexpected (pid, tid) pair: expected (" 75 << it->second.get()->pid << ", " << tid << "), actual (" << pid 76 << ", " << tid << ")"; 77 } 78 } 79 return it->second.get(); 80 } 81 82 ThreadEntry* ThreadTree::CreateThread(int pid, int tid) { 83 MapSet* maps = nullptr; 84 if (pid == tid) { 85 maps = new MapSet; 86 map_set_storage_.push_back(std::unique_ptr<MapSet>(maps)); 87 } else { 88 // Share maps among threads in the same thread group. 89 ThreadEntry* process = FindThreadOrNew(pid, pid); 90 maps = process->maps; 91 } 92 ThreadEntry* thread = new ThreadEntry{ 93 pid, tid, 94 "unknown", 95 maps, 96 }; 97 auto pair = thread_tree_.insert(std::make_pair(tid, std::unique_ptr<ThreadEntry>(thread))); 98 CHECK(pair.second); 99 return thread; 100 } 101 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. 105 if (len == 0) { 106 return; 107 } 108 Dso* dso = FindKernelDsoOrNew(filename); 109 MapEntry* map = 110 AllocateMap(MapEntry(start_addr, len, pgoff, time, dso, true)); 111 FixOverlappedMap(&kernel_maps_, map); 112 auto pair = kernel_maps_.maps.insert(map); 113 CHECK(pair.second); 114 } 115 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(); 120 } 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); 125 } 126 return it->second.get(); 127 } 128 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); 134 MapEntry* map = 135 AllocateMap(MapEntry(start_addr, len, pgoff, time, dso, false)); 136 FixOverlappedMap(thread->maps, map); 137 auto pair = thread->maps->maps.insert(map); 138 CHECK(pair.second); 139 thread->maps->version++; 140 } 141 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); 148 } 149 return it->second.get(); 150 } 151 152 MapEntry* ThreadTree::AllocateMap(const MapEntry& value) { 153 MapEntry* map = new MapEntry(value); 154 map_storage_.push_back(std::unique_ptr<MapEntry>(map)); 155 return map; 156 } 157 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. 162 break; 163 } 164 if ((*it)->get_end_addr() <= map->start_addr) { 165 ++it; 166 } else { 167 MapEntry* old = *it; 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); 173 } 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); 180 } 181 182 it = maps->maps.erase(it); 183 } 184 } 185 } 186 187 static bool IsAddrInMap(uint64_t addr, const MapEntry* map) { 188 return (addr >= map->start_addr && addr < map->get_end_addr()); 189 } 190 191 static MapEntry* FindMapByAddr(const MapSet& maps, uint64_t addr) { 192 // Construct a map_entry which is strictly after the searched map_entry, based 193 // on MapComparator. 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)) { 198 return *it; 199 } 200 return nullptr; 201 } 202 203 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip, 204 bool in_kernel) { 205 MapEntry* result = nullptr; 206 if (!in_kernel) { 207 result = FindMapByAddr(*thread->maps, ip); 208 } else { 209 result = FindMapByAddr(kernel_maps_, ip); 210 } 211 return result != nullptr ? result : &unknown_map_; 212 } 213 214 const MapEntry* ThreadTree::FindMap(const ThreadEntry* thread, uint64_t ip) { 215 MapEntry* result = FindMapByAddr(*thread->maps, ip); 216 if (result != nullptr) { 217 return result; 218 } 219 result = FindMapByAddr(kernel_maps_, ip); 220 return result != nullptr ? result : &unknown_map_; 221 } 222 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; 227 Dso* dso = map->dso; 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); 232 } else { 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); 237 } 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. 241 vaddr_in_file = ip; 242 dso = kernel_dso_.get(); 243 symbol = dso->FindSymbol(vaddr_in_file); 244 } 245 } 246 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); 255 } else { 256 symbol = &unknown_symbol_; 257 } 258 } 259 if (pvaddr_in_file != nullptr) { 260 *pvaddr_in_file = vaddr_in_file; 261 } 262 if (pdso != nullptr) { 263 *pdso = dso; 264 } 265 return symbol; 266 } 267 268 const Symbol* ThreadTree::FindKernelSymbol(uint64_t ip) { 269 const MapEntry* map = FindMap(nullptr, ip, true); 270 return FindSymbol(map, ip, nullptr); 271 } 272 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(); 279 } 280 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); 284 Dso* dso = nullptr; 285 if (dso_type == DSO_KERNEL || dso_type == DSO_KERNEL_MODULE) { 286 dso = FindKernelDsoOrNew(file_path); 287 } else { 288 dso = FindUserDsoOrNew(file_path); 289 } 290 dso->SetMinVirtualAddress(min_vaddr); 291 dso->SetSymbols(symbols); 292 } 293 294 void ThreadTree::Update(const Record& record) { 295 if (record.type() == PERF_RECORD_MMAP) { 296 const MmapRecord& r = *static_cast<const MmapRecord*>(&record); 297 if (r.InKernel()) { 298 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, 299 r.sample_id.time_data.time, r.filename); 300 } else { 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); 303 } 304 } else if (record.type() == PERF_RECORD_MMAP2) { 305 const Mmap2Record& r = *static_cast<const Mmap2Record*>(&record); 306 if (r.InKernel()) { 307 AddKernelMap(r.data->addr, r.data->len, r.data->pgoff, 308 r.sample_id.time_data.time, r.filename); 309 } else { 310 std::string filename = (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) 311 ? "[unknown]" 312 : r.filename; 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); 315 } 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)); 325 } 326 } 327 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()); 333 } 334 for (auto& p : user_dso_tree_) { 335 result.push_back(p.second.get()); 336 } 337 result.push_back(unknown_dso_.get()); 338 return result; 339 } 340 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()); 345 } 346 return threads; 347 } 348 349 } // namespace simpleperf 350