xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/DebugInfo/Symbolize/Symbolize.h (revision 68d75eff68281c1b445e3010bb975eae07aac225)
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/Support/Error.h"
20 #include <algorithm>
21 #include <cstdint>
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 namespace llvm {
29 namespace symbolize {
30 
31 using namespace object;
32 
33 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
34 
35 class LLVMSymbolizer {
36 public:
37   struct Options {
38     FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName;
39     bool UseSymbolTable = true;
40     bool Demangle = true;
41     bool RelativeAddresses = false;
42     bool UntagAddresses = false;
43     std::string DefaultArch;
44     std::vector<std::string> DsymHints;
45     std::string FallbackDebugPath;
46     std::string DWPName;
47   };
48 
49   LLVMSymbolizer() = default;
50   LLVMSymbolizer(const Options &Opts) : Opts(Opts) {}
51 
52   ~LLVMSymbolizer() {
53     flush();
54   }
55 
56   Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj,
57                                      object::SectionedAddress ModuleOffset);
58   Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
59                                      object::SectionedAddress ModuleOffset);
60   Expected<DIInliningInfo>
61   symbolizeInlinedCode(const std::string &ModuleName,
62                        object::SectionedAddress ModuleOffset);
63   Expected<DIGlobal> symbolizeData(const std::string &ModuleName,
64                                    object::SectionedAddress ModuleOffset);
65   Expected<std::vector<DILocal>>
66   symbolizeFrame(const std::string &ModuleName,
67                  object::SectionedAddress 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<const ObjectFile *, const ObjectFile *>;
78 
79   Expected<DILineInfo>
80   symbolizeCodeCommon(SymbolizableModule *Info,
81                       object::SectionedAddress ModuleOffset);
82 
83   /// Returns a SymbolizableModule or an error if loading debug info failed.
84   /// Only one attempt is made to load a module, and errors during loading are
85   /// only reported once. Subsequent calls to get module info for a module that
86   /// failed to load will return nullptr.
87   Expected<SymbolizableModule *>
88   getOrCreateModuleInfo(const std::string &ModuleName);
89 
90   Expected<SymbolizableModule *>
91   createModuleInfo(const ObjectFile *Obj,
92                    std::unique_ptr<DIContext> Context,
93                    StringRef ModuleName);
94 
95   ObjectFile *lookUpDsymFile(const std::string &Path,
96                              const MachOObjectFile *ExeObj,
97                              const std::string &ArchName);
98   ObjectFile *lookUpDebuglinkObject(const std::string &Path,
99                                     const ObjectFile *Obj,
100                                     const std::string &ArchName);
101 
102   /// Returns pair of pointers to object and debug object.
103   Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
104                                             const std::string &ArchName);
105 
106   /// Return a pointer to object file at specified path, for a specified
107   /// architecture (e.g. if path refers to a Mach-O universal binary, only one
108   /// object file from it will be returned).
109   Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
110                                           const std::string &ArchName);
111 
112   std::map<std::string, std::unique_ptr<SymbolizableModule>> Modules;
113 
114   /// Contains cached results of getOrCreateObjectPair().
115   std::map<std::pair<std::string, std::string>, ObjectPair>
116       ObjectPairForPathArch;
117 
118   /// Contains parsed binary for each path, or parsing error.
119   std::map<std::string, OwningBinary<Binary>> BinaryForPath;
120 
121   /// Parsed object file for path/architecture pair, where "path" refers
122   /// to Mach-O universal binary.
123   std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
124       ObjectForUBPathAndArch;
125 
126   Options Opts;
127 };
128 
129 } // end namespace symbolize
130 } // end namespace llvm
131 
132 #endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
133