blob: ea7237660e7a09bb047e473d0a8db9ef4c6ed666 [file] [log] [blame]
Yabin Cui60a0ea92015-07-22 20:30:43 -07001/*
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 Cui60a0ea92015-07-22 20:30:43 -070020#include <stdint.h>
Yabin Cuic8485602015-08-20 15:04:39 -070021
22#include <limits>
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070023#include <map>
Yabin Cuic8485602015-08-20 15:04:39 -070024#include <memory>
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070025#include <unordered_map>
Yabin Cuic8485602015-08-20 15:04:39 -070026
Yabin Cui60a0ea92015-07-22 20:30:43 -070027#include "dso.h"
Yabin Cui767dd172016-06-02 21:02:43 -070028
Yabin Cuifaa7b922021-01-11 17:35:57 -080029namespace simpleperf {
30
Yabin Cui767dd172016-06-02 21:02:43 -070031struct Record;
Yabin Cui60a0ea92015-07-22 20:30:43 -070032
Yabin Cui003b2452016-09-29 15:32:45 -070033constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]";
Yabin Cui003b2452016-09-29 15:32:45 -070034constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon";
35
Yabin Cuicdc11a32018-03-20 15:29:03 -070036namespace map_flags {
37constexpr uint32_t PROT_JIT_SYMFILE_MAP = 0x4000;
38} // namespace map_flags
39
Yabin Cui60a0ea92015-07-22 20:30:43 -070040struct MapEntry {
41 uint64_t start_addr;
42 uint64_t len;
43 uint64_t pgoff;
Yabin Cuic8485602015-08-20 15:04:39 -070044 Dso* dso;
Yabin Cuib4212972016-05-25 14:08:05 -070045 bool in_kernel;
Yabin Cuicdc11a32018-03-20 15:29:03 -070046 uint32_t flags;
47
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020048 MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff, Dso* dso, bool in_kernel,
49 uint32_t flags = 0)
Yabin Cui9970a232016-06-29 12:18:11 -070050 : start_addr(start_addr),
51 len(len),
52 pgoff(pgoff),
Yabin Cui9970a232016-06-29 12:18:11 -070053 dso(dso),
Yabin Cuicdc11a32018-03-20 15:29:03 -070054 in_kernel(in_kernel),
55 flags(flags) {}
Yabin Cui9970a232016-06-29 12:18:11 -070056 MapEntry() {}
Yabin Cui547c60e2015-10-12 16:56:05 -070057
Yabin Cui9970a232016-06-29 12:18:11 -070058 uint64_t get_end_addr() const { return start_addr + len; }
Yabin Cui418ba0d2020-03-24 11:53:39 -070059
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020060 uint64_t Contains(uint64_t addr) const { return addr >= start_addr && addr < get_end_addr(); }
Yabin Cui418ba0d2020-03-24 11:53:39 -070061
62 uint64_t GetVaddrInFile(uint64_t addr) const {
63 if (Contains(addr)) {
64 return dso->IpToVaddrInFile(addr, start_addr, pgoff);
65 }
66 return 0;
67 }
Yabin Cui60a0ea92015-07-22 20:30:43 -070068};
69
Christopher Ferris15933b62018-02-22 19:06:42 -080070struct MapSet {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070071 std::map<uint64_t, const MapEntry*> maps; // Map from start_addr to a MapEntry.
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020072 uint64_t version = 0u; // incremented each time changing maps
Yabin Cui418ba0d2020-03-24 11:53:39 -070073
74 const MapEntry* FindMapByAddr(uint64_t addr) const;
Christopher Ferris15933b62018-02-22 19:06:42 -080075};
Yabin Cuiaa0dd192016-12-15 11:24:03 -080076
Yabin Cui60a0ea92015-07-22 20:30:43 -070077struct ThreadEntry {
78 int pid;
79 int tid;
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020080 const char* comm; // It always refers to the latest comm.
Yabin Cui847f3fd2019-05-02 12:58:05 -070081 std::shared_ptr<MapSet> maps; // maps is shared by threads in the same process.
Yabin Cui60a0ea92015-07-22 20:30:43 -070082};
83
Yabin Cui8d005de2020-10-21 13:48:53 -070084struct FileFeature;
85
Yabin Cui767dd172016-06-02 21:02:43 -070086// ThreadTree contains thread information (in ThreadEntry) and mmap information
87// (in MapEntry) of the monitored threads. It also has interface to access
88// symbols in executable binaries mapped in the monitored threads.
Yabin Cui60a0ea92015-07-22 20:30:43 -070089class ThreadTree {
90 public:
Yabin Cui9970a232016-06-29 12:18:11 -070091 ThreadTree()
Yabin Cui15475e62016-07-14 13:26:19 -070092 : show_ip_for_unknown_symbol_(false),
Yabin Cui71b533b2016-07-21 12:29:47 -070093 show_mark_for_unknown_symbol_(false),
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020094 unknown_symbol_("unknown", 0, std::numeric_limits<unsigned long long>::max()) {
Yabin Cuic36ea8b2018-04-16 18:21:40 -070095 unknown_dso_ = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown");
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020096 unknown_map_ =
97 MapEntry(0, std::numeric_limits<unsigned long long>::max(), 0, unknown_dso_.get(), false);
Yabin Cuia447c0d2016-08-26 11:35:28 -070098 // We can't dump comm for pid 0 from /proc, so add it's name here.
Yabin Cuiaa0dd192016-12-15 11:24:03 -080099 SetThreadName(0, 0, "swapper");
Yabin Cui60a0ea92015-07-22 20:30:43 -0700100 }
Yabin Cui561bf1b2020-11-03 12:11:07 -0800101 virtual ~ThreadTree() {}
Yabin Cui60a0ea92015-07-22 20:30:43 -0700102
Yabin Cui5a1b6262023-05-01 09:53:34 -0700103 void DisableThreadExitRecords() { disable_thread_exit_records_ = true; }
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800104 void SetThreadName(int pid, int tid, const std::string& comm);
Yabin Cui70fa40a2022-12-14 15:36:31 -0800105 bool ForkThread(int pid, int tid, int ppid, int ptid);
Yabin Cuia89a3742021-02-11 13:14:54 -0800106 virtual ThreadEntry* FindThread(int tid) const;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700107 ThreadEntry* FindThreadOrNew(int pid, int tid);
Yabin Cui847f3fd2019-05-02 12:58:05 -0700108 void ExitThread(int pid, int tid);
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200109 void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff, const std::string& filename);
Yabin Cui418ba0d2020-03-24 11:53:39 -0700110 const MapSet& GetKernelMaps() { return kernel_maps_; }
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200111 void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff,
112 const std::string& filename, uint32_t flags = 0);
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200113
114 // Add process symbols that do not correspond to any real dso.
115 // For example, these might be symbols generated by a JIT.
116 void AddSymbolsForProcess(int pid, std::vector<Symbol>* symbols);
117
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200118 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip, bool in_kernel);
Yabin Cuib64a8632016-05-24 18:23:33 -0700119 // Find map for an ip address when we don't know whether it is in kernel.
120 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip);
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200121 const Symbol* FindSymbol(const MapEntry* map, uint64_t ip, uint64_t* pvaddr_in_file,
122 Dso** pdso = nullptr);
Yabin Cui6965d422016-06-15 11:41:42 -0700123 const Symbol* FindKernelSymbol(uint64_t ip);
Yabin Cui98c75842017-04-28 13:43:08 -0700124 bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); }
Yabin Cuidd9c9482016-07-11 16:27:52 -0700125 const Symbol* UnknownSymbol() const { return &unknown_symbol_; }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700126
Yabin Cui15475e62016-07-14 13:26:19 -0700127 void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; }
Yabin Cui71b533b2016-07-21 12:29:47 -0700128 void ShowMarkForUnknownSymbol() {
129 show_mark_for_unknown_symbol_ = true;
130 unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX);
131 }
Yabin Cui767dd172016-06-02 21:02:43 -0700132 // Clear thread and map information, but keep loaded dso information. It saves
133 // the time to reload dso information.
134 void ClearThreadAndMap();
Yabin Cui90a547e2022-12-07 16:29:13 -0800135 bool AddDsoInfo(FileFeature& file);
Yabin Cui516a87c2018-03-26 17:34:00 -0700136 void AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700137
Yabin Cui767dd172016-06-02 21:02:43 -0700138 // Update thread tree with information provided by record.
139 void Update(const Record& record);
Yabin Cuib7f481f2015-10-23 19:48:42 -0700140
Yabin Cui78fddd12016-10-24 14:09:26 -0700141 std::vector<Dso*> GetAllDsos() const;
Yabin Cui9ce72af2021-06-17 11:38:25 -0700142 Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0,
143 DsoType dso_type = DSO_ELF_FILE);
Yabin Cui78fddd12016-10-24 14:09:26 -0700144
Yabin Cui60a0ea92015-07-22 20:30:43 -0700145 private:
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800146 ThreadEntry* CreateThread(int pid, int tid);
Yabin Cuif3da1ed2020-11-25 15:37:38 -0800147 Dso* FindKernelDsoOrNew();
148 Dso* FindKernelModuleDsoOrNew(const std::string& filename, uint64_t memory_start,
149 uint64_t memory_end);
Yabin Cui9ce72af2021-06-17 11:38:25 -0700150
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700151 const MapEntry* AllocateMap(const MapEntry& entry);
152 void InsertMap(MapSet& maps, const MapEntry& entry);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700153
Evgeny Eltsin91dbae02020-08-27 15:46:09 +0200154 // Add thread maps to cover symbols in dso.
155 void AddThreadMapsForDsoSymbols(ThreadEntry* thread, Dso* dso);
156
Yabin Cui60a0ea92015-07-22 20:30:43 -0700157 std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
158 std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
159
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800160 MapSet kernel_maps_;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700161 std::vector<std::unique_ptr<MapEntry>> map_storage_;
162 MapEntry unknown_map_;
163
Yabin Cuic8485602015-08-20 15:04:39 -0700164 std::unique_ptr<Dso> kernel_dso_;
165 std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_;
166 std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_;
167 std::unique_ptr<Dso> unknown_dso_;
Yabin Cui15475e62016-07-14 13:26:19 -0700168 bool show_ip_for_unknown_symbol_;
Yabin Cui71b533b2016-07-21 12:29:47 -0700169 bool show_mark_for_unknown_symbol_;
Yabin Cuic8485602015-08-20 15:04:39 -0700170 Symbol unknown_symbol_;
Yabin Cui5a1b6262023-05-01 09:53:34 -0700171 bool disable_thread_exit_records_ = false;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700172};
173
Yabin Cui040f7b42016-04-13 21:28:54 -0700174} // namespace simpleperf
175
Yabin Cui60a0ea92015-07-22 20:30:43 -0700176#endif // SIMPLE_PERF_THREAD_TREE_H_