1321369Sdim//===- Symbolize.h ----------------------------------------------*- C++ -*-===//
2292915Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6292915Sdim//
7292915Sdim//===----------------------------------------------------------------------===//
8292915Sdim//
9292915Sdim// Header for LLVM symbolization library.
10292915Sdim//
11292915Sdim//===----------------------------------------------------------------------===//
12321369Sdim
13292915Sdim#ifndef LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
14292915Sdim#define LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
15292915Sdim
16292915Sdim#include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
17321369Sdim#include "llvm/Object/Binary.h"
18292915Sdim#include "llvm/Object/ObjectFile.h"
19360784Sdim#include "llvm/Object/ELFObjectFile.h"
20321369Sdim#include "llvm/Support/Error.h"
21321369Sdim#include <algorithm>
22321369Sdim#include <cstdint>
23292915Sdim#include <map>
24292915Sdim#include <memory>
25292915Sdim#include <string>
26309124Sdim#include <utility>
27321369Sdim#include <vector>
28292915Sdim
29292915Sdimnamespace llvm {
30292915Sdimnamespace symbolize {
31292915Sdim
32292915Sdimusing namespace object;
33321369Sdim
34292915Sdimusing FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
35292915Sdim
36292915Sdimclass LLVMSymbolizer {
37292915Sdimpublic:
38292915Sdim  struct Options {
39353358Sdim    FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName;
40353358Sdim    bool UseSymbolTable = true;
41353358Sdim    bool Demangle = true;
42353358Sdim    bool RelativeAddresses = false;
43360784Sdim    bool UntagAddresses = false;
44292915Sdim    std::string DefaultArch;
45292915Sdim    std::vector<std::string> DsymHints;
46353358Sdim    std::string FallbackDebugPath;
47353358Sdim    std::string DWPName;
48360784Sdim    std::vector<std::string> DebugFileDirectory;
49292915Sdim  };
50292915Sdim
51353358Sdim  LLVMSymbolizer() = default;
52353358Sdim  LLVMSymbolizer(const Options &Opts) : Opts(Opts) {}
53321369Sdim
54292915Sdim  ~LLVMSymbolizer() {
55292915Sdim    flush();
56292915Sdim  }
57292915Sdim
58353358Sdim  Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj,
59353358Sdim                                     object::SectionedAddress ModuleOffset);
60309124Sdim  Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
61353358Sdim                                     object::SectionedAddress ModuleOffset);
62353358Sdim  Expected<DIInliningInfo>
63353358Sdim  symbolizeInlinedCode(const std::string &ModuleName,
64353358Sdim                       object::SectionedAddress ModuleOffset);
65309124Sdim  Expected<DIGlobal> symbolizeData(const std::string &ModuleName,
66353358Sdim                                   object::SectionedAddress ModuleOffset);
67353358Sdim  Expected<std::vector<DILocal>>
68353358Sdim  symbolizeFrame(const std::string &ModuleName,
69353358Sdim                 object::SectionedAddress ModuleOffset);
70292915Sdim  void flush();
71292915Sdim
72321369Sdim  static std::string
73321369Sdim  DemangleName(const std::string &Name,
74321369Sdim               const SymbolizableModule *DbiModuleDescriptor);
75321369Sdim
76292915Sdimprivate:
77292915Sdim  // Bundles together object file with code/data and object file with
78292915Sdim  // corresponding debug info. These objects can be the same.
79353358Sdim  using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
80292915Sdim
81353358Sdim  Expected<DILineInfo>
82353358Sdim  symbolizeCodeCommon(SymbolizableModule *Info,
83353358Sdim                      object::SectionedAddress ModuleOffset);
84353358Sdim
85309124Sdim  /// Returns a SymbolizableModule or an error if loading debug info failed.
86309124Sdim  /// Only one attempt is made to load a module, and errors during loading are
87309124Sdim  /// only reported once. Subsequent calls to get module info for a module that
88309124Sdim  /// failed to load will return nullptr.
89309124Sdim  Expected<SymbolizableModule *>
90353358Sdim  getOrCreateModuleInfo(const std::string &ModuleName);
91309124Sdim
92353358Sdim  Expected<SymbolizableModule *>
93353358Sdim  createModuleInfo(const ObjectFile *Obj,
94353358Sdim                   std::unique_ptr<DIContext> Context,
95353358Sdim                   StringRef ModuleName);
96353358Sdim
97292915Sdim  ObjectFile *lookUpDsymFile(const std::string &Path,
98292915Sdim                             const MachOObjectFile *ExeObj,
99292915Sdim                             const std::string &ArchName);
100292915Sdim  ObjectFile *lookUpDebuglinkObject(const std::string &Path,
101292915Sdim                                    const ObjectFile *Obj,
102292915Sdim                                    const std::string &ArchName);
103360784Sdim  ObjectFile *lookUpBuildIDObject(const std::string &Path,
104360784Sdim                                  const ELFObjectFileBase *Obj,
105360784Sdim                                  const std::string &ArchName);
106292915Sdim
107341825Sdim  /// Returns pair of pointers to object and debug object.
108309124Sdim  Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
109292915Sdim                                            const std::string &ArchName);
110292915Sdim
111341825Sdim  /// Return a pointer to object file at specified path, for a specified
112292915Sdim  /// architecture (e.g. if path refers to a Mach-O universal binary, only one
113292915Sdim  /// object file from it will be returned).
114309124Sdim  Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
115292915Sdim                                          const std::string &ArchName);
116292915Sdim
117309124Sdim  std::map<std::string, std::unique_ptr<SymbolizableModule>> Modules;
118292915Sdim
119341825Sdim  /// Contains cached results of getOrCreateObjectPair().
120309124Sdim  std::map<std::pair<std::string, std::string>, ObjectPair>
121292915Sdim      ObjectPairForPathArch;
122292915Sdim
123341825Sdim  /// Contains parsed binary for each path, or parsing error.
124309124Sdim  std::map<std::string, OwningBinary<Binary>> BinaryForPath;
125292915Sdim
126341825Sdim  /// Parsed object file for path/architecture pair, where "path" refers
127292915Sdim  /// to Mach-O universal binary.
128309124Sdim  std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
129292915Sdim      ObjectForUBPathAndArch;
130292915Sdim
131292915Sdim  Options Opts;
132292915Sdim};
133292915Sdim
134321369Sdim} // end namespace symbolize
135321369Sdim} // end namespace llvm
136292915Sdim
137321369Sdim#endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
138