• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = "")
PrintFunctionsOptions42         : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
43           Demangle(Demangle), RelativeAddresses(RelativeAddresses),
44           DefaultArch(std::move(DefaultArch)) {}
45   };
46 
Opts(Opts)47   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
~LLVMSymbolizer()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 DemangleName(const std::string &Name,
60                                   const SymbolizableModule *ModInfo);
61 
62 private:
63   // Bundles together object file with code/data and object file with
64   // corresponding debug info. These objects can be the same.
65   typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
66 
67   /// Returns a SymbolizableModule or an error if loading debug info failed.
68   /// Only one attempt is made to load a module, and errors during loading are
69   /// only reported once. Subsequent calls to get module info for a module that
70   /// failed to load will return nullptr.
71   Expected<SymbolizableModule *>
72   getOrCreateModuleInfo(const std::string &ModuleName);
73 
74   ObjectFile *lookUpDsymFile(const std::string &Path,
75                              const MachOObjectFile *ExeObj,
76                              const std::string &ArchName);
77   ObjectFile *lookUpDebuglinkObject(const std::string &Path,
78                                     const ObjectFile *Obj,
79                                     const std::string &ArchName);
80 
81   /// \brief Returns pair of pointers to object and debug object.
82   Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
83                                             const std::string &ArchName);
84 
85   /// \brief Return a pointer to object file at specified path, for a specified
86   /// architecture (e.g. if path refers to a Mach-O universal binary, only one
87   /// object file from it will be returned).
88   Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
89                                           const std::string &ArchName);
90 
91   std::map<std::string, std::unique_ptr<SymbolizableModule>> Modules;
92 
93   /// \brief Contains cached results of getOrCreateObjectPair().
94   std::map<std::pair<std::string, std::string>, ObjectPair>
95       ObjectPairForPathArch;
96 
97   /// \brief Contains parsed binary for each path, or parsing error.
98   std::map<std::string, OwningBinary<Binary>> BinaryForPath;
99 
100   /// \brief Parsed object file for path/architecture pair, where "path" refers
101   /// to Mach-O universal binary.
102   std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
103       ObjectForUBPathAndArch;
104 
105   Options Opts;
106 };
107 
108 } // namespace symbolize
109 } // namespace llvm
110 
111 #endif
112