summaryrefslogtreecommitdiff
path: root/source/Plugins/SymbolFile/DWARF/DWARFDebugAranges.cpp
blob: 55d2f6ffe1e3cfb7b344b7d98547845a18e7853e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//===-- DWARFDebugAranges.cpp -----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "DWARFDebugAranges.h"

#include <assert.h>
#include <stdio.h>

#include <algorithm>

#include "lldb/Utility/Log.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/Timer.h"

#include "DWARFUnit.h"
#include "DWARFDebugInfo.h"
#include "LogChannelDWARF.h"
#include "SymbolFileDWARF.h"

using namespace lldb;
using namespace lldb_private;

//----------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------
DWARFDebugAranges::DWARFDebugAranges() : m_aranges() {}

//----------------------------------------------------------------------
// CountArangeDescriptors
//----------------------------------------------------------------------
class CountArangeDescriptors {
public:
  CountArangeDescriptors(uint32_t &count_ref) : count(count_ref) {
    //      printf("constructor CountArangeDescriptors()\n");
  }
  void operator()(const DWARFDebugArangeSet &set) {
    count += set.NumDescriptors();
  }
  uint32_t &count;
};

//----------------------------------------------------------------------
// Extract
//----------------------------------------------------------------------
bool DWARFDebugAranges::Extract(const DWARFDataExtractor &debug_aranges_data) {
  if (debug_aranges_data.ValidOffset(0)) {
    lldb::offset_t offset = 0;

    DWARFDebugArangeSet set;
    Range range;
    while (set.Extract(debug_aranges_data, &offset)) {
      const uint32_t num_descriptors = set.NumDescriptors();
      if (num_descriptors > 0) {
        const dw_offset_t cu_offset = set.GetCompileUnitDIEOffset();

        for (uint32_t i = 0; i < num_descriptors; ++i) {
          const DWARFDebugArangeSet::Descriptor &descriptor =
              set.GetDescriptorRef(i);
          m_aranges.Append(RangeToDIE::Entry(descriptor.address,
                                             descriptor.length, cu_offset));
        }
      }
      set.Clear();
    }
  }
  return false;
}

//----------------------------------------------------------------------
// Generate
//----------------------------------------------------------------------
bool DWARFDebugAranges::Generate(SymbolFileDWARF *dwarf2Data) {
  Clear();
  DWARFDebugInfo *debug_info = dwarf2Data->DebugInfo();
  if (debug_info) {
    uint32_t cu_idx = 0;
    const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
    for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
      DWARFUnit *cu = debug_info->GetCompileUnitAtIndex(cu_idx);
      if (cu)
        cu->BuildAddressRangeTable(dwarf2Data, this);
    }
  }
  return !IsEmpty();
}

void DWARFDebugAranges::Dump(Log *log) const {
  if (log == NULL)
    return;

  const size_t num_entries = m_aranges.GetSize();
  for (size_t i = 0; i < num_entries; ++i) {
    const RangeToDIE::Entry *entry = m_aranges.GetEntryAtIndex(i);
    if (entry)
      log->Printf("0x%8.8x: [0x%" PRIx64 " - 0x%" PRIx64 ")", entry->data,
                  entry->GetRangeBase(), entry->GetRangeEnd());
  }
}

void DWARFDebugAranges::AppendRange(dw_offset_t offset, dw_addr_t low_pc,
                                    dw_addr_t high_pc) {
  if (high_pc > low_pc)
    m_aranges.Append(RangeToDIE::Entry(low_pc, high_pc - low_pc, offset));
}

void DWARFDebugAranges::Sort(bool minimize) {
  static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
  Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
                     static_cast<void *>(this));

  Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
  size_t orig_arange_size = 0;
  if (log) {
    orig_arange_size = m_aranges.GetSize();
    log->Printf("DWARFDebugAranges::Sort(minimize = %u) with %" PRIu64
                " entries",
                minimize, (uint64_t)orig_arange_size);
  }

  m_aranges.Sort();
  m_aranges.CombineConsecutiveEntriesWithEqualData();

  if (log) {
    if (minimize) {
      const size_t new_arange_size = m_aranges.GetSize();
      const size_t delta = orig_arange_size - new_arange_size;
      log->Printf("DWARFDebugAranges::Sort() %" PRIu64
                  " entries after minimizing (%" PRIu64
                  " entries combined for %" PRIu64 " bytes saved)",
                  (uint64_t)new_arange_size, (uint64_t)delta,
                  (uint64_t)delta * sizeof(Range));
    }
    Dump(log);
  }
}

//----------------------------------------------------------------------
// FindAddress
//----------------------------------------------------------------------
dw_offset_t DWARFDebugAranges::FindAddress(dw_addr_t address) const {
  const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address);
  if (entry)
    return entry->data;
  return DW_INVALID_OFFSET;
}