1//===-- 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"
15#include "DWARFDebugRangeList.h"
16#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
47  /// hasn't already been done. Returns the number of DIEs parsed at this call.
48  size_t extractDIEsIfNeeded(bool cu_die_only);
49  /// 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;
54  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
79  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
87  const char *getCompilationDir();
88
89  /// 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  }
110
111  void clearDIEs(bool keep_compile_unit_die);
112
113  void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
114                              bool clear_dies_if_already_not_parsed);
115
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);
120};
121
122}
123
124#endif
125