blob: 03c765cc9c309a8d6aa282b0f03b4c070bddd1dc [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 Samsonov0ed872c2013-06-28 15:08:29 +000051 ~LLVMSymbolizer() {
52 flush();
53 }
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000054
55 // Returns the result of symbolization for module name/offset as
56 // a string (possibly containing newlines).
Alexey Samsonovc4439c32013-02-15 08:54:47 +000057 std::string
58 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
59 std::string
60 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
Dmitry Vyukove9e10d12013-03-19 10:24:42 +000061 void flush();
Alexey Samsonovc071f892013-06-28 12:06:25 +000062 static std::string DemangleName(const std::string &Name);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000063private:
Alexey Samsonov8175bc32013-06-28 08:15:40 +000064 typedef std::pair<Binary*, Binary*> BinaryPair;
65
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000066 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
Alexey Samsonov8175bc32013-06-28 08:15:40 +000067 /// \brief Returns pair of pointers to binary and debug binary.
68 BinaryPair getOrCreateBinary(const std::string &Path);
69 /// \brief Returns a parsed object file for a given architecture in a
70 /// universal binary (or the binary itself if it is an object file).
71 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
72
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000073 std::string printDILineInfo(DILineInfo LineInfo) const;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000074
Alexey Samsonov8175bc32013-06-28 08:15:40 +000075 // Owns all the parsed binaries and object files.
76 SmallVector<Binary*, 4> ParsedBinariesAndObjects;
77 // Owns module info objects.
Alexey Samsonovc4439c32013-02-15 08:54:47 +000078 typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000079 ModuleMapTy Modules;
Alexey Samsonov8175bc32013-06-28 08:15:40 +000080 typedef std::map<std::string, BinaryPair> BinaryMapTy;
81 BinaryMapTy BinaryForPath;
82 typedef std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
83 ObjectFileForArchMapTy;
84 ObjectFileForArchMapTy ObjectFileForArch;
85
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000086 Options Opts;
Alexey Samsonov638c63c2013-02-04 15:55:26 +000087 static const char kBadString[];
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000088};
89
90class ModuleInfo {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000091public:
Dmitry Vyukovb1819192013-02-14 13:06:18 +000092 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000093
Alexey Samsonovc4439c32013-02-15 08:54:47 +000094 DILineInfo symbolizeCode(uint64_t ModuleOffset,
95 const LLVMSymbolizer::Options &Opts) const;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000096 DIInliningInfo symbolizeInlinedCode(
Alexey Samsonovc4439c32013-02-15 08:54:47 +000097 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
98 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
99 uint64_t &Size) const;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000100
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000101private:
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000102 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
103 std::string &Name, uint64_t &Addr,
104 uint64_t &Size) const;
Alexey Samsonov8175bc32013-06-28 08:15:40 +0000105 ObjectFile *Module;
Dmitry Vyukovb1819192013-02-14 13:06:18 +0000106 OwningPtr<DIContext> DebugInfoContext;
107
108 struct SymbolDesc {
109 uint64_t Addr;
Alexey Samsonovb6564642013-06-07 15:25:27 +0000110 // If size is 0, assume that symbol occupies the whole memory range up to
111 // the following symbol.
112 uint64_t Size;
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000113 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
Alexey Samsonov888ca962013-06-04 07:57:38 +0000114 return s1.Addr < s2.Addr;
Dmitry Vyukovb1819192013-02-14 13:06:18 +0000115 }
116 };
117 typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
118 SymbolMapTy Functions;
119 SymbolMapTy Objects;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000120};
121
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000122} // namespace symbolize
123} // namespace llvm
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +0000124
Alexey Samsonovc4439c32013-02-15 08:54:47 +0000125#endif // LLVM_SYMBOLIZE_H