Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 1 | //===- DWARFDebugLine.cpp -------------------------------------------------===// |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 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 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallVector.h" |
| 13 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 14 | #include "llvm/BinaryFormat/Dwarf.h" |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Format.h" |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | a57c46a | 2011-09-15 02:19:33 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 21 | #include <cassert> |
| 22 | #include <cinttypes> |
| 23 | #include <cstdint> |
| 24 | #include <cstdio> |
| 25 | #include <utility> |
| 26 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | using namespace dwarf; |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 29 | |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 30 | using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind; |
| 31 | |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 32 | namespace { |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 33 | |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 34 | struct ContentDescriptor { |
| 35 | dwarf::LineNumberEntryFormat Type; |
| 36 | dwarf::Form Form; |
| 37 | }; |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 38 | |
| 39 | using ContentDescriptors = SmallVector<ContentDescriptor, 4>; |
| 40 | |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 41 | } // end anonmyous namespace |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 42 | |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 43 | DWARFDebugLine::Prologue::Prologue() { clear(); } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 44 | |
| 45 | void DWARFDebugLine::Prologue::clear() { |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 46 | TotalLength = PrologueLength = 0; |
| 47 | SegSelectorSize = 0; |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 48 | MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0; |
| 49 | OpcodeBase = 0; |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 50 | FormParams = DWARFFormParams({0, 0, DWARF32}); |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 51 | HasMD5 = false; |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 52 | StandardOpcodeLengths.clear(); |
| 53 | IncludeDirectories.clear(); |
| 54 | FileNames.clear(); |
| 55 | } |
| 56 | |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 57 | void DWARFDebugLine::Prologue::dump(raw_ostream &OS, |
| 58 | DIDumpOptions DumpOptions) const { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 59 | OS << "Line table prologue:\n" |
Ed Maste | 6d0bee5 | 2015-05-28 15:38:17 +0000 | [diff] [blame] | 60 | << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength) |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 61 | << format(" version: %u\n", getVersion()); |
| 62 | if (getVersion() >= 5) |
| 63 | OS << format(" address_size: %u\n", getAddressSize()) |
| 64 | << format(" seg_select_size: %u\n", SegSelectorSize); |
| 65 | OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength) |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 66 | << format(" min_inst_length: %u\n", MinInstLength) |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 67 | << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst) |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 68 | << format(" default_is_stmt: %u\n", DefaultIsStmt) |
| 69 | << format(" line_base: %i\n", LineBase) |
| 70 | << format(" line_range: %u\n", LineRange) |
| 71 | << format(" opcode_base: %u\n", OpcodeBase); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 72 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 73 | for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I) |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 74 | OS << format("standard_opcode_lengths[%s] = %u\n", |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 75 | LNStandardString(I + 1).data(), StandardOpcodeLengths[I]); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 76 | |
Paul Robinson | 8181d23 | 2018-01-18 20:33:35 +0000 | [diff] [blame] | 77 | if (!IncludeDirectories.empty()) { |
| 78 | // DWARF v5 starts directory indexes at 0. |
| 79 | uint32_t DirBase = getVersion() >= 5 ? 0 : 1; |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 80 | for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) { |
| 81 | OS << format("include_directories[%3u] = ", I + DirBase); |
| 82 | IncludeDirectories[I].dump(OS, DumpOptions); |
| 83 | OS << '\n'; |
| 84 | } |
Paul Robinson | 8181d23 | 2018-01-18 20:33:35 +0000 | [diff] [blame] | 85 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 86 | |
| 87 | if (!FileNames.empty()) { |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 88 | if (HasMD5) |
| 89 | OS << " Dir MD5 Checksum File Name\n" |
| 90 | << " ---- -------------------------------- -----------" |
| 91 | "---------------\n"; |
| 92 | else |
| 93 | OS << " Dir Mod Time File Len File Name\n" |
| 94 | << " ---- ---------- ---------- -----------" |
| 95 | "----------------\n"; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 96 | for (uint32_t I = 0; I != FileNames.size(); ++I) { |
| 97 | const FileNameEntry &FileEntry = FileNames[I]; |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 98 | OS << format("file_names[%3u] %4" PRIu64 " ", I + 1, FileEntry.DirIdx); |
| 99 | if (HasMD5) |
| 100 | OS << FileEntry.Checksum.digest(); |
| 101 | else |
| 102 | OS << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64, FileEntry.ModTime, |
| 103 | FileEntry.Length); |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 104 | OS << ' '; |
| 105 | FileEntry.Name.dump(OS, DumpOptions); |
| 106 | OS << '\n'; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 111 | // Parse v2-v4 directory and file tables. |
| 112 | static void |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 113 | parseV2DirFileTables(const DWARFDataExtractor &DebugLineData, |
| 114 | uint32_t *OffsetPtr, uint64_t EndPrologueOffset, |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 115 | std::vector<DWARFFormValue> &IncludeDirectories, |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 116 | std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { |
| 117 | while (*OffsetPtr < EndPrologueOffset) { |
| 118 | StringRef S = DebugLineData.getCStrRef(OffsetPtr); |
| 119 | if (S.empty()) |
| 120 | break; |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 121 | DWARFFormValue Dir(dwarf::DW_FORM_string); |
| 122 | Dir.setPValue(S.data()); |
| 123 | IncludeDirectories.push_back(Dir); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | while (*OffsetPtr < EndPrologueOffset) { |
| 127 | StringRef Name = DebugLineData.getCStrRef(OffsetPtr); |
| 128 | if (Name.empty()) |
| 129 | break; |
| 130 | DWARFDebugLine::FileNameEntry FileEntry; |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 131 | FileEntry.Name.setForm(dwarf::DW_FORM_string); |
| 132 | FileEntry.Name.setPValue(Name.data()); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 133 | FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr); |
| 134 | FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr); |
| 135 | FileEntry.Length = DebugLineData.getULEB128(OffsetPtr); |
| 136 | FileNames.push_back(FileEntry); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Parse v5 directory/file entry content descriptions. |
| 141 | // Returns the descriptors, or an empty vector if we did not find a path or |
| 142 | // ran off the end of the prologue. |
| 143 | static ContentDescriptors |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 144 | parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr, |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 145 | uint64_t EndPrologueOffset, bool *HasMD5) { |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 146 | ContentDescriptors Descriptors; |
| 147 | int FormatCount = DebugLineData.getU8(OffsetPtr); |
| 148 | bool HasPath = false; |
| 149 | for (int I = 0; I != FormatCount; ++I) { |
| 150 | if (*OffsetPtr >= EndPrologueOffset) |
| 151 | return ContentDescriptors(); |
| 152 | ContentDescriptor Descriptor; |
| 153 | Descriptor.Type = |
| 154 | dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr)); |
| 155 | Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr)); |
| 156 | if (Descriptor.Type == dwarf::DW_LNCT_path) |
| 157 | HasPath = true; |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 158 | else if (Descriptor.Type == dwarf::DW_LNCT_MD5 && HasMD5) |
| 159 | *HasMD5 = true; |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 160 | Descriptors.push_back(Descriptor); |
| 161 | } |
| 162 | return HasPath ? Descriptors : ContentDescriptors(); |
| 163 | } |
| 164 | |
| 165 | static bool |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 166 | parseV5DirFileTables(const DWARFDataExtractor &DebugLineData, |
| 167 | uint32_t *OffsetPtr, uint64_t EndPrologueOffset, |
Paul Robinson | bf750c8 | 2018-01-29 20:57:43 +0000 | [diff] [blame] | 168 | const DWARFFormParams &FormParams, const DWARFContext &Ctx, |
| 169 | const DWARFUnit *U, bool &HasMD5, |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 170 | std::vector<DWARFFormValue> &IncludeDirectories, |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 171 | std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { |
| 172 | // Get the directory entry description. |
| 173 | ContentDescriptors DirDescriptors = |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 174 | parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 175 | if (DirDescriptors.empty()) |
| 176 | return false; |
| 177 | |
| 178 | // Get the directory entries, according to the format described above. |
| 179 | int DirEntryCount = DebugLineData.getU8(OffsetPtr); |
| 180 | for (int I = 0; I != DirEntryCount; ++I) { |
| 181 | if (*OffsetPtr >= EndPrologueOffset) |
| 182 | return false; |
| 183 | for (auto Descriptor : DirDescriptors) { |
| 184 | DWARFFormValue Value(Descriptor.Form); |
| 185 | switch (Descriptor.Type) { |
| 186 | case DW_LNCT_path: |
Paul Robinson | bf750c8 | 2018-01-29 20:57:43 +0000 | [diff] [blame] | 187 | if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U)) |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 188 | return false; |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 189 | IncludeDirectories.push_back(Value); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 190 | break; |
| 191 | default: |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 192 | if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams)) |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Get the file entry description. |
| 199 | ContentDescriptors FileDescriptors = |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 200 | parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, &HasMD5); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 201 | if (FileDescriptors.empty()) |
| 202 | return false; |
| 203 | |
| 204 | // Get the file entries, according to the format described above. |
| 205 | int FileEntryCount = DebugLineData.getU8(OffsetPtr); |
| 206 | for (int I = 0; I != FileEntryCount; ++I) { |
| 207 | if (*OffsetPtr >= EndPrologueOffset) |
| 208 | return false; |
| 209 | DWARFDebugLine::FileNameEntry FileEntry; |
| 210 | for (auto Descriptor : FileDescriptors) { |
| 211 | DWARFFormValue Value(Descriptor.Form); |
Paul Robinson | bf750c8 | 2018-01-29 20:57:43 +0000 | [diff] [blame] | 212 | if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U)) |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 213 | return false; |
| 214 | switch (Descriptor.Type) { |
| 215 | case DW_LNCT_path: |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 216 | FileEntry.Name = Value; |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 217 | break; |
| 218 | case DW_LNCT_directory_index: |
| 219 | FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue(); |
| 220 | break; |
| 221 | case DW_LNCT_timestamp: |
| 222 | FileEntry.ModTime = Value.getAsUnsignedConstant().getValue(); |
| 223 | break; |
| 224 | case DW_LNCT_size: |
| 225 | FileEntry.Length = Value.getAsUnsignedConstant().getValue(); |
| 226 | break; |
Paul Robinson | a06f8dc | 2017-12-18 19:08:35 +0000 | [diff] [blame] | 227 | case DW_LNCT_MD5: |
| 228 | assert(Value.getAsBlock().getValue().size() == 16); |
| 229 | std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16, |
| 230 | FileEntry.Checksum.Bytes.begin()); |
| 231 | break; |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 232 | default: |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | FileNames.push_back(FileEntry); |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 241 | bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData, |
Paul Robinson | bf750c8 | 2018-01-29 20:57:43 +0000 | [diff] [blame] | 242 | uint32_t *OffsetPtr, |
| 243 | const DWARFContext &Ctx, |
| 244 | const DWARFUnit *U) { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 245 | const uint64_t PrologueOffset = *OffsetPtr; |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 246 | |
| 247 | clear(); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 248 | TotalLength = DebugLineData.getU32(OffsetPtr); |
Ed Maste | 6d0bee5 | 2015-05-28 15:38:17 +0000 | [diff] [blame] | 249 | if (TotalLength == UINT32_MAX) { |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 250 | FormParams.Format = dwarf::DWARF64; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 251 | TotalLength = DebugLineData.getU64(OffsetPtr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 252 | } else if (TotalLength >= 0xffffff00) { |
Ed Maste | 6d0bee5 | 2015-05-28 15:38:17 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 255 | FormParams.Version = DebugLineData.getU16(OffsetPtr); |
| 256 | if (getVersion() < 2) |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 257 | return false; |
| 258 | |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 259 | if (getVersion() >= 5) { |
| 260 | FormParams.AddrSize = DebugLineData.getU8(OffsetPtr); |
Paul Robinson | 63811a4 | 2017-11-22 15:33:17 +0000 | [diff] [blame] | 261 | assert((DebugLineData.getAddressSize() == 0 || |
| 262 | DebugLineData.getAddressSize() == getAddressSize()) && |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 263 | "Line table header and data extractor disagree"); |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 264 | SegSelectorSize = DebugLineData.getU8(OffsetPtr); |
| 265 | } |
| 266 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 267 | PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength()); |
| 268 | const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr; |
| 269 | MinInstLength = DebugLineData.getU8(OffsetPtr); |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 270 | if (getVersion() >= 4) |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 271 | MaxOpsPerInst = DebugLineData.getU8(OffsetPtr); |
| 272 | DefaultIsStmt = DebugLineData.getU8(OffsetPtr); |
| 273 | LineBase = DebugLineData.getU8(OffsetPtr); |
| 274 | LineRange = DebugLineData.getU8(OffsetPtr); |
| 275 | OpcodeBase = DebugLineData.getU8(OffsetPtr); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 276 | |
| 277 | StandardOpcodeLengths.reserve(OpcodeBase - 1); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 278 | for (uint32_t I = 1; I < OpcodeBase; ++I) { |
| 279 | uint8_t OpLen = DebugLineData.getU8(OffsetPtr); |
| 280 | StandardOpcodeLengths.push_back(OpLen); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Paul Robinson | 75c068c | 2017-06-26 18:43:01 +0000 | [diff] [blame] | 283 | if (getVersion() >= 5) { |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 284 | if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset, |
Paul Robinson | bf750c8 | 2018-01-29 20:57:43 +0000 | [diff] [blame] | 285 | FormParams, Ctx, U, HasMD5, IncludeDirectories, |
Paul Robinson | e5400f8 | 2017-11-07 19:57:12 +0000 | [diff] [blame] | 286 | FileNames)) { |
Paul Robinson | 2bc3873 | 2017-05-02 21:40:47 +0000 | [diff] [blame] | 287 | fprintf(stderr, |
| 288 | "warning: parsing line table prologue at 0x%8.8" PRIx64 |
| 289 | " found an invalid directory or file table description at" |
| 290 | " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr); |
| 291 | return false; |
| 292 | } |
| 293 | } else |
| 294 | parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset, |
| 295 | IncludeDirectories, FileNames); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 296 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 297 | if (*OffsetPtr != EndPrologueOffset) { |
| 298 | fprintf(stderr, |
| 299 | "warning: parsing line table prologue at 0x%8.8" PRIx64 |
| 300 | " should have ended at 0x%8.8" PRIx64 |
| 301 | " but it ended at 0x%8.8" PRIx64 "\n", |
| 302 | PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr); |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | return true; |
| 306 | } |
| 307 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 308 | DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 309 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 310 | void DWARFDebugLine::Row::postAppend() { |
| 311 | BasicBlock = false; |
| 312 | PrologueEnd = false; |
| 313 | EpilogueBegin = false; |
| 314 | } |
| 315 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 316 | void DWARFDebugLine::Row::reset(bool DefaultIsStmt) { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 317 | Address = 0; |
| 318 | Line = 1; |
| 319 | Column = 0; |
| 320 | File = 1; |
| 321 | Isa = 0; |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 322 | Discriminator = 0; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 323 | IsStmt = DefaultIsStmt; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 324 | BasicBlock = false; |
| 325 | EndSequence = false; |
| 326 | PrologueEnd = false; |
| 327 | EpilogueBegin = false; |
| 328 | } |
| 329 | |
Greg Clayton | 6707046 | 2017-05-02 22:48:52 +0000 | [diff] [blame] | 330 | void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) { |
| 331 | OS << "Address Line Column File ISA Discriminator Flags\n" |
| 332 | << "------------------ ------ ------ ------ --- ------------- " |
| 333 | "-------------\n"; |
| 334 | } |
| 335 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 336 | void DWARFDebugLine::Row::dump(raw_ostream &OS) const { |
Benjamin Kramer | f3da529 | 2011-11-05 08:57:40 +0000 | [diff] [blame] | 337 | OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column) |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 338 | << format(" %6u %3u %13u ", File, Isa, Discriminator) |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 339 | << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "") |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 340 | << (PrologueEnd ? " prologue_end" : "") |
| 341 | << (EpilogueBegin ? " epilogue_begin" : "") |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 342 | << (EndSequence ? " end_sequence" : "") << '\n'; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 345 | DWARFDebugLine::Sequence::Sequence() { reset(); } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 346 | |
| 347 | void DWARFDebugLine::Sequence::reset() { |
| 348 | LowPC = 0; |
| 349 | HighPC = 0; |
| 350 | FirstRowIndex = 0; |
| 351 | LastRowIndex = 0; |
| 352 | Empty = true; |
| 353 | } |
| 354 | |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 355 | DWARFDebugLine::LineTable::LineTable() { clear(); } |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 356 | |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 357 | void DWARFDebugLine::LineTable::dump(raw_ostream &OS, |
| 358 | DIDumpOptions DumpOptions) const { |
| 359 | Prologue.dump(OS, DumpOptions); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 360 | OS << '\n'; |
| 361 | |
| 362 | if (!Rows.empty()) { |
Greg Clayton | 6707046 | 2017-05-02 22:48:52 +0000 | [diff] [blame] | 363 | Row::dumpTableHeader(OS); |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 364 | for (const Row &R : Rows) { |
| 365 | R.dump(OS); |
| 366 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 370 | void DWARFDebugLine::LineTable::clear() { |
| 371 | Prologue.clear(); |
| 372 | Rows.clear(); |
| 373 | Sequences.clear(); |
| 374 | } |
| 375 | |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 376 | DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT) |
Eugene Zelenko | 2db0cfa | 2017-06-23 21:57:40 +0000 | [diff] [blame] | 377 | : LineTable(LT) { |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 378 | resetRowAndSequence(); |
| 379 | } |
Nick Lewycky | 4d04492 | 2011-09-15 03:41:51 +0000 | [diff] [blame] | 380 | |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 381 | void DWARFDebugLine::ParsingState::resetRowAndSequence() { |
| 382 | Row.reset(LineTable->Prologue.DefaultIsStmt); |
| 383 | Sequence.reset(); |
| 384 | } |
| 385 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 386 | void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) { |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 387 | if (Sequence.Empty) { |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 388 | // Record the beginning of instruction sequence. |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 389 | Sequence.Empty = false; |
| 390 | Sequence.LowPC = Row.Address; |
| 391 | Sequence.FirstRowIndex = RowNumber; |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 392 | } |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 393 | ++RowNumber; |
| 394 | LineTable->appendRow(Row); |
| 395 | if (Row.EndSequence) { |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 396 | // Record the end of instruction sequence. |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 397 | Sequence.HighPC = Row.Address; |
| 398 | Sequence.LastRowIndex = RowNumber; |
| 399 | if (Sequence.isValid()) |
| 400 | LineTable->appendSequence(Sequence); |
| 401 | Sequence.reset(); |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 402 | } |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 403 | Row.postAppend(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 406 | const DWARFDebugLine::LineTable * |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 407 | DWARFDebugLine::getLineTable(uint32_t Offset) const { |
| 408 | LineTableConstIter Pos = LineTableMap.find(Offset); |
| 409 | if (Pos != LineTableMap.end()) |
| 410 | return &Pos->second; |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 411 | return nullptr; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 414 | const DWARFDebugLine::LineTable * |
Paul Robinson | 511b54c | 2017-11-22 15:48:30 +0000 | [diff] [blame] | 415 | DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData, |
Paul Robinson | bf750c8 | 2018-01-29 20:57:43 +0000 | [diff] [blame] | 416 | uint32_t Offset, const DWARFContext &Ctx, |
| 417 | const DWARFUnit *U) { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 418 | std::pair<LineTableIter, bool> Pos = |
| 419 | LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable())); |
| 420 | LineTable *LT = &Pos.first->second; |
| 421 | if (Pos.second) { |
Paul Robinson | bf750c8 | 2018-01-29 20:57:43 +0000 | [diff] [blame] | 422 | if (!LT->parse(DebugLineData, &Offset, Ctx, U)) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 423 | return nullptr; |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 424 | } |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 425 | return LT; |
Benjamin Kramer | 679e175 | 2011-09-15 20:43:18 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Paul Robinson | 511b54c | 2017-11-22 15:48:30 +0000 | [diff] [blame] | 428 | bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData, |
Paul Robinson | bf750c8 | 2018-01-29 20:57:43 +0000 | [diff] [blame] | 429 | uint32_t *OffsetPtr, |
| 430 | const DWARFContext &Ctx, |
| 431 | const DWARFUnit *U, raw_ostream *OS) { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 432 | const uint32_t DebugLineOffset = *OffsetPtr; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 433 | |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 434 | clear(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 435 | |
Paul Robinson | bf750c8 | 2018-01-29 20:57:43 +0000 | [diff] [blame] | 436 | if (!Prologue.parse(DebugLineData, OffsetPtr, Ctx, U)) { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 437 | // Restore our offset and return false to indicate failure! |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 438 | *OffsetPtr = DebugLineOffset; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 442 | if (OS) { |
| 443 | // The presence of OS signals verbose dumping. |
| 444 | DIDumpOptions DumpOptions; |
| 445 | DumpOptions.Verbose = true; |
| 446 | Prologue.dump(*OS, DumpOptions); |
| 447 | } |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 448 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 449 | const uint32_t EndOffset = |
| 450 | DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 451 | |
Paul Robinson | 511b54c | 2017-11-22 15:48:30 +0000 | [diff] [blame] | 452 | // See if we should tell the data extractor the address size. |
| 453 | if (DebugLineData.getAddressSize() == 0) |
| 454 | DebugLineData.setAddressSize(Prologue.getAddressSize()); |
| 455 | else |
| 456 | assert(Prologue.getAddressSize() == 0 || |
| 457 | Prologue.getAddressSize() == DebugLineData.getAddressSize()); |
| 458 | |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 459 | ParsingState State(this); |
Benjamin Kramer | 112ec17 | 2011-09-15 21:59:13 +0000 | [diff] [blame] | 460 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 461 | while (*OffsetPtr < EndOffset) { |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 462 | if (OS) |
| 463 | *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr); |
| 464 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 465 | uint8_t Opcode = DebugLineData.getU8(OffsetPtr); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 466 | |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 467 | if (OS) |
| 468 | *OS << format("%02.02" PRIx8 " ", Opcode); |
| 469 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 470 | if (Opcode == 0) { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 471 | // Extended Opcodes always start with a zero opcode followed by |
| 472 | // a uleb128 length so you can skip ones you don't know about |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 473 | uint64_t Len = DebugLineData.getULEB128(OffsetPtr); |
Paul Robinson | e083334 | 2017-11-22 15:14:49 +0000 | [diff] [blame] | 474 | uint32_t ExtOffset = *OffsetPtr; |
| 475 | |
| 476 | // Tolerate zero-length; assume length is correct and soldier on. |
| 477 | if (Len == 0) { |
| 478 | if (OS) |
| 479 | *OS << "Badly formed extended line op (length 0)\n"; |
| 480 | continue; |
| 481 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 482 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 483 | uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 484 | if (OS) |
| 485 | *OS << LNExtendedString(SubOpcode); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 486 | switch (SubOpcode) { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 487 | case DW_LNE_end_sequence: |
| 488 | // Set the end_sequence register of the state machine to true and |
| 489 | // append a row to the matrix using the current values of the |
| 490 | // state-machine registers. Then reset the registers to the initial |
| 491 | // values specified above. Every statement program sequence must end |
| 492 | // with a DW_LNE_end_sequence instruction which creates a row whose |
| 493 | // address is that of the byte after the last target machine instruction |
| 494 | // of the sequence. |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 495 | State.Row.EndSequence = true; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 496 | State.appendRowToMatrix(*OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 497 | if (OS) { |
| 498 | *OS << "\n"; |
| 499 | OS->indent(12); |
| 500 | State.Row.dump(*OS); |
| 501 | } |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 502 | State.resetRowAndSequence(); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 503 | break; |
| 504 | |
| 505 | case DW_LNE_set_address: |
| 506 | // Takes a single relocatable address as an operand. The size of the |
| 507 | // operand is the size appropriate to hold an address on the target |
| 508 | // machine. Set the address register to the value given by the |
| 509 | // relocatable address. All of the other statement program opcodes |
| 510 | // that affect the address register add a delta to it. This instruction |
| 511 | // stores a relocatable value into it instead. |
Paul Robinson | 511b54c | 2017-11-22 15:48:30 +0000 | [diff] [blame] | 512 | // |
| 513 | // Make sure the extractor knows the address size. If not, infer it |
| 514 | // from the size of the operand. |
| 515 | if (DebugLineData.getAddressSize() == 0) |
| 516 | DebugLineData.setAddressSize(Len - 1); |
| 517 | else |
| 518 | assert(DebugLineData.getAddressSize() == Len - 1); |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 519 | State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 520 | if (OS) |
| 521 | *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 522 | break; |
| 523 | |
| 524 | case DW_LNE_define_file: |
| 525 | // Takes 4 arguments. The first is a null terminated string containing |
| 526 | // a source file name. The second is an unsigned LEB128 number |
| 527 | // representing the directory index of the directory in which the file |
| 528 | // was found. The third is an unsigned LEB128 number representing the |
| 529 | // time of last modification of the file. The fourth is an unsigned |
| 530 | // LEB128 number representing the length in bytes of the file. The time |
| 531 | // and length fields may contain LEB128(0) if the information is not |
| 532 | // available. |
| 533 | // |
| 534 | // The directory index represents an entry in the include_directories |
| 535 | // section of the statement program prologue. The index is LEB128(0) |
| 536 | // if the file was found in the current directory of the compilation, |
| 537 | // LEB128(1) if it was found in the first directory in the |
| 538 | // include_directories section, and so on. The directory index is |
| 539 | // ignored for file names that represent full path names. |
| 540 | // |
| 541 | // The files are numbered, starting at 1, in the order in which they |
| 542 | // appear; the names in the prologue come before names defined by |
| 543 | // the DW_LNE_define_file instruction. These numbers are used in the |
| 544 | // the file register of the state machine. |
| 545 | { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 546 | FileNameEntry FileEntry; |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 547 | const char *Name = DebugLineData.getCStr(OffsetPtr); |
| 548 | FileEntry.Name.setForm(dwarf::DW_FORM_string); |
| 549 | FileEntry.Name.setPValue(Name); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 550 | FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr); |
| 551 | FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr); |
| 552 | FileEntry.Length = DebugLineData.getULEB128(OffsetPtr); |
| 553 | Prologue.FileNames.push_back(FileEntry); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 554 | if (OS) |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 555 | *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time=" |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 556 | << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime) |
| 557 | << ", length=" << FileEntry.Length << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 558 | } |
| 559 | break; |
| 560 | |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 561 | case DW_LNE_set_discriminator: |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 562 | State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 563 | if (OS) |
| 564 | *OS << " (" << State.Row.Discriminator << ")"; |
Diego Novillo | 5b5cf50 | 2014-02-14 19:27:53 +0000 | [diff] [blame] | 565 | break; |
| 566 | |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 567 | default: |
Paul Robinson | e083334 | 2017-11-22 15:14:49 +0000 | [diff] [blame] | 568 | if (OS) |
| 569 | *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode) |
| 570 | << format(" length %" PRIx64, Len); |
| 571 | // Len doesn't include the zero opcode byte or the length itself, but |
| 572 | // it does include the sub_opcode, so we have to adjust for that. |
| 573 | (*OffsetPtr) += Len - 1; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 574 | break; |
| 575 | } |
Paul Robinson | e083334 | 2017-11-22 15:14:49 +0000 | [diff] [blame] | 576 | // Make sure the stated and parsed lengths are the same. |
| 577 | // Otherwise we have an unparseable line-number program. |
| 578 | if (*OffsetPtr - ExtOffset != Len) { |
| 579 | fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32 |
| 580 | " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n", |
| 581 | ExtOffset, Len, *OffsetPtr - ExtOffset); |
| 582 | // Skip the rest of the line-number program. |
| 583 | *OffsetPtr = EndOffset; |
| 584 | return false; |
| 585 | } |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 586 | } else if (Opcode < Prologue.OpcodeBase) { |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 587 | if (OS) |
| 588 | *OS << LNStandardString(Opcode); |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 589 | switch (Opcode) { |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 590 | // Standard Opcodes |
| 591 | case DW_LNS_copy: |
| 592 | // Takes no arguments. Append a row to the matrix using the |
| 593 | // current values of the state-machine registers. Then set |
| 594 | // the basic_block register to false. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 595 | State.appendRowToMatrix(*OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 596 | if (OS) { |
| 597 | *OS << "\n"; |
| 598 | OS->indent(12); |
| 599 | State.Row.dump(*OS); |
| 600 | *OS << "\n"; |
| 601 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | case DW_LNS_advance_pc: |
| 605 | // Takes a single unsigned LEB128 operand, multiplies it by the |
| 606 | // min_inst_length field of the prologue, and adds the |
| 607 | // result to the address register of the state machine. |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 608 | { |
| 609 | uint64_t AddrOffset = |
| 610 | DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength; |
| 611 | State.Row.Address += AddrOffset; |
| 612 | if (OS) |
| 613 | *OS << " (" << AddrOffset << ")"; |
| 614 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 615 | break; |
| 616 | |
| 617 | case DW_LNS_advance_line: |
| 618 | // Takes a single signed LEB128 operand and adds that value to |
| 619 | // the line register of the state machine. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 620 | State.Row.Line += DebugLineData.getSLEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 621 | if (OS) |
| 622 | *OS << " (" << State.Row.Line << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 623 | break; |
| 624 | |
| 625 | case DW_LNS_set_file: |
| 626 | // Takes a single unsigned LEB128 operand and stores it in the file |
| 627 | // register of the state machine. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 628 | State.Row.File = DebugLineData.getULEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 629 | if (OS) |
| 630 | *OS << " (" << State.Row.File << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 631 | break; |
| 632 | |
| 633 | case DW_LNS_set_column: |
| 634 | // Takes a single unsigned LEB128 operand and stores it in the |
| 635 | // column register of the state machine. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 636 | State.Row.Column = DebugLineData.getULEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 637 | if (OS) |
| 638 | *OS << " (" << State.Row.Column << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 639 | break; |
| 640 | |
| 641 | case DW_LNS_negate_stmt: |
| 642 | // Takes no arguments. Set the is_stmt register of the state |
| 643 | // machine to the logical negation of its current value. |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 644 | State.Row.IsStmt = !State.Row.IsStmt; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 645 | break; |
| 646 | |
| 647 | case DW_LNS_set_basic_block: |
| 648 | // Takes no arguments. Set the basic_block register of the |
| 649 | // state machine to true |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 650 | State.Row.BasicBlock = true; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 651 | break; |
| 652 | |
| 653 | case DW_LNS_const_add_pc: |
| 654 | // Takes no arguments. Add to the address register of the state |
| 655 | // machine the address increment value corresponding to special |
| 656 | // opcode 255. The motivation for DW_LNS_const_add_pc is this: |
| 657 | // when the statement program needs to advance the address by a |
| 658 | // small amount, it can use a single special opcode, which occupies |
| 659 | // a single byte. When it needs to advance the address by up to |
| 660 | // twice the range of the last special opcode, it can use |
| 661 | // DW_LNS_const_add_pc followed by a special opcode, for a total |
| 662 | // of two bytes. Only if it needs to advance the address by more |
| 663 | // than twice that range will it need to use both DW_LNS_advance_pc |
| 664 | // and a special opcode, requiring three or more bytes. |
| 665 | { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 666 | uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase; |
| 667 | uint64_t AddrOffset = |
| 668 | (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength; |
| 669 | State.Row.Address += AddrOffset; |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 670 | if (OS) |
| 671 | *OS |
| 672 | << format(" (0x%16.16" PRIx64 ")", AddrOffset); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 673 | } |
| 674 | break; |
| 675 | |
| 676 | case DW_LNS_fixed_advance_pc: |
| 677 | // Takes a single uhalf operand. Add to the address register of |
| 678 | // the state machine the value of the (unencoded) operand. This |
| 679 | // is the only extended opcode that takes an argument that is not |
| 680 | // a variable length number. The motivation for DW_LNS_fixed_advance_pc |
| 681 | // is this: existing assemblers cannot emit DW_LNS_advance_pc or |
| 682 | // special opcodes because they cannot encode LEB128 numbers or |
| 683 | // judge when the computation of a special opcode overflows and |
| 684 | // requires the use of DW_LNS_advance_pc. Such assemblers, however, |
| 685 | // can use DW_LNS_fixed_advance_pc instead, sacrificing compression. |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 686 | { |
| 687 | uint16_t PCOffset = DebugLineData.getU16(OffsetPtr); |
| 688 | State.Row.Address += PCOffset; |
| 689 | if (OS) |
| 690 | *OS |
| 691 | << format(" (0x%16.16" PRIx64 ")", PCOffset); |
| 692 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 693 | break; |
| 694 | |
| 695 | case DW_LNS_set_prologue_end: |
| 696 | // Takes no arguments. Set the prologue_end register of the |
| 697 | // state machine to true |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 698 | State.Row.PrologueEnd = true; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 699 | break; |
| 700 | |
| 701 | case DW_LNS_set_epilogue_begin: |
| 702 | // Takes no arguments. Set the basic_block register of the |
| 703 | // state machine to true |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 704 | State.Row.EpilogueBegin = true; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 705 | break; |
| 706 | |
| 707 | case DW_LNS_set_isa: |
| 708 | // Takes a single unsigned LEB128 operand and stores it in the |
| 709 | // column register of the state machine. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 710 | State.Row.Isa = DebugLineData.getULEB128(OffsetPtr); |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 711 | if (OS) |
| 712 | *OS << " (" << State.Row.Isa << ")"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 713 | break; |
| 714 | |
| 715 | default: |
| 716 | // Handle any unknown standard opcodes here. We know the lengths |
| 717 | // of such opcodes because they are specified in the prologue |
| 718 | // as a multiple of LEB128 operands for each opcode. |
| 719 | { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 720 | assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size()); |
| 721 | uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1]; |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 722 | for (uint8_t I = 0; I < OpcodeLength; ++I) { |
| 723 | uint64_t Value = DebugLineData.getULEB128(OffsetPtr); |
| 724 | if (OS) |
| 725 | *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n", |
| 726 | Value); |
| 727 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 728 | } |
| 729 | break; |
| 730 | } |
| 731 | } else { |
| 732 | // Special Opcodes |
| 733 | |
| 734 | // A special opcode value is chosen based on the amount that needs |
| 735 | // to be added to the line and address registers. The maximum line |
| 736 | // increment for a special opcode is the value of the line_base |
| 737 | // field in the header, plus the value of the line_range field, |
| 738 | // minus 1 (line base + line range - 1). If the desired line |
| 739 | // increment is greater than the maximum line increment, a standard |
NAKAMURA Takumi | f995985 | 2011-10-08 11:22:47 +0000 | [diff] [blame] | 740 | // opcode must be used instead of a special opcode. The "address |
| 741 | // advance" is calculated by dividing the desired address increment |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 742 | // by the minimum_instruction_length field from the header. The |
| 743 | // special opcode is then calculated using the following formula: |
| 744 | // |
| 745 | // opcode = (desired line increment - line_base) + |
| 746 | // (line_range * address advance) + opcode_base |
| 747 | // |
| 748 | // If the resulting opcode is greater than 255, a standard opcode |
| 749 | // must be used instead. |
| 750 | // |
| 751 | // To decode a special opcode, subtract the opcode_base from the |
| 752 | // opcode itself to give the adjusted opcode. The amount to |
| 753 | // increment the address register is the result of the adjusted |
| 754 | // opcode divided by the line_range multiplied by the |
| 755 | // minimum_instruction_length field from the header. That is: |
| 756 | // |
| 757 | // address increment = (adjusted opcode / line_range) * |
| 758 | // minimum_instruction_length |
| 759 | // |
| 760 | // The amount to increment the line register is the line_base plus |
| 761 | // the result of the adjusted opcode modulo the line_range. That is: |
| 762 | // |
| 763 | // line increment = line_base + (adjusted opcode % line_range) |
| 764 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 765 | uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase; |
| 766 | uint64_t AddrOffset = |
| 767 | (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength; |
| 768 | int32_t LineOffset = |
| 769 | Prologue.LineBase + (AdjustOpcode % Prologue.LineRange); |
| 770 | State.Row.Line += LineOffset; |
| 771 | State.Row.Address += AddrOffset; |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 772 | |
| 773 | if (OS) { |
| 774 | *OS << "address += " << ((uint32_t)AdjustOpcode) |
| 775 | << ", line += " << LineOffset << "\n"; |
| 776 | OS->indent(12); |
| 777 | State.Row.dump(*OS); |
| 778 | } |
| 779 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 780 | State.appendRowToMatrix(*OffsetPtr); |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 781 | // Reset discriminator to 0. |
| 782 | State.Row.Discriminator = 0; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 783 | } |
Jonas Devlieghere | 26f9a0c | 2017-09-21 20:15:30 +0000 | [diff] [blame] | 784 | if(OS) |
| 785 | *OS << "\n"; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Alexey Samsonov | 110d595 | 2014-04-30 00:09:19 +0000 | [diff] [blame] | 788 | if (!State.Sequence.Empty) { |
| 789 | fprintf(stderr, "warning: last sequence in debug line table is not" |
| 790 | "terminated!\n"); |
| 791 | } |
| 792 | |
| 793 | // Sort all sequences so that address lookup will work faster. |
| 794 | if (!Sequences.empty()) { |
| 795 | std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC); |
| 796 | // Note: actually, instruction address ranges of sequences should not |
| 797 | // overlap (in shared objects and executables). If they do, the address |
| 798 | // lookup would still work, though, but result would be ambiguous. |
| 799 | // We don't report warning in this case. For example, |
| 800 | // sometimes .so compiled from multiple object files contains a few |
| 801 | // rudimentary sequences for address ranges [0x0, 0xsomething). |
| 802 | } |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 803 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 804 | return EndOffset; |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 807 | uint32_t |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 808 | DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq, |
| 809 | uint64_t Address) const { |
| 810 | if (!Seq.containsPC(Address)) |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 811 | return UnknownRowIndex; |
| 812 | // Search for instruction address in the rows describing the sequence. |
| 813 | // Rows are stored in a vector, so we may use arithmetical operations with |
| 814 | // iterators. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 815 | DWARFDebugLine::Row Row; |
| 816 | Row.Address = Address; |
| 817 | RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex; |
| 818 | RowIter LastRow = Rows.begin() + Seq.LastRowIndex; |
| 819 | LineTable::RowIter RowPos = std::lower_bound( |
| 820 | FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress); |
| 821 | if (RowPos == LastRow) { |
| 822 | return Seq.LastRowIndex - 1; |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 823 | } |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 824 | uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow); |
| 825 | if (RowPos->Address > Address) { |
| 826 | if (RowPos == FirstRow) |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 827 | return UnknownRowIndex; |
| 828 | else |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 829 | Index--; |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 830 | } |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 831 | return Index; |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 834 | uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const { |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 835 | if (Sequences.empty()) |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 836 | return UnknownRowIndex; |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 837 | // First, find an instruction sequence containing the given address. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 838 | DWARFDebugLine::Sequence Sequence; |
| 839 | Sequence.LowPC = Address; |
| 840 | SequenceIter FirstSeq = Sequences.begin(); |
| 841 | SequenceIter LastSeq = Sequences.end(); |
| 842 | SequenceIter SeqPos = std::lower_bound( |
| 843 | FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC); |
| 844 | DWARFDebugLine::Sequence FoundSeq; |
| 845 | if (SeqPos == LastSeq) { |
| 846 | FoundSeq = Sequences.back(); |
| 847 | } else if (SeqPos->LowPC == Address) { |
| 848 | FoundSeq = *SeqPos; |
Alexey Samsonov | 947228c | 2012-08-07 11:46:57 +0000 | [diff] [blame] | 849 | } else { |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 850 | if (SeqPos == FirstSeq) |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 851 | return UnknownRowIndex; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 852 | FoundSeq = *(SeqPos - 1); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 853 | } |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 854 | return findRowInSeq(FoundSeq, Address); |
Benjamin Kramer | 5acab50 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 855 | } |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 856 | |
Alexey Samsonov | 836b1ae | 2014-04-29 21:28:13 +0000 | [diff] [blame] | 857 | bool DWARFDebugLine::LineTable::lookupAddressRange( |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 858 | uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const { |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 859 | if (Sequences.empty()) |
| 860 | return false; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 861 | uint64_t EndAddr = Address + Size; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 862 | // First, find an instruction sequence containing the given address. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 863 | DWARFDebugLine::Sequence Sequence; |
| 864 | Sequence.LowPC = Address; |
| 865 | SequenceIter FirstSeq = Sequences.begin(); |
| 866 | SequenceIter LastSeq = Sequences.end(); |
| 867 | SequenceIter SeqPos = std::lower_bound( |
| 868 | FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC); |
| 869 | if (SeqPos == LastSeq || SeqPos->LowPC != Address) { |
| 870 | if (SeqPos == FirstSeq) |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 871 | return false; |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 872 | SeqPos--; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 873 | } |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 874 | if (!SeqPos->containsPC(Address)) |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 875 | return false; |
| 876 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 877 | SequenceIter StartPos = SeqPos; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 878 | |
| 879 | // Add the rows from the first sequence to the vector, starting with the |
| 880 | // index we just calculated |
| 881 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 882 | while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) { |
| 883 | const DWARFDebugLine::Sequence &CurSeq = *SeqPos; |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 884 | // For the first sequence, we need to find which row in the sequence is the |
| 885 | // first in our range. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 886 | uint32_t FirstRowIndex = CurSeq.FirstRowIndex; |
| 887 | if (SeqPos == StartPos) |
| 888 | FirstRowIndex = findRowInSeq(CurSeq, Address); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 889 | |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 890 | // Figure out the last row in the range. |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 891 | uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1); |
| 892 | if (LastRowIndex == UnknownRowIndex) |
| 893 | LastRowIndex = CurSeq.LastRowIndex - 1; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 894 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 895 | assert(FirstRowIndex != UnknownRowIndex); |
| 896 | assert(LastRowIndex != UnknownRowIndex); |
Keno Fischer | c2c6018 | 2015-05-31 23:37:04 +0000 | [diff] [blame] | 897 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 898 | for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) { |
| 899 | Result.push_back(I); |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 902 | ++SeqPos; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 903 | } |
NAKAMURA Takumi | 4b86cdb | 2013-01-26 01:45:06 +0000 | [diff] [blame] | 904 | |
| 905 | return true; |
Andrew Kaylor | 9a8ff81 | 2013-01-26 00:28:05 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 908 | bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const { |
Pete Cooper | b2ba776 | 2016-07-22 01:41:32 +0000 | [diff] [blame] | 909 | return FileIndex != 0 && FileIndex <= Prologue.FileNames.size(); |
| 910 | } |
| 911 | |
Paul Robinson | 9d4eb69 | 2017-05-01 23:27:55 +0000 | [diff] [blame] | 912 | bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex, |
| 913 | const char *CompDir, |
| 914 | FileLineInfoKind Kind, |
| 915 | std::string &Result) const { |
Pete Cooper | b2ba776 | 2016-07-22 01:41:32 +0000 | [diff] [blame] | 916 | if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex)) |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 917 | return false; |
| 918 | const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1]; |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 919 | StringRef FileName = Entry.Name.getAsCString().getValue(); |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 920 | if (Kind != FileLineInfoKind::AbsoluteFilePath || |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 921 | sys::path::is_absolute(FileName)) { |
| 922 | Result = FileName; |
| 923 | return true; |
| 924 | } |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 925 | |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 926 | SmallString<16> FilePath; |
| 927 | uint64_t IncludeDirIndex = Entry.DirIdx; |
Paul Robinson | ba1c915 | 2017-05-02 17:37:32 +0000 | [diff] [blame] | 928 | StringRef IncludeDir; |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 929 | // Be defensive about the contents of Entry. |
| 930 | if (IncludeDirIndex > 0 && |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 931 | IncludeDirIndex <= Prologue.IncludeDirectories.size()) |
Paul Robinson | 0a22709 | 2018-02-05 20:43:15 +0000 | [diff] [blame^] | 932 | IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1] |
| 933 | .getAsCString() |
| 934 | .getValue(); |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 935 | |
| 936 | // We may still need to append compilation directory of compile unit. |
| 937 | // We know that FileName is not absolute, the only way to have an |
| 938 | // absolute path at this point would be if IncludeDir is absolute. |
| 939 | if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath && |
| 940 | sys::path::is_relative(IncludeDir)) |
| 941 | sys::path::append(FilePath, CompDir); |
| 942 | |
| 943 | // sys::path::append skips empty strings. |
| 944 | sys::path::append(FilePath, IncludeDir, FileName); |
Alexey Samsonov | 45be793 | 2012-08-30 07:49:50 +0000 | [diff] [blame] | 945 | Result = FilePath.str(); |
| 946 | return true; |
| 947 | } |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 948 | |
Dehao Chen | 1b54fce | 2016-04-28 22:09:37 +0000 | [diff] [blame] | 949 | bool DWARFDebugLine::LineTable::getFileLineInfoForAddress( |
| 950 | uint64_t Address, const char *CompDir, FileLineInfoKind Kind, |
| 951 | DILineInfo &Result) const { |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 952 | // Get the index of row we're looking for in the line table. |
| 953 | uint32_t RowIndex = lookupAddress(Address); |
| 954 | if (RowIndex == -1U) |
| 955 | return false; |
| 956 | // Take file number and line/column from the row. |
| 957 | const auto &Row = Rows[RowIndex]; |
| 958 | if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName)) |
| 959 | return false; |
| 960 | Result.Line = Row.Line; |
| 961 | Result.Column = Row.Column; |
Eric Christopher | ba1024c | 2016-12-14 18:29:39 +0000 | [diff] [blame] | 962 | Result.Discriminator = Row.Discriminator; |
Frederic Riss | 101b5e2 | 2014-09-19 15:11:51 +0000 | [diff] [blame] | 963 | return true; |
| 964 | } |