1249259Sdim//===-- LLVMSymbolize.h ----------------------------------------- C++ -----===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim// Header for LLVM symbolization library.
11249259Sdim//
12249259Sdim//===----------------------------------------------------------------------===//
13249259Sdim#ifndef LLVM_SYMBOLIZE_H
14249259Sdim#define LLVM_SYMBOLIZE_H
15249259Sdim
16249259Sdim#include "llvm/ADT/OwningPtr.h"
17263509Sdim#include "llvm/ADT/SmallVector.h"
18249259Sdim#include "llvm/DebugInfo/DIContext.h"
19263509Sdim#include "llvm/Object/MachOUniversal.h"
20249259Sdim#include "llvm/Object/ObjectFile.h"
21249259Sdim#include "llvm/Support/MemoryBuffer.h"
22249259Sdim#include <map>
23249259Sdim#include <string>
24249259Sdim
25249259Sdimnamespace llvm {
26249259Sdim
27249259Sdimusing namespace object;
28249259Sdim
29249259Sdimnamespace symbolize {
30249259Sdim
31249259Sdimclass ModuleInfo;
32249259Sdim
33249259Sdimclass LLVMSymbolizer {
34249259Sdimpublic:
35249259Sdim  struct Options {
36249259Sdim    bool UseSymbolTable : 1;
37249259Sdim    bool PrintFunctions : 1;
38249259Sdim    bool PrintInlining : 1;
39249259Sdim    bool Demangle : 1;
40263509Sdim    std::string DefaultArch;
41249259Sdim    Options(bool UseSymbolTable = true, bool PrintFunctions = true,
42263509Sdim            bool PrintInlining = true, bool Demangle = true,
43263509Sdim            std::string DefaultArch = "")
44249259Sdim        : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
45263509Sdim          PrintInlining(PrintInlining), Demangle(Demangle),
46263509Sdim          DefaultArch(DefaultArch) {
47249259Sdim    }
48249259Sdim  };
49249259Sdim
50249259Sdim  LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
51263509Sdim  ~LLVMSymbolizer() {
52263509Sdim    flush();
53263509Sdim  }
54249259Sdim
55249259Sdim  // Returns the result of symbolization for module name/offset as
56249259Sdim  // a string (possibly containing newlines).
57249259Sdim  std::string
58249259Sdim  symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
59249259Sdim  std::string
60249259Sdim  symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
61249259Sdim  void flush();
62263509Sdim  static std::string DemangleName(const std::string &Name);
63249259Sdimprivate:
64263509Sdim  typedef std::pair<Binary*, Binary*> BinaryPair;
65263509Sdim
66249259Sdim  ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
67263509Sdim  /// \brief Returns pair of pointers to binary and debug binary.
68263509Sdim  BinaryPair getOrCreateBinary(const std::string &Path);
69263509Sdim  /// \brief Returns a parsed object file for a given architecture in a
70263509Sdim  /// universal binary (or the binary itself if it is an object file).
71263509Sdim  ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
72263509Sdim
73249259Sdim  std::string printDILineInfo(DILineInfo LineInfo) const;
74263509Sdim  static std::string DemangleGlobalName(const std::string &Name);
75249259Sdim
76263509Sdim  // Owns all the parsed binaries and object files.
77263509Sdim  SmallVector<Binary*, 4> ParsedBinariesAndObjects;
78263509Sdim  // Owns module info objects.
79249259Sdim  typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
80249259Sdim  ModuleMapTy Modules;
81263509Sdim  typedef std::map<std::string, BinaryPair> BinaryMapTy;
82263509Sdim  BinaryMapTy BinaryForPath;
83263509Sdim  typedef std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
84263509Sdim      ObjectFileForArchMapTy;
85263509Sdim  ObjectFileForArchMapTy ObjectFileForArch;
86263509Sdim
87249259Sdim  Options Opts;
88249259Sdim  static const char kBadString[];
89249259Sdim};
90249259Sdim
91249259Sdimclass ModuleInfo {
92249259Sdimpublic:
93249259Sdim  ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
94249259Sdim
95249259Sdim  DILineInfo symbolizeCode(uint64_t ModuleOffset,
96249259Sdim                           const LLVMSymbolizer::Options &Opts) const;
97249259Sdim  DIInliningInfo symbolizeInlinedCode(
98249259Sdim      uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
99249259Sdim  bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
100249259Sdim                     uint64_t &Size) const;
101249259Sdim
102249259Sdimprivate:
103249259Sdim  bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
104249259Sdim                              std::string &Name, uint64_t &Addr,
105249259Sdim                              uint64_t &Size) const;
106263509Sdim  ObjectFile *Module;
107249259Sdim  OwningPtr<DIContext> DebugInfoContext;
108249259Sdim
109249259Sdim  struct SymbolDesc {
110249259Sdim    uint64_t Addr;
111263509Sdim    // If size is 0, assume that symbol occupies the whole memory range up to
112263509Sdim    // the following symbol.
113263509Sdim    uint64_t Size;
114249259Sdim    friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
115263509Sdim      return s1.Addr < s2.Addr;
116249259Sdim    }
117249259Sdim  };
118249259Sdim  typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
119249259Sdim  SymbolMapTy Functions;
120249259Sdim  SymbolMapTy Objects;
121249259Sdim};
122249259Sdim
123249259Sdim} // namespace symbolize
124249259Sdim} // namespace llvm
125249259Sdim
126249259Sdim#endif // LLVM_SYMBOLIZE_H
127