blob: b9b6209cf55b8847201a993962d662a6a9d27ee9 [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>
23#include <memory>
Yabin Cui60a0ea92015-07-22 20:30:43 -070024#include <set>
Yabin Cuic8485602015-08-20 15:04:39 -070025
Yabin Cui60a0ea92015-07-22 20:30:43 -070026#include "dso.h"
Yabin Cui767dd172016-06-02 21:02:43 -070027
28struct Record;
Yabin Cui60a0ea92015-07-22 20:30:43 -070029
Yabin Cui003b2452016-09-29 15:32:45 -070030constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]";
Daniel Friederichb2465ad2016-10-17 12:28:03 -050031// Seen in perf.data file generated by perf.
32constexpr char DEFAULT_KERNEL_MMAP_NAME_PERF[] = "[kernel.kallsyms]_text";
Yabin Cui003b2452016-09-29 15:32:45 -070033constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon";
34
Yabin Cui040f7b42016-04-13 21:28:54 -070035namespace simpleperf {
36
Yabin Cuicdc11a32018-03-20 15:29:03 -070037namespace map_flags {
38constexpr uint32_t PROT_JIT_SYMFILE_MAP = 0x4000;
39} // namespace map_flags
40
Yabin Cui60a0ea92015-07-22 20:30:43 -070041struct MapEntry {
42 uint64_t start_addr;
43 uint64_t len;
44 uint64_t pgoff;
45 uint64_t time; // Map creation time.
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 Cui547c60e2015-10-12 16:56:05 -070050
Yabin Cui9970a232016-06-29 12:18:11 -070051 MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time,
Yabin Cuicdc11a32018-03-20 15:29:03 -070052 Dso* dso, bool in_kernel, uint32_t flags = 0)
Yabin Cui9970a232016-06-29 12:18:11 -070053 : start_addr(start_addr),
54 len(len),
55 pgoff(pgoff),
56 time(time),
57 dso(dso),
Yabin Cuicdc11a32018-03-20 15:29:03 -070058 in_kernel(in_kernel),
59 flags(flags) {}
Yabin Cui9970a232016-06-29 12:18:11 -070060 MapEntry() {}
Yabin Cui547c60e2015-10-12 16:56:05 -070061
Yabin Cui9970a232016-06-29 12:18:11 -070062 uint64_t get_end_addr() const { return start_addr + len; }
Yabin Cui60a0ea92015-07-22 20:30:43 -070063};
64
65struct MapComparator {
66 bool operator()(const MapEntry* map1, const MapEntry* map2) const;
67};
68
Christopher Ferris15933b62018-02-22 19:06:42 -080069struct MapSet {
70 std::set<MapEntry*, MapComparator> maps;
71 uint64_t version = 0u; // incremented each time changing maps
72};
Yabin Cuiaa0dd192016-12-15 11:24:03 -080073
Yabin Cui60a0ea92015-07-22 20:30:43 -070074struct ThreadEntry {
75 int pid;
76 int tid;
77 const char* comm; // It always refers to the latest comm.
Yabin Cuiaa0dd192016-12-15 11:24:03 -080078 MapSet* maps;
Yabin Cui60a0ea92015-07-22 20:30:43 -070079};
80
Yabin Cui767dd172016-06-02 21:02:43 -070081// ThreadTree contains thread information (in ThreadEntry) and mmap information
82// (in MapEntry) of the monitored threads. It also has interface to access
83// symbols in executable binaries mapped in the monitored threads.
Yabin Cui60a0ea92015-07-22 20:30:43 -070084class ThreadTree {
85 public:
Yabin Cui9970a232016-06-29 12:18:11 -070086 ThreadTree()
Yabin Cui15475e62016-07-14 13:26:19 -070087 : show_ip_for_unknown_symbol_(false),
Yabin Cui71b533b2016-07-21 12:29:47 -070088 show_mark_for_unknown_symbol_(false),
Yabin Cui15475e62016-07-14 13:26:19 -070089 unknown_symbol_("unknown", 0,
Yabin Cui9970a232016-06-29 12:18:11 -070090 std::numeric_limits<unsigned long long>::max()) {
Yabin Cuic36ea8b2018-04-16 18:21:40 -070091 unknown_dso_ = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown");
Yabin Cui9970a232016-06-29 12:18:11 -070092 unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(),
93 0, 0, unknown_dso_.get(), false);
Yabin Cui767dd172016-06-02 21:02:43 -070094 kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
Yabin Cuia447c0d2016-08-26 11:35:28 -070095 // We can't dump comm for pid 0 from /proc, so add it's name here.
Yabin Cuiaa0dd192016-12-15 11:24:03 -080096 SetThreadName(0, 0, "swapper");
Yabin Cui60a0ea92015-07-22 20:30:43 -070097 }
98
Yabin Cuiaa0dd192016-12-15 11:24:03 -080099 void SetThreadName(int pid, int tid, const std::string& comm);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700100 void ForkThread(int pid, int tid, int ppid, int ptid);
101 ThreadEntry* FindThreadOrNew(int pid, int tid);
Yabin Cui9970a232016-06-29 12:18:11 -0700102 void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
Yabin Cui60a0ea92015-07-22 20:30:43 -0700103 uint64_t time, const std::string& filename);
Yabin Cui9970a232016-06-29 12:18:11 -0700104 void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len,
Yabin Cuicdc11a32018-03-20 15:29:03 -0700105 uint64_t pgoff, uint64_t time, const std::string& filename,
106 uint32_t flags = 0);
Yabin Cui9970a232016-06-29 12:18:11 -0700107 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip,
108 bool in_kernel);
Yabin Cuib64a8632016-05-24 18:23:33 -0700109 // Find map for an ip address when we don't know whether it is in kernel.
110 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip);
Yabin Cui9970a232016-06-29 12:18:11 -0700111 const Symbol* FindSymbol(const MapEntry* map, uint64_t ip,
Yabin Cui16501ff2016-10-19 15:06:29 -0700112 uint64_t* pvaddr_in_file, Dso** pdso = nullptr);
Yabin Cui6965d422016-06-15 11:41:42 -0700113 const Symbol* FindKernelSymbol(uint64_t ip);
Yabin Cui98c75842017-04-28 13:43:08 -0700114 bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); }
Yabin Cuidd9c9482016-07-11 16:27:52 -0700115 const Symbol* UnknownSymbol() const { return &unknown_symbol_; }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700116
Yabin Cui15475e62016-07-14 13:26:19 -0700117 void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; }
Yabin Cui71b533b2016-07-21 12:29:47 -0700118 void ShowMarkForUnknownSymbol() {
119 show_mark_for_unknown_symbol_ = true;
120 unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX);
121 }
Yabin Cui767dd172016-06-02 21:02:43 -0700122 // Clear thread and map information, but keep loaded dso information. It saves
123 // the time to reload dso information.
124 void ClearThreadAndMap();
125
Yabin Cuic5b4a312016-10-24 13:38:38 -0700126 void AddDsoInfo(const std::string& file_path, uint32_t file_type,
Yabin Cui516a87c2018-03-26 17:34:00 -0700127 uint64_t min_vaddr, std::vector<Symbol>* symbols,
128 const std::vector<uint64_t>& dex_file_offsets);
129 void AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700130
Yabin Cui767dd172016-06-02 21:02:43 -0700131 // Update thread tree with information provided by record.
132 void Update(const Record& record);
Yabin Cuib7f481f2015-10-23 19:48:42 -0700133
Yabin Cui78fddd12016-10-24 14:09:26 -0700134 std::vector<Dso*> GetAllDsos() const;
Yabin Cui52190a32017-06-02 14:47:33 -0700135 std::vector<const ThreadEntry*> GetAllThreads() const;
Yabin Cui78fddd12016-10-24 14:09:26 -0700136
Yabin Cui60a0ea92015-07-22 20:30:43 -0700137 private:
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800138 ThreadEntry* CreateThread(int pid, int tid);
Yabin Cuic8485602015-08-20 15:04:39 -0700139 Dso* FindKernelDsoOrNew(const std::string& filename);
Yabin Cui516a87c2018-03-26 17:34:00 -0700140 Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0,
141 DsoType dso_type = DSO_ELF_FILE);
Yabin Cui547c60e2015-10-12 16:56:05 -0700142 MapEntry* AllocateMap(const MapEntry& value);
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800143 void FixOverlappedMap(MapSet* maps, const MapEntry* map);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700144
145 std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
146 std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
147
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800148 std::vector<std::unique_ptr<MapSet>> map_set_storage_;
149 MapSet kernel_maps_;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700150 std::vector<std::unique_ptr<MapEntry>> map_storage_;
151 MapEntry unknown_map_;
152
Yabin Cuic8485602015-08-20 15:04:39 -0700153 std::unique_ptr<Dso> kernel_dso_;
154 std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_;
155 std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_;
156 std::unique_ptr<Dso> unknown_dso_;
Yabin Cui15475e62016-07-14 13:26:19 -0700157 bool show_ip_for_unknown_symbol_;
Yabin Cui71b533b2016-07-21 12:29:47 -0700158 bool show_mark_for_unknown_symbol_;
Yabin Cuic8485602015-08-20 15:04:39 -0700159 Symbol unknown_symbol_;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700160};
161
Yabin Cui040f7b42016-04-13 21:28:54 -0700162} // namespace simpleperf
163
164using MapEntry = simpleperf::MapEntry;
165using ThreadEntry = simpleperf::ThreadEntry;
166using ThreadTree = simpleperf::ThreadTree;
167
Yabin Cui60a0ea92015-07-22 20:30:43 -0700168#endif // SIMPLE_PERF_THREAD_TREE_H_