2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef SIMPLE_PERF_DSO_H_
18 #define SIMPLE_PERF_DSO_H_
22 #include <unordered_map>
25 #include <android-base/logging.h>
26 #include <android-base/test_utils.h>
33 // TODO: make len uint32_t.
36 Symbol(const std::string& name, uint64_t addr, uint64_t len);
37 const char* Name() const { return name_; }
39 const char* DemangledName() const;
41 bool HasDumpId() const {
42 return dump_id_ != UINT_MAX;
45 bool GetDumpId(uint32_t* pdump_id) const {
53 static bool CompareByDumpId(const Symbol* s1, const Symbol* s2) {
54 uint32_t id1 = UINT_MAX;
56 uint32_t id2 = UINT_MAX;
61 static bool CompareByAddr(const Symbol* s1, const Symbol* s2) {
62 return s1->addr < s2->addr;
65 static bool CompareValueByAddr(const Symbol& s1, const Symbol& s2) {
66 return s1.addr < s2.addr;
71 mutable const char* demangled_name_;
72 mutable uint32_t dump_id_;
88 static void SetDemangle(bool demangle);
89 static std::string Demangle(const std::string& name);
90 static bool SetSymFsDir(const std::string& symfs_dir);
91 static void SetVmlinux(const std::string& vmlinux);
92 static void SetKallsyms(std::string kallsyms) {
93 if (!kallsyms.empty()) {
94 kallsyms_ = std::move(kallsyms);
97 static void ReadKernelSymbolsFromProc() {
98 read_kernel_symbols_from_proc_ = true;
100 static void SetBuildIds(
101 const std::vector<std::pair<std::string, BuildId>>& build_ids);
102 static BuildId FindExpectedBuildIdForPath(const std::string& path);
103 static void SetVdsoFile(std::unique_ptr<TemporaryFile> vdso_file, bool is_64bit);
105 static std::unique_ptr<Dso> CreateDso(DsoType dso_type, const std::string& dso_path,
106 bool force_64bit = false);
110 DsoType type() const { return type_; }
112 // Return the path recorded in perf.data.
113 const std::string& Path() const { return path_; }
114 // Return the path containing symbol table and debug information.
115 const std::string& GetDebugFilePath() const { return debug_file_path_; }
116 // Return the file name without directory info.
117 const std::string& FileName() const { return file_name_; }
120 return dump_id_ != UINT_MAX;
123 bool GetDumpId(uint32_t* pdump_id) {
127 *pdump_id = dump_id_;
131 uint32_t CreateDumpId();
132 uint32_t CreateSymbolDumpId(const Symbol* symbol);
134 // Return the minimum virtual address in program header.
135 uint64_t MinVirtualAddress();
136 void SetMinVirtualAddress(uint64_t min_vaddr) { min_vaddr_ = min_vaddr; }
138 const Symbol* FindSymbol(uint64_t vaddr_in_dso);
140 const std::vector<Symbol>& GetSymbols();
141 void SetSymbols(std::vector<Symbol>* symbols);
143 // Create a symbol for a virtual address which can't find a corresponding
144 // symbol in symbol table.
145 void AddUnknownSymbol(uint64_t vaddr_in_dso, const std::string& name);
148 static bool demangle_;
149 static std::string symfs_dir_;
150 static std::string vmlinux_;
151 static std::string kallsyms_;
152 static bool read_kernel_symbols_from_proc_;
153 static std::unordered_map<std::string, BuildId> build_id_map_;
154 static size_t dso_count_;
155 static uint32_t g_dump_id_;
156 static std::unique_ptr<TemporaryFile> vdso_64bit_;
157 static std::unique_ptr<TemporaryFile> vdso_32bit_;
159 Dso(DsoType type, const std::string& path, bool force_64bit);
162 bool LoadKernelModule();
164 bool LoadEmbeddedElfFile();
165 void FixupSymbolLength();
166 BuildId GetExpectedBuildId();
167 bool CheckReadSymbolResult(ElfStatus result, const std::string& filename);
170 // path of the shared library used by the profiled program
171 const std::string path_;
172 // path of the shared library having symbol table and debug information
173 // It is the same as path_, or has the same build id as path_.
174 std::string debug_file_path_;
175 // File name of the shared library, got by removing directories in path_.
176 std::string file_name_;
178 std::vector<Symbol> symbols_;
179 // unknown symbols are like [libc.so+0x1234].
180 std::unordered_map<uint64_t, Symbol> unknown_symbols_;
182 // Used to identify current dso if it needs to be dumped.
184 // Used to assign dump_id for symbols in current dso.
185 uint32_t symbol_dump_id_;
186 android::base::LogSeverity symbol_warning_loglevel_;
189 const char* DsoTypeToString(DsoType dso_type);
191 #endif // SIMPLE_PERF_DSO_H_