OSDN Git Service

am 0ad1301e: (-s ours) am 68cb694f: Merge "Simpleperf: support multiple event types...
[android-x86/system-extras.git] / simpleperf / dso.h
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   uint64_t len;
30
31   Symbol(const std::string& name, uint64_t addr, uint64_t len);
32   const char* Name() const {
33     return name_;
34   }
35
36   const char* DemangledName() const;
37
38  private:
39   const char* name_;
40   mutable const char* demangled_name_;
41 };
42
43 enum DsoType {
44   DSO_KERNEL,
45   DSO_KERNEL_MODULE,
46   DSO_ELF_FILE,
47 };
48
49 struct KernelSymbol;
50 struct ElfFileSymbol;
51
52 struct Dso {
53  public:
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);
59
60   static std::unique_ptr<Dso> CreateDso(DsoType dso_type, const std::string& dso_path = "");
61
62   ~Dso();
63   const std::string& Path() const {
64     return path_;
65   }
66
67   const Symbol* FindSymbol(uint64_t offset_in_dso);
68
69  private:
70   static BuildId GetExpectedBuildId(const std::string& filename);
71   static bool KernelSymbolCallback(const KernelSymbol& kernel_symbol, Dso* dso);
72   static void VmlinuxSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso);
73   static void ElfFileSymbolCallback(const ElfFileSymbol& elf_symbol, Dso* dso,
74                                     bool (*filter)(const ElfFileSymbol&));
75
76   static bool demangle_;
77   static std::string symfs_dir_;
78   static std::string vmlinux_;
79   static std::unordered_map<std::string, BuildId> build_id_map_;
80   static size_t dso_count_;
81
82   Dso(DsoType type, const std::string& path);
83   bool Load();
84   bool LoadKernel();
85   bool LoadKernelModule();
86   bool LoadElfFile();
87   void InsertSymbol(const Symbol& symbol);
88   void FixupSymbolLength();
89
90   const DsoType type_;
91   const std::string path_;
92   std::vector<Symbol> symbols_;
93   bool is_loaded_;
94 };
95
96 #endif  // SIMPLE_PERF_DSO_H_