Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 21 | |
| 22 | #include <limits> |
Yabin Cui | 9d2ebcf | 2018-05-30 15:50:21 -0700 | [diff] [blame] | 23 | #include <map> |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 24 | #include <memory> |
Yabin Cui | 9d2ebcf | 2018-05-30 15:50:21 -0700 | [diff] [blame] | 25 | #include <unordered_map> |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 26 | |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 27 | #include "dso.h" |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 28 | |
| 29 | struct Record; |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 30 | |
Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 31 | constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]"; |
Daniel Friederich | b2465ad | 2016-10-17 12:28:03 -0500 | [diff] [blame] | 32 | // Seen in perf.data file generated by perf. |
| 33 | constexpr char DEFAULT_KERNEL_MMAP_NAME_PERF[] = "[kernel.kallsyms]_text"; |
Yabin Cui | 003b245 | 2016-09-29 15:32:45 -0700 | [diff] [blame] | 34 | constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon"; |
| 35 | |
Yabin Cui | 040f7b4 | 2016-04-13 21:28:54 -0700 | [diff] [blame] | 36 | namespace simpleperf { |
| 37 | |
Yabin Cui | cdc11a3 | 2018-03-20 15:29:03 -0700 | [diff] [blame] | 38 | namespace map_flags { |
| 39 | constexpr uint32_t PROT_JIT_SYMFILE_MAP = 0x4000; |
| 40 | } // namespace map_flags |
| 41 | |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 42 | struct MapEntry { |
| 43 | uint64_t start_addr; |
| 44 | uint64_t len; |
| 45 | uint64_t pgoff; |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 46 | Dso* dso; |
Yabin Cui | b421297 | 2016-05-25 14:08:05 -0700 | [diff] [blame] | 47 | bool in_kernel; |
Yabin Cui | cdc11a3 | 2018-03-20 15:29:03 -0700 | [diff] [blame] | 48 | uint32_t flags; |
| 49 | |
Yabin Cui | 9d2ebcf | 2018-05-30 15:50:21 -0700 | [diff] [blame] | 50 | MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff, |
Yabin Cui | cdc11a3 | 2018-03-20 15:29:03 -0700 | [diff] [blame] | 51 | Dso* dso, bool in_kernel, uint32_t flags = 0) |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 52 | : start_addr(start_addr), |
| 53 | len(len), |
| 54 | pgoff(pgoff), |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 55 | dso(dso), |
Yabin Cui | cdc11a3 | 2018-03-20 15:29:03 -0700 | [diff] [blame] | 56 | in_kernel(in_kernel), |
| 57 | flags(flags) {} |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 58 | MapEntry() {} |
Yabin Cui | 547c60e | 2015-10-12 16:56:05 -0700 | [diff] [blame] | 59 | |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 60 | uint64_t get_end_addr() const { return start_addr + len; } |
Yabin Cui | 10f527c | 2020-03-24 11:53:39 -0700 | [diff] [blame] | 61 | |
| 62 | uint64_t Contains(uint64_t addr) const { |
| 63 | return addr >= start_addr && addr < get_end_addr(); |
| 64 | } |
| 65 | |
| 66 | uint64_t GetVaddrInFile(uint64_t addr) const { |
| 67 | if (Contains(addr)) { |
| 68 | return dso->IpToVaddrInFile(addr, start_addr, pgoff); |
| 69 | } |
| 70 | return 0; |
| 71 | } |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Christopher Ferris | 15933b6 | 2018-02-22 19:06:42 -0800 | [diff] [blame] | 74 | struct MapSet { |
Yabin Cui | 9d2ebcf | 2018-05-30 15:50:21 -0700 | [diff] [blame] | 75 | std::map<uint64_t, const MapEntry*> maps; // Map from start_addr to a MapEntry. |
Christopher Ferris | 15933b6 | 2018-02-22 19:06:42 -0800 | [diff] [blame] | 76 | uint64_t version = 0u; // incremented each time changing maps |
Yabin Cui | 10f527c | 2020-03-24 11:53:39 -0700 | [diff] [blame] | 77 | |
| 78 | const MapEntry* FindMapByAddr(uint64_t addr) const; |
Christopher Ferris | 15933b6 | 2018-02-22 19:06:42 -0800 | [diff] [blame] | 79 | }; |
Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 80 | |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 81 | struct ThreadEntry { |
| 82 | int pid; |
| 83 | int tid; |
| 84 | const char* comm; // It always refers to the latest comm. |
Yabin Cui | 847f3fd | 2019-05-02 12:58:05 -0700 | [diff] [blame] | 85 | std::shared_ptr<MapSet> maps; // maps is shared by threads in the same process. |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 88 | // ThreadTree contains thread information (in ThreadEntry) and mmap information |
| 89 | // (in MapEntry) of the monitored threads. It also has interface to access |
| 90 | // symbols in executable binaries mapped in the monitored threads. |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 91 | class ThreadTree { |
| 92 | public: |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 93 | ThreadTree() |
Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 94 | : show_ip_for_unknown_symbol_(false), |
Yabin Cui | 71b533b | 2016-07-21 12:29:47 -0700 | [diff] [blame] | 95 | show_mark_for_unknown_symbol_(false), |
Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 96 | unknown_symbol_("unknown", 0, |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 97 | std::numeric_limits<unsigned long long>::max()) { |
Yabin Cui | c36ea8b | 2018-04-16 18:21:40 -0700 | [diff] [blame] | 98 | unknown_dso_ = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown"); |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 99 | unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(), |
Yabin Cui | 9d2ebcf | 2018-05-30 15:50:21 -0700 | [diff] [blame] | 100 | 0, unknown_dso_.get(), false); |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 101 | kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME); |
Yabin Cui | a447c0d | 2016-08-26 11:35:28 -0700 | [diff] [blame] | 102 | // We can't dump comm for pid 0 from /proc, so add it's name here. |
Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 103 | SetThreadName(0, 0, "swapper"); |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 106 | void SetThreadName(int pid, int tid, const std::string& comm); |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 107 | void ForkThread(int pid, int tid, int ppid, int ptid); |
Yabin Cui | fc9da9b | 2019-08-08 18:15:14 -0700 | [diff] [blame] | 108 | ThreadEntry* FindThread(int tid); |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 109 | ThreadEntry* FindThreadOrNew(int pid, int tid); |
Yabin Cui | 847f3fd | 2019-05-02 12:58:05 -0700 | [diff] [blame] | 110 | void ExitThread(int pid, int tid); |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 111 | void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, |
Yabin Cui | 9d2ebcf | 2018-05-30 15:50:21 -0700 | [diff] [blame] | 112 | const std::string& filename); |
Yabin Cui | 10f527c | 2020-03-24 11:53:39 -0700 | [diff] [blame] | 113 | const MapSet& GetKernelMaps() { return kernel_maps_; } |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 114 | void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, |
Yabin Cui | 9d2ebcf | 2018-05-30 15:50:21 -0700 | [diff] [blame] | 115 | uint64_t pgoff, const std::string& filename, uint32_t flags = 0); |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 116 | const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip, |
| 117 | bool in_kernel); |
Yabin Cui | b64a863 | 2016-05-24 18:23:33 -0700 | [diff] [blame] | 118 | // Find map for an ip address when we don't know whether it is in kernel. |
| 119 | const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip); |
Yabin Cui | 9970a23 | 2016-06-29 12:18:11 -0700 | [diff] [blame] | 120 | const Symbol* FindSymbol(const MapEntry* map, uint64_t ip, |
Yabin Cui | 16501ff | 2016-10-19 15:06:29 -0700 | [diff] [blame] | 121 | uint64_t* pvaddr_in_file, Dso** pdso = nullptr); |
Yabin Cui | 6965d42 | 2016-06-15 11:41:42 -0700 | [diff] [blame] | 122 | const Symbol* FindKernelSymbol(uint64_t ip); |
Yabin Cui | 98c7584 | 2017-04-28 13:43:08 -0700 | [diff] [blame] | 123 | bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); } |
Yabin Cui | dd9c948 | 2016-07-11 16:27:52 -0700 | [diff] [blame] | 124 | const Symbol* UnknownSymbol() const { return &unknown_symbol_; } |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 125 | |
Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 126 | void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; } |
Yabin Cui | 71b533b | 2016-07-21 12:29:47 -0700 | [diff] [blame] | 127 | void ShowMarkForUnknownSymbol() { |
| 128 | show_mark_for_unknown_symbol_ = true; |
| 129 | unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX); |
| 130 | } |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 131 | // Clear thread and map information, but keep loaded dso information. It saves |
| 132 | // the time to reload dso information. |
| 133 | void ClearThreadAndMap(); |
| 134 | |
Yabin Cui | c5b4a31 | 2016-10-24 13:38:38 -0700 | [diff] [blame] | 135 | void AddDsoInfo(const std::string& file_path, uint32_t file_type, |
Yabin Cui | db2c493 | 2019-02-07 15:06:42 -0800 | [diff] [blame] | 136 | uint64_t min_vaddr, uint64_t file_offset_of_min_vaddr, |
| 137 | std::vector<Symbol>* symbols, const std::vector<uint64_t>& dex_file_offsets); |
Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 138 | void AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset); |
Yabin Cui | c5b4a31 | 2016-10-24 13:38:38 -0700 | [diff] [blame] | 139 | |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 140 | // Update thread tree with information provided by record. |
| 141 | void Update(const Record& record); |
Yabin Cui | b7f481f | 2015-10-23 19:48:42 -0700 | [diff] [blame] | 142 | |
Yabin Cui | 78fddd1 | 2016-10-24 14:09:26 -0700 | [diff] [blame] | 143 | std::vector<Dso*> GetAllDsos() const; |
| 144 | |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 145 | private: |
Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 146 | ThreadEntry* CreateThread(int pid, int tid); |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 147 | Dso* FindKernelDsoOrNew(const std::string& filename); |
Yabin Cui | 516a87c | 2018-03-26 17:34:00 -0700 | [diff] [blame] | 148 | Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0, |
| 149 | DsoType dso_type = DSO_ELF_FILE); |
Yabin Cui | 9d2ebcf | 2018-05-30 15:50:21 -0700 | [diff] [blame] | 150 | const MapEntry* AllocateMap(const MapEntry& entry); |
| 151 | void InsertMap(MapSet& maps, const MapEntry& entry); |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 152 | |
| 153 | std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_; |
| 154 | std::vector<std::unique_ptr<std::string>> thread_comm_storage_; |
| 155 | |
Yabin Cui | aa0dd19 | 2016-12-15 11:24:03 -0800 | [diff] [blame] | 156 | MapSet kernel_maps_; |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 157 | std::vector<std::unique_ptr<MapEntry>> map_storage_; |
| 158 | MapEntry unknown_map_; |
| 159 | |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 160 | std::unique_ptr<Dso> kernel_dso_; |
| 161 | std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_; |
| 162 | std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_; |
| 163 | std::unique_ptr<Dso> unknown_dso_; |
Yabin Cui | 15475e6 | 2016-07-14 13:26:19 -0700 | [diff] [blame] | 164 | bool show_ip_for_unknown_symbol_; |
Yabin Cui | 71b533b | 2016-07-21 12:29:47 -0700 | [diff] [blame] | 165 | bool show_mark_for_unknown_symbol_; |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 166 | Symbol unknown_symbol_; |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
Yabin Cui | 040f7b4 | 2016-04-13 21:28:54 -0700 | [diff] [blame] | 169 | } // namespace simpleperf |
| 170 | |
| 171 | using MapEntry = simpleperf::MapEntry; |
| 172 | using ThreadEntry = simpleperf::ThreadEntry; |
| 173 | using ThreadTree = simpleperf::ThreadTree; |
| 174 | |
Yabin Cui | 60a0ea9 | 2015-07-22 20:30:43 -0700 | [diff] [blame] | 175 | #endif // SIMPLE_PERF_THREAD_TREE_H_ |