DWARFCompileUnit.h revision 239462
169626Sru//===-- DWARFCompileUnit.h --------------------------------------*- C++ -*-===//
269626Sru//
3104862Sru//                     The LLVM Compiler Infrastructure
4104862Sru//
569626Sru// This file is distributed under the University of Illinois Open Source
6104862Sru// License. See LICENSE.TXT for details.
769626Sru//
8104862Sru//===----------------------------------------------------------------------===//
969626Sru
1069626Sru#ifndef LLVM_DEBUGINFO_DWARFCOMPILEUNIT_H
11#define LLVM_DEBUGINFO_DWARFCOMPILEUNIT_H
12
13#include "DWARFDebugAbbrev.h"
14#include "DWARFDebugInfoEntry.h"
15#include <vector>
16
17namespace llvm {
18
19class DWARFContext;
20class raw_ostream;
21
22class DWARFCompileUnit {
23  DWARFContext &Context;
24
25  uint32_t Offset;
26  uint32_t Length;
27  uint16_t Version;
28  const DWARFAbbreviationDeclarationSet *Abbrevs;
29  uint8_t AddrSize;
30  uint64_t BaseAddr;
31  // The compile unit debug information entry item.
32  std::vector<DWARFDebugInfoEntryMinimal> DieArray;
33public:
34  DWARFCompileUnit(DWARFContext &context) : Context(context) {
35    clear();
36  }
37
38  DWARFContext &getContext() const { return Context; }
39  DataExtractor getDebugInfoExtractor() const;
40
41  bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
42  uint32_t extract(uint32_t offset, DataExtractor debug_info_data,
43                   const DWARFAbbreviationDeclarationSet *abbrevs);
44
45  /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
46  /// hasn't already been done. Returns the number of DIEs parsed at this call.
47  size_t extractDIEsIfNeeded(bool cu_die_only);
48  void clear();
49  void dump(raw_ostream &OS);
50  uint32_t getOffset() const { return Offset; }
51  /// Size in bytes of the compile unit header.
52  uint32_t getSize() const { return 11; }
53  bool containsDIEOffset(uint32_t die_offset) const {
54    return die_offset >= getFirstDIEOffset() &&
55      die_offset < getNextCompileUnitOffset();
56  }
57  uint32_t getFirstDIEOffset() const { return Offset + getSize(); }
58  uint32_t getNextCompileUnitOffset() const { return Offset + Length + 4; }
59  /// Size in bytes of the .debug_info data associated with this compile unit.
60  size_t getDebugInfoSize() const { return Length + 4 - getSize(); }
61  uint32_t getLength() const { return Length; }
62  uint16_t getVersion() const { return Version; }
63  const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
64    return Abbrevs;
65  }
66  uint8_t getAddressByteSize() const { return AddrSize; }
67  uint64_t getBaseAddress() const { return BaseAddr; }
68
69  void setBaseAddress(uint64_t base_addr) {
70    BaseAddr = base_addr;
71  }
72
73  const DWARFDebugInfoEntryMinimal *
74  getCompileUnitDIE(bool extract_cu_die_only = true) {
75    extractDIEsIfNeeded(extract_cu_die_only);
76    if (DieArray.empty())
77      return NULL;
78    return &DieArray[0];
79  }
80
81  const char *getCompilationDir();
82
83  /// setDIERelations - We read in all of the DIE entries into our flat list
84  /// of DIE entries and now we need to go back through all of them and set the
85  /// parent, sibling and child pointers for quick DIE navigation.
86  void setDIERelations();
87
88  void addDIE(DWARFDebugInfoEntryMinimal &die) {
89    // The average bytes per DIE entry has been seen to be
90    // around 14-20 so lets pre-reserve the needed memory for
91    // our DIE entries accordingly. Search forward for "Compute
92    // average bytes per DIE" to see #if'ed out code that does
93    // that determination.
94
95    // Only reserve the memory if we are adding children of
96    // the main compile unit DIE. The compile unit DIE is always
97    // the first entry, so if our size is 1, then we are adding
98    // the first compile unit child DIE and should reserve
99    // the memory.
100    if (DieArray.empty())
101      DieArray.reserve(getDebugInfoSize() / 14);
102    DieArray.push_back(die);
103  }
104
105  void clearDIEs(bool keep_compile_unit_die);
106
107  void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
108                              bool clear_dies_if_already_not_parsed);
109  /// getFunctionDIEForAddress - Returns pointer to parsed subprogram DIE,
110  /// address ranges of which contain the provided address,
111  /// or NULL if there is no such subprogram. The pointer
112  /// is valid until DWARFCompileUnit::clear() or clearDIEs() is called.
113  const DWARFDebugInfoEntryMinimal *getFunctionDIEForAddress(int64_t address);
114};
115
116}
117
118#endif
119