blob: a1283a511af635e47dff505f22d50fd6070be19f [file] [log] [blame]
Alexey Samsonovea83baf2013-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
Alexey Samsonov2ca65362013-06-28 08:15:40 +000016#include "llvm/ADT/SmallVector.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000017#include "llvm/DebugInfo/DIContext.h"
Alexey Samsonov2ca65362013-06-28 08:15:40 +000018#include "llvm/Object/MachOUniversal.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000019#include "llvm/Object/ObjectFile.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include <map>
David Blaikie2f2021a2014-04-22 05:26:14 +000022#include <memory>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000023#include <string>
24
25namespace llvm {
26
27using namespace object;
28
29namespace symbolize {
30
31class ModuleInfo;
32
33class LLVMSymbolizer {
34public:
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000035 struct Options {
Alexey Samsonovea83baf2013-01-22 14:21:19 +000036 bool UseSymbolTable : 1;
37 bool PrintFunctions : 1;
38 bool PrintInlining : 1;
39 bool Demangle : 1;
Alexey Samsonov2ca65362013-06-28 08:15:40 +000040 std::string DefaultArch;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000041 Options(bool UseSymbolTable = true, bool PrintFunctions = true,
Alexey Samsonov2ca65362013-06-28 08:15:40 +000042 bool PrintInlining = true, bool Demangle = true,
43 std::string DefaultArch = "")
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000044 : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
Alexey Samsonov2ca65362013-06-28 08:15:40 +000045 PrintInlining(PrintInlining), Demangle(Demangle),
46 DefaultArch(DefaultArch) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000047 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +000048 };
49
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000050 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +000051 ~LLVMSymbolizer() {
52 flush();
53 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +000054
55 // Returns the result of symbolization for module name/offset as
56 // a string (possibly containing newlines).
Alexey Samsonovd5d7bb52013-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 Vyukove8504e22013-03-19 10:24:42 +000061 void flush();
Alexey Samsonov601beb72013-06-28 12:06:25 +000062 static std::string DemangleName(const std::string &Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +000063private:
Alexey Samsonov2ca65362013-06-28 08:15:40 +000064 typedef std::pair<Binary*, Binary*> BinaryPair;
65
Alexey Samsonovea83baf2013-01-22 14:21:19 +000066 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
Alexey Samsonov2ca65362013-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 Samsonovea83baf2013-01-22 14:21:19 +000073 std::string printDILineInfo(DILineInfo LineInfo) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000074
Alexey Samsonov2ca65362013-06-28 08:15:40 +000075 // Owns all the parsed binaries and object files.
David Blaikie2f2021a2014-04-22 05:26:14 +000076 SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
Alexey Samsonov2ca65362013-06-28 08:15:40 +000077 // Owns module info objects.
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000078 typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000079 ModuleMapTy Modules;
Alexey Samsonov2ca65362013-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 Samsonovea83baf2013-01-22 14:21:19 +000086 Options Opts;
Alexey Samsonovd6cef102013-02-04 15:55:26 +000087 static const char kBadString[];
Alexey Samsonovea83baf2013-01-22 14:21:19 +000088};
89
90class ModuleInfo {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000091public:
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000092 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
Alexey Samsonovea83baf2013-01-22 14:21:19 +000093
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000094 DILineInfo symbolizeCode(uint64_t ModuleOffset,
95 const LLVMSymbolizer::Options &Opts) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000096 DIInliningInfo symbolizeInlinedCode(
Alexey Samsonovd5d7bb52013-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 Samsonovea83baf2013-01-22 14:21:19 +0000100
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000101private:
Alexey Samsonovea83baf2013-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 Samsonov464d2e42014-03-17 07:28:19 +0000105 void addSymbol(const SymbolRef &Symbol);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000106 ObjectFile *Module;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000107 std::unique_ptr<DIContext> DebugInfoContext;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000108
109 struct SymbolDesc {
110 uint64_t Addr;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000111 // If size is 0, assume that symbol occupies the whole memory range up to
112 // the following symbol.
113 uint64_t Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000114 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
Alexey Samsonov5239d582013-06-04 07:57:38 +0000115 return s1.Addr < s2.Addr;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000116 }
117 };
118 typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
119 SymbolMapTy Functions;
120 SymbolMapTy Objects;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000121};
122
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000123} // namespace symbolize
124} // namespace llvm
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000125
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000126#endif // LLVM_SYMBOLIZE_H