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 | |
| 25 | #include "build_id.h" |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 26 | |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 27 | struct Symbol { |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 28 | uint64_t addr; |
| 29 | uint64_t len; |
Yabin Cui | b10a8fb | 2015-08-18 16:32:18 -0700 | [diff] [blame] | 30 | |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 31 | Symbol(const std::string& name, uint64_t addr, uint64_t len); |
| 32 | const char* Name() const { |
| 33 | return name_; |
Yabin Cui | b10a8fb | 2015-08-18 16:32:18 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 36 | const char* DemangledName() const; |
Yabin Cui | b10a8fb | 2015-08-18 16:32:18 -0700 | [diff] [blame] | 37 | |
| 38 | private: |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 39 | const char* name_; |
| 40 | mutable const char* demangled_name_; |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
Yabin Cui | ba50c4b | 2015-07-21 11:24:48 -0700 | [diff] [blame] | 43 | enum DsoType { |
| 44 | DSO_KERNEL, |
| 45 | DSO_KERNEL_MODULE, |
| 46 | DSO_ELF_FILE, |
| 47 | }; |
| 48 | |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 49 | struct KernelSymbol; |
| 50 | struct ElfFileSymbol; |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 51 | |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 52 | struct Dso { |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 53 | public: |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 54 | static void SetDemangle(bool demangle); |
| 55 | static std::string Demangle(const std::string& name); |
| 56 | static bool SetSymFsDir(const std::string& symfs_dir); |
| 57 | static void SetVmlinux(const std::string& vmlinux); |
| 58 | static void SetBuildIds(const std::vector<std::pair<std::string, BuildId>>& build_ids); |
Yabin Cui | b10a8fb | 2015-08-18 16:32:18 -0700 | [diff] [blame] | 59 | |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 60 | static std::unique_ptr<Dso> CreateDso(DsoType dso_type, const std::string& dso_path = ""); |
Yabin Cui | b10a8fb | 2015-08-18 16:32:18 -0700 | [diff] [blame] | 61 | |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 62 | ~Dso(); |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 63 | |
| 64 | // Return the path recorded in perf.data. |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 65 | const std::string& Path() const { |
| 66 | return path_; |
| 67 | } |
| 68 | |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 69 | // Return the accessible path. It may be the same as Path(), or |
| 70 | // return the path with prefix set by SetSymFsDir(). |
| 71 | std::string GetAccessiblePath() const; |
| 72 | |
Yabin Cui | 547c60e | 2015-10-12 16:56:05 -0700 | [diff] [blame] | 73 | // Return the minimum virtual address in program header. |
| 74 | uint64_t MinVirtualAddress(); |
| 75 | |
| 76 | const Symbol* FindSymbol(uint64_t vaddr_in_dso); |
Yabin Cui | b378355 | 2015-06-11 11:15:42 -0700 | [diff] [blame] | 77 | |
| 78 | private: |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 79 | static BuildId GetExpectedBuildId(const std::string& filename); |
| 80 | static bool KernelSymbolCallback(const KernelSymbol& kernel_symbol, Dso* dso); |
| 81 | static void VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso); |
| 82 | static void ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso, |
| 83 | bool (*filter)(const ElfFileSymbol&)); |
Yabin Cui | 638c558 | 2015-07-01 16:16:57 -0700 | [diff] [blame] | 84 | |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 85 | static bool demangle_; |
| 86 | static std::string symfs_dir_; |
| 87 | static std::string vmlinux_; |
| 88 | static std::unordered_map<std::string, BuildId> build_id_map_; |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 89 | static size_t dso_count_; |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 90 | |
| 91 | Dso(DsoType type, const std::string& path); |
| 92 | bool Load(); |
| 93 | bool LoadKernel(); |
| 94 | bool LoadKernelModule(); |
| 95 | bool LoadElfFile(); |
Yabin Cui | 7288501 | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 96 | bool LoadEmbeddedElfFile(); |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 97 | void InsertSymbol(const Symbol& symbol); |
| 98 | void FixupSymbolLength(); |
| 99 | |
| 100 | const DsoType type_; |
| 101 | const std::string path_; |
Yabin Cui | 547c60e | 2015-10-12 16:56:05 -0700 | [diff] [blame] | 102 | uint64_t min_vaddr_; |
Yabin Cui | cc2e59e | 2015-08-21 14:23:43 -0700 | [diff] [blame] | 103 | std::vector<Symbol> symbols_; |
Yabin Cui | c848560 | 2015-08-20 15:04:39 -0700 | [diff] [blame] | 104 | bool is_loaded_; |
Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #endif // SIMPLE_PERF_DSO_H_ |