1 //===- Symbolize.h ----------------------------------------------*- C++ -*-===// 2 // 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 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 { 38 FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName; 39 bool UseSymbolTable = true; 40 bool Demangle = true; 41 bool RelativeAddresses = false; 42 std::string DefaultArch; 43 std::vector<std::string> DsymHints; 44 std::string FallbackDebugPath; 45 std::string DWPName; 46 }; 47 48 LLVMSymbolizer() = default; LLVMSymbolizer(const Options & Opts)49 LLVMSymbolizer(const Options &Opts) : Opts(Opts) {} 50 ~LLVMSymbolizer()51 ~LLVMSymbolizer() { 52 flush(); 53 } 54 55 Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj, 56 object::SectionedAddress ModuleOffset); 57 Expected<DILineInfo> symbolizeCode(const std::string &ModuleName, 58 object::SectionedAddress ModuleOffset); 59 Expected<DIInliningInfo> 60 symbolizeInlinedCode(const std::string &ModuleName, 61 object::SectionedAddress ModuleOffset); 62 Expected<DIGlobal> symbolizeData(const std::string &ModuleName, 63 object::SectionedAddress ModuleOffset); 64 Expected<std::vector<DILocal>> 65 symbolizeFrame(const std::string &ModuleName, 66 object::SectionedAddress ModuleOffset); 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. 76 using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>; 77 78 Expected<DILineInfo> 79 symbolizeCodeCommon(SymbolizableModule *Info, 80 object::SectionedAddress ModuleOffset); 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 *> 87 getOrCreateModuleInfo(const std::string &ModuleName); 88 89 Expected<SymbolizableModule *> 90 createModuleInfo(const ObjectFile *Obj, 91 std::unique_ptr<DIContext> Context, 92 StringRef ModuleName); 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 101 /// Returns pair of pointers to object and debug object. 102 Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path, 103 const std::string &ArchName); 104 105 /// Return a pointer to object file at specified path, for a specified 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 113 /// Contains cached results of getOrCreateObjectPair(). 114 std::map<std::pair<std::string, std::string>, ObjectPair> 115 ObjectPairForPathArch; 116 117 /// Contains parsed binary for each path, or parsing error. 118 std::map<std::string, OwningBinary<Binary>> BinaryForPath; 119 120 /// Parsed object file for path/architecture pair, where "path" refers 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 132