llvm-symbolizer: make name demangling a public static method of LLVMSymbolizer
[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_SYMBOLIZE_H
14 #define LLVM_SYMBOLIZE_H
15
16 #include "llvm/ADT/OwningPtr.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/DebugInfo/DIContext.h"
19 #include "llvm/Object/MachOUniversal.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include <map>
23 #include <string>
24
25 namespace llvm {
26
27 using namespace object;
28
29 namespace symbolize {
30
31 class ModuleInfo;
32
33 class LLVMSymbolizer {
34 public:
35   struct Options {
36     bool UseSymbolTable : 1;
37     bool PrintFunctions : 1;
38     bool PrintInlining : 1;
39     bool Demangle : 1;
40     std::string DefaultArch;
41     Options(bool UseSymbolTable = true, bool PrintFunctions = true,
42             bool PrintInlining = true, bool Demangle = true,
43             std::string DefaultArch = "")
44         : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
45           PrintInlining(PrintInlining), Demangle(Demangle),
46           DefaultArch(DefaultArch) {
47     }
48   };
49
50   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
51
52   // Returns the result of symbolization for module name/offset as
53   // a string (possibly containing newlines).
54   std::string
55   symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
56   std::string
57   symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
58   void flush();
59   static std::string DemangleName(const std::string &Name);
60 private:
61   typedef std::pair<Binary*, Binary*> BinaryPair;
62
63   ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
64   /// \brief Returns pair of pointers to binary and debug binary.
65   BinaryPair getOrCreateBinary(const std::string &Path);
66   /// \brief Returns a parsed object file for a given architecture in a
67   /// universal binary (or the binary itself if it is an object file).
68   ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
69
70   std::string printDILineInfo(DILineInfo LineInfo) const;
71
72   // Owns all the parsed binaries and object files.
73   SmallVector<Binary*, 4> ParsedBinariesAndObjects;
74   // Owns module info objects.
75   typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
76   ModuleMapTy Modules;
77   typedef std::map<std::string, BinaryPair> BinaryMapTy;
78   BinaryMapTy BinaryForPath;
79   typedef std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
80       ObjectFileForArchMapTy;
81   ObjectFileForArchMapTy ObjectFileForArch;
82
83   Options Opts;
84   static const char kBadString[];
85 };
86
87 class ModuleInfo {
88 public:
89   ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
90
91   DILineInfo symbolizeCode(uint64_t ModuleOffset,
92                            const LLVMSymbolizer::Options &Opts) const;
93   DIInliningInfo symbolizeInlinedCode(
94       uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
95   bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
96                      uint64_t &Size) const;
97
98 private:
99   bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
100                               std::string &Name, uint64_t &Addr,
101                               uint64_t &Size) const;
102   ObjectFile *Module;
103   OwningPtr<DIContext> DebugInfoContext;
104
105   struct SymbolDesc {
106     uint64_t Addr;
107     // If size is 0, assume that symbol occupies the whole memory range up to
108     // the following symbol.
109     uint64_t Size;
110     friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
111       return s1.Addr < s2.Addr;
112     }
113   };
114   typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
115   SymbolMapTy Functions;
116   SymbolMapTy Objects;
117 };
118
119 } // namespace symbolize
120 } // namespace llvm
121
122 #endif // LLVM_SYMBOLIZE_H