• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ContainsMapEntry62   uint64_t Contains(uint64_t addr) const {
63     return addr >= start_addr && addr < get_end_addr();
64   }
65 
GetVaddrInFileMapEntry66   uint64_t GetVaddrInFile(uint64_t addr) const {
67     if (Contains(addr)) {
68       return dso->IpToVaddrInFile(addr, start_addr, pgoff);
69     }
70     return 0;
71   }
72 };
73 
74 struct MapSet {
75   std::map<uint64_t, const MapEntry*> maps;  // Map from start_addr to a MapEntry.
76   uint64_t version = 0u;  // incremented each time changing maps
77 
78   const MapEntry* FindMapByAddr(uint64_t addr) const;
79 };
80 
81 struct ThreadEntry {
82   int pid;
83   int tid;
84   const char* comm;  // It always refers to the latest comm.
85   std::shared_ptr<MapSet> maps;  // maps is shared by threads in the same process.
86 };
87 
88 // 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.
91 class ThreadTree {
92  public:
ThreadTree()93   ThreadTree()
94       : show_ip_for_unknown_symbol_(false),
95         show_mark_for_unknown_symbol_(false),
96         unknown_symbol_("unknown", 0,
97                         std::numeric_limits<unsigned long long>::max()) {
98     unknown_dso_ = Dso::CreateDso(DSO_UNKNOWN_FILE, "unknown");
99     unknown_map_ = MapEntry(0, std::numeric_limits<unsigned long long>::max(),
100                             0, unknown_dso_.get(), false);
101     kernel_dso_ = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
102     // We can't dump comm for pid 0 from /proc, so add it's name here.
103     SetThreadName(0, 0, "swapper");
104   }
105 
106   void SetThreadName(int pid, int tid, const std::string& comm);
107   void ForkThread(int pid, int tid, int ppid, int ptid);
108   ThreadEntry* FindThread(int tid);
109   ThreadEntry* FindThreadOrNew(int pid, int tid);
110   void ExitThread(int pid, int tid);
111   void AddKernelMap(uint64_t start_addr, uint64_t len, uint64_t pgoff,
112                     const std::string& filename);
GetKernelMaps()113   const MapSet& GetKernelMaps() { return kernel_maps_; }
114   void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len,
115                     uint64_t pgoff, const std::string& filename, uint32_t flags = 0);
116   const MapEntry* FindMap(const ThreadEntry* thread, uint64_t ip,
117                           bool in_kernel);
118   // 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);
120   const Symbol* FindSymbol(const MapEntry* map, uint64_t ip,
121                            uint64_t* pvaddr_in_file, Dso** pdso = nullptr);
122   const Symbol* FindKernelSymbol(uint64_t ip);
IsUnknownDso(const Dso * dso)123   bool IsUnknownDso(const Dso* dso) const { return dso == unknown_dso_.get(); }
UnknownSymbol()124   const Symbol* UnknownSymbol() const { return &unknown_symbol_; }
125 
ShowIpForUnknownSymbol()126   void ShowIpForUnknownSymbol() { show_ip_for_unknown_symbol_ = true; }
ShowMarkForUnknownSymbol()127   void ShowMarkForUnknownSymbol() {
128     show_mark_for_unknown_symbol_ = true;
129     unknown_symbol_ = Symbol("*unknown", 0, ULLONG_MAX);
130   }
131   // Clear thread and map information, but keep loaded dso information. It saves
132   // the time to reload dso information.
133   void ClearThreadAndMap();
134 
135   void AddDsoInfo(const std::string& file_path, uint32_t file_type,
136                   uint64_t min_vaddr, uint64_t file_offset_of_min_vaddr,
137                   std::vector<Symbol>* symbols, const std::vector<uint64_t>& dex_file_offsets);
138   void AddDexFileOffset(const std::string& file_path, uint64_t dex_file_offset);
139 
140   // Update thread tree with information provided by record.
141   void Update(const Record& record);
142 
143   std::vector<Dso*> GetAllDsos() const;
144 
145  private:
146   ThreadEntry* CreateThread(int pid, int tid);
147   Dso* FindKernelDsoOrNew(const std::string& filename);
148   Dso* FindUserDsoOrNew(const std::string& filename, uint64_t start_addr = 0,
149                         DsoType dso_type = DSO_ELF_FILE);
150   const MapEntry* AllocateMap(const MapEntry& entry);
151   void InsertMap(MapSet& maps, const MapEntry& entry);
152 
153   std::unordered_map<int, std::unique_ptr<ThreadEntry>> thread_tree_;
154   std::vector<std::unique_ptr<std::string>> thread_comm_storage_;
155 
156   MapSet kernel_maps_;
157   std::vector<std::unique_ptr<MapEntry>> map_storage_;
158   MapEntry unknown_map_;
159 
160   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_;
164   bool show_ip_for_unknown_symbol_;
165   bool show_mark_for_unknown_symbol_;
166   Symbol unknown_symbol_;
167 };
168 
169 }  // namespace simpleperf
170 
171 using MapEntry = simpleperf::MapEntry;
172 using ThreadEntry = simpleperf::ThreadEntry;
173 using ThreadTree = simpleperf::ThreadTree;
174 
175 #endif  // SIMPLE_PERF_THREAD_TREE_H_
176