blob: d3da28ca0b7bf47cb236f19537109aee9881a762 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- Symbolize.h ----------------------------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
28namespace llvm {
29namespace symbolize {
30
31using namespace object;
32
33using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
34
35class LLVMSymbolizer {
36public:
37 struct Options {
Andrew Walbran3d2c1972020-04-07 12:24:26 +010038 FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName;
39 bool UseSymbolTable = true;
40 bool Demangle = true;
41 bool RelativeAddresses = false;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042 std::string DefaultArch;
43 std::vector<std::string> DsymHints;
Andrew Walbran16937d02019-10-22 13:54:20 +010044 std::string FallbackDebugPath;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010045 std::string DWPName;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046 };
47
Andrew Walbran3d2c1972020-04-07 12:24:26 +010048 LLVMSymbolizer() = default;
49 LLVMSymbolizer(const Options &Opts) : Opts(Opts) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050
51 ~LLVMSymbolizer() {
52 flush();
53 }
54
Andrew Walbran3d2c1972020-04-07 12:24:26 +010055 Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj,
56 object::SectionedAddress ModuleOffset);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057 Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010058 object::SectionedAddress ModuleOffset);
59 Expected<DIInliningInfo>
60 symbolizeInlinedCode(const std::string &ModuleName,
61 object::SectionedAddress ModuleOffset);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010062 Expected<DIGlobal> symbolizeData(const std::string &ModuleName,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010063 object::SectionedAddress ModuleOffset);
64 Expected<std::vector<DILocal>>
65 symbolizeFrame(const std::string &ModuleName,
66 object::SectionedAddress ModuleOffset);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010067 void flush();
68
69 static std::string
70 DemangleName(const std::string &Name,
71 const SymbolizableModule *DbiModuleDescriptor);
72
73private:
74 // Bundles together object file with code/data and object file with
75 // corresponding debug info. These objects can be the same.
Andrew Walbran3d2c1972020-04-07 12:24:26 +010076 using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
77
78 Expected<DILineInfo>
79 symbolizeCodeCommon(SymbolizableModule *Info,
80 object::SectionedAddress ModuleOffset);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010081
82 /// Returns a SymbolizableModule or an error if loading debug info failed.
83 /// Only one attempt is made to load a module, and errors during loading are
84 /// only reported once. Subsequent calls to get module info for a module that
85 /// failed to load will return nullptr.
86 Expected<SymbolizableModule *>
Andrew Walbran3d2c1972020-04-07 12:24:26 +010087 getOrCreateModuleInfo(const std::string &ModuleName);
88
89 Expected<SymbolizableModule *>
90 createModuleInfo(const ObjectFile *Obj,
91 std::unique_ptr<DIContext> Context,
92 StringRef ModuleName);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010093
94 ObjectFile *lookUpDsymFile(const std::string &Path,
95 const MachOObjectFile *ExeObj,
96 const std::string &ArchName);
97 ObjectFile *lookUpDebuglinkObject(const std::string &Path,
98 const ObjectFile *Obj,
99 const std::string &ArchName);
100
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100101 /// Returns pair of pointers to object and debug object.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100102 Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
103 const std::string &ArchName);
104
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100105 /// Return a pointer to object file at specified path, for a specified
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100106 /// architecture (e.g. if path refers to a Mach-O universal binary, only one
107 /// object file from it will be returned).
108 Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
109 const std::string &ArchName);
110
111 std::map<std::string, std::unique_ptr<SymbolizableModule>> Modules;
112
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100113 /// Contains cached results of getOrCreateObjectPair().
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100114 std::map<std::pair<std::string, std::string>, ObjectPair>
115 ObjectPairForPathArch;
116
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100117 /// Contains parsed binary for each path, or parsing error.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100118 std::map<std::string, OwningBinary<Binary>> BinaryForPath;
119
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100120 /// Parsed object file for path/architecture pair, where "path" refers
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100121 /// to Mach-O universal binary.
122 std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
123 ObjectForUBPathAndArch;
124
125 Options Opts;
126};
127
128} // end namespace symbolize
129} // end namespace llvm
130
131#endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H