blob: 03e28620d4b3bc1edda36748146096b5820cdc3e [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- DWARFCompileUnit.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_DWARFCOMPILEUNIT_H
11#define LLVM_DEBUGINFO_DWARFCOMPILEUNIT_H
12
13#include "DWARFDebugAbbrev.h"
14#include "DWARFDebugInfoEntry.h"
Alexey Samsonov5eae90d2012-09-04 08:12:33 +000015#include "DWARFDebugRangeList.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000016#include <vector>
17
18namespace llvm {
19
20class DWARFContext;
21class raw_ostream;
22
23class DWARFCompileUnit {
24 DWARFContext &Context;
25
26 uint32_t Offset;
27 uint32_t Length;
28 uint16_t Version;
29 const DWARFAbbreviationDeclarationSet *Abbrevs;
30 uint8_t AddrSize;
31 uint64_t BaseAddr;
32 // The compile unit debug information entry item.
33 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
34public:
35 DWARFCompileUnit(DWARFContext &context) : Context(context) {
36 clear();
37 }
38
39 DWARFContext &getContext() const { return Context; }
40 DataExtractor getDebugInfoExtractor() const;
41
42 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
43 uint32_t extract(uint32_t offset, DataExtractor debug_info_data,
44 const DWARFAbbreviationDeclarationSet *abbrevs);
45
46 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
Alexey Samsonova9543aa2012-07-04 09:42:54 +000047 /// hasn't already been done. Returns the number of DIEs parsed at this call.
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000048 size_t extractDIEsIfNeeded(bool cu_die_only);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +000049 /// extractRangeList - extracts the range list referenced by this compile
50 /// unit from .debug_ranges section. Returns true on success.
51 /// Requires that compile unit is already extracted.
52 bool extractRangeList(uint32_t RangeListOffset,
53 DWARFDebugRangeList &RangeList) const;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000054 void clear();
55 void dump(raw_ostream &OS);
56 uint32_t getOffset() const { return Offset; }
57 /// Size in bytes of the compile unit header.
58 uint32_t getSize() const { return 11; }
59 bool containsDIEOffset(uint32_t die_offset) const {
60 return die_offset >= getFirstDIEOffset() &&
61 die_offset < getNextCompileUnitOffset();
62 }
63 uint32_t getFirstDIEOffset() const { return Offset + getSize(); }
64 uint32_t getNextCompileUnitOffset() const { return Offset + Length + 4; }
65 /// Size in bytes of the .debug_info data associated with this compile unit.
66 size_t getDebugInfoSize() const { return Length + 4 - getSize(); }
67 uint32_t getLength() const { return Length; }
68 uint16_t getVersion() const { return Version; }
69 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
70 return Abbrevs;
71 }
72 uint8_t getAddressByteSize() const { return AddrSize; }
73 uint64_t getBaseAddress() const { return BaseAddr; }
74
75 void setBaseAddress(uint64_t base_addr) {
76 BaseAddr = base_addr;
77 }
78
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000079 const DWARFDebugInfoEntryMinimal *
80 getCompileUnitDIE(bool extract_cu_die_only = true) {
81 extractDIEsIfNeeded(extract_cu_die_only);
82 if (DieArray.empty())
83 return NULL;
84 return &DieArray[0];
85 }
86
Alexey Samsonov71d94f82012-07-19 07:03:58 +000087 const char *getCompilationDir();
88
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000089 /// setDIERelations - We read in all of the DIE entries into our flat list
90 /// of DIE entries and now we need to go back through all of them and set the
91 /// parent, sibling and child pointers for quick DIE navigation.
92 void setDIERelations();
93
94 void addDIE(DWARFDebugInfoEntryMinimal &die) {
95 // The average bytes per DIE entry has been seen to be
96 // around 14-20 so lets pre-reserve the needed memory for
97 // our DIE entries accordingly. Search forward for "Compute
98 // average bytes per DIE" to see #if'ed out code that does
99 // that determination.
100
101 // Only reserve the memory if we are adding children of
102 // the main compile unit DIE. The compile unit DIE is always
103 // the first entry, so if our size is 1, then we are adding
104 // the first compile unit child DIE and should reserve
105 // the memory.
106 if (DieArray.empty())
107 DieArray.reserve(getDebugInfoSize() / 14);
108 DieArray.push_back(die);
109 }
Benjamin Kramer10df8062011-09-14 20:52:27 +0000110
111 void clearDIEs(bool keep_compile_unit_die);
112
113 void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
114 bool clear_dies_if_already_not_parsed);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000115
116 /// getInlinedChainForAddress - fetches inlined chain for a given address.
117 /// Returns empty chain if there is no subprogram containing address.
118 DWARFDebugInfoEntryMinimal::InlinedChain getInlinedChainForAddress(
119 uint64_t Address);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000120};
121
122}
123
124#endif