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