blob: b742a9cc5c3003502c1a52633abf5acb3e6bf381 [file] [log] [blame]
Yabin Cuiec12ed92015-06-08 10:38:10 -07001/*
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 Cuiec12ed92015-06-08 10:38:10 -070021#include <string>
Yabin Cui638c5582015-07-01 16:16:57 -070022#include <unordered_map>
23#include <vector>
24
25#include "build_id.h"
Yabin Cuiec12ed92015-06-08 10:38:10 -070026
Yabin Cuic8485602015-08-20 15:04:39 -070027struct Symbol {
Yabin Cuiec12ed92015-06-08 10:38:10 -070028 uint64_t addr;
Yabin Cui16501ff2016-10-19 15:06:29 -070029 // TODO: make len uint32_t.
Yabin Cuiec12ed92015-06-08 10:38:10 -070030 uint64_t len;
Yabin Cuib10a8fb2015-08-18 16:32:18 -070031
Yabin Cuicc2e59e2015-08-21 14:23:43 -070032 Symbol(const std::string& name, uint64_t addr, uint64_t len);
Yabin Cui767dd172016-06-02 21:02:43 -070033 const char* Name() const { return name_; }
Yabin Cuib10a8fb2015-08-18 16:32:18 -070034
Yabin Cuicc2e59e2015-08-21 14:23:43 -070035 const char* DemangledName() const;
Yabin Cuib10a8fb2015-08-18 16:32:18 -070036
Yabin Cui16501ff2016-10-19 15:06:29 -070037 bool HasDumpId() const {
38 return dump_id_ != UINT_MAX;
39 }
Yabin Cui767dd172016-06-02 21:02:43 -070040
Yabin Cui16501ff2016-10-19 15:06:29 -070041 bool GetDumpId(uint32_t* pdump_id) const {
42 if (!HasDumpId()) {
43 return false;
44 }
45 *pdump_id = dump_id_;
46 return true;
47 }
Yabin Cui767dd172016-06-02 21:02:43 -070048
Yabin Cuiaba7e292016-11-11 14:53:52 -080049 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
57 static bool CompareByAddr(const Symbol* s1, const Symbol* s2) {
58 return s1->addr < s2->addr;
59 }
60
61 static bool CompareValueByAddr(const Symbol& s1, const Symbol& s2) {
62 return s1.addr < s2.addr;
63 }
64
Yabin Cuib10a8fb2015-08-18 16:32:18 -070065 private:
Yabin Cuicc2e59e2015-08-21 14:23:43 -070066 const char* name_;
67 mutable const char* demangled_name_;
Yabin Cui16501ff2016-10-19 15:06:29 -070068 mutable uint32_t dump_id_;
69
70 friend class Dso;
Yabin Cui767dd172016-06-02 21:02:43 -070071};
72
Yabin Cuiba50c4b2015-07-21 11:24:48 -070073enum DsoType {
74 DSO_KERNEL,
75 DSO_KERNEL_MODULE,
76 DSO_ELF_FILE,
77};
78
Yabin Cuic8485602015-08-20 15:04:39 -070079struct KernelSymbol;
80struct ElfFileSymbol;
Yabin Cuiec12ed92015-06-08 10:38:10 -070081
Yabin Cui16501ff2016-10-19 15:06:29 -070082class Dso {
Yabin Cuiec12ed92015-06-08 10:38:10 -070083 public:
Yabin Cuic8485602015-08-20 15:04:39 -070084 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);
Yabin Cuib4212972016-05-25 14:08:05 -070088 static void SetKallsyms(std::string kallsyms) {
89 if (!kallsyms.empty()) {
90 kallsyms_ = std::move(kallsyms);
91 }
92 }
Yabin Cuia9392452017-01-12 18:07:27 -080093 static void ReadKernelSymbolsFromProc() {
94 read_kernel_symbols_from_proc_ = true;
95 }
Yabin Cui767dd172016-06-02 21:02:43 -070096 static void SetBuildIds(
97 const std::vector<std::pair<std::string, BuildId>>& build_ids);
Yabin Cui52c63692016-11-28 17:28:08 -080098 static BuildId FindExpectedBuildIdForPath(const std::string& path);
Yabin Cuib10a8fb2015-08-18 16:32:18 -070099
Yabin Cui767dd172016-06-02 21:02:43 -0700100 static std::unique_ptr<Dso> CreateDso(DsoType dso_type,
101 const std::string& dso_path);
Yabin Cuib10a8fb2015-08-18 16:32:18 -0700102
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700103 ~Dso();
Yabin Cui3c8c2132015-08-13 20:30:20 -0700104
Yabin Cui767dd172016-06-02 21:02:43 -0700105 DsoType type() const { return type_; }
106
Yabin Cui3c8c2132015-08-13 20:30:20 -0700107 // Return the path recorded in perf.data.
Yabin Cui767dd172016-06-02 21:02:43 -0700108 const std::string& Path() const { return path_; }
Yabin Cuieec606c2016-07-07 13:53:33 -0700109 // Return the path containing symbol table and debug information.
110 const std::string& GetDebugFilePath() const { return debug_file_path_; }
Yabin Cui15475e62016-07-14 13:26:19 -0700111 // Return the file name without directory info.
112 const std::string& FileName() const { return file_name_; }
Yabin Cui767dd172016-06-02 21:02:43 -0700113
Yabin Cui16501ff2016-10-19 15:06:29 -0700114 bool HasDumpId() {
115 return dump_id_ != UINT_MAX;
116 }
117
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);
Yabin Cui78fddd12016-10-24 14:09:26 -0700128
Yabin Cui547c60e2015-10-12 16:56:05 -0700129 // Return the minimum virtual address in program header.
130 uint64_t MinVirtualAddress();
Yabin Cuic855ecc2016-07-11 17:04:54 -0700131 void SetMinVirtualAddress(uint64_t min_vaddr) { min_vaddr_ = min_vaddr; }
Yabin Cui547c60e2015-10-12 16:56:05 -0700132
133 const Symbol* FindSymbol(uint64_t vaddr_in_dso);
Yabin Cuic5b4a312016-10-24 13:38:38 -0700134
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);
Yabin Cuib3783552015-06-11 11:15:42 -0700141
142 private:
Yabin Cuic8485602015-08-20 15:04:39 -0700143 static bool demangle_;
144 static std::string symfs_dir_;
145 static std::string vmlinux_;
Yabin Cuib4212972016-05-25 14:08:05 -0700146 static std::string kallsyms_;
Yabin Cuia9392452017-01-12 18:07:27 -0800147 static bool read_kernel_symbols_from_proc_;
Yabin Cuic8485602015-08-20 15:04:39 -0700148 static std::unordered_map<std::string, BuildId> build_id_map_;
Yabin Cuicc2e59e2015-08-21 14:23:43 -0700149 static size_t dso_count_;
Yabin Cui16501ff2016-10-19 15:06:29 -0700150 static uint32_t g_dump_id_;
Yabin Cuic8485602015-08-20 15:04:39 -0700151
Yabin Cuic5b4a312016-10-24 13:38:38 -0700152 Dso(DsoType type, const std::string& path);
153 void Load();
Yabin Cuic8485602015-08-20 15:04:39 -0700154 bool LoadKernel();
155 bool LoadKernelModule();
156 bool LoadElfFile();
Yabin Cuib1a885b2016-02-14 19:18:02 -0800157 bool LoadEmbeddedElfFile();
Yabin Cuic8485602015-08-20 15:04:39 -0700158 void FixupSymbolLength();
Yabin Cuieec606c2016-07-07 13:53:33 -0700159 BuildId GetExpectedBuildId();
Yabin Cuic8485602015-08-20 15:04:39 -0700160
161 const DsoType type_;
Yabin Cuieec606c2016-07-07 13:53:33 -0700162 // path of the shared library used by the profiled program
Yabin Cuic8485602015-08-20 15:04:39 -0700163 const std::string path_;
Yabin Cuieec606c2016-07-07 13:53:33 -0700164 // 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_;
Yabin Cui15475e62016-07-14 13:26:19 -0700167 // File name of the shared library, got by removing directories in path_.
168 std::string file_name_;
Yabin Cui547c60e2015-10-12 16:56:05 -0700169 uint64_t min_vaddr_;
Yabin Cuic5b4a312016-10-24 13:38:38 -0700170 std::vector<Symbol> symbols_;
171 // unknown symbols are like [libc.so+0x1234].
172 std::unordered_map<uint64_t, Symbol> unknown_symbols_;
Yabin Cuic8485602015-08-20 15:04:39 -0700173 bool is_loaded_;
Yabin Cui16501ff2016-10-19 15:06:29 -0700174 // 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_;
Yabin Cuiec12ed92015-06-08 10:38:10 -0700178};
179
Yabin Cui767dd172016-06-02 21:02:43 -0700180const char* DsoTypeToString(DsoType dso_type);
181
Yabin Cuiec12ed92015-06-08 10:38:10 -0700182#endif // SIMPLE_PERF_DSO_H_