e6220aa4ce9c86018ec469bdc730a8204c5720df
[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/DebugInfo/DIContext.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include <map>
21 #include <string>
22
23 namespace llvm {
24
25 using namespace object;
26
27 namespace symbolize {
28
29 class ModuleInfo;
30
31 class LLVMSymbolizer {
32 public:
33   struct Options {
34     bool UseSymbolTable : 1;
35     bool PrintFunctions : 1;
36     bool PrintInlining : 1;
37     bool Demangle : 1;
38     Options(bool UseSymbolTable = true, bool PrintFunctions = true,
39             bool PrintInlining = true, bool Demangle = true)
40         : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
41           PrintInlining(PrintInlining), Demangle(Demangle) {
42     }
43   };
44
45   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
46
47   // Returns the result of symbolization for module name/offset as
48   // a string (possibly containing newlines).
49   std::string
50   symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
51   std::string
52   symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
53 private:
54   ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
55   std::string printDILineInfo(DILineInfo LineInfo) const;
56   void DemangleName(std::string &Name) const;
57
58   typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
59   ModuleMapTy Modules;
60   Options Opts;
61   static const char kBadString[];
62 };
63
64 class ModuleInfo {
65 public:
66   ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
67
68   DILineInfo symbolizeCode(uint64_t ModuleOffset,
69                            const LLVMSymbolizer::Options &Opts) const;
70   DIInliningInfo symbolizeInlinedCode(
71       uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
72   bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
73                      uint64_t &Size) const;
74
75 private:
76   bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
77                               std::string &Name, uint64_t &Addr,
78                               uint64_t &Size) const;
79   OwningPtr<ObjectFile> Module;
80   OwningPtr<DIContext> DebugInfoContext;
81
82   struct SymbolDesc {
83     uint64_t Addr;
84     uint64_t AddrEnd;
85     friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
86       return s1.AddrEnd <= s2.Addr;
87     }
88   };
89   typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
90   SymbolMapTy Functions;
91   SymbolMapTy Objects;
92 };
93
94 } // namespace symbolize
95 } // namespace llvm
96
97 #endif // LLVM_SYMBOLIZE_H