blob: 0733dfbbc52e1ef7938e51bb892a7c20980decea [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"
17#include "llvm/DebugInfo/DIContext.h"
18#include "llvm/Object/ObjectFile.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include <map>
21#include <string>
22
23namespace llvm {
24
25using namespace object;
26
27namespace symbolize {
28
29class ModuleInfo;
30
31class LLVMSymbolizer {
32public:
Alexey Samsonovc4439c32013-02-15 08:54:47 +000033 struct Options {
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000034 bool UseSymbolTable : 1;
35 bool PrintFunctions : 1;
36 bool PrintInlining : 1;
37 bool Demangle : 1;
38 Options(bool UseSymbolTable = true, bool PrintFunctions = true,
39 bool PrintInlining = true, bool Demangle = true)
Alexey Samsonovc4439c32013-02-15 08:54:47 +000040 : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
41 PrintInlining(PrintInlining), Demangle(Demangle) {
42 }
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000043 };
44
Alexey Samsonovc4439c32013-02-15 08:54:47 +000045 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000046
47 // Returns the result of symbolization for module name/offset as
48 // a string (possibly containing newlines).
Alexey Samsonovc4439c32013-02-15 08:54:47 +000049 std::string
50 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
51 std::string
52 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
Dmitry Vyukove9e10d12013-03-19 10:24:42 +000053 void flush();
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000054private:
55 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
56 std::string printDILineInfo(DILineInfo LineInfo) const;
57 void DemangleName(std::string &Name) const;
58
Alexey Samsonovc4439c32013-02-15 08:54:47 +000059 typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000060 ModuleMapTy Modules;
61 Options Opts;
Alexey Samsonov638c63c2013-02-04 15:55:26 +000062 static const char kBadString[];
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000063};
64
65class ModuleInfo {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000066public:
Dmitry Vyukovb1819192013-02-14 13:06:18 +000067 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000068
Alexey Samsonovc4439c32013-02-15 08:54:47 +000069 DILineInfo symbolizeCode(uint64_t ModuleOffset,
70 const LLVMSymbolizer::Options &Opts) const;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000071 DIInliningInfo symbolizeInlinedCode(
Alexey Samsonovc4439c32013-02-15 08:54:47 +000072 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
73 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
74 uint64_t &Size) const;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000075
Alexey Samsonovc4439c32013-02-15 08:54:47 +000076private:
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000077 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
78 std::string &Name, uint64_t &Addr,
79 uint64_t &Size) const;
Dmitry Vyukovb1819192013-02-14 13:06:18 +000080 OwningPtr<ObjectFile> Module;
81 OwningPtr<DIContext> DebugInfoContext;
82
83 struct SymbolDesc {
84 uint64_t Addr;
85 uint64_t AddrEnd;
Alexey Samsonovc4439c32013-02-15 08:54:47 +000086 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
Dmitry Vyukovb1819192013-02-14 13:06:18 +000087 return s1.AddrEnd <= s2.Addr;
88 }
89 };
90 typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
91 SymbolMapTy Functions;
92 SymbolMapTy Objects;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000093};
94
Alexey Samsonovc4439c32013-02-15 08:54:47 +000095} // namespace symbolize
96} // namespace llvm
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000097
Alexey Samsonovc4439c32013-02-15 08:54:47 +000098#endif // LLVM_SYMBOLIZE_H