DWARFDebugLoc.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFDebugLoc.h ------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
  17. #include "llvm/Support/Errc.h"
  18. #include <cstdint>
  19. namespace llvm {
  20. class DWARFUnit;
  21. class MCRegisterInfo;
  22. class raw_ostream;
  23. class DWARFObject;
  24. struct DIDumpOptions;
  25. struct DWARFLocationExpression;
  26. namespace object {
  27. struct SectionedAddress;
  28. }
  29. /// A single location within a location list. Entries are stored in the DWARF5
  30. /// form even if they originally come from a DWARF<=4 location list.
  31. struct DWARFLocationEntry {
  32. /// The entry kind (DW_LLE_***).
  33. uint8_t Kind;
  34. /// The first value of the location entry (if applicable).
  35. uint64_t Value0;
  36. /// The second value of the location entry (if applicable).
  37. uint64_t Value1;
  38. /// The index of the section this entry is relative to (if applicable).
  39. uint64_t SectionIndex;
  40. /// The location expression itself (if applicable).
  41. SmallVector<uint8_t, 4> Loc;
  42. };
  43. /// An abstract base class for various kinds of location tables (.debug_loc,
  44. /// .debug_loclists, and their dwo variants).
  45. class DWARFLocationTable {
  46. public:
  47. DWARFLocationTable(DWARFDataExtractor Data) : Data(std::move(Data)) {}
  48. virtual ~DWARFLocationTable() = default;
  49. /// Call the user-provided callback for each entry (including the end-of-list
  50. /// entry) in the location list starting at \p Offset. The callback can return
  51. /// false to terminate the iteration early. Returns an error if it was unable
  52. /// to parse the entire location list correctly. Upon successful termination
  53. /// \p Offset will be updated point past the end of the list.
  54. virtual Error visitLocationList(
  55. uint64_t *Offset,
  56. function_ref<bool(const DWARFLocationEntry &)> Callback) const = 0;
  57. /// Dump the location list at the given \p Offset. The function returns true
  58. /// iff it has successfully reched the end of the list. This means that one
  59. /// can attempt to parse another list after the current one (\p Offset will be
  60. /// updated to point past the end of the current list).
  61. bool dumpLocationList(uint64_t *Offset, raw_ostream &OS,
  62. std::optional<object::SectionedAddress> BaseAddr,
  63. const DWARFObject &Obj, DWARFUnit *U,
  64. DIDumpOptions DumpOpts, unsigned Indent) const;
  65. Error visitAbsoluteLocationList(
  66. uint64_t Offset, std::optional<object::SectionedAddress> BaseAddr,
  67. std::function<std::optional<object::SectionedAddress>(uint32_t)>
  68. LookupAddr,
  69. function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const;
  70. const DWARFDataExtractor &getData() { return Data; }
  71. protected:
  72. DWARFDataExtractor Data;
  73. virtual void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
  74. unsigned Indent, DIDumpOptions DumpOpts,
  75. const DWARFObject &Obj) const = 0;
  76. };
  77. class DWARFDebugLoc final : public DWARFLocationTable {
  78. public:
  79. /// A list of locations that contain one variable.
  80. struct LocationList {
  81. /// The beginning offset where this location list is stored in the debug_loc
  82. /// section.
  83. uint64_t Offset;
  84. /// All the locations in which the variable is stored.
  85. SmallVector<DWARFLocationEntry, 2> Entries;
  86. };
  87. private:
  88. using LocationLists = SmallVector<LocationList, 4>;
  89. /// A list of all the variables in the debug_loc section, each one describing
  90. /// the locations in which the variable is stored.
  91. LocationLists Locations;
  92. public:
  93. DWARFDebugLoc(DWARFDataExtractor Data)
  94. : DWARFLocationTable(std::move(Data)) {}
  95. /// Print the location lists found within the debug_loc section.
  96. void dump(raw_ostream &OS, const DWARFObject &Obj, DIDumpOptions DumpOpts,
  97. std::optional<uint64_t> Offset) const;
  98. Error visitLocationList(
  99. uint64_t *Offset,
  100. function_ref<bool(const DWARFLocationEntry &)> Callback) const override;
  101. protected:
  102. void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
  103. unsigned Indent, DIDumpOptions DumpOpts,
  104. const DWARFObject &Obj) const override;
  105. };
  106. class DWARFDebugLoclists final : public DWARFLocationTable {
  107. public:
  108. DWARFDebugLoclists(DWARFDataExtractor Data, uint16_t Version)
  109. : DWARFLocationTable(std::move(Data)), Version(Version) {}
  110. Error visitLocationList(
  111. uint64_t *Offset,
  112. function_ref<bool(const DWARFLocationEntry &)> Callback) const override;
  113. /// Dump all location lists within the given range.
  114. void dumpRange(uint64_t StartOffset, uint64_t Size, raw_ostream &OS,
  115. const DWARFObject &Obj, DIDumpOptions DumpOpts);
  116. protected:
  117. void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
  118. unsigned Indent, DIDumpOptions DumpOpts,
  119. const DWARFObject &Obj) const override;
  120. private:
  121. uint16_t Version;
  122. };
  123. class ResolverError : public ErrorInfo<ResolverError> {
  124. public:
  125. static char ID;
  126. ResolverError(uint32_t Index, dwarf::LoclistEntries Kind) : Index(Index), Kind(Kind) {}
  127. void log(raw_ostream &OS) const override;
  128. std::error_code convertToErrorCode() const override {
  129. return llvm::errc::invalid_argument;
  130. }
  131. private:
  132. uint32_t Index;
  133. dwarf::LoclistEntries Kind;
  134. };
  135. } // end namespace llvm
  136. #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H
  137. #ifdef __GNUC__
  138. #pragma GCC diagnostic pop
  139. #endif