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 
     23 namespace llvm {
     24 namespace symbolize {
     25 
     26 using namespace object;
     27 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
     28 
     29 class LLVMSymbolizer {
     30 public:
     31   struct Options {
     32     FunctionNameKind PrintFunctions;
     33     bool UseSymbolTable : 1;
     34     bool Demangle : 1;
     35     bool RelativeAddresses : 1;
     36     std::string DefaultArch;
     37     std::vector<std::string> DsymHints;
     38     Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
     39             bool UseSymbolTable = true, bool Demangle = true,
     40             bool RelativeAddresses = false, std::string DefaultArch = "")
     41         : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
     42           Demangle(Demangle), RelativeAddresses(RelativeAddresses),
     43           DefaultArch(DefaultArch) {}
     44   };
     45 
     46   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
     47   ~LLVMSymbolizer() {
     48     flush();
     49   }
     50 
     51   ErrorOr<DILineInfo> symbolizeCode(const std::string &ModuleName,
     52                                     uint64_t ModuleOffset);
     53   ErrorOr<DIInliningInfo> symbolizeInlinedCode(const std::string &ModuleName,
     54                                                uint64_t ModuleOffset);
     55   ErrorOr<DIGlobal> symbolizeData(const std::string &ModuleName,
     56                                   uint64_t ModuleOffset);
     57   void flush();
     58   static std::string DemangleName(const std::string &Name,
     59                                   const SymbolizableModule *ModInfo);
     60 
     61 private:
     62   // Bundles together object file with code/data and object file with
     63   // corresponding debug info. These objects can be the same.
     64   typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
     65 
     66   ErrorOr<SymbolizableModule *>
     67   getOrCreateModuleInfo(const std::string &ModuleName);
     68   ObjectFile *lookUpDsymFile(const std::string &Path,
     69                              const MachOObjectFile *ExeObj,
     70                              const std::string &ArchName);
     71   ObjectFile *lookUpDebuglinkObject(const std::string &Path,
     72                                     const ObjectFile *Obj,
     73                                     const std::string &ArchName);
     74 
     75   /// \brief Returns pair of pointers to object and debug object.
     76   ErrorOr<ObjectPair> getOrCreateObjectPair(const std::string &Path,
     77                                             const std::string &ArchName);
     78 
     79   /// \brief Return a pointer to object file at specified path, for a specified
     80   /// architecture (e.g. if path refers to a Mach-O universal binary, only one
     81   /// object file from it will be returned).
     82   ErrorOr<ObjectFile *> getOrCreateObject(const std::string &Path,
     83                                           const std::string &ArchName);
     84 
     85   std::map<std::string, ErrorOr<std::unique_ptr<SymbolizableModule>>> Modules;
     86 
     87   /// \brief Contains cached results of getOrCreateObjectPair().
     88   std::map<std::pair<std::string, std::string>, ErrorOr<ObjectPair>>
     89       ObjectPairForPathArch;
     90 
     91   /// \brief Contains parsed binary for each path, or parsing error.
     92   std::map<std::string, ErrorOr<OwningBinary<Binary>>> BinaryForPath;
     93 
     94   /// \brief Parsed object file for path/architecture pair, where "path" refers
     95   /// to Mach-O universal binary.
     96   std::map<std::pair<std::string, std::string>, ErrorOr<std::unique_ptr<ObjectFile>>>
     97       ObjectForUBPathAndArch;
     98 
     99   Options Opts;
    100 };
    101 
    102 } // namespace symbolize
    103 } // namespace llvm
    104 
    105 #endif
    106