1 //===-- DWARFDebugInfoEntry.cpp -------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SyntaxHighlighting.h"
11 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
12 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
13 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
14 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
15 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/Dwarf.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace dwarf;
23 using namespace syntax;
25 // Small helper to extract a DIE pointed by a reference
26 // attribute. It looks up the Unit containing the DIE and calls
27 // DIE.extractFast with the right unit. Returns new unit on success,
29 static const DWARFUnit *findUnitAndExtractFast(DWARFDebugInfoEntryMinimal &DIE,
30 const DWARFUnit *Unit,
32 Unit = Unit->getUnitSection().getUnitForOffset(*Offset);
33 return (Unit && DIE.extractFast(Unit, Offset)) ? Unit : nullptr;
36 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, DWARFUnit *u,
37 unsigned recurseDepth,
38 unsigned indent) const {
39 DataExtractor debug_info_data = u->getDebugInfoExtractor();
40 uint32_t offset = Offset;
42 if (debug_info_data.isValidOffset(offset)) {
43 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
44 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
48 const char *tagString = TagString(getTag());
50 WithColor(OS, syntax::Tag).get().indent(indent) << tagString;
52 WithColor(OS, syntax::Tag).get().indent(indent) <<
53 format("DW_TAG_Unknown_%x", getTag());
55 OS << format(" [%u] %c\n", abbrCode,
56 AbbrevDecl->hasChildren() ? '*' : ' ');
58 // Dump all data in the DIE for the attributes.
59 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
60 dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
63 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
64 if (recurseDepth > 0 && child) {
66 child->dump(OS, u, recurseDepth-1, indent+2);
67 child = child->getSibling();
71 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
75 OS.indent(indent) << "NULL\n";
80 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
83 uint64_t Shift = countTrailingZeros(Val);
84 assert(Shift < 64 && "undefined behavior");
85 uint64_t Bit = 1ULL << Shift;
86 if (const char *PropName = ApplePropertyString(Bit))
89 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
97 static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
98 unsigned AddressSize, unsigned Indent) {
102 for (const auto &Range: Ranges) {
105 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
106 AddressSize*2, Range.first,
107 AddressSize*2, Range.second);
111 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
113 uint32_t *offset_ptr,
114 uint16_t attr, uint16_t form,
115 unsigned indent) const {
116 const char BaseIndent[] = " ";
119 const char *attrString = AttributeString(attr);
121 WithColor(OS, syntax::Attribute) << attrString;
123 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", attr);
125 const char *formString = FormEncodingString(form);
127 OS << " [" << formString << ']';
129 OS << format(" [DW_FORM_Unknown_%x]", form);
131 DWARFFormValue formValue(form);
133 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
138 const char *Name = nullptr;
140 auto Color = syntax::Enumerator;
141 if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
142 Color = syntax::String;
143 if (const auto *LT = u->getContext().getLineTableForUnit(u))
144 if (LT->getFileNameByIndex(
145 formValue.getAsUnsignedConstant().getValue(),
146 u->getCompilationDir(),
147 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
148 File = '"' + File + '"';
151 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
152 Name = AttributeValueString(attr, *Val);
155 WithColor(OS, Color) << Name;
156 else if (attr == DW_AT_decl_line || attr == DW_AT_call_line)
157 OS << *formValue.getAsUnsignedConstant();
159 formValue.dump(OS, u);
161 // We have dumped the attribute raw value. For some attributes
162 // having both the raw value and the pretty-printed value is
163 // interesting. These attributes are handled below.
164 if (attr == DW_AT_specification || attr == DW_AT_abstract_origin) {
165 Optional<uint64_t> Ref = formValue.getAsReference(u);
166 if (Ref.hasValue()) {
167 uint32_t RefOffset = Ref.getValue();
168 DWARFDebugInfoEntryMinimal DIE;
169 if (const DWARFUnit *RefU = findUnitAndExtractFast(DIE, u, &RefOffset))
170 if (const char *Name = DIE.getName(RefU, DINameKind::LinkageName))
171 OS << " \"" << Name << '\"';
173 } else if (attr == DW_AT_APPLE_property_attribute) {
174 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
175 dumpApplePropertyAttribute(OS, *OptVal);
176 } else if (attr == DW_AT_ranges) {
177 dumpRanges(OS, getAddressRanges(u), u->getAddressByteSize(),
178 sizeof(BaseIndent)+indent+4);
184 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
185 uint32_t *OffsetPtr) {
187 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
188 uint32_t UEndOffset = U->getNextUnitOffset();
189 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
191 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
193 // NULL debug tag entry.
194 AbbrevDecl = nullptr;
197 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
198 if (nullptr == AbbrevDecl) {
199 // Restore the original offset.
203 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
204 U->getAddressByteSize(), U->getVersion());
205 assert(FixedFormSizes.size() > 0);
207 // Skip all data in the .debug_info for the attributes
208 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
209 uint16_t Form = AttrSpec.Form;
211 uint8_t FixedFormSize =
212 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
214 *OffsetPtr += FixedFormSize;
215 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
216 // Restore the original offset.
224 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
225 return getTag() == DW_TAG_subprogram;
228 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
229 uint32_t Tag = getTag();
230 return Tag == DW_TAG_subprogram ||
231 Tag == DW_TAG_inlined_subroutine;
234 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
235 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
239 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
243 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
244 uint32_t DebugInfoOffset = getOffset();
246 // Skip the abbreviation code so we are at the data for the attributes
247 DebugInfoData.getULEB128(&DebugInfoOffset);
249 // Skip preceding attribute values.
250 for (uint32_t i = 0; i < AttrIdx; ++i) {
251 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
252 DebugInfoData, &DebugInfoOffset, U);
255 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
256 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
259 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
260 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
261 DWARFFormValue FormValue;
262 if (!getAttributeValue(U, Attr, FormValue))
264 Optional<const char *> Result = FormValue.getAsCString(U);
265 return Result.hasValue() ? Result.getValue() : FailValue;
268 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
269 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
270 DWARFFormValue FormValue;
271 if (!getAttributeValue(U, Attr, FormValue))
273 Optional<uint64_t> Result = FormValue.getAsAddress(U);
274 return Result.hasValue() ? Result.getValue() : FailValue;
277 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
278 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
279 DWARFFormValue FormValue;
280 if (!getAttributeValue(U, Attr, FormValue))
282 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
283 return Result.hasValue() ? Result.getValue() : FailValue;
286 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
287 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
288 DWARFFormValue FormValue;
289 if (!getAttributeValue(U, Attr, FormValue))
291 Optional<uint64_t> Result = FormValue.getAsReference(U);
292 return Result.hasValue() ? Result.getValue() : FailValue;
295 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
296 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
297 DWARFFormValue FormValue;
298 if (!getAttributeValue(U, Attr, FormValue))
300 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
301 return Result.hasValue() ? Result.getValue() : FailValue;
305 DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
306 uint64_t FailValue) const {
308 getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
311 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
314 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
316 uint64_t &HighPC) const {
317 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
320 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
321 if (HighPC == -1ULL) {
322 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
323 // it represents function size.
324 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
328 return (HighPC != -1ULL);
331 DWARFAddressRangesVector
332 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
334 return DWARFAddressRangesVector();
335 // Single range specified by low/high PC.
336 uint64_t LowPC, HighPC;
337 if (getLowAndHighPC(U, LowPC, HighPC)) {
338 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
340 // Multiple ranges from .debug_ranges section.
341 uint32_t RangesOffset =
342 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
343 if (RangesOffset != -1U) {
344 DWARFDebugRangeList RangeList;
345 if (U->extractRangeList(RangesOffset, RangeList))
346 return RangeList.getAbsoluteRanges(U->getBaseAddress());
348 return DWARFAddressRangesVector();
351 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
352 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
355 if (isSubprogramDIE()) {
356 const auto &DIERanges = getAddressRanges(U);
357 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
360 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
362 Child->collectChildrenAddressRanges(U, Ranges);
363 Child = Child->getSibling();
367 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
368 const DWARFUnit *U, const uint64_t Address) const {
369 for (const auto& R : getAddressRanges(U)) {
370 if (R.first <= Address && Address < R.second)
377 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
378 DINameKind Kind) const {
379 if (!isSubroutineDIE())
381 return getName(U, Kind);
385 DWARFDebugInfoEntryMinimal::getName(const DWARFUnit *U,
386 DINameKind Kind) const {
387 if (Kind == DINameKind::None)
389 // Try to get mangled name only if it was asked for.
390 if (Kind == DINameKind::LinkageName) {
391 if (const char *name =
392 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
394 if (const char *name =
395 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
398 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
400 // Try to get name from specification DIE.
402 getAttributeValueAsReference(U, DW_AT_specification, -1U);
403 if (spec_ref != -1U) {
404 DWARFDebugInfoEntryMinimal spec_die;
405 if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
406 if (const char *name = spec_die.getName(RefU, Kind))
410 // Try to get name from abstract origin DIE.
411 uint32_t abs_origin_ref =
412 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
413 if (abs_origin_ref != -1U) {
414 DWARFDebugInfoEntryMinimal abs_origin_die;
415 if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
417 if (const char *name = abs_origin_die.getName(RefU, Kind))
424 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
427 uint32_t &CallColumn) const {
428 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
429 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
430 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
433 DWARFDebugInfoEntryInlinedChain
434 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
435 const DWARFUnit *U, const uint64_t Address) const {
436 DWARFDebugInfoEntryInlinedChain InlinedChain;
440 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
441 // Append current DIE to inlined chain only if it has correct tag
442 // (e.g. it is not a lexical block).
443 if (DIE->isSubroutineDIE()) {
444 InlinedChain.DIEs.push_back(*DIE);
446 // Try to get child which also contains provided address.
447 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
449 if (Child->addressRangeContainsAddress(U, Address)) {
450 // Assume there is only one such child.
453 Child = Child->getSibling();
457 // Reverse the obtained chain to make the root of inlined chain last.
458 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());