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 
20 #include <stdint.h>
21 
22 #include <limits>
23 #include <map>
24 #include <memory>
25 #include <unordered_map>
26 
27 #include "dso.h"
28 
29 struct Record;
30 
31 constexpr char DEFAULT_KERNEL_MMAP_NAME[] = "[kernel.kallsyms]";
32 // Seen in perf.data file generated by perf.
33 constexpr char DEFAULT_KERNEL_MMAP_NAME_PERF[] = "[kernel.kallsyms]_text";
34 constexpr char DEFAULT_EXECNAME_FOR_THREAD_MMAP[] = "//anon";
35 
36 namespace simpleperf {
37 
38 namespace map_flags {
39 constexpr uint32_t PROT_JIT_SYMFILE_MAP = 0x4000;
40 }  // namespace map_flags
41 
42 struct MapEntry {
43   uint64_t start_addr;
44   uint64_t len;
45   uint64_t pgoff;
46   Dso* dso;
47   bool in_kernel;
48   uint32_t flags;
49 
50   MapEntry(uint64_t start_addr, uint64_t len, uint64_t pgoff,
51            Dso* dso, bool in_kernel, uint32_t flags = 0)
start_addrMapEntry52       : start_addr(start_addr),
53         len(len),
54         pgoff(pgoff),
55         dso(dso),
56         in_kernel(in_kernel),
57         flags(flags) {}
MapEntryMapEntry58   MapEntry() {}
59 
get_end_addrMapEntry60   uint64_t get_end_addr() const { return start_addr + len; }
61 };
62 
63 struct MapSet {
64   std::map<uint64_t, const MapEntry*> maps;  // Map from start_addr to a MapEntry.
65   uint64_t version = 0u;  // incremented each time changing maps
66 };
67 
68 struct ThreadEntry {
69   int pid;
70   int tid;
71   const char* comm;  // It always refers to the latest comm.
72   std::shared_ptr<MapSet> maps;  // maps is shared by threads in the same process.
73 };
74 
75 // 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.
78 class ThreadTree {
79  public:
ThreadTree()80   ThreadTree()
81       : show_ip_for_unknown_symbol_(false),
82         show_mark_for_unknown_symbol_(false),
83         unknown_symbol_("unknown", 0,
84                         std::numeric_limits<unsigned long long>::max()) {
85     unknown_dso_ = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown");
86     unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(),
87                             0, unknown_dso_.get(), false);
88     kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
89     // We can't dump comm for pid 0 from /proc, so add it's name here.
90     SetThreadName(0, 0, "swapper");
91   }
92 
93   void SetThreadName(int pid, int tid, const std::string& comm);
94   void ForkThread(int pid, int tid, int ppid, int ptid);
95   ThreadEntry* FindThread(int tid);
96   ThreadEntry* FindThreadOrNew(int pid, int tid);
97   void ExitThread(int pid, int tid);
98   void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
99                     const std::string& filename);
100   void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len,
101                     uint64_t pgoff, const std::string& filename, uint32_t flags = 0);
102   const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip,
103                           bool in_kernel);
104   // Find map for an ip address when we don't know whether it is in kernel.
105   const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip);
106   const Symbol* FindSymbol(const MapEntry* map, uint64_t ip,
107                            uint64_t* pvaddr_in_file, Dso** pdso = nullptr);
108   const Symbol* FindKernelSymbol(uint64_t ip);
IsUnknownDso(const Dso * dso)109   bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); }
UnknownSymbol()110   const Symbol* UnknownSymbol() const { return &unknown_symbol_; }
111 
ShowIpForUnknownSymbol()112   void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; }
ShowMarkForUnknownSymbol()113   void ShowMarkForUnknownSymbol() {
114     show_mark_for_unknown_symbol_ = true;
115     unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX);
116   }
117   // Clear thread and map information, but keep loaded dso information. It saves
118   // the time to reload dso information.
119   void ClearThreadAndMap();
120 
121   void AddDsoInfo(const std::string& file_path, uint32_t file_type,
122                   uint64_t min_vaddr, uint64_t file_offset_of_min_vaddr,
123                   std::vector<Symbol>* symbols, const std::vector<uint64_t>& dex_file_offsets);
124   void AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset);
125 
126   // Update thread tree with information provided by record.
127   void Update(const Record& record);
128 
129   std::vector<Dso*> GetAllDsos() const;
130 
131  private:
132   ThreadEntry* CreateThread(int pid, int tid);
133   Dso* FindKernelDsoOrNew(const std::string& filename);
134   Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0,
135                         DsoType dso_type = DSO_ELF_FILE);
136   const MapEntry* AllocateMap(const MapEntry& entry);
137   void InsertMap(MapSet& maps, const MapEntry& entry);
138 
139   std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
140   std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
141 
142   MapSet kernel_maps_;
143   std::vector<std::unique_ptr<MapEntry>> map_storage_;
144   MapEntry unknown_map_;
145 
146   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_;
150   bool show_ip_for_unknown_symbol_;
151   bool show_mark_for_unknown_symbol_;
152   Symbol unknown_symbol_;
153 };
154 
155 }  // namespace simpleperf
156 
157 using MapEntry = simpleperf::MapEntry;
158 using ThreadEntry = simpleperf::ThreadEntry;
159 using ThreadTree = simpleperf::ThreadTree;
160 
161 #endif  // SIMPLE_PERF_THREAD_TREE_H_
162