1 //===- Symbolize.h ----------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Header for LLVM symbolization library.
10 //
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/Binary.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Object/ELFObjectFile.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 = FunctionNameKind::LinkageName;
40     bool UseSymbolTable = true;
41     bool Demangle = true;
42     bool RelativeAddresses = false;
43     bool UntagAddresses = false;
44     std::string DefaultArch;
45     std::vector<std::string> DsymHints;
46     std::string FallbackDebugPath;
47     std::string DWPName;
48     std::vector<std::string> DebugFileDirectory;
49   };
50 
51   LLVMSymbolizer() = default;
LLVMSymbolizer(const Options & Opts)52   LLVMSymbolizer(const Options &Opts) : Opts(Opts) {}
53 
~LLVMSymbolizer()54   ~LLVMSymbolizer() {
55     flush();
56   }
57 
58   Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj,
59                                      object::SectionedAddress ModuleOffset);
60   Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
61                                      object::SectionedAddress ModuleOffset);
62   Expected<DIInliningInfo>
63   symbolizeInlinedCode(const std::string &ModuleName,
64                        object::SectionedAddress ModuleOffset);
65   Expected<DIGlobal> symbolizeData(const std::string &ModuleName,
66                                    object::SectionedAddress ModuleOffset);
67   Expected<std::vector<DILocal>>
68   symbolizeFrame(const std::string &ModuleName,
69                  object::SectionedAddress ModuleOffset);
70   void flush();
71 
72   static std::string
73   DemangleName(const std::string &Name,
74                const SymbolizableModule *DbiModuleDescriptor);
75 
76 private:
77   // Bundles together object file with code/data and object file with
78   // corresponding debug info. These objects can be the same.
79   using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
80 
81   Expected<DILineInfo>
82   symbolizeCodeCommon(SymbolizableModule *Info,
83                       object::SectionedAddress ModuleOffset);
84 
85   /// Returns a SymbolizableModule or an error if loading debug info failed.
86   /// Only one attempt is made to load a module, and errors during loading are
87   /// only reported once. Subsequent calls to get module info for a module that
88   /// failed to load will return nullptr.
89   Expected<SymbolizableModule *>
90   getOrCreateModuleInfo(const std::string &ModuleName);
91 
92   Expected<SymbolizableModule *>
93   createModuleInfo(const ObjectFile *Obj,
94                    std::unique_ptr<DIContext> Context,
95                    StringRef ModuleName);
96 
97   ObjectFile *lookUpDsymFile(const std::string &Path,
98                              const MachOObjectFile *ExeObj,
99                              const std::string &ArchName);
100   ObjectFile *lookUpDebuglinkObject(const std::string &Path,
101                                     const ObjectFile *Obj,
102                                     const std::string &ArchName);
103   ObjectFile *lookUpBuildIDObject(const std::string &Path,
104                                   const ELFObjectFileBase *Obj,
105                                   const std::string &ArchName);
106 
107   /// Returns pair of pointers to object and debug object.
108   Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
109                                             const std::string &ArchName);
110 
111   /// Return a pointer to object file at specified path, for a specified
112   /// architecture (e.g. if path refers to a Mach-O universal binary, only one
113   /// object file from it will be returned).
114   Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
115                                           const std::string &ArchName);
116 
117   std::map<std::string, std::unique_ptr<SymbolizableModule>> Modules;
118 
119   /// Contains cached results of getOrCreateObjectPair().
120   std::map<std::pair<std::string, std::string>, ObjectPair>
121       ObjectPairForPathArch;
122 
123   /// Contains parsed binary for each path, or parsing error.
124   std::map<std::string, OwningBinary<Binary>> BinaryForPath;
125 
126   /// Parsed object file for path/architecture pair, where "path" refers
127   /// to Mach-O universal binary.
128   std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
129       ObjectForUBPathAndArch;
130 
131   Options Opts;
132 };
133 
134 } // end namespace symbolize
135 } // end namespace llvm
136 
137 #endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
138