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