[llvm-symbolizer] Remove underscores and other C mangling on Windows
[oota-llvm.git] / tools / llvm-symbolizer / LLVMSymbolize.h
1 //===-- LLVMSymbolize.h ----------------------------------------- C++ -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Header for LLVM symbolization library.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H
14 #define LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H
15
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/DebugInfo/DIContext.h"
18 #include "llvm/Object/MachOUniversal.h"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/Support/DataExtractor.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include <map>
23 #include <memory>
24 #include <string>
25
26 namespace llvm {
27
28 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
29 using namespace object;
30
31 namespace symbolize {
32
33 class ModuleInfo;
34
35 class LLVMSymbolizer {
36 public:
37   struct Options {
38     FunctionNameKind PrintFunctions;
39     bool UseSymbolTable : 1;
40     bool PrintInlining : 1;
41     bool Demangle : 1;
42     bool RelativeAddresses : 1;
43     std::string DefaultArch;
44     std::vector<std::string> DsymHints;
45     Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
46             bool UseSymbolTable = true, bool PrintInlining = true,
47             bool Demangle = true, bool RelativeAddresses = false,
48             std::string DefaultArch = "")
49         : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
50           PrintInlining(PrintInlining), Demangle(Demangle),
51           RelativeAddresses(RelativeAddresses), DefaultArch(DefaultArch) {}
52   };
53
54   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
55   ~LLVMSymbolizer() {
56     flush();
57   }
58
59   // Returns the result of symbolization for module name/offset as
60   // a string (possibly containing newlines).
61   std::string
62   symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
63   std::string
64   symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
65   void flush();
66   static std::string DemangleName(const std::string &Name, ModuleInfo *ModInfo);
67
68 private:
69   typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
70
71   ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
72   ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj,
73                              const std::string &ArchName);
74
75   /// \brief Returns pair of pointers to object and debug object.
76   ObjectPair getOrCreateObjects(const std::string &Path,
77                                 const std::string &ArchName);
78   /// \brief Returns a parsed object file for a given architecture in a
79   /// universal binary (or the binary itself if it is an object file).
80   ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
81
82   std::string printDILineInfo(DILineInfo LineInfo, ModuleInfo *ModInfo) const;
83
84   // Owns all the parsed binaries and object files.
85   SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
86   SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
87   void addOwningBinary(OwningBinary<Binary> OwningBin) {
88     std::unique_ptr<Binary> Bin;
89     std::unique_ptr<MemoryBuffer> MemBuf;
90     std::tie(Bin, MemBuf) = OwningBin.takeBinary();
91     ParsedBinariesAndObjects.push_back(std::move(Bin));
92     MemoryBuffers.push_back(std::move(MemBuf));
93   }
94
95   // Owns module info objects.
96   std::map<std::string, ModuleInfo *> Modules;
97   std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
98       ObjectFileForArch;
99   std::map<std::pair<std::string, std::string>, ObjectPair>
100       ObjectPairForPathArch;
101
102   Options Opts;
103   static const char kBadString[];
104 };
105
106 class ModuleInfo {
107 public:
108   ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
109
110   DILineInfo symbolizeCode(uint64_t ModuleOffset,
111                            const LLVMSymbolizer::Options &Opts) const;
112   DIInliningInfo symbolizeInlinedCode(
113       uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
114   bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
115                      uint64_t &Size) const;
116
117   // Return true if this is a 32-bit x86 PE COFF module.
118   bool isWin32Module() const;
119
120 private:
121   bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
122                               std::string &Name, uint64_t &Addr,
123                               uint64_t &Size) const;
124   // For big-endian PowerPC64 ELF, OpdAddress is the address of the .opd
125   // (function descriptor) section and OpdExtractor refers to its contents.
126   void addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
127                  DataExtractor *OpdExtractor = nullptr,
128                  uint64_t OpdAddress = 0);
129   ObjectFile *Module;
130   std::unique_ptr<DIContext> DebugInfoContext;
131
132   struct SymbolDesc {
133     uint64_t Addr;
134     // If size is 0, assume that symbol occupies the whole memory range up to
135     // the following symbol.
136     uint64_t Size;
137     friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
138       return s1.Addr < s2.Addr;
139     }
140   };
141   std::map<SymbolDesc, StringRef> Functions;
142   std::map<SymbolDesc, StringRef> Objects;
143 };
144
145 } // namespace symbolize
146 } // namespace llvm
147
148 #endif