dwarfdump: Dump the contents of DWP indexes
[oota-llvm.git] / lib / DebugInfo / DWARF / DWARFUnitIndex.cpp
1 //===-- DWARFUnitIndex.cpp ------------------------------------------------===//
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 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
11
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/ErrorHandling.h"
14
15 namespace llvm {
16
17 bool DWARFUnitIndex::Header::parse(DataExtractor IndexData,
18                                    uint32_t *OffsetPtr) {
19   Version = IndexData.getU32(OffsetPtr);
20   NumColumns = IndexData.getU32(OffsetPtr);
21   NumUnits = IndexData.getU32(OffsetPtr);
22   NumBuckets = IndexData.getU32(OffsetPtr);
23   return Version <= 2;
24 }
25
26 void DWARFUnitIndex::Header::dump(raw_ostream &OS) const {
27   OS << format("version = %u slots = %u\n\n", Version, NumBuckets);
28 }
29
30 bool DWARFUnitIndex::parse(DataExtractor IndexData) {
31   uint32_t Offset = 0;
32   if (!Header.parse(IndexData, &Offset))
33     return false;
34
35   Rows = llvm::make_unique<HashRow[]>(Header.NumBuckets);
36   auto Contribs =
37       llvm::make_unique<HashRow::SectionContribution *[]>(Header.NumUnits);
38   ColumnKinds = llvm::make_unique<DwarfSection[]>(Header.NumColumns);
39
40   // Read Hash Table of Signatures
41   for (unsigned i = 0; i != Header.NumBuckets; ++i)
42     Rows[i].Signature = IndexData.getU64(&Offset);
43
44   // Read Parallel Table of Indexes
45   for (unsigned i = 0; i != Header.NumBuckets; ++i) {
46     auto Index = IndexData.getU32(&Offset);
47     if (!Index)
48       continue;
49     Rows[i].Contributions =
50         llvm::make_unique<HashRow::SectionContribution[]>(Header.NumColumns);
51     Contribs[Index - 1] = Rows[i].Contributions.get();
52   }
53
54   // Read the Column Headers
55   for (unsigned i = 0; i != Header.NumColumns; ++i)
56     ColumnKinds[i] = static_cast<DwarfSection>(IndexData.getU32(&Offset));
57
58   // Read Table of Section Offsets
59   for (unsigned i = 0; i != Header.NumUnits; ++i) {
60     auto *Contrib = Contribs[i];
61     for (unsigned i = 0; i != Header.NumColumns; ++i) {
62       Contrib[i].Offset = IndexData.getU32(&Offset);
63     }
64   }
65
66   // Read Table of Section Sizes
67   for (unsigned i = 0; i != Header.NumUnits; ++i) {
68     auto *Contrib = Contribs[i];
69     for (unsigned i = 0; i != Header.NumColumns; ++i) {
70       Contrib[i].Size = IndexData.getU32(&Offset);
71     }
72   }
73
74   return true;
75 }
76
77 StringRef DWARFUnitIndex::getColumnHeader(DwarfSection DS) {
78 #define CASE(DS)                                                               \
79   case DW_SECT_##DS:                                                           \
80     return #DS;
81   switch (DS) {
82     CASE(INFO);
83     CASE(TYPES);
84     CASE(ABBREV);
85     CASE(LINE);
86     CASE(LOC);
87     CASE(STR_OFFSETS);
88     CASE(MACINFO);
89     CASE(MACRO);
90   }
91   llvm_unreachable("unknown DwarfSection");
92 }
93
94 void DWARFUnitIndex::dump(raw_ostream &OS) const {
95   Header.dump(OS);
96   OS << "Index Signature         ";
97   for (unsigned i = 0; i != Header.NumColumns; ++i)
98     OS << format(" %-24s", getColumnHeader(ColumnKinds[i]));
99   OS << "\n----- ------------------";
100   for (unsigned i = 0; i != Header.NumColumns; ++i)
101     OS << " ------------------------";
102   OS << '\n';
103   for (unsigned i = 0; i != Header.NumBuckets; ++i) {
104     auto &Row = Rows[i];
105     if (auto *Contribs = Row.Contributions.get()) {
106       OS << format("%5u 0x%016" PRIx64 " ", i, Row.Signature);
107       for (unsigned i = 0; i != Header.NumColumns; ++i) {
108         auto &Contrib = Contribs[i];
109         OS << format("[0x%08u, 0x%08u) ", Contrib.Offset,
110                      Contrib.Offset + Contrib.Size);
111       }
112       OS << '\n';
113     }
114   }
115 }
116 }