blob: 5bdaadae1cc813abb21cbc9bcbd7e5d5521d703a [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 Cui60a0ea92015-07-22 20:30:43 -070061};
62
Christopher Ferris15933b62018-02-22 19:06:42 -080063struct MapSet {
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070064 std::map<uint64_t, const MapEntry*> maps; // Map from start_addr to a MapEntry.
Christopher Ferris15933b62018-02-22 19:06:42 -080065 uint64_t version = 0u; // incremented each time changing maps
66};
Yabin Cuiaa0dd192016-12-15 11:24:03 -080067
Yabin Cui60a0ea92015-07-22 20:30:43 -070068struct ThreadEntry {
69 int pid;
70 int tid;
71 const char* comm; // It always refers to the latest comm.
Yabin Cuiaa0dd192016-12-15 11:24:03 -080072 MapSet* maps;
Yabin Cui60a0ea92015-07-22 20:30:43 -070073};
74
Yabin Cui767dd172016-06-02 21:02:43 -070075// ThreadTree contains thread information (in ThreadEntry) and mmap information
76// (in MapEntry) of the monitored threads. It also has interface to access
77// symbols in executable binaries mapped in the monitored threads.
Yabin Cui60a0ea92015-07-22 20:30:43 -070078class ThreadTree {
79 public:
Yabin Cui9970a232016-06-29 12:18:11 -070080 ThreadTree()
Yabin Cui15475e62016-07-14 13:26:19 -070081 : show_ip_for_unknown_symbol_(false),
Yabin Cui71b533b2016-07-21 12:29:47 -070082 show_mark_for_unknown_symbol_(false),
Yabin Cui15475e62016-07-14 13:26:19 -070083 unknown_symbol_("unknown", 0,
Yabin Cui9970a232016-06-29 12:18:11 -070084 std::numeric_limits<unsigned long long>::max()) {
Yabin Cuic36ea8b2018-04-16 18:21:40 -070085 unknown_dso_ = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown");
Yabin Cui9970a232016-06-29 12:18:11 -070086 unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(),
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070087 0, unknown_dso_.get(), false);
Yabin Cui767dd172016-06-02 21:02:43 -070088 kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
Yabin Cuia447c0d2016-08-26 11:35:28 -070089 // We can't dump comm for pid 0 from /proc, so add it's name here.
Yabin Cuiaa0dd192016-12-15 11:24:03 -080090 SetThreadName(0, 0, "swapper");
Yabin Cui60a0ea92015-07-22 20:30:43 -070091 }
92
Yabin Cuiaa0dd192016-12-15 11:24:03 -080093 void SetThreadName(int pid, int tid, const std::string& comm);
Yabin Cui60a0ea92015-07-22 20:30:43 -070094 void ForkThread(int pid, int tid, int ppid, int ptid);
95 ThreadEntry* FindThreadOrNew(int pid, int tid);
Yabin Cui9970a232016-06-29 12:18:11 -070096 void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070097 const std::string& filename);
Yabin Cui9970a232016-06-29 12:18:11 -070098 void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len,
Yabin Cui9d2ebcf2018-05-30 15:50:21 -070099 uint64_t pgoff, const std::string& filename, uint32_t flags = 0);
Yabin Cui9970a232016-06-29 12:18:11 -0700100 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip,
101 bool in_kernel);
Yabin Cuib64a8632016-05-24 18:23:33 -0700102 // Find map for an ip address when we don't know whether it is in kernel.
103 const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip);
Yabin Cui9970a232016-06-29 12:18:11 -0700104 const Symbol* FindSymbol(const MapEntry* map, uint64_t ip,
Yabin Cui16501ff2016-10-19 15:06:29 -0700105 uint64_t* pvaddr_in_file, Dso** pdso = nullptr);
Yabin Cui6965d422016-06-15 11:41:42 -0700106 const Symbol* FindKernelSymbol(uint64_t ip);
Yabin Cui98c75842017-04-28 13:43:08 -0700107 bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); }
Yabin Cuidd9c9482016-07-11 16:27:52 -0700108 const Symbol* UnknownSymbol() const { return &unknown_symbol_; }
Yabin Cui60a0ea92015-07-22 20:30:43 -0700109
Yabin Cui15475e62016-07-14 13:26:19 -0700110 void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; }
Yabin Cui71b533b2016-07-21 12:29:47 -0700111 void ShowMarkForUnknownSymbol() {
112 show_mark_for_unknown_symbol_ = true;
113 unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX);
114 }
Yabin Cui767dd172016-06-02 21:02:43 -0700115 // Clear thread and map information, but keep loaded dso information. It saves
116 // the time to reload dso information.
117 void ClearThreadAndMap();
118
Yabin Cuic5b4a312016-10-24 13:38:38 -0700119 void AddDsoInfo(const std::string& file_path, uint32_t file_type,
Yabin Cui516a87c2018-03-26 17:34:00 -0700120 uint64_t min_vaddr, std::vector<Symbol>* symbols,
121 const std::vector<uint64_t>& dex_file_offsets);
122 void AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700123
Yabin Cui767dd172016-06-02 21:02:43 -0700124 // Update thread tree with information provided by record.
125 void Update(const Record& record);
Yabin Cuib7f481f2015-10-23 19:48:42 -0700126
Yabin Cui78fddd12016-10-24 14:09:26 -0700127 std::vector<Dso*> GetAllDsos() const;
Yabin Cui52190a32017-06-02 14:47:33 -0700128 std::vector<const ThreadEntry*> GetAllThreads() const;
Yabin Cui78fddd12016-10-24 14:09:26 -0700129
Yabin Cui60a0ea92015-07-22 20:30:43 -0700130 private:
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800131 ThreadEntry* CreateThread(int pid, int tid);
Yabin Cuic8485602015-08-20 15:04:39 -0700132 Dso* FindKernelDsoOrNew(const std::string& filename);
Yabin Cui516a87c2018-03-26 17:34:00 -0700133 Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0,
134 DsoType dso_type = DSO_ELF_FILE);
Yabin Cui9d2ebcf2018-05-30 15:50:21 -0700135 const MapEntry* AllocateMap(const MapEntry& entry);
136 void InsertMap(MapSet& maps, const MapEntry& entry);
Yabin Cui60a0ea92015-07-22 20:30:43 -0700137
138 std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
139 std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
140
Yabin Cuiaa0dd192016-12-15 11:24:03 -0800141 std::vector<std::unique_ptr<MapSet>> map_set_storage_;
142 MapSet kernel_maps_;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700143 std::vector<std::unique_ptr<MapEntry>> map_storage_;
144 MapEntry unknown_map_;
145
Yabin Cuic8485602015-08-20 15:04:39 -0700146 std::unique_ptr<Dso> kernel_dso_;
147 std::unordered_map<std::string, std::unique_ptr<Dso>> module_dso_tree_;
148 std::unordered_map<std::string, std::unique_ptr<Dso>> user_dso_tree_;
149 std::unique_ptr<Dso> unknown_dso_;
Yabin Cui15475e62016-07-14 13:26:19 -0700150 bool show_ip_for_unknown_symbol_;
Yabin Cui71b533b2016-07-21 12:29:47 -0700151 bool show_mark_for_unknown_symbol_;
Yabin Cuic8485602015-08-20 15:04:39 -0700152 Symbol unknown_symbol_;
Yabin Cui60a0ea92015-07-22 20:30:43 -0700153};
154
Yabin Cui040f7b42016-04-13 21:28:54 -0700155} // namespace simpleperf
156
157using MapEntry = simpleperf::MapEntry;
158using ThreadEntry = simpleperf::ThreadEntry;
159using ThreadTree = simpleperf::ThreadTree;
160
Yabin Cui60a0ea92015-07-22 20:30:43 -0700161#endif // SIMPLE_PERF_THREAD_TREE_H_