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