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 #ifndef SIMPLE_PERF_THREAD_TREE_H_ 18 #define SIMPLE_PERF_THREAD_TREE_H_ 19 20 #include <stdint.h> 21 22 #include <limits> 23 #include <map> 24 #include <memory> 25 #include <unordered_map> 26 27 #include "dso.h" 28 29 struct Record; 30 31 constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]"; 32 // Seen in perf.data file generated by perf. 33 constexpr char DEFAULT_KERNEL_MMAP_NAME_PERF[] = "[kernel.kallsyms]_text"; 34 constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon"; 35 36 namespace simpleperf { 37 38 namespace map_flags { 39 constexpr uint32_t PROT_JIT_SYMFILE_MAP = 0x4000; 40 } // namespace map_flags 41 42 struct MapEntry { 43 uint64_t start_addr; 44 uint64_t len; 45 uint64_t pgoff; 46 Dso* dso; 47 bool in_kernel; 48 uint32_t flags; 49 50 MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff, 51 Dso* dso, bool in_kernel, uint32_t flags = 0) start_addrMapEntry52 : start_addr(start_addr), 53 len(len), 54 pgoff(pgoff), 55 dso(dso), 56 in_kernel(in_kernel), 57 flags(flags) {} MapEntryMapEntry58 MapEntry() {} 59 get_end_addrMapEntry60 uint64_t get_end_addr() const { return start_addr + len; } 61 }; 62 63 struct MapSet { 64 std::map<uint64_t, const MapEntry*> maps; // Map from start_addr to a MapEntry. 65 uint64_t version = 0u; // incremented each time changing maps 66 }; 67 68 struct ThreadEntry { 69 int pid; 70 int tid; 71 const char* comm; // It always refers to the latest comm. 72 MapSet* maps; 73 }; 74 75 // ThreadTree contains thread information (in ThreadEntry) and mmap information 76 // (in MapEntry) of the monitored threads. It also has interface to access 77 // symbols in executable binaries mapped in the monitored threads. 78 class ThreadTree { 79 public: ThreadTree()80 ThreadTree() 81 : show_ip_for_unknown_symbol_(false), 82 show_mark_for_unknown_symbol_(false), 83 unknown_symbol_("unknown", 0, 84 std::numeric_limits<unsigned long long>::max()) { 85 unknown_dso_ = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown"); 86 unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(), 87 0, unknown_dso_.get(), false); 88 kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME); 89 // We can't dump comm for pid 0 from /proc, so add it's name here. 90 SetThreadName(0, 0, "swapper"); 91 } 92 93 void SetThreadName(int pid, int tid, const std::string& comm); 94 void ForkThread(int pid, int tid, int ppid, int ptid); 95 ThreadEntry* FindThreadOrNew(int pid, int tid); 96 void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, 97 const std::string& filename); 98 void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, 99 uint64_t pgoff, const std::string& filename, uint32_t flags = 0); 100 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip, 101 bool in_kernel); 102 // Find map for an ip address when we don't know whether it is in kernel. 103 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip); 104 const Symbol* FindSymbol(const MapEntry* map, uint64_t ip, 105 uint64_t* pvaddr_in_file, Dso** pdso = nullptr); 106 const Symbol* FindKernelSymbol(uint64_t ip); IsUnknownDso(const Dso * dso)107 bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); } UnknownSymbol()108 const Symbol* UnknownSymbol() const { return &unknown_symbol_; } 109 ShowIpForUnknownSymbol()110 void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; } ShowMarkForUnknownSymbol()111 void ShowMarkForUnknownSymbol() { 112 show_mark_for_unknown_symbol_ = true; 113 unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX); 114 } 115 // Clear thread and map information, but keep loaded dso information. It saves 116 // the time to reload dso information. 117 void ClearThreadAndMap(); 118 119 void AddDsoInfo(const std::string& file_path, uint32_t file_type, 120 uint64_t min_vaddr, uint64_t file_offset_of_min_vaddr, 121 std::vector<Symbol>* symbols, const std::vector<uint64_t>& dex_file_offsets); 122 void AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset); 123 124 // Update thread tree with information provided by record. 125 void Update(const Record& record); 126 127 std::vector<Dso*> GetAllDsos() const; 128 std::vector<const ThreadEntry*> GetAllThreads() const; 129 130 private: 131 ThreadEntry* CreateThread(int pid, int tid); 132 Dso* FindKernelDsoOrNew(const std::string& filename); 133 Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0, 134 DsoType dso_type = DSO_ELF_FILE); 135 const MapEntry* AllocateMap(const MapEntry& entry); 136 void InsertMap(MapSet& maps, const MapEntry& entry); 137 138 std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_; 139 std::vector<std::unique_ptr<std::string>> thread_comm_storage_; 140 141 std::vector<std::unique_ptr<MapSet>> map_set_storage_; 142 MapSet kernel_maps_; 143 std::vector<std::unique_ptr<MapEntry>> map_storage_; 144 MapEntry unknown_map_; 145 146 std::unique_ptr<Dso> kernel_dso_; 147 std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_; 148 std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_; 149 std::unique_ptr<Dso> unknown_dso_; 150 bool show_ip_for_unknown_symbol_; 151 bool show_mark_for_unknown_symbol_; 152 Symbol unknown_symbol_; 153 }; 154 155 } // namespace simpleperf 156 157 using MapEntry = simpleperf::MapEntry; 158 using ThreadEntry = simpleperf::ThreadEntry; 159 using ThreadTree = simpleperf::ThreadTree; 160 161 #endif // SIMPLE_PERF_THREAD_TREE_H_ 162