blob: ff343de716a824e3a59112e984263ec76f380d0f [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- DWARFContext.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#ifndef LLVM_DEBUGINFO_DWARFCONTEXT_H
11#define LLVM_DEBUGINFO_DWARFCONTEXT_H
12
13#include "DWARFCompileUnit.h"
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000014#include "DWARFDebugAranges.h"
Benjamin Kramerb848e972011-09-15 02:12:05 +000015#include "DWARFDebugLine.h"
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000016#include "DWARFDebugRangeList.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000017#include "llvm/DebugInfo/DIContext.h"
18#include "llvm/ADT/OwningPtr.h"
19#include "llvm/ADT/SmallVector.h"
20
21namespace llvm {
22
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000023/// DWARFContext
24/// This data structure is the top level entity that deals with dwarf debug
25/// information parsing. The actual data is supplied through pure virtual
26/// methods that a concrete implementation provides.
27class DWARFContext : public DIContext {
28 bool IsLittleEndian;
29
30 SmallVector<DWARFCompileUnit, 1> CUs;
31 OwningPtr<DWARFDebugAbbrev> Abbrev;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000032 OwningPtr<DWARFDebugAranges> Aranges;
Benjamin Kramerb848e972011-09-15 02:12:05 +000033 OwningPtr<DWARFDebugLine> Line;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000034
35 DWARFContext(DWARFContext &); // = delete
36 DWARFContext &operator=(DWARFContext &); // = delete
37
38 /// Read compile units from the debug_info section and store them in CUs.
39 void parseCompileUnits();
40protected:
41 DWARFContext(bool isLittleEndian) : IsLittleEndian(isLittleEndian) {}
42public:
43 virtual void dump(raw_ostream &OS);
44 /// Get the number of compile units in this context.
45 unsigned getNumCompileUnits() {
46 if (CUs.empty())
47 parseCompileUnits();
48 return CUs.size();
49 }
50 /// Get the compile unit at the specified index for this compile unit.
51 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
52 if (CUs.empty())
53 parseCompileUnits();
54 return &CUs[index];
55 }
56
57 /// Get a pointer to the parsed DebugAbbrev object.
58 const DWARFDebugAbbrev *getDebugAbbrev();
59
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000060 /// Get a pointer to the parsed DebugAranges object.
61 const DWARFDebugAranges *getDebugAranges();
62
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +000063 /// Get a pointer to a parsed line table corresponding to a compile unit.
64 const DWARFDebugLine::LineTable *
65 getLineTableForCompileUnit(DWARFCompileUnit *cu);
Benjamin Kramerb848e972011-09-15 02:12:05 +000066
Alexey Samsonov38a63812012-08-30 07:49:50 +000067 virtual DILineInfo getLineInfoForAddress(uint64_t Address,
68 DILineInfoSpecifier Specifier = DILineInfoSpecifier());
Benjamin Kramer101b1c52011-09-15 20:43:22 +000069
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000070 bool isLittleEndian() const { return IsLittleEndian; }
71
72 virtual StringRef getInfoSection() = 0;
73 virtual StringRef getAbbrevSection() = 0;
74 virtual StringRef getARangeSection() = 0;
75 virtual StringRef getLineSection() = 0;
76 virtual StringRef getStringSection() = 0;
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000077 virtual StringRef getRangeSection() = 0;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000078
79 static bool isSupportedVersion(unsigned version) {
80 return version == 2 || version == 3;
81 }
Alexey Samsonov38a63812012-08-30 07:49:50 +000082private:
83 /// Return the compile unit that includes an offset (relative to .debug_info).
84 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000085
Alexey Samsonov38a63812012-08-30 07:49:50 +000086 /// Return the compile unit which contains instruction with provided
87 /// address.
88 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
89
90 /// Fetches filename, line and column number for given address and
91 /// compile unit. Returns true on success.
92 bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
93 uint64_t Address,
94 bool NeedsAbsoluteFilePath,
95 std::string &FileName,
96 uint32_t &Line, uint32_t &Column);
97};
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000098
99/// DWARFContextInMemory is the simplest possible implementation of a
100/// DWARFContext. It assumes all content is available in memory and stores
101/// pointers to it.
102class DWARFContextInMemory : public DWARFContext {
David Blaikie2d24e2a2011-12-20 02:50:00 +0000103 virtual void anchor();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000104 StringRef InfoSection;
105 StringRef AbbrevSection;
106 StringRef ARangeSection;
107 StringRef LineSection;
108 StringRef StringSection;
Alexey Samsonoveceb5b92012-08-27 07:17:47 +0000109 StringRef RangeSection;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000110public:
111 DWARFContextInMemory(bool isLittleEndian,
112 StringRef infoSection,
113 StringRef abbrevSection,
114 StringRef aRangeSection,
115 StringRef lineSection,
Alexey Samsonoveceb5b92012-08-27 07:17:47 +0000116 StringRef stringSection,
117 StringRef rangeSection)
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000118 : DWARFContext(isLittleEndian),
119 InfoSection(infoSection),
120 AbbrevSection(abbrevSection),
121 ARangeSection(aRangeSection),
122 LineSection(lineSection),
Alexey Samsonoveceb5b92012-08-27 07:17:47 +0000123 StringSection(stringSection),
124 RangeSection(rangeSection)
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000125 {}
126
127 virtual StringRef getInfoSection() { return InfoSection; }
128 virtual StringRef getAbbrevSection() { return AbbrevSection; }
129 virtual StringRef getARangeSection() { return ARangeSection; }
130 virtual StringRef getLineSection() { return LineSection; }
131 virtual StringRef getStringSection() { return StringSection; }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +0000132 virtual StringRef getRangeSection() { return RangeSection; }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000133};
134
135}
136
137#endif