blob: 53fd81837866a0363b2af3b13da6c821c2639e21 [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>
22#include <string>
23
24namespace llvm {
25
26using namespace object;
27
28namespace symbolize {
29
30class ModuleInfo;
31
32class LLVMSymbolizer {
33public:
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000034 struct Options {
Alexey Samsonovea83baf2013-01-22 14:21:19 +000035 bool UseSymbolTable : 1;
36 bool PrintFunctions : 1;
37 bool PrintInlining : 1;
38 bool Demangle : 1;
Alexey Samsonov2ca65362013-06-28 08:15:40 +000039 std::string DefaultArch;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000040 Options(bool UseSymbolTable = true, bool PrintFunctions = true,
Alexey Samsonov2ca65362013-06-28 08:15:40 +000041 bool PrintInlining = true, bool Demangle = true,
42 std::string DefaultArch = "")
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000043 : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
Alexey Samsonov2ca65362013-06-28 08:15:40 +000044 PrintInlining(PrintInlining), Demangle(Demangle),
45 DefaultArch(DefaultArch) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000046 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +000047 };
48
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000049 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +000050 ~LLVMSymbolizer() {
51 flush();
52 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +000053
54 // Returns the result of symbolization for module name/offset as
55 // a string (possibly containing newlines).
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000056 std::string
57 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
58 std::string
59 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
Dmitry Vyukove8504e22013-03-19 10:24:42 +000060 void flush();
Alexey Samsonov601beb72013-06-28 12:06:25 +000061 static std::string DemangleName(const std::string &Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +000062private:
Alexey Samsonov2ca65362013-06-28 08:15:40 +000063 typedef std::pair<Binary*, Binary*> BinaryPair;
64
Alexey Samsonovea83baf2013-01-22 14:21:19 +000065 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
Alexey Samsonov2ca65362013-06-28 08:15:40 +000066 /// \brief Returns pair of pointers to binary and debug binary.
67 BinaryPair getOrCreateBinary(const std::string &Path);
68 /// \brief Returns a parsed object file for a given architecture in a
69 /// universal binary (or the binary itself if it is an object file).
70 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
71
Alexey Samsonovea83baf2013-01-22 14:21:19 +000072 std::string printDILineInfo(DILineInfo LineInfo) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000073
Alexey Samsonov2ca65362013-06-28 08:15:40 +000074 // Owns all the parsed binaries and object files.
75 SmallVector<Binary*, 4> ParsedBinariesAndObjects;
76 // Owns module info objects.
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000077 typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000078 ModuleMapTy Modules;
Alexey Samsonov2ca65362013-06-28 08:15:40 +000079 typedef std::map<std::string, BinaryPair> BinaryMapTy;
80 BinaryMapTy BinaryForPath;
81 typedef std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
82 ObjectFileForArchMapTy;
83 ObjectFileForArchMapTy ObjectFileForArch;
84
Alexey Samsonovea83baf2013-01-22 14:21:19 +000085 Options Opts;
Alexey Samsonovd6cef102013-02-04 15:55:26 +000086 static const char kBadString[];
Alexey Samsonovea83baf2013-01-22 14:21:19 +000087};
88
89class ModuleInfo {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000090public:
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000091 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
Alexey Samsonovea83baf2013-01-22 14:21:19 +000092
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000093 DILineInfo symbolizeCode(uint64_t ModuleOffset,
94 const LLVMSymbolizer::Options &Opts) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000095 DIInliningInfo symbolizeInlinedCode(
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000096 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
97 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
98 uint64_t &Size) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000099
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000100private:
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000101 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
102 std::string &Name, uint64_t &Addr,
103 uint64_t &Size) const;
Alexey Samsonova5f07682014-02-26 13:10:01 +0000104 void addSymbol(const symbol_iterator &Sym);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000105 ObjectFile *Module;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000106 std::unique_ptr<DIContext> DebugInfoContext;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000107
108 struct SymbolDesc {
109 uint64_t Addr;
Alexey Samsonov35c987d2013-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 Samsonovd5d7bb52013-02-15 08:54:47 +0000113 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
Alexey Samsonov5239d582013-06-04 07:57:38 +0000114 return s1.Addr < s2.Addr;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000115 }
116 };
117 typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
118 SymbolMapTy Functions;
119 SymbolMapTy Objects;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000120};
121
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000122} // namespace symbolize
123} // namespace llvm
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000124
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000125#endif // LLVM_SYMBOLIZE_H