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