blob: 52f1fc99171569e81689c1225d2f370cea19d618 [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;
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000042 std::vector<std::string> DsymHints;
Alexey Samsonovcd014722014-05-17 00:07:48 +000043 Options(bool UseSymbolTable = true,
44 FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
Alexey Samsonov2ca65362013-06-28 08:15:40 +000045 bool PrintInlining = true, bool Demangle = true,
46 std::string DefaultArch = "")
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000047 : UseSymbolTable(UseSymbolTable),
48 PrintFunctions(PrintFunctions), PrintInlining(PrintInlining),
49 Demangle(Demangle), DefaultArch(DefaultArch) {}
Alexey Samsonovea83baf2013-01-22 14:21:19 +000050 };
51
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000052 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +000053 ~LLVMSymbolizer() {
54 flush();
55 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +000056
57 // Returns the result of symbolization for module name/offset as
58 // a string (possibly containing newlines).
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000059 std::string
60 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
61 std::string
62 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
Dmitry Vyukove8504e22013-03-19 10:24:42 +000063 void flush();
Alexey Samsonov601beb72013-06-28 12:06:25 +000064 static std::string DemangleName(const std::string &Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +000065private:
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000066 typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
Alexey Samsonov2ca65362013-06-28 08:15:40 +000067
Alexey Samsonovea83baf2013-01-22 14:21:19 +000068 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000069 ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj,
70 const std::string &ArchName);
71
72 /// \brief Returns pair of pointers to object and debug object.
73 ObjectPair getOrCreateObjects(const std::string &Path,
74 const std::string &ArchName);
Alexey Samsonov2ca65362013-06-28 08:15:40 +000075 /// \brief Returns a parsed object file for a given architecture in a
76 /// universal binary (or the binary itself if it is an object file).
77 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
78
Alexey Samsonovea83baf2013-01-22 14:21:19 +000079 std::string printDILineInfo(DILineInfo LineInfo) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000080
Alexey Samsonov2ca65362013-06-28 08:15:40 +000081 // Owns all the parsed binaries and object files.
David Blaikie2f2021a2014-04-22 05:26:14 +000082 SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
Rafael Espindola48af1c22014-08-19 18:44:46 +000083 SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
84 void addOwningBinary(OwningBinary<Binary> Bin) {
85 ParsedBinariesAndObjects.push_back(std::move(Bin.getBinary()));
86 MemoryBuffers.push_back(std::move(Bin.getBuffer()));
87 }
88
Alexey Samsonov2ca65362013-06-28 08:15:40 +000089 // Owns module info objects.
Alexander Potapenko45bfe372014-10-14 13:40:44 +000090 std::map<std::string, ModuleInfo *> Modules;
Alexander Potapenko45bfe372014-10-14 13:40:44 +000091 std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
92 ObjectFileForArch;
Alexander Potapenko7aaf5142014-10-17 00:50:19 +000093 std::map<std::pair<std::string, std::string>, ObjectPair>
94 ObjectPairForPathArch;
Alexey Samsonov2ca65362013-06-28 08:15:40 +000095
Alexey Samsonovea83baf2013-01-22 14:21:19 +000096 Options Opts;
Alexey Samsonovd6cef102013-02-04 15:55:26 +000097 static const char kBadString[];
Alexey Samsonovea83baf2013-01-22 14:21:19 +000098};
99
100class ModuleInfo {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000101public:
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000102 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000103
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000104 DILineInfo symbolizeCode(uint64_t ModuleOffset,
105 const LLVMSymbolizer::Options &Opts) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000106 DIInliningInfo symbolizeInlinedCode(
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000107 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
108 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
109 uint64_t &Size) const;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000110
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000111private:
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000112 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
113 std::string &Name, uint64_t &Addr,
114 uint64_t &Size) const;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000115 void addSymbol(const SymbolRef &Symbol);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000116 ObjectFile *Module;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000117 std::unique_ptr<DIContext> DebugInfoContext;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000118
119 struct SymbolDesc {
120 uint64_t Addr;
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000121 // If size is 0, assume that symbol occupies the whole memory range up to
122 // the following symbol.
123 uint64_t Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000124 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
Alexey Samsonov5239d582013-06-04 07:57:38 +0000125 return s1.Addr < s2.Addr;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000126 }
127 };
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000128 std::map<SymbolDesc, StringRef> Functions;
129 std::map<SymbolDesc, StringRef> Objects;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000130};
131
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000132} // namespace symbolize
133} // namespace llvm
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000134
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000135#endif