Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- Symbolize.h ----------------------------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 19 | #include "llvm/Object/ELFObjectFile.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 35 | using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 36 | |
| 37 | class LLVMSymbolizer { |
| 38 | public: |
| 39 | struct Options { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 40 | FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 41 | FileLineInfoKind PathStyle = FileLineInfoKind::AbsoluteFilePath; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 42 | bool UseSymbolTable = true; |
| 43 | bool Demangle = true; |
| 44 | bool RelativeAddresses = false; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 45 | bool UntagAddresses = false; |
| 46 | bool UseDIA = false; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 47 | std::string DefaultArch; |
| 48 | std::vector<std::string> DsymHints; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 49 | std::string FallbackDebugPath; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 50 | std::string DWPName; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 51 | std::vector<std::string> DebugFileDirectory; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 54 | LLVMSymbolizer() = default; |
| 55 | LLVMSymbolizer(const Options &Opts) : Opts(Opts) {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 56 | |
| 57 | ~LLVMSymbolizer() { |
| 58 | flush(); |
| 59 | } |
| 60 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 61 | Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj, |
| 62 | object::SectionedAddress ModuleOffset); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 63 | Expected<DILineInfo> symbolizeCode(const std::string &ModuleName, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 64 | object::SectionedAddress ModuleOffset); |
| 65 | Expected<DIInliningInfo> |
| 66 | symbolizeInlinedCode(const std::string &ModuleName, |
| 67 | object::SectionedAddress ModuleOffset); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | Expected<DIGlobal> symbolizeData(const std::string &ModuleName, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 69 | object::SectionedAddress ModuleOffset); |
| 70 | Expected<std::vector<DILocal>> |
| 71 | symbolizeFrame(const std::string &ModuleName, |
| 72 | object::SectionedAddress ModuleOffset); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | void flush(); |
| 74 | |
| 75 | static std::string |
| 76 | DemangleName(const std::string &Name, |
| 77 | const SymbolizableModule *DbiModuleDescriptor); |
| 78 | |
| 79 | private: |
| 80 | // Bundles together object file with code/data and object file with |
| 81 | // corresponding debug info. These objects can be the same. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 82 | using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>; |
| 83 | |
| 84 | Expected<DILineInfo> |
| 85 | symbolizeCodeCommon(SymbolizableModule *Info, |
| 86 | object::SectionedAddress ModuleOffset); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 87 | |
| 88 | /// Returns a SymbolizableModule or an error if loading debug info failed. |
| 89 | /// Only one attempt is made to load a module, and errors during loading are |
| 90 | /// only reported once. Subsequent calls to get module info for a module that |
| 91 | /// failed to load will return nullptr. |
| 92 | Expected<SymbolizableModule *> |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 93 | getOrCreateModuleInfo(const std::string &ModuleName); |
| 94 | |
| 95 | Expected<SymbolizableModule *> |
| 96 | createModuleInfo(const ObjectFile *Obj, |
| 97 | std::unique_ptr<DIContext> Context, |
| 98 | StringRef ModuleName); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 99 | |
| 100 | ObjectFile *lookUpDsymFile(const std::string &Path, |
| 101 | const MachOObjectFile *ExeObj, |
| 102 | const std::string &ArchName); |
| 103 | ObjectFile *lookUpDebuglinkObject(const std::string &Path, |
| 104 | const ObjectFile *Obj, |
| 105 | const std::string &ArchName); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 106 | ObjectFile *lookUpBuildIDObject(const std::string &Path, |
| 107 | const ELFObjectFileBase *Obj, |
| 108 | const std::string &ArchName); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 109 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 110 | /// Returns pair of pointers to object and debug object. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 111 | Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path, |
| 112 | const std::string &ArchName); |
| 113 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 114 | /// Return a pointer to object file at specified path, for a specified |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 115 | /// architecture (e.g. if path refers to a Mach-O universal binary, only one |
| 116 | /// object file from it will be returned). |
| 117 | Expected<ObjectFile *> getOrCreateObject(const std::string &Path, |
| 118 | const std::string &ArchName); |
| 119 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 120 | std::map<std::string, std::unique_ptr<SymbolizableModule>, std::less<>> |
| 121 | Modules; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 122 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 123 | /// Contains cached results of getOrCreateObjectPair(). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 124 | std::map<std::pair<std::string, std::string>, ObjectPair> |
| 125 | ObjectPairForPathArch; |
| 126 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 127 | /// Contains parsed binary for each path, or parsing error. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 128 | std::map<std::string, OwningBinary<Binary>> BinaryForPath; |
| 129 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 130 | /// Parsed object file for path/architecture pair, where "path" refers |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 131 | /// to Mach-O universal binary. |
| 132 | std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>> |
| 133 | ObjectForUBPathAndArch; |
| 134 | |
| 135 | Options Opts; |
| 136 | }; |
| 137 | |
| 138 | } // end namespace symbolize |
| 139 | } // end namespace llvm |
| 140 | |
| 141 | #endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H |