blob: c7f87b1429c0b933191dba22c702b0022555c413 [file] [log] [blame]
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +00001//===-- 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"
Alexey Samsonov8175bc32013-06-28 08:15:40 +000017#include "llvm/ADT/SmallVector.h"
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000018#include "llvm/DebugInfo/DIContext.h"
Alexey Samsonov8175bc32013-06-28 08:15:40 +000019#include "llvm/Object/MachOUniversal.h"
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000020#include "llvm/Object/ObjectFile.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include <map>
23#include <string>
24
25namespace llvm {
26
27using namespace object;
28
29namespace symbolize {
30
31class ModuleInfo;
32
33class LLVMSymbolizer {
34public:
Alexey Samsonovc4439c32013-02-15 08:54:47 +000035 struct Options {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000036 bool UseSymbolTable : 1;
37 bool PrintFunctions : 1;
38 bool PrintInlining : 1;
39 bool Demangle : 1;
Alexey Samsonov8175bc32013-06-28 08:15:40 +000040 std::string DefaultArch;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000041 Options(bool UseSymbolTable = true, bool PrintFunctions = true,
Alexey Samsonov8175bc32013-06-28 08:15:40 +000042 bool PrintInlining = true, bool Demangle = true,
43 std::string DefaultArch = "")
Alexey Samsonovc4439c32013-02-15 08:54:47 +000044 : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
Alexey Samsonov8175bc32013-06-28 08:15:40 +000045 PrintInlining(PrintInlining), Demangle(Demangle),
46 DefaultArch(DefaultArch) {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000047 }
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000048 };
49
Alexey Samsonovc4439c32013-02-15 08:54:47 +000050 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000051
52 // Returns the result of symbolization for module name/offset as
53 // a string (possibly containing newlines).
Alexey Samsonovc4439c32013-02-15 08:54:47 +000054 std::string
55 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
56 std::string
57 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
Dmitry Vyukove9e10d12013-03-19 10:24:42 +000058 void flush();
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000059private:
Alexey Samsonov8175bc32013-06-28 08:15:40 +000060 typedef std::pair<Binary*, Binary*> BinaryPair;
61
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000062 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
Alexey Samsonov8175bc32013-06-28 08:15:40 +000063 /// \brief Returns pair of pointers to binary and debug binary.
64 BinaryPair getOrCreateBinary(const std::string &Path);
65 /// \brief Returns a parsed object file for a given architecture in a
66 /// universal binary (or the binary itself if it is an object file).
67 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
68
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000069 std::string printDILineInfo(DILineInfo LineInfo) const;
70 void DemangleName(std::string &Name) const;
71
Alexey Samsonov8175bc32013-06-28 08:15:40 +000072 // Owns all the parsed binaries and object files.
73 SmallVector<Binary*, 4> ParsedBinariesAndObjects;
74 // Owns module info objects.
Alexey Samsonovc4439c32013-02-15 08:54:47 +000075 typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000076 ModuleMapTy Modules;
Alexey Samsonov8175bc32013-06-28 08:15:40 +000077 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
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000083 Options Opts;
Alexey Samsonov638c63c2013-02-04 15:55:26 +000084 static const char kBadString[];
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000085};
86
87class ModuleInfo {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000088public:
Dmitry Vyukovb1819192013-02-14 13:06:18 +000089 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000090
Alexey Samsonovc4439c32013-02-15 08:54:47 +000091 DILineInfo symbolizeCode(uint64_t ModuleOffset,
92 const LLVMSymbolizer::Options &Opts) const;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000093 DIInliningInfo symbolizeInlinedCode(
Alexey Samsonovc4439c32013-02-15 08:54:47 +000094 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;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000097
Alexey Samsonovc4439c32013-02-15 08:54:47 +000098private:
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000099 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
100 std::string &Name, uint64_t &Addr,
101 uint64_t &Size) const;
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000102 ObjectFile *Module;
Dmitry Vyukovb1819192013-02-14 13:06:18 +0000103 OwningPtr<DIContext> DebugInfoContext;
104
105 struct SymbolDesc {
106 uint64_t Addr;
Alexey Samsonovb6564642013-06-07 15:25:27 +0000107 // If size is 0, assume that symbol occupies the whole memory range up to
108 // the following symbol.
109 uint64_t Size;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000110 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
Alexey Samsonov888ca962013-06-04 07:57:38 +0000111 return s1.Addr < s2.Addr;
Dmitry Vyukovb1819192013-02-14 13:06:18 +0000112 }
113 };
114 typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
115 SymbolMapTy Functions;
116 SymbolMapTy Objects;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000117};
118
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000119} // namespace symbolize
120} // namespace llvm
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000121
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000122#endif // LLVM_SYMBOLIZE_H