Line data Source code
1 : //===-- DWARFDebugInfoEntry.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 "SyntaxHighlighting.h"
11 : #include "llvm/DebugInfo/DWARFCompileUnit.h"
12 : #include "llvm/DebugInfo/DWARFContext.h"
13 : #include "llvm/DebugInfo/DWARFDebugAbbrev.h"
14 : #include "llvm/DebugInfo/DWARFDebugInfoEntry.h"
15 : #include "llvm/DebugInfo/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"
21 : using namespace llvm;
22 : using namespace dwarf;
23 : using namespace syntax;
24 :
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,
28 : // nullptr otherwise.
29 414 : static const DWARFUnit *findUnitAndExtractFast(DWARFDebugInfoEntryMinimal &DIE,
30 : const DWARFUnit *Unit,
31 : uint32_t *Offset) {
32 414 : Unit = Unit->getUnitSection().getUnitForOffset(*Offset);
33 414 : return (Unit && DIE.extractFast(Unit, Offset)) ? Unit : nullptr;
34 : }
35 :
36 3163 : void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, DWARFUnit *u,
37 : unsigned recurseDepth,
38 : unsigned indent) const {
39 3163 : DataExtractor debug_info_data = u->getDebugInfoExtractor();
40 3163 : uint32_t offset = Offset;
41 :
42 6326 : if (debug_info_data.isValidOffset(offset)) {
43 3163 : uint32_t abbrCode = debug_info_data.getULEB128(&offset);
44 12652 : WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
45 :
46 3163 : if (abbrCode) {
47 2357 : if (AbbrevDecl) {
48 4714 : const char *tagString = TagString(getTag());
49 2357 : if (tagString)
50 4714 : WithColor(OS, syntax::Tag).get().indent(indent) << tagString;
51 : else
52 0 : WithColor(OS, syntax::Tag).get().indent(indent) <<
53 0 : format("DW_TAG_Unknown_%x", getTag());
54 :
55 : OS << format(" [%u] %c\n", abbrCode,
56 7071 : AbbrevDecl->hasChildren() ? '*' : ' ');
57 :
58 : // Dump all data in the DIE for the attributes.
59 15152 : for (const auto &AttrSpec : AbbrevDecl->attributes()) {
60 10438 : dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
61 : }
62 :
63 2357 : const DWARFDebugInfoEntryMinimal *child = getFirstChild();
64 2357 : if (recurseDepth > 0 && child) {
65 3729 : while (child) {
66 2923 : child->dump(OS, u, recurseDepth-1, indent+2);
67 : child = child->getSibling();
68 : }
69 : }
70 : } else {
71 0 : OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
72 0 : << abbrCode << '\n';
73 : }
74 : } else {
75 806 : OS.indent(indent) << "NULL\n";
76 : }
77 : }
78 3163 : }
79 :
80 13 : static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
81 13 : OS << " (";
82 : do {
83 27 : uint64_t Shift = countTrailingZeros(Val);
84 : assert(Shift < 64 && "undefined behavior");
85 27 : uint64_t Bit = 1ULL << Shift;
86 27 : if (const char *PropName = ApplePropertyString(Bit))
87 27 : OS << PropName;
88 : else
89 0 : OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
90 27 : if (!(Val ^= Bit))
91 : break;
92 14 : OS << ", ";
93 : } while (true);
94 14 : OS << ")";
95 13 : }
96 :
97 25 : static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
98 : unsigned AddressSize, unsigned Indent) {
99 25 : if (Ranges.empty())
100 25 : return;
101 :
102 67 : for (const auto &Range: Ranges) {
103 27 : OS << '\n';
104 27 : OS.indent(Indent);
105 : OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
106 : AddressSize*2, Range.first,
107 81 : AddressSize*2, Range.second);
108 : }
109 : }
110 :
111 10438 : void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
112 : DWARFUnit *u,
113 : uint32_t *offset_ptr,
114 : uint16_t attr, uint16_t form,
115 : unsigned indent) const {
116 10438 : const char BaseIndent[] = " ";
117 10438 : OS << BaseIndent;
118 10438 : OS.indent(indent+2);
119 10438 : const char *attrString = AttributeString(attr);
120 10438 : if (attrString)
121 31311 : WithColor(OS, syntax::Attribute) << attrString;
122 : else
123 4 : WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", attr);
124 :
125 10438 : const char *formString = FormEncodingString(form);
126 10438 : if (formString)
127 10438 : OS << " [" << formString << ']';
128 : else
129 0 : OS << format(" [DW_FORM_Unknown_%x]", form);
130 :
131 10438 : DWARFFormValue formValue(form);
132 :
133 20876 : if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
134 0 : return;
135 :
136 10438 : OS << "\t(";
137 :
138 10438 : const char *Name = nullptr;
139 : std::string File;
140 10438 : auto Color = syntax::Enumerator;
141 10438 : if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
142 1148 : Color = syntax::String;
143 1148 : if (const auto *LT = u->getContext().getLineTableForUnit(u))
144 2156 : if (LT->getFileNameByIndex(
145 2156 : formValue.getAsUnsignedConstant().getValue(),
146 : u->getCompilationDir(),
147 2156 : DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
148 3201 : File = '"' + File + '"';
149 1067 : Name = File.c_str();
150 : }
151 9290 : } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
152 3817 : Name = AttributeValueString(attr, *Val);
153 :
154 10438 : if (Name)
155 4812 : WithColor(OS, Color) << Name;
156 8834 : else if (attr == DW_AT_decl_line || attr == DW_AT_call_line)
157 3444 : OS << *formValue.getAsUnsignedConstant();
158 : else
159 7686 : formValue.dump(OS, u);
160 :
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 10596 : if ((attr == DW_AT_specification || attr == DW_AT_abstract_origin) &&
165 : // The signature references aren't handled.
166 158 : formValue.getForm() != DW_FORM_ref_sig8) {
167 474 : uint32_t Ref = formValue.getAsReference(u).getValue();
168 : DWARFDebugInfoEntryMinimal DIE;
169 158 : if (const DWARFUnit *RefU = findUnitAndExtractFast(DIE, u, &Ref))
170 158 : if (const char *Ref = DIE.getName(RefU, DINameKind::LinkageName))
171 158 : OS << " \"" << Ref << '\"';
172 10280 : } else if (attr == DW_AT_APPLE_property_attribute) {
173 13 : if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
174 13 : dumpApplePropertyAttribute(OS, *OptVal);
175 10267 : } else if (attr == DW_AT_ranges) {
176 25 : dumpRanges(OS, getAddressRanges(u), u->getAddressByteSize(),
177 75 : sizeof(BaseIndent)+indent+4);
178 : }
179 :
180 10438 : OS << ")\n";
181 : }
182 :
183 10076874 : bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
184 : uint32_t *OffsetPtr) {
185 10076874 : Offset = *OffsetPtr;
186 10076874 : DataExtractor DebugInfoData = U->getDebugInfoExtractor();
187 20153748 : uint32_t UEndOffset = U->getNextUnitOffset();
188 20153748 : if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
189 : return false;
190 10076874 : uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
191 10076874 : if (0 == AbbrCode) {
192 : // NULL debug tag entry.
193 2238892 : AbbrevDecl = nullptr;
194 2238892 : return true;
195 : }
196 7837982 : AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
197 7837982 : if (nullptr == AbbrevDecl) {
198 : // Restore the original offset.
199 0 : *OffsetPtr = Offset;
200 0 : return false;
201 : }
202 : ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
203 7837982 : U->getAddressByteSize(), U->getVersion());
204 : assert(FixedFormSizes.size() > 0);
205 :
206 : // Skip all data in the .debug_info for the attributes
207 44923544 : for (const auto &AttrSpec : AbbrevDecl->attributes()) {
208 29247580 : uint16_t Form = AttrSpec.Form;
209 :
210 : uint8_t FixedFormSize =
211 58494938 : (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
212 29247580 : if (FixedFormSize)
213 23320210 : *OffsetPtr += FixedFormSize;
214 5927370 : else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
215 : // Restore the original offset.
216 0 : *OffsetPtr = Offset;
217 0 : return false;
218 : }
219 : }
220 : return true;
221 : }
222 :
223 16385870 : bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
224 32771740 : return getTag() == DW_TAG_subprogram;
225 : }
226 :
227 788 : bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
228 1576 : uint32_t Tag = getTag();
229 788 : return Tag == DW_TAG_subprogram ||
230 788 : Tag == DW_TAG_inlined_subroutine;
231 : }
232 :
233 6277017 : bool DWARFDebugInfoEntryMinimal::getAttributeValue(
234 : const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
235 6277017 : if (!AbbrevDecl)
236 : return false;
237 :
238 6277017 : uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
239 6277017 : if (AttrIdx == -1U)
240 : return false;
241 :
242 91448 : DataExtractor DebugInfoData = U->getDebugInfoExtractor();
243 91448 : uint32_t DebugInfoOffset = getOffset();
244 :
245 : // Skip the abbreviation code so we are at the data for the attributes
246 91448 : DebugInfoData.getULEB128(&DebugInfoOffset);
247 :
248 : // Skip preceding attribute values.
249 362874 : for (uint32_t i = 0; i < AttrIdx; ++i) {
250 271426 : DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
251 271426 : DebugInfoData, &DebugInfoOffset, U);
252 : }
253 :
254 274344 : FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
255 91448 : return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
256 : }
257 :
258 3233 : const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
259 : const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
260 : DWARFFormValue FormValue;
261 3233 : if (!getAttributeValue(U, Attr, FormValue))
262 : return FailValue;
263 1640 : Optional<const char *> Result = FormValue.getAsCString(U);
264 1640 : return Result.hasValue() ? Result.getValue() : FailValue;
265 : }
266 :
267 3139393 : uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
268 : const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
269 : DWARFFormValue FormValue;
270 3139393 : if (!getAttributeValue(U, Attr, FormValue))
271 : return FailValue;
272 58712 : Optional<uint64_t> Result = FormValue.getAsAddress(U);
273 58712 : return Result.hasValue() ? Result.getValue() : FailValue;
274 : }
275 :
276 30539 : uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
277 : const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
278 : DWARFFormValue FormValue;
279 30539 : if (!getAttributeValue(U, Attr, FormValue))
280 : return FailValue;
281 17666 : Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
282 17666 : return Result.hasValue() ? Result.getValue() : FailValue;
283 : }
284 :
285 322 : uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
286 : const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
287 : DWARFFormValue FormValue;
288 322 : if (!getAttributeValue(U, Attr, FormValue))
289 : return FailValue;
290 256 : Optional<uint64_t> Result = FormValue.getAsReference(U);
291 256 : return Result.hasValue() ? Result.getValue() : FailValue;
292 : }
293 :
294 3103530 : uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
295 : const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
296 : DWARFFormValue FormValue;
297 3103530 : if (!getAttributeValue(U, Attr, FormValue))
298 : return FailValue;
299 13174 : Optional<uint64_t> Result = FormValue.getAsSectionOffset();
300 13174 : return Result.hasValue() ? Result.getValue() : FailValue;
301 : }
302 :
303 : uint64_t
304 1 : DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
305 : uint64_t FailValue) const {
306 : uint64_t Result =
307 1 : getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
308 1 : if (Result != -1ULL)
309 : return Result;
310 1 : return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
311 : }
312 :
313 3098074 : bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
314 : uint64_t &LowPC,
315 : uint64_t &HighPC) const {
316 3098074 : LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
317 3098074 : if (LowPC == -1ULL)
318 : return false;
319 30416 : HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
320 30416 : if (HighPC == -1ULL) {
321 : // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
322 : // it represents function size.
323 30345 : HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
324 30345 : if (HighPC != -1ULL)
325 17536 : HighPC += LowPC;
326 : }
327 30416 : return (HighPC != -1ULL);
328 : }
329 :
330 : DWARFAddressRangesVector
331 3098275 : DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
332 3098275 : if (isNULL())
333 : return DWARFAddressRangesVector();
334 : // Single range specified by low/high PC.
335 : uint64_t LowPC, HighPC;
336 3098074 : if (getLowAndHighPC(U, LowPC, HighPC)) {
337 52821 : return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
338 : }
339 : // Multiple ranges from .debug_ranges section.
340 : uint32_t RangesOffset =
341 3080467 : getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
342 3080467 : if (RangesOffset != -1U) {
343 : DWARFDebugRangeList RangeList;
344 11763 : if (U->extractRangeList(RangesOffset, RangeList))
345 11763 : return RangeList.getAbsoluteRanges(U->getBaseAddress());
346 : }
347 : return DWARFAddressRangesVector();
348 : }
349 :
350 34781 : void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
351 : const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
352 34781 : if (isNULL())
353 34781 : return;
354 29041 : if (isSubprogramDIE()) {
355 4956 : const auto &DIERanges = getAddressRanges(U);
356 19824 : Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
357 : }
358 :
359 29041 : const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
360 92821 : while (Child) {
361 34739 : Child->collectChildrenAddressRanges(U, Ranges);
362 : Child = Child->getSibling();
363 : }
364 : }
365 :
366 3082840 : bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
367 : const DWARFUnit *U, const uint64_t Address) const {
368 12352016 : for (const auto& R : getAddressRanges(U)) {
369 21163 : if (R.first <= Address && Address < R.second)
370 507 : return true;
371 : }
372 3082333 : return false;
373 : }
374 :
375 : const char *
376 281 : DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
377 : DINameKind Kind) const {
378 281 : if (!isSubroutineDIE())
379 : return nullptr;
380 281 : return getName(U, Kind);
381 : }
382 :
383 : const char *
384 695 : DWARFDebugInfoEntryMinimal::getName(const DWARFUnit *U,
385 : DINameKind Kind) const {
386 695 : if (Kind == DINameKind::None)
387 : return nullptr;
388 : // Try to get mangled name only if it was asked for.
389 695 : if (Kind == DINameKind::LinkageName) {
390 694 : if (const char *name =
391 694 : getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
392 : return name;
393 629 : if (const char *name =
394 629 : getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
395 : return name;
396 : }
397 470 : if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
398 : return name;
399 : // Try to get name from specification DIE.
400 : uint32_t spec_ref =
401 256 : getAttributeValueAsReference(U, DW_AT_specification, -1U);
402 256 : if (spec_ref != -1U) {
403 : DWARFDebugInfoEntryMinimal spec_die;
404 190 : if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
405 190 : if (const char *name = spec_die.getName(RefU, Kind))
406 190 : return name;
407 : }
408 : }
409 : // Try to get name from abstract origin DIE.
410 : uint32_t abs_origin_ref =
411 66 : getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
412 66 : if (abs_origin_ref != -1U) {
413 : DWARFDebugInfoEntryMinimal abs_origin_die;
414 66 : if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
415 66 : &abs_origin_ref)) {
416 66 : if (const char *name = abs_origin_die.getName(RefU, Kind))
417 66 : return name;
418 : }
419 : }
420 : return nullptr;
421 : }
422 :
423 64 : void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
424 : uint32_t &CallFile,
425 : uint32_t &CallLine,
426 : uint32_t &CallColumn) const {
427 64 : CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
428 64 : CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
429 64 : CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
430 64 : }
431 :
432 : DWARFDebugInfoEntryInlinedChain
433 217 : DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
434 : const DWARFUnit *U, const uint64_t Address) const {
435 : DWARFDebugInfoEntryInlinedChain InlinedChain;
436 217 : InlinedChain.U = U;
437 217 : if (isNULL())
438 : return InlinedChain;
439 724 : for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
440 : // Append current DIE to inlined chain only if it has correct tag
441 : // (e.g. it is not a lexical block).
442 507 : if (DIE->isSubroutineDIE()) {
443 281 : InlinedChain.DIEs.push_back(*DIE);
444 : }
445 : // Try to get child which also contains provided address.
446 507 : const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
447 7513 : while (Child) {
448 6789 : if (Child->addressRangeContainsAddress(U, Address)) {
449 : // Assume there is only one such child.
450 : break;
451 : }
452 : Child = Child->getSibling();
453 : }
454 507 : DIE = Child;
455 : }
456 : // Reverse the obtained chain to make the root of inlined chain last.
457 217 : std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
458 : return InlinedChain;
459 : }
|