Home | History | Annotate | Download | only in simpleperf
      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 <android-base/logging.h>
     26 #include <android-base/test_utils.h>
     27 
     28 #include "build_id.h"
     29 #include "read_elf.h"
     30 
     31 struct Symbol {
     32   uint64_t addr;
     33   // TODO: make len uint32_t.
     34   uint64_t len;
     35 
     36   Symbol(const std::string& name, uint64_t addr, uint64_t len);
     37   const char* Name() const { return name_; }
     38 
     39   const char* DemangledName() const;
     40 
     41   bool HasDumpId() const {
     42     return dump_id_ != UINT_MAX;
     43   }
     44 
     45   bool GetDumpId(uint32_t* pdump_id) const {
     46     if (!HasDumpId()) {
     47       return false;
     48     }
     49     *pdump_id = dump_id_;
     50     return true;
     51   }
     52 
     53   static bool CompareByDumpId(const Symbol* s1, const Symbol* s2) {
     54     uint32_t id1 = UINT_MAX;
     55     s1->GetDumpId(&id1);
     56     uint32_t id2 = UINT_MAX;
     57     s2->GetDumpId(&id2);
     58     return id1 < id2;
     59   }
     60 
     61   static bool CompareByAddr(const Symbol* s1, const Symbol* s2) {
     62     return s1->addr < s2->addr;
     63   }
     64 
     65   static bool CompareValueByAddr(const Symbol& s1, const Symbol& s2) {
     66     return s1.addr < s2.addr;
     67   }
     68 
     69  private:
     70   const char* name_;
     71   mutable const char* demangled_name_;
     72   mutable uint32_t dump_id_;
     73 
     74   friend class Dso;
     75 };
     76 
     77 enum DsoType {
     78   DSO_KERNEL,
     79   DSO_KERNEL_MODULE,
     80   DSO_ELF_FILE,
     81 };
     82 
     83 struct KernelSymbol;
     84 struct ElfFileSymbol;
     85 
     86 class Dso {
     87  public:
     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);
     95     }
     96   }
     97   static void ReadKernelSymbolsFromProc() {
     98     read_kernel_symbols_from_proc_ = true;
     99   }
    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(const std::string& vdso_file, bool is_64bit);
    104 
    105   static std::unique_ptr<Dso> CreateDso(DsoType dso_type, const std::string& dso_path,
    106                                         bool force_64bit = false);
    107 
    108   ~Dso();
    109 
    110   DsoType type() const { return type_; }
    111 
    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_; }
    118 
    119   bool HasDumpId() {
    120     return dump_id_ != UINT_MAX;
    121   }
    122 
    123   bool GetDumpId(uint32_t* pdump_id) {
    124     if (!HasDumpId()) {
    125       return false;
    126     }
    127     *pdump_id = dump_id_;
    128     return true;
    129   }
    130 
    131   uint32_t CreateDumpId();
    132   uint32_t CreateSymbolDumpId(const Symbol* symbol);
    133 
    134   // Return the minimum virtual address in program header.
    135   uint64_t MinVirtualAddress();
    136   void SetMinVirtualAddress(uint64_t min_vaddr) { min_vaddr_ = min_vaddr; }
    137 
    138   const Symbol* FindSymbol(uint64_t vaddr_in_dso);
    139 
    140   const std::vector<Symbol>& GetSymbols();
    141   void SetSymbols(std::vector<Symbol>* symbols);
    142 
    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);
    146 
    147  private:
    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::string vdso_64bit_;
    157   static std::string vdso_32bit_;
    158 
    159   Dso(DsoType type, const std::string& path, bool force_64bit);
    160   void Load();
    161   bool LoadKernel();
    162   bool LoadKernelModule();
    163   bool LoadElfFile();
    164   bool LoadEmbeddedElfFile();
    165   void FixupSymbolLength();
    166   BuildId GetExpectedBuildId();
    167   bool CheckReadSymbolResult(ElfStatus result, const std::string& filename);
    168 
    169   const DsoType type_;
    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_;
    177   uint64_t min_vaddr_;
    178   std::vector<Symbol> symbols_;
    179   // unknown symbols are like [libc.so+0x1234].
    180   std::unordered_map<uint64_t, Symbol> unknown_symbols_;
    181   bool is_loaded_;
    182   // Used to identify current dso if it needs to be dumped.
    183   uint32_t dump_id_;
    184   // Used to assign dump_id for symbols in current dso.
    185   uint32_t symbol_dump_id_;
    186   android::base::LogSeverity symbol_warning_loglevel_;
    187 };
    188 
    189 const char* DsoTypeToString(DsoType dso_type);
    190 
    191 #endif  // SIMPLE_PERF_DSO_H_
    192