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_DSO_H_ 18 #define SIMPLE_PERF_DSO_H_ 19 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "build_id.h" 26 27 struct Symbol { 28 uint64_t addr; 29 // TODO: make len uint32_t. 30 uint64_t len; 31 32 Symbol(const std::string& name, uint64_t addr, uint64_t len); NameSymbol33 const char* Name() const { return name_; } 34 35 const char* DemangledName() const; 36 HasDumpIdSymbol37 bool HasDumpId() const { 38 return dump_id_ != UINT_MAX; 39 } 40 GetDumpIdSymbol41 bool GetDumpId(uint32_t* pdump_id) const { 42 if (!HasDumpId()) { 43 return false; 44 } 45 *pdump_id = dump_id_; 46 return true; 47 } 48 CompareByDumpIdSymbol49 static bool CompareByDumpId(const Symbol* s1, const Symbol* s2) { 50 uint32_t id1 = UINT_MAX; 51 s1->GetDumpId(&id1); 52 uint32_t id2 = UINT_MAX; 53 s2->GetDumpId(&id2); 54 return id1 < id2; 55 } 56 CompareByAddrSymbol57 static bool CompareByAddr(const Symbol* s1, const Symbol* s2) { 58 return s1->addr < s2->addr; 59 } 60 CompareValueByAddrSymbol61 static bool CompareValueByAddr(const Symbol& s1, const Symbol& s2) { 62 return s1.addr < s2.addr; 63 } 64 65 private: 66 const char* name_; 67 mutable const char* demangled_name_; 68 mutable uint32_t dump_id_; 69 70 friend class Dso; 71 }; 72 73 enum DsoType { 74 DSO_KERNEL, 75 DSO_KERNEL_MODULE, 76 DSO_ELF_FILE, 77 }; 78 79 struct KernelSymbol; 80 struct ElfFileSymbol; 81 82 class Dso { 83 public: 84 static void SetDemangle(bool demangle); 85 static std::string Demangle(const std::string& name); 86 static bool SetSymFsDir(const std::string& symfs_dir); 87 static void SetVmlinux(const std::string& vmlinux); SetKallsyms(std::string kallsyms)88 static void SetKallsyms(std::string kallsyms) { 89 if (!kallsyms.empty()) { 90 kallsyms_ = std::move(kallsyms); 91 } 92 } ReadKernelSymbolsFromProc()93 static void ReadKernelSymbolsFromProc() { 94 read_kernel_symbols_from_proc_ = true; 95 } 96 static void SetBuildIds( 97 const std::vector<std::pair<std::string, BuildId>>& build_ids); 98 static BuildId FindExpectedBuildIdForPath(const std::string& path); 99 100 static std::unique_ptr<Dso> CreateDso(DsoType dso_type, 101 const std::string& dso_path); 102 103 ~Dso(); 104 type()105 DsoType type() const { return type_; } 106 107 // Return the path recorded in perf.data. Path()108 const std::string& Path() const { return path_; } 109 // Return the path containing symbol table and debug information. GetDebugFilePath()110 const std::string& GetDebugFilePath() const { return debug_file_path_; } 111 // Return the file name without directory info. FileName()112 const std::string& FileName() const { return file_name_; } 113 HasDumpId()114 bool HasDumpId() { 115 return dump_id_ != UINT_MAX; 116 } 117 GetDumpId(uint32_t * pdump_id)118 bool GetDumpId(uint32_t* pdump_id) { 119 if (!HasDumpId()) { 120 return false; 121 } 122 *pdump_id = dump_id_; 123 return true; 124 } 125 126 uint32_t CreateDumpId(); 127 uint32_t CreateSymbolDumpId(const Symbol* symbol); 128 129 // Return the minimum virtual address in program header. 130 uint64_t MinVirtualAddress(); SetMinVirtualAddress(uint64_t min_vaddr)131 void SetMinVirtualAddress(uint64_t min_vaddr) { min_vaddr_ = min_vaddr; } 132 133 const Symbol* FindSymbol(uint64_t vaddr_in_dso); 134 135 const std::vector<Symbol>& GetSymbols(); 136 void SetSymbols(std::vector<Symbol>* symbols); 137 138 // Create a symbol for a virtual address which can't find a corresponding 139 // symbol in symbol table. 140 void AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name); 141 142 private: 143 static bool demangle_; 144 static std::string symfs_dir_; 145 static std::string vmlinux_; 146 static std::string kallsyms_; 147 static bool read_kernel_symbols_from_proc_; 148 static std::unordered_map<std::string, BuildId> build_id_map_; 149 static size_t dso_count_; 150 static uint32_t g_dump_id_; 151 152 Dso(DsoType type, const std::string& path); 153 void Load(); 154 bool LoadKernel(); 155 bool LoadKernelModule(); 156 bool LoadElfFile(); 157 bool LoadEmbeddedElfFile(); 158 void FixupSymbolLength(); 159 BuildId GetExpectedBuildId(); 160 161 const DsoType type_; 162 // path of the shared library used by the profiled program 163 const std::string path_; 164 // path of the shared library having symbol table and debug information 165 // It is the same as path_, or has the same build id as path_. 166 std::string debug_file_path_; 167 // File name of the shared library, got by removing directories in path_. 168 std::string file_name_; 169 uint64_t min_vaddr_; 170 std::vector<Symbol> symbols_; 171 // unknown symbols are like [libc.so+0x1234]. 172 std::unordered_map<uint64_t, Symbol> unknown_symbols_; 173 bool is_loaded_; 174 // Used to identify current dso if it needs to be dumped. 175 uint32_t dump_id_; 176 // Used to assign dump_id for symbols in current dso. 177 uint32_t symbol_dump_id_; 178 }; 179 180 const char* DsoTypeToString(DsoType dso_type); 181 182 #endif // SIMPLE_PERF_DSO_H_ 183