blob: 76d3403d133e6eacd15e66001f3b1b9ecf968c6b [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
29struct Record;
Yabin Cui60a0ea92015-07-22 20:30:43 -070030
Yabin Cui003b2452016-09-29 15:32:45 -070031constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]";
Daniel Friederichb2465ad2016-10-17 12:28:03 -050032// Seen in perf.data file generated by perf.
33constexpr char DEFAULT_KERNEL_MMAP_NAME_PERF[] = "[kernel.kallsyms]_text";
Yabin Cui003b2452016-09-29 15:32:45 -070034constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon";
35
Yabin Cui040f7b42016-04-13 21:28:54 -070036namespace simpleperf {
37
Yabin Cuicdc11a32018-03-20 15:29:03 -070038namespace map_flags {
39constexpr uint32_t PROT_JIT_SYMFILE_MAP = 0x4000;
40} // namespace map_flags
41
Yabin Cui60a0ea92015-07-22 20:30:43 -070042struct MapEntry {
43 uint64_t start_addr;
44 uint64_t len;
45 uint64_t pgoff;
Yabin Cuic8485602015-08-20 15:04:39 -070046 Dso* dso;
Yabin Cuib4212972016-05-25 14:08:05 -070047 bool in_kernel;
Yabin Cuicdc11a32018-03-20 15:29:03 -070048 uint32_t flags;
49
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070050 MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff,
Yabin Cuicdc11a32018-03-20 15:29:03 -070051 Dso* dso, bool in_kernel, uint32_t flags = 0)
Yabin Cui9970a232016-06-29 12:18:11 -070052 : start_addr(start_addr),
53 len(len),
54 pgoff(pgoff),
Yabin Cui9970a232016-06-29 12:18:11 -070055 dso(dso),
Yabin Cuicdc11a32018-03-20 15:29:03 -070056 in_kernel(in_kernel),
57 flags(flags) {}
Yabin Cui9970a232016-06-29 12:18:11 -070058 MapEntry() {}
Yabin Cui547c60e2015-10-12 16:56:05 -070059
Yabin Cui9970a232016-06-29 12:18:11 -070060 uint64_t get_end_addr() const { return start_addr + len; }
Yabin Cui10f527c2020-03-24 11:53:39 -070061
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 Cui60a0ea92015-07-22 20:30:43 -070072};
73
Christopher Ferris15933b62018-02-22 19:06:42 -080074struct MapSet {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070075 std::map<uint64_t, const MapEntry*> maps; // Map from start_addr to a MapEntry.
Christopher Ferris15933b62018-02-22 19:06:42 -080076 uint64_t version = 0u; // incremented each time changing maps
Yabin Cui10f527c2020-03-24 11:53:39 -070077
78 const MapEntry* FindMapByAddr(uint64_t addr) const;
Christopher Ferris15933b62018-02-22 19:06:42 -080079};
Yabin Cuiaa0dd192016-12-15 11:24:03 -080080
Yabin Cui60a0ea92015-07-22 20:30:43 -070081struct ThreadEntry {
82 int pid;
83 int tid;
84 const char* comm; // It always refers to the latest comm.
Yabin Cui847f3fd2019-05-02 12:58:05 -070085 std::shared_ptr<MapSet> maps; // maps is shared by threads in the same process.
Yabin Cui60a0ea92015-07-22 20:30:43 -070086};
87
Yabin Cui767dd172016-06-02 21:02:43 -070088// 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 Cui60a0ea92015-07-22 20:30:43 -070091class ThreadTree {
92 public:
Yabin Cui9970a232016-06-29 12:18:11 -070093 ThreadTree()
Yabin Cui15475e62016-07-14 13:26:19 -070094 : show_ip_for_unknown_symbol_(false),
Yabin Cui71b533b2016-07-21 12:29:47 -070095 show_mark_for_unknown_symbol_(false),
Yabin Cui15475e62016-07-14 13:26:19 -070096 unknown_symbol_("unknown", 0,
Yabin Cui9970a232016-06-29 12:18:11 -070097 std::numeric_limits<unsigned long long>::max()) {
Yabin Cuic36ea8b2018-04-16 18:21:40 -070098 unknown_dso_ = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown");
Yabin Cui9970a232016-06-29 12:18:11 -070099 unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(),
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700100 0, unknown_dso_.get(), false);
Yabin Cui767dd172016-06-02 21:02:43 -0700101 kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
Yabin Cuia447c0d2016-08-26 11:35:28 -0700102 // We can't dump comm for pid 0 from /proc, so add it's name here.
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800103 SetThreadName(0, 0, "swapper");
Yabin Cui60a0ea92015-07-22 20:30:43 -0700104 }
105
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800106 void SetThreadName(int pid, int tid, const std::string& comm);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700107 void ForkThread(int pid, int tid, int ppid, int ptid);
Yabin Cuifc9da9b2019-08-08 18:15:14 -0700108 ThreadEntry* FindThread(int tid);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700109 ThreadEntry* FindThreadOrNew(int pid, int tid);
Yabin Cui847f3fd2019-05-02 12:58:05 -0700110 void ExitThread(int pid, int tid);
Yabin Cui9970a232016-06-29 12:18:11 -0700111 void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700112 const std::string& filename);
Yabin Cui10f527c2020-03-24 11:53:39 -0700113 const MapSet& GetKernelMaps() { return kernel_maps_; }
Yabin Cui9970a232016-06-29 12:18:11 -0700114 void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len,
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700115 uint64_t pgoff, const std::string& filename, uint32_t flags = 0);
Yabin Cui9970a232016-06-29 12:18:11 -0700116 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip,
117 bool in_kernel);
Yabin Cuib64a8632016-05-24 18:23:33 -0700118 // 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 Cui9970a232016-06-29 12:18:11 -0700120 const Symbol* FindSymbol(const MapEntry* map, uint64_t ip,
Yabin Cui16501ff2016-10-19 15:06:29 -0700121 uint64_t* pvaddr_in_file, Dso** pdso = nullptr);
Yabin Cui6965d422016-06-15 11:41:42 -0700122 const Symbol* FindKernelSymbol(uint64_t ip);
Yabin Cui98c75842017-04-28 13:43:08 -0700123 bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); }
Yabin Cuidd9c9482016-07-11 16:27:52 -0700124 const Symbol* UnknownSymbol() const { return &unknown_symbol_; }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700125
Yabin Cui15475e62016-07-14 13:26:19 -0700126 void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; }
Yabin Cui71b533b2016-07-21 12:29:47 -0700127 void ShowMarkForUnknownSymbol() {
128 show_mark_for_unknown_symbol_ = true;
129 unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX);
130 }
Yabin Cui767dd172016-06-02 21:02:43 -0700131 // Clear thread and map information, but keep loaded dso information. It saves
132 // the time to reload dso information.
133 void ClearThreadAndMap();
134
Yabin Cuic5b4a312016-10-24 13:38:38 -0700135 void AddDsoInfo(const std::string& file_path, uint32_t file_type,
Yabin Cuidb2c4932019-02-07 15:06:42 -0800136 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 Cui516a87c2018-03-26 17:34:00 -0700138 void AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700139
Yabin Cui767dd172016-06-02 21:02:43 -0700140 // Update thread tree with information provided by record.
141 void Update(const Record& record);
Yabin Cuib7f481f2015-10-23 19:48:42 -0700142
Yabin Cui78fddd12016-10-24 14:09:26 -0700143 std::vector<Dso*> GetAllDsos() const;
144
Yabin Cui60a0ea92015-07-22 20:30:43 -0700145 private:
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800146 ThreadEntry* CreateThread(int pid, int tid);
Yabin Cuic8485602015-08-20 15:04:39 -0700147 Dso* FindKernelDsoOrNew(const std::string& filename);
Yabin Cui516a87c2018-03-26 17:34:00 -0700148 Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0,
149 DsoType dso_type = DSO_ELF_FILE);
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700150 const MapEntry* AllocateMap(const MapEntry& entry);
151 void InsertMap(MapSet& maps, const MapEntry& entry);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700152
153 std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
154 std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
155
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800156 MapSet kernel_maps_;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700157 std::vector<std::unique_ptr<MapEntry>> map_storage_;
158 MapEntry unknown_map_;
159
Yabin Cuic8485602015-08-20 15:04:39 -0700160 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 Cui15475e62016-07-14 13:26:19 -0700164 bool show_ip_for_unknown_symbol_;
Yabin Cui71b533b2016-07-21 12:29:47 -0700165 bool show_mark_for_unknown_symbol_;
Yabin Cuic8485602015-08-20 15:04:39 -0700166 Symbol unknown_symbol_;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700167};
168
Yabin Cui040f7b42016-04-13 21:28:54 -0700169} // namespace simpleperf
170
171using MapEntry = simpleperf::MapEntry;
172using ThreadEntry = simpleperf::ThreadEntry;
173using ThreadTree = simpleperf::ThreadTree;
174
Yabin Cui60a0ea92015-07-22 20:30:43 -0700175#endif // SIMPLE_PERF_THREAD_TREE_H_