blob: a56f1329fd61933364a569d37d36ebacdbf2d3d5 [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//===----------------------------------------------------------------------===//
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000013#ifndef LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H
14#define LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H
Alexey Samsonovea83baf2013-01-22 14:21:19 +000015
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
Alexey Samsonovcd014722014-05-17 00:07:48 +000027typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000028using namespace object;
29
30namespace symbolize {
31
32class ModuleInfo;
33
34class LLVMSymbolizer {
35public:
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000036 struct Options {
Alexey Samsonovea83baf2013-01-22 14:21:19 +000037 bool UseSymbolTable : 1;
Alexey Samsonovcd014722014-05-17 00:07:48 +000038 FunctionNameKind PrintFunctions;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000039 bool PrintInlining : 1;
40 bool Demangle : 1;
Alexey Samsonov2ca65362013-06-28 08:15:40 +000041 std::string DefaultArch;
Alexey Samsonovcd014722014-05-17 00:07:48 +000042 Options(bool UseSymbolTable = true,
43 FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
Alexey Samsonov2ca65362013-06-28 08:15:40 +000044 bool PrintInlining = true, bool Demangle = true,
45 std::string DefaultArch = "")
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000046 : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
Alexey Samsonov2ca65362013-06-28 08:15:40 +000047 PrintInlining(PrintInlining), Demangle(Demangle),
Alexey Samsonovcd014722014-05-17 00:07:48 +000048 DefaultArch(DefaultArch) {}
Alexey Samsonovea83baf2013-01-22 14:21:19 +000049 };
50
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000051 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +000052 ~LLVMSymbolizer() {
53 flush();
54 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +000055
56 // Returns the result of symbolization for module name/offset as
57 // a string (possibly containing newlines).
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000058 std::string
59 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
60 std::string
61 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
Dmitry Vyukove8504e22013-03-19 10:24:42 +000062 void flush();
Alexey Samsonov601beb72013-06-28 12:06:25 +000063 static std::string DemangleName(const std::string &Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +000064private:
Alexey Samsonov2ca65362013-06-28 08:15:40 +000065 typedef std::pair<Binary*, Binary*> BinaryPair;
66
Alexey Samsonovea83baf2013-01-22 14:21:19 +000067 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
Alexey Samsonov2ca65362013-06-28 08:15:40 +000068 /// \brief Returns pair of pointers to binary and debug binary.
69 BinaryPair getOrCreateBinary(const std::string &Path);
70 /// \brief Returns a parsed object file for a given architecture in a
71 /// universal binary (or the binary itself if it is an object file).
72 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
73
Alexey Samsonovea83baf2013-01-22 14:21:19 +000074 std::string printDILineInfo(DILineInfo LineInfo) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000075
Alexey Samsonov2ca65362013-06-28 08:15:40 +000076 // Owns all the parsed binaries and object files.
David Blaikie2f2021a2014-04-22 05:26:14 +000077 SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
Rafael Espindola48af1c22014-08-19 18:44:46 +000078 SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
79 void addOwningBinary(OwningBinary<Binary> Bin) {
80 ParsedBinariesAndObjects.push_back(std::move(Bin.getBinary()));
81 MemoryBuffers.push_back(std::move(Bin.getBuffer()));
82 }
83
Alexey Samsonov2ca65362013-06-28 08:15:40 +000084 // Owns module info objects.
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000085 typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000086 ModuleMapTy Modules;
Alexey Samsonov2ca65362013-06-28 08:15:40 +000087 typedef std::map<std::string, BinaryPair> BinaryMapTy;
88 BinaryMapTy BinaryForPath;
89 typedef std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
90 ObjectFileForArchMapTy;
91 ObjectFileForArchMapTy ObjectFileForArch;
92
Alexey Samsonovea83baf2013-01-22 14:21:19 +000093 Options Opts;
Alexey Samsonovd6cef102013-02-04 15:55:26 +000094 static const char kBadString[];
Alexey Samsonovea83baf2013-01-22 14:21:19 +000095};
96
97class ModuleInfo {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000098public:
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000099 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000100
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000101 DILineInfo symbolizeCode(uint64_t ModuleOffset,
102 const LLVMSymbolizer::Options &Opts) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000103 DIInliningInfo symbolizeInlinedCode(
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000104 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
105 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
106 uint64_t &Size) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000107
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000108private:
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000109 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
110 std::string &Name, uint64_t &Addr,
111 uint64_t &Size) const;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000112 void addSymbol(const SymbolRef &Symbol);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000113 ObjectFile *Module;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000114 std::unique_ptr<DIContext> DebugInfoContext;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000115
116 struct SymbolDesc {
117 uint64_t Addr;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000118 // If size is 0, assume that symbol occupies the whole memory range up to
119 // the following symbol.
120 uint64_t Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000121 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
Alexey Samsonov5239d582013-06-04 07:57:38 +0000122 return s1.Addr < s2.Addr;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000123 }
124 };
125 typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
126 SymbolMapTy Functions;
127 SymbolMapTy Objects;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000128};
129
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000130} // namespace symbolize
131} // namespace llvm
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000132
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000133#endif