Home | History | Annotate | Download | only in Symbolize
      1 //===-- Symbolize.h --------------------------------------------- C++ -----===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Header for LLVM symbolization library.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 #ifndef LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
     14 #define LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
     15 
     16 #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
     17 #include "llvm/Object/ObjectFile.h"
     18 #include "llvm/Support/ErrorOr.h"
     19 #include <map>
     20 #include <memory>
     21 #include <string>
     22 #include <utility>
     23 
     24 namespace llvm {
     25 namespace symbolize {
     26 
     27 using namespace object;
     28 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
     29 
     30 class LLVMSymbolizer {
     31 public:
     32   struct Options {
     33     FunctionNameKind PrintFunctions;
     34     bool UseSymbolTable : 1;
     35     bool Demangle : 1;
     36     bool RelativeAddresses : 1;
     37     std::string DefaultArch;
     38     std::vector<std::string> DsymHints;
     39     Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
     40             bool UseSymbolTable = true, bool Demangle = true,
     41             bool RelativeAddresses = false, std::string DefaultArch = "")
     42         : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
     43           Demangle(Demangle), RelativeAddresses(RelativeAddresses),
     44           DefaultArch(std::move(DefaultArch)) {}
     45   };
     46 
     47   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
     48   ~LLVMSymbolizer() {
     49     flush();
     50   }
     51 
     52   Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
     53                                      uint64_t ModuleOffset);
     54   Expected<DIInliningInfo> symbolizeInlinedCode(const std::string &ModuleName,
     55                                                 uint64_t ModuleOffset);
     56   Expected<DIGlobal> symbolizeData(const std::string &ModuleName,
     57                                    uint64_t ModuleOffset);
     58   void flush();
     59   static std::string
     60   DemangleName(const std::string &Name,
     61                const SymbolizableModule *DbiModuleDescriptor);
     62 
     63 private:
     64   // Bundles together object file with code/data and object file with
     65   // corresponding debug info. These objects can be the same.
     66   typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
     67 
     68   /// Returns a SymbolizableModule or an error if loading debug info failed.
     69   /// Only one attempt is made to load a module, and errors during loading are
     70   /// only reported once. Subsequent calls to get module info for a module that
     71   /// failed to load will return nullptr.
     72   Expected<SymbolizableModule *>
     73   getOrCreateModuleInfo(const std::string &ModuleName);
     74 
     75   ObjectFile *lookUpDsymFile(const std::string &Path,
     76                              const MachOObjectFile *ExeObj,
     77                              const std::string &ArchName);
     78   ObjectFile *lookUpDebuglinkObject(const std::string &Path,
     79                                     const ObjectFile *Obj,
     80                                     const std::string &ArchName);
     81 
     82   /// \brief Returns pair of pointers to object and debug object.
     83   Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
     84                                             const std::string &ArchName);
     85 
     86   /// \brief Return a pointer to object file at specified path, for a specified
     87   /// architecture (e.g. if path refers to a Mach-O universal binary, only one
     88   /// object file from it will be returned).
     89   Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
     90                                           const std::string &ArchName);
     91 
     92   std::map<std::string, std::unique_ptr<SymbolizableModule>> Modules;
     93 
     94   /// \brief Contains cached results of getOrCreateObjectPair().
     95   std::map<std::pair<std::string, std::string>, ObjectPair>
     96       ObjectPairForPathArch;
     97 
     98   /// \brief Contains parsed binary for each path, or parsing error.
     99   std::map<std::string, OwningBinary<Binary>> BinaryForPath;
    100 
    101   /// \brief Parsed object file for path/architecture pair, where "path" refers
    102   /// to Mach-O universal binary.
    103   std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
    104       ObjectForUBPathAndArch;
    105 
    106   Options Opts;
    107 };
    108 
    109 } // namespace symbolize
    110 } // namespace llvm
    111 
    112 #endif
    113