2013-09-24 06:44:40 +08:00
|
|
|
//===-- DWARFUnit.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-11-24 07:16:32 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
|
2015-01-31 02:07:45 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
2016-11-24 07:16:32 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/DataExtractor.h"
|
2013-09-24 06:44:40 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-11-24 07:16:32 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2013-09-24 07:15:57 +08:00
|
|
|
#include <cstdio>
|
2016-11-24 07:16:32 +08:00
|
|
|
#include <vector>
|
2013-09-24 06:44:40 +08:00
|
|
|
|
2015-11-17 08:39:55 +08:00
|
|
|
namespace llvm {
|
2016-11-24 07:16:32 +08:00
|
|
|
|
2013-09-24 06:44:40 +08:00
|
|
|
using namespace dwarf;
|
|
|
|
|
2014-10-08 08:07:53 +08:00
|
|
|
void DWARFUnitSectionBase::parse(DWARFContext &C, const DWARFSection &Section) {
|
|
|
|
parseImpl(C, Section, C.getDebugAbbrev(), C.getRangeSection(),
|
|
|
|
C.getStringSection(), StringRef(), C.getAddrSection(),
|
2016-04-23 06:50:56 +08:00
|
|
|
C.getLineSection().Data, C.isLittleEndian(), false);
|
2014-10-06 11:36:18 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 08:07:53 +08:00
|
|
|
void DWARFUnitSectionBase::parseDWO(DWARFContext &C,
|
2015-11-17 08:39:55 +08:00
|
|
|
const DWARFSection &DWOSection,
|
|
|
|
DWARFUnitIndex *Index) {
|
2014-10-08 08:07:53 +08:00
|
|
|
parseImpl(C, DWOSection, C.getDebugAbbrevDWO(), C.getRangeDWOSection(),
|
2014-10-06 11:36:18 +08:00
|
|
|
C.getStringDWOSection(), C.getStringOffsetDWOSection(),
|
2016-04-23 06:50:56 +08:00
|
|
|
C.getAddrSection(), C.getLineDWOSection().Data, C.isLittleEndian(),
|
|
|
|
true);
|
2014-10-06 11:36:18 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 08:24:41 +08:00
|
|
|
DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
|
|
|
|
const DWARFDebugAbbrev *DA, StringRef RS, StringRef SS,
|
2015-11-18 05:08:05 +08:00
|
|
|
StringRef SOS, StringRef AOS, StringRef LS, bool LE,
|
2016-04-23 06:50:56 +08:00
|
|
|
bool IsDWO, const DWARFUnitSectionBase &UnitSection,
|
2015-11-17 08:39:55 +08:00
|
|
|
const DWARFUnitIndex::Entry *IndexEntry)
|
2014-10-08 08:24:41 +08:00
|
|
|
: Context(DC), InfoSection(Section), Abbrev(DA), RangeSection(RS),
|
2015-11-18 06:39:23 +08:00
|
|
|
LineSection(LS), StringSection(SS), StringOffsetSection([&]() {
|
2015-11-18 06:39:26 +08:00
|
|
|
if (IndexEntry)
|
|
|
|
if (const auto *C = IndexEntry->getOffset(DW_SECT_STR_OFFSETS))
|
|
|
|
return SOS.slice(C->Offset, C->Offset + C->Length);
|
2015-11-18 06:39:23 +08:00
|
|
|
return SOS;
|
|
|
|
}()),
|
2016-04-23 06:50:56 +08:00
|
|
|
AddrOffsetSection(AOS), isLittleEndian(LE), isDWO(IsDWO),
|
|
|
|
UnitSection(UnitSection), IndexEntry(IndexEntry) {
|
2013-09-24 06:44:40 +08:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:16:32 +08:00
|
|
|
DWARFUnit::~DWARFUnit() = default;
|
2013-09-24 06:44:40 +08:00
|
|
|
|
|
|
|
bool DWARFUnit::getAddrOffsetSectionItem(uint32_t Index,
|
|
|
|
uint64_t &Result) const {
|
|
|
|
uint32_t Offset = AddrOffsetSectionBase + Index * AddrSize;
|
|
|
|
if (AddrOffsetSection.size() < Offset + AddrSize)
|
|
|
|
return false;
|
|
|
|
DataExtractor DA(AddrOffsetSection, isLittleEndian, AddrSize);
|
|
|
|
Result = DA.getAddress(&Offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFUnit::getStringOffsetSectionItem(uint32_t Index,
|
|
|
|
uint32_t &Result) const {
|
|
|
|
// FIXME: string offset section entries are 8-byte for DWARF64.
|
|
|
|
const uint32_t ItemSize = 4;
|
|
|
|
uint32_t Offset = Index * ItemSize;
|
|
|
|
if (StringOffsetSection.size() < Offset + ItemSize)
|
|
|
|
return false;
|
|
|
|
DataExtractor DA(StringOffsetSection, isLittleEndian, 0);
|
|
|
|
Result = DA.getU32(&Offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFUnit::extractImpl(DataExtractor debug_info, uint32_t *offset_ptr) {
|
|
|
|
Length = debug_info.getU32(offset_ptr);
|
|
|
|
Version = debug_info.getU16(offset_ptr);
|
2014-04-25 06:51:03 +08:00
|
|
|
uint64_t AbbrOffset = debug_info.getU32(offset_ptr);
|
2015-11-17 08:39:55 +08:00
|
|
|
if (IndexEntry) {
|
|
|
|
if (AbbrOffset)
|
|
|
|
return false;
|
|
|
|
auto *UnitContrib = IndexEntry->getOffset();
|
|
|
|
if (!UnitContrib || UnitContrib->Length != (Length + 4))
|
|
|
|
return false;
|
|
|
|
auto *AbbrEntry = IndexEntry->getOffset(DW_SECT_ABBREV);
|
|
|
|
if (!AbbrEntry)
|
|
|
|
return false;
|
|
|
|
AbbrOffset = AbbrEntry->Offset;
|
|
|
|
}
|
2013-09-24 06:44:40 +08:00
|
|
|
AddrSize = debug_info.getU8(offset_ptr);
|
|
|
|
|
2014-04-25 06:51:03 +08:00
|
|
|
bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
|
|
|
|
bool VersionOK = DWARFContext::isSupportedVersion(Version);
|
|
|
|
bool AddrSizeOK = AddrSize == 4 || AddrSize == 8;
|
2013-09-24 06:44:40 +08:00
|
|
|
|
2014-04-25 06:51:03 +08:00
|
|
|
if (!LengthOK || !VersionOK || !AddrSizeOK)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Abbrevs = Abbrev->getAbbreviationDeclarationSet(AbbrOffset);
|
2015-05-25 21:28:03 +08:00
|
|
|
return Abbrevs != nullptr;
|
2013-09-24 06:44:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFUnit::extract(DataExtractor debug_info, uint32_t *offset_ptr) {
|
|
|
|
clear();
|
|
|
|
|
|
|
|
Offset = *offset_ptr;
|
|
|
|
|
|
|
|
if (debug_info.isValidOffset(*offset_ptr)) {
|
|
|
|
if (extractImpl(debug_info, offset_ptr))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// reset the offset to where we tried to parse from if anything went wrong
|
|
|
|
*offset_ptr = Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFUnit::extractRangeList(uint32_t RangeListOffset,
|
|
|
|
DWARFDebugRangeList &RangeList) const {
|
|
|
|
// Require that compile unit is extracted.
|
2016-11-24 07:16:32 +08:00
|
|
|
assert(!DieArray.empty());
|
2013-09-24 06:44:40 +08:00
|
|
|
DataExtractor RangesData(RangeSection, isLittleEndian, AddrSize);
|
|
|
|
uint32_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
|
|
|
|
return RangeList.extract(RangesData, &ActualRangeListOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFUnit::clear() {
|
|
|
|
Offset = 0;
|
|
|
|
Length = 0;
|
|
|
|
Version = 0;
|
2014-04-15 14:32:26 +08:00
|
|
|
Abbrevs = nullptr;
|
2013-09-24 06:44:40 +08:00
|
|
|
AddrSize = 0;
|
|
|
|
BaseAddr = 0;
|
|
|
|
RangeSectionBase = 0;
|
|
|
|
AddrOffsetSectionBase = 0;
|
|
|
|
clearDIEs(false);
|
|
|
|
DWO.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *DWARFUnit::getCompilationDir() {
|
2016-12-14 02:25:19 +08:00
|
|
|
return getUnitDIE().getAttributeValueAsString(DW_AT_comp_dir, nullptr);
|
2013-09-24 06:44:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-15 06:38:08 +08:00
|
|
|
Optional<uint64_t> DWARFUnit::getDWOId() {
|
|
|
|
return getUnitDIE().getAttributeValueAsUnsignedConstant(DW_AT_GNU_dwo_id);
|
2013-09-24 06:44:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFUnit::extractDIEsToVector(
|
|
|
|
bool AppendCUDie, bool AppendNonCUDies,
|
2016-12-14 02:25:19 +08:00
|
|
|
std::vector<DWARFDebugInfoEntry> &Dies) const {
|
2013-09-24 06:44:40 +08:00
|
|
|
if (!AppendCUDie && !AppendNonCUDies)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Set the offset to that of the first DIE and calculate the start of the
|
|
|
|
// next compilation unit header.
|
2014-04-25 07:08:56 +08:00
|
|
|
uint32_t DIEOffset = Offset + getHeaderSize();
|
2013-09-24 06:44:40 +08:00
|
|
|
uint32_t NextCUOffset = getNextUnitOffset();
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDebugInfoEntry DIE;
|
2016-11-15 09:23:06 +08:00
|
|
|
DataExtractor DebugInfoData = getDebugInfoExtractor();
|
2013-09-24 06:44:40 +08:00
|
|
|
uint32_t Depth = 0;
|
|
|
|
bool IsCUDie = true;
|
|
|
|
|
2016-12-22 05:37:06 +08:00
|
|
|
while (DIE.extractFast(*this, &DIEOffset, DebugInfoData, NextCUOffset,
|
|
|
|
Depth)) {
|
2013-09-24 06:44:40 +08:00
|
|
|
if (IsCUDie) {
|
|
|
|
if (AppendCUDie)
|
|
|
|
Dies.push_back(DIE);
|
|
|
|
if (!AppendNonCUDies)
|
|
|
|
break;
|
|
|
|
// The average bytes per DIE entry has been seen to be
|
|
|
|
// around 14-20 so let's pre-reserve the needed memory for
|
|
|
|
// our DIE entries accordingly.
|
|
|
|
Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
|
|
|
|
IsCUDie = false;
|
|
|
|
} else {
|
|
|
|
Dies.push_back(DIE);
|
|
|
|
}
|
|
|
|
|
2014-04-30 01:12:42 +08:00
|
|
|
if (const DWARFAbbreviationDeclaration *AbbrDecl =
|
|
|
|
DIE.getAbbreviationDeclarationPtr()) {
|
2013-09-24 06:44:40 +08:00
|
|
|
// Normal DIE
|
|
|
|
if (AbbrDecl->hasChildren())
|
|
|
|
++Depth;
|
|
|
|
} else {
|
|
|
|
// NULL DIE.
|
|
|
|
if (Depth > 0)
|
|
|
|
--Depth;
|
|
|
|
if (Depth == 0)
|
|
|
|
break; // We are done with this compile unit!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give a little bit of info if we encounter corrupt DWARF (our offset
|
|
|
|
// should always terminate at or before the start of the next compilation
|
|
|
|
// unit header).
|
2014-04-25 07:08:56 +08:00
|
|
|
if (DIEOffset > NextCUOffset)
|
2013-09-24 06:44:40 +08:00
|
|
|
fprintf(stderr, "warning: DWARF compile unit extends beyond its "
|
2014-04-25 07:08:56 +08:00
|
|
|
"bounds cu 0x%8.8x at 0x%8.8x'\n", getOffset(), DIEOffset);
|
2013-09-24 06:44:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
|
2016-11-24 07:16:32 +08:00
|
|
|
if ((CUDieOnly && !DieArray.empty()) ||
|
2013-09-24 06:44:40 +08:00
|
|
|
DieArray.size() > 1)
|
|
|
|
return 0; // Already parsed.
|
|
|
|
|
2016-11-24 07:16:32 +08:00
|
|
|
bool HasCUDie = !DieArray.empty();
|
2013-09-24 06:44:40 +08:00
|
|
|
extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
|
|
|
|
|
|
|
|
if (DieArray.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If CU DIE was just parsed, copy several attribute values from it.
|
|
|
|
if (!HasCUDie) {
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDie UnitDie = getUnitDIE();
|
2016-12-15 06:38:08 +08:00
|
|
|
auto BaseAddr = UnitDie.getAttributeValueAsAddress(DW_AT_low_pc);
|
|
|
|
if (!BaseAddr)
|
|
|
|
BaseAddr = UnitDie.getAttributeValueAsAddress(DW_AT_entry_pc);
|
|
|
|
if (BaseAddr)
|
|
|
|
setBaseAddress(*BaseAddr);
|
2016-12-14 02:25:19 +08:00
|
|
|
AddrOffsetSectionBase = UnitDie.getAttributeValueAsSectionOffset(
|
|
|
|
DW_AT_GNU_addr_base, 0);
|
|
|
|
RangeSectionBase = UnitDie.getAttributeValueAsSectionOffset(
|
|
|
|
DW_AT_rnglists_base, 0);
|
2014-06-14 06:31:03 +08:00
|
|
|
// Don't fall back to DW_AT_GNU_ranges_base: it should be ignored for
|
|
|
|
// skeleton CU DIE, so that DWARF users not aware of it are not broken.
|
2013-09-24 06:44:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return DieArray.size();
|
|
|
|
}
|
|
|
|
|
2014-09-06 03:29:45 +08:00
|
|
|
DWARFUnit::DWOHolder::DWOHolder(StringRef DWOPath)
|
2016-11-24 07:16:32 +08:00
|
|
|
: DWOU(nullptr) {
|
2014-09-06 03:29:45 +08:00
|
|
|
auto Obj = object::ObjectFile::createObjectFile(DWOPath);
|
2016-04-07 06:14:09 +08:00
|
|
|
if (!Obj) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(Obj.takeError());
|
2014-09-06 03:29:45 +08:00
|
|
|
return;
|
2016-04-07 06:14:09 +08:00
|
|
|
}
|
2014-09-06 03:29:45 +08:00
|
|
|
DWOFile = std::move(Obj.get());
|
|
|
|
DWOContext.reset(
|
2015-04-24 01:37:47 +08:00
|
|
|
cast<DWARFContext>(new DWARFContextInMemory(*DWOFile.getBinary())));
|
2013-09-24 06:44:40 +08:00
|
|
|
if (DWOContext->getNumDWOCompileUnits() > 0)
|
|
|
|
DWOU = DWOContext->getDWOCompileUnitAtIndex(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFUnit::parseDWO() {
|
2016-04-23 06:50:56 +08:00
|
|
|
if (isDWO)
|
|
|
|
return false;
|
2014-04-15 14:32:26 +08:00
|
|
|
if (DWO.get())
|
2013-09-24 06:44:40 +08:00
|
|
|
return false;
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDie UnitDie = getUnitDIE();
|
|
|
|
if (!UnitDie)
|
2013-09-24 06:44:40 +08:00
|
|
|
return false;
|
|
|
|
const char *DWOFileName =
|
2016-12-14 02:25:19 +08:00
|
|
|
UnitDie.getAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr);
|
2014-04-15 14:32:26 +08:00
|
|
|
if (!DWOFileName)
|
2013-09-24 06:44:40 +08:00
|
|
|
return false;
|
|
|
|
const char *CompilationDir =
|
2016-12-14 02:25:19 +08:00
|
|
|
UnitDie.getAttributeValueAsString(DW_AT_comp_dir, nullptr);
|
2013-09-24 06:44:40 +08:00
|
|
|
SmallString<16> AbsolutePath;
|
2014-04-15 14:32:26 +08:00
|
|
|
if (sys::path::is_relative(DWOFileName) && CompilationDir != nullptr) {
|
2013-09-24 06:44:40 +08:00
|
|
|
sys::path::append(AbsolutePath, CompilationDir);
|
|
|
|
}
|
|
|
|
sys::path::append(AbsolutePath, DWOFileName);
|
2014-09-06 03:29:45 +08:00
|
|
|
DWO = llvm::make_unique<DWOHolder>(AbsolutePath);
|
2013-09-24 06:44:40 +08:00
|
|
|
DWARFUnit *DWOCU = DWO->getUnit();
|
|
|
|
// Verify that compile unit in .dwo file is valid.
|
2014-04-15 14:32:26 +08:00
|
|
|
if (!DWOCU || DWOCU->getDWOId() != getDWOId()) {
|
2013-09-24 06:44:40 +08:00
|
|
|
DWO.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Share .debug_addr and .debug_ranges section with compile unit in .dwo
|
|
|
|
DWOCU->setAddrOffsetSection(AddrOffsetSection, AddrOffsetSectionBase);
|
2016-12-15 06:38:08 +08:00
|
|
|
auto DWORangesBase = UnitDie.getRangesBaseAttribute();
|
|
|
|
DWOCU->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
|
2013-09-24 06:44:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DWARFUnit::clearDIEs(bool KeepCUDie) {
|
|
|
|
if (DieArray.size() > (unsigned)KeepCUDie) {
|
|
|
|
// std::vectors never get any smaller when resized to a smaller size,
|
|
|
|
// or when clear() or erase() are called, the size will report that it
|
|
|
|
// is smaller, but the memory allocated remains intact (call capacity()
|
|
|
|
// to see this). So we need to create a temporary vector and swap the
|
|
|
|
// contents which will cause just the internal pointers to be swapped
|
|
|
|
// so that when temporary vector goes out of scope, it will destroy the
|
|
|
|
// contents.
|
2016-12-14 02:25:19 +08:00
|
|
|
std::vector<DWARFDebugInfoEntry> TmpArray;
|
2013-09-24 06:44:40 +08:00
|
|
|
DieArray.swap(TmpArray);
|
|
|
|
// Save at least the compile unit DIE
|
|
|
|
if (KeepCUDie)
|
|
|
|
DieArray.push_back(TmpArray.front());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-19 01:25:46 +08:00
|
|
|
void DWARFUnit::collectAddressRanges(DWARFAddressRangesVector &CURanges) {
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDie UnitDie = getUnitDIE();
|
|
|
|
if (!UnitDie)
|
2015-05-20 05:54:32 +08:00
|
|
|
return;
|
|
|
|
// First, check if unit DIE describes address ranges for the whole unit.
|
2016-12-14 02:25:19 +08:00
|
|
|
const auto &CUDIERanges = UnitDie.getAddressRanges();
|
2014-04-19 04:30:27 +08:00
|
|
|
if (!CUDIERanges.empty()) {
|
|
|
|
CURanges.insert(CURanges.end(), CUDIERanges.begin(), CUDIERanges.end());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-24 06:44:40 +08:00
|
|
|
// This function is usually called if there in no .debug_aranges section
|
|
|
|
// in order to produce a compile unit level set of address ranges that
|
|
|
|
// is accurate. If the DIEs weren't parsed, then we don't want all dies for
|
|
|
|
// all compile units to stay loaded when they weren't needed. So we can end
|
|
|
|
// up parsing the DWARF and then throwing them all away to keep memory usage
|
|
|
|
// down.
|
2014-04-19 01:25:46 +08:00
|
|
|
const bool ClearDIEs = extractDIEsIfNeeded(false) > 1;
|
2016-12-14 02:25:19 +08:00
|
|
|
getUnitDIE().collectChildrenAddressRanges(CURanges);
|
2014-04-19 01:25:46 +08:00
|
|
|
|
|
|
|
// Collect address ranges from DIEs in .dwo if necessary.
|
2013-09-24 06:44:40 +08:00
|
|
|
bool DWOCreated = parseDWO();
|
2014-04-19 01:25:46 +08:00
|
|
|
if (DWO.get())
|
|
|
|
DWO->getUnit()->collectAddressRanges(CURanges);
|
|
|
|
if (DWOCreated)
|
2013-09-24 06:44:40 +08:00
|
|
|
DWO.reset();
|
|
|
|
|
|
|
|
// Keep memory down by clearing DIEs if this generate function
|
|
|
|
// caused them to be parsed.
|
2014-04-19 01:25:46 +08:00
|
|
|
if (ClearDIEs)
|
2013-09-24 06:44:40 +08:00
|
|
|
clearDIEs(true);
|
|
|
|
}
|
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDie
|
2013-09-24 06:44:40 +08:00
|
|
|
DWARFUnit::getSubprogramForAddress(uint64_t Address) {
|
|
|
|
extractDIEsIfNeeded(false);
|
2016-12-14 02:25:19 +08:00
|
|
|
for (const DWARFDebugInfoEntry &D : DieArray) {
|
|
|
|
DWARFDie DIE(this, &D);
|
2014-03-13 15:52:54 +08:00
|
|
|
if (DIE.isSubprogramDIE() &&
|
2016-12-14 02:25:19 +08:00
|
|
|
DIE.addressRangeContainsAddress(Address)) {
|
|
|
|
return DIE;
|
2013-09-24 06:44:40 +08:00
|
|
|
}
|
2014-03-13 15:52:54 +08:00
|
|
|
}
|
2016-12-14 02:25:19 +08:00
|
|
|
return DWARFDie();
|
2013-09-24 06:44:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
void
|
|
|
|
DWARFUnit::getInlinedChainForAddress(uint64_t Address,
|
|
|
|
SmallVectorImpl<DWARFDie> &InlinedChain) {
|
2013-09-24 06:44:40 +08:00
|
|
|
// First, find a subprogram that contains the given address (the root
|
|
|
|
// of inlined chain).
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDie SubprogramDIE;
|
2016-04-23 05:32:59 +08:00
|
|
|
// Try to look for subprogram DIEs in the DWO file.
|
|
|
|
parseDWO();
|
2016-12-14 02:25:19 +08:00
|
|
|
if (DWO)
|
|
|
|
SubprogramDIE = DWO->getUnit()->getSubprogramForAddress(Address);
|
|
|
|
else
|
|
|
|
SubprogramDIE = getSubprogramForAddress(Address);
|
2013-09-24 06:44:40 +08:00
|
|
|
|
|
|
|
// Get inlined chain rooted at this subprogram DIE.
|
2016-12-14 02:25:19 +08:00
|
|
|
if (SubprogramDIE)
|
|
|
|
SubprogramDIE.getInlinedChainForAddress(Address, InlinedChain);
|
|
|
|
else
|
|
|
|
InlinedChain.clear();
|
2013-09-24 06:44:40 +08:00
|
|
|
}
|
2015-11-17 08:39:55 +08:00
|
|
|
|
|
|
|
const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
|
|
|
|
DWARFSectionKind Kind) {
|
|
|
|
if (Kind == DW_SECT_INFO)
|
|
|
|
return Context.getCUIndex();
|
|
|
|
assert(Kind == DW_SECT_TYPES);
|
|
|
|
return Context.getTUIndex();
|
|
|
|
}
|
2016-11-24 07:16:32 +08:00
|
|
|
|
2016-12-22 05:37:06 +08:00
|
|
|
DWARFDie DWARFUnit::getParent(const DWARFDebugInfoEntry *Die) {
|
|
|
|
if (!Die)
|
|
|
|
return DWARFDie();
|
|
|
|
const uint32_t Depth = Die->getDepth();
|
|
|
|
// Unit DIEs always have a depth of zero and never have parents.
|
|
|
|
if (Depth == 0)
|
|
|
|
return DWARFDie();
|
|
|
|
// Depth of 1 always means parent is the compile/type unit.
|
|
|
|
if (Depth == 1)
|
|
|
|
return getUnitDIE();
|
|
|
|
// Look for previous DIE with a depth that is one less than the Die's depth.
|
|
|
|
const uint32_t ParentDepth = Depth - 1;
|
|
|
|
for (uint32_t I = getDIEIndex(Die) - 1; I > 0; --I) {
|
|
|
|
if (DieArray[I].getDepth() == ParentDepth)
|
|
|
|
return DWARFDie(this, &DieArray[I]);
|
|
|
|
}
|
|
|
|
return DWARFDie();
|
|
|
|
}
|
|
|
|
|
|
|
|
DWARFDie DWARFUnit::getSibling(const DWARFDebugInfoEntry *Die) {
|
|
|
|
if (!Die)
|
|
|
|
return DWARFDie();
|
|
|
|
uint32_t Depth = Die->getDepth();
|
|
|
|
// Unit DIEs always have a depth of zero and never have siblings.
|
|
|
|
if (Depth == 0)
|
|
|
|
return DWARFDie();
|
|
|
|
// NULL DIEs don't have siblings.
|
|
|
|
if (Die->getAbbreviationDeclarationPtr() == nullptr)
|
|
|
|
return DWARFDie();
|
|
|
|
|
|
|
|
// Find the next DIE whose depth is the same as the Die's depth.
|
|
|
|
for (size_t I=getDIEIndex(Die)+1, EndIdx = DieArray.size(); I<EndIdx; ++I) {
|
|
|
|
if (DieArray[I].getDepth() == Depth)
|
|
|
|
return DWARFDie(this, &DieArray[I]);
|
|
|
|
}
|
|
|
|
return DWARFDie();
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:16:32 +08:00
|
|
|
} // end namespace llvm
|