blob: 4e57fe43847811f2012f07558ad06470f3a20176 [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 {
38 FunctionNameKind PrintFunctions;
39 bool UseSymbolTable : 1;
40 bool Demangle : 1;
41 bool RelativeAddresses : 1;
42 std::string DefaultArch;
43 std::vector<std::string> DsymHints;
Andrew Walbran16937d02019-10-22 13:54:20 +010044 std::string FallbackDebugPath;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045
46 Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
47 bool UseSymbolTable = true, bool Demangle = true,
Andrew Walbran16937d02019-10-22 13:54:20 +010048 bool RelativeAddresses = false, std::string DefaultArch = "",
49 std::string FallbackDebugPath = "")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050 : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
51 Demangle(Demangle), RelativeAddresses(RelativeAddresses),
Andrew Walbran16937d02019-10-22 13:54:20 +010052 DefaultArch(std::move(DefaultArch)),
53 FallbackDebugPath(std::move(FallbackDebugPath)) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054 };
55
56 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
57
58 ~LLVMSymbolizer() {
59 flush();
60 }
61
62 Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
63 uint64_t ModuleOffset,
64 StringRef DWPName = "");
65 Expected<DIInliningInfo> symbolizeInlinedCode(const std::string &ModuleName,
66 uint64_t ModuleOffset,
67 StringRef DWPName = "");
68 Expected<DIGlobal> symbolizeData(const std::string &ModuleName,
69 uint64_t ModuleOffset);
70 void flush();
71
72 static std::string
73 DemangleName(const std::string &Name,
74 const SymbolizableModule *DbiModuleDescriptor);
75
76private:
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<ObjectFile *, ObjectFile *>;
80
81 /// Returns a SymbolizableModule or an error if loading debug info failed.
82 /// Only one attempt is made to load a module, and errors during loading are
83 /// only reported once. Subsequent calls to get module info for a module that
84 /// failed to load will return nullptr.
85 Expected<SymbolizableModule *>
86 getOrCreateModuleInfo(const std::string &ModuleName, StringRef DWPName = "");
87
88 ObjectFile *lookUpDsymFile(const std::string &Path,
89 const MachOObjectFile *ExeObj,
90 const std::string &ArchName);
91 ObjectFile *lookUpDebuglinkObject(const std::string &Path,
92 const ObjectFile *Obj,
93 const std::string &ArchName);
94
Andrew Scullcdfcccc2018-10-05 20:58:37 +010095 /// Returns pair of pointers to object and debug object.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096 Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
97 const std::string &ArchName);
98
Andrew Scullcdfcccc2018-10-05 20:58:37 +010099 /// Return a pointer to object file at specified path, for a specified
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100100 /// architecture (e.g. if path refers to a Mach-O universal binary, only one
101 /// object file from it will be returned).
102 Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
103 const std::string &ArchName);
104
105 std::map<std::string, std::unique_ptr<SymbolizableModule>> Modules;
106
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100107 /// Contains cached results of getOrCreateObjectPair().
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100108 std::map<std::pair<std::string, std::string>, ObjectPair>
109 ObjectPairForPathArch;
110
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100111 /// Contains parsed binary for each path, or parsing error.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100112 std::map<std::string, OwningBinary<Binary>> BinaryForPath;
113
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100114 /// Parsed object file for path/architecture pair, where "path" refers
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100115 /// to Mach-O universal binary.
116 std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
117 ObjectForUBPathAndArch;
118
119 Options Opts;
120};
121
122} // end namespace symbolize
123} // end namespace llvm
124
125#endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H