blob: e6220aa4ce9c86018ec469bdc730a8204c5720df [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);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000053private:
54 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
55 std::string printDILineInfo(DILineInfo LineInfo) const;
56 void DemangleName(std::string &Name) const;
57
Alexey Samsonovc4439c32013-02-15 08:54:47 +000058 typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000059 ModuleMapTy Modules;
60 Options Opts;
Alexey Samsonov638c63c2013-02-04 15:55:26 +000061 static const char kBadString[];
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000062};
63
64class ModuleInfo {
Alexey Samsonovc4439c32013-02-15 08:54:47 +000065public:
Dmitry Vyukovb1819192013-02-14 13:06:18 +000066 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000067
Alexey Samsonovc4439c32013-02-15 08:54:47 +000068 DILineInfo symbolizeCode(uint64_t ModuleOffset,
69 const LLVMSymbolizer::Options &Opts) const;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000070 DIInliningInfo symbolizeInlinedCode(
Alexey Samsonovc4439c32013-02-15 08:54:47 +000071 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
72 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
73 uint64_t &Size) const;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000074
Alexey Samsonovc4439c32013-02-15 08:54:47 +000075private:
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000076 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
77 std::string &Name, uint64_t &Addr,
78 uint64_t &Size) const;
Dmitry Vyukovb1819192013-02-14 13:06:18 +000079 OwningPtr<ObjectFile> Module;
80 OwningPtr<DIContext> DebugInfoContext;
81
82 struct SymbolDesc {
83 uint64_t Addr;
84 uint64_t AddrEnd;
Alexey Samsonovc4439c32013-02-15 08:54:47 +000085 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
Dmitry Vyukovb1819192013-02-14 13:06:18 +000086 return s1.AddrEnd <= s2.Addr;
87 }
88 };
89 typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
90 SymbolMapTy Functions;
91 SymbolMapTy Objects;
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000092};
93
Alexey Samsonovc4439c32013-02-15 08:54:47 +000094} // namespace symbolize
95} // namespace llvm
Alexey Samsonovc4c7ea32013-01-22 14:21:19 +000096
Alexey Samsonovc4439c32013-02-15 08:54:47 +000097#endif // LLVM_SYMBOLIZE_H