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" |
| 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 { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 38 | FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName; |
| 39 | bool UseSymbolTable = true; |
| 40 | bool Demangle = true; |
| 41 | bool RelativeAddresses = false; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | std::string DefaultArch; |
| 43 | std::vector<std::string> DsymHints; |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 44 | std::string FallbackDebugPath; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 45 | std::string DWPName; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 48 | LLVMSymbolizer() = default; |
| 49 | LLVMSymbolizer(const Options &Opts) : Opts(Opts) {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | |
| 51 | ~LLVMSymbolizer() { |
| 52 | flush(); |
| 53 | } |
| 54 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 55 | Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj, |
| 56 | object::SectionedAddress ModuleOffset); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 57 | Expected<DILineInfo> symbolizeCode(const std::string &ModuleName, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 58 | object::SectionedAddress ModuleOffset); |
| 59 | Expected<DIInliningInfo> |
| 60 | symbolizeInlinedCode(const std::string &ModuleName, |
| 61 | object::SectionedAddress ModuleOffset); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 62 | Expected<DIGlobal> symbolizeData(const std::string &ModuleName, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 63 | object::SectionedAddress ModuleOffset); |
| 64 | Expected<std::vector<DILocal>> |
| 65 | symbolizeFrame(const std::string &ModuleName, |
| 66 | object::SectionedAddress ModuleOffset); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 67 | void flush(); |
| 68 | |
| 69 | static std::string |
| 70 | DemangleName(const std::string &Name, |
| 71 | const SymbolizableModule *DbiModuleDescriptor); |
| 72 | |
| 73 | private: |
| 74 | // Bundles together object file with code/data and object file with |
| 75 | // corresponding debug info. These objects can be the same. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 76 | using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>; |
| 77 | |
| 78 | Expected<DILineInfo> |
| 79 | symbolizeCodeCommon(SymbolizableModule *Info, |
| 80 | object::SectionedAddress ModuleOffset); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 81 | |
| 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 Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 87 | getOrCreateModuleInfo(const std::string &ModuleName); |
| 88 | |
| 89 | Expected<SymbolizableModule *> |
| 90 | createModuleInfo(const ObjectFile *Obj, |
| 91 | std::unique_ptr<DIContext> Context, |
| 92 | StringRef ModuleName); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 93 | |
| 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 101 | /// Returns pair of pointers to object and debug object. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 102 | Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path, |
| 103 | const std::string &ArchName); |
| 104 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 105 | /// Return a pointer to object file at specified path, for a specified |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 106 | /// 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 113 | /// Contains cached results of getOrCreateObjectPair(). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 114 | std::map<std::pair<std::string, std::string>, ObjectPair> |
| 115 | ObjectPairForPathArch; |
| 116 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 117 | /// Contains parsed binary for each path, or parsing error. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 118 | std::map<std::string, OwningBinary<Binary>> BinaryForPath; |
| 119 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 120 | /// Parsed object file for path/architecture pair, where "path" refers |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 121 | /// 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 |