Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 1 | //===-- DWARFDebugLine.cpp ------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "DWARFDebugLine.h" |
| 11 | #include "llvm/Support/Dwarf.h" |
| 12 | #include "llvm/Support/Format.h" |
| 13 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 7393c7f | 2011-09-15 02:19:33 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 15 | using namespace llvm; |
| 16 | using namespace dwarf; |
| 17 | |
| 18 | void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const { |
| 19 | OS << "Line table prologue:\n" |
| 20 | << format(" total_length: 0x%8.8x\n", TotalLength) |
| 21 | << format(" version: %u\n", Version) |
| 22 | << format("prologue_length: 0x%8.8x\n", PrologueLength) |
| 23 | << format("min_inst_length: %u\n", MinInstLength) |
| 24 | << format("default_is_stmt: %u\n", DefaultIsStmt) |
| 25 | << format(" line_base: %i\n", LineBase) |
| 26 | << format(" line_range: %u\n", LineRange) |
| 27 | << format(" opcode_base: %u\n", OpcodeBase); |
| 28 | |
| 29 | for (uint32_t i = 0; i < StandardOpcodeLengths.size(); ++i) |
| 30 | OS << format("standard_opcode_lengths[%s] = %u\n", LNStandardString(i+1), |
| 31 | StandardOpcodeLengths[i]); |
| 32 | |
| 33 | if (!IncludeDirectories.empty()) |
| 34 | for (uint32_t i = 0; i < IncludeDirectories.size(); ++i) |
| 35 | OS << format("include_directories[%3u] = '", i+1) |
| 36 | << IncludeDirectories[i] << "'\n"; |
| 37 | |
| 38 | if (!FileNames.empty()) { |
| 39 | OS << " Dir Mod Time File Len File Name\n" |
| 40 | << " ---- ---------- ---------- -----------" |
| 41 | "----------------\n"; |
| 42 | for (uint32_t i = 0; i < FileNames.size(); ++i) { |
| 43 | const FileNameEntry& fileEntry = FileNames[i]; |
| 44 | OS << format("file_names[%3u] %4u ", i+1, fileEntry.DirIdx) |
| 45 | << format("0x%8.8x 0x%8.8x ", fileEntry.ModTime, fileEntry.Length) |
| 46 | << fileEntry.Name << '\n'; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void DWARFDebugLine::Row::postAppend() { |
| 52 | BasicBlock = false; |
| 53 | PrologueEnd = false; |
| 54 | EpilogueBegin = false; |
| 55 | } |
| 56 | |
| 57 | void DWARFDebugLine::Row::reset(bool default_is_stmt) { |
| 58 | Address = 0; |
| 59 | Line = 1; |
| 60 | Column = 0; |
| 61 | File = 1; |
| 62 | Isa = 0; |
| 63 | IsStmt = default_is_stmt; |
| 64 | BasicBlock = false; |
| 65 | EndSequence = false; |
| 66 | PrologueEnd = false; |
| 67 | EpilogueBegin = false; |
| 68 | } |
| 69 | |
| 70 | void DWARFDebugLine::Row::dump(raw_ostream &OS) const { |
| 71 | OS << format("0x%16.16llx %6u %6u", Address, Line, Column) |
| 72 | << format(" %6u %3u ", File, Isa) |
| 73 | << (IsStmt ? " is_stmt" : "") |
| 74 | << (BasicBlock ? " basic_block" : "") |
| 75 | << (PrologueEnd ? " prologue_end" : "") |
| 76 | << (EpilogueBegin ? " epilogue_begin" : "") |
| 77 | << (EndSequence ? " end_sequence" : "") |
| 78 | << '\n'; |
| 79 | } |
| 80 | |
| 81 | void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const { |
| 82 | Prologue.dump(OS); |
| 83 | OS << '\n'; |
| 84 | |
| 85 | if (!Rows.empty()) { |
| 86 | OS << "Address Line Column File ISA Flags\n" |
| 87 | << "------------------ ------ ------ ------ --- -------------\n"; |
| 88 | for (std::vector<Row>::const_iterator pos = Rows.begin(), |
| 89 | end = Rows.end(); pos != end; ++pos) |
| 90 | pos->dump(OS); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void DWARFDebugLine::State::appendRowToMatrix(uint32_t offset) { |
| 95 | ++row; // Increase the row number. |
| 96 | LineTable::appendRow(*this); |
| 97 | Row::postAppend(); |
| 98 | } |
| 99 | |
| 100 | void DWARFDebugLine::parse(const DataExtractor debug_line_data) { |
| 101 | LineTableMap.clear(); |
| 102 | uint32_t offset = 0; |
| 103 | State state; |
| 104 | while (debug_line_data.isValidOffset(offset)) { |
| 105 | const uint32_t debug_line_offset = offset; |
| 106 | |
| 107 | if (parseStatementTable(debug_line_data, &offset, state)) { |
| 108 | // Make sure we don't don't loop infinitely |
| 109 | if (offset <= debug_line_offset) |
| 110 | break; |
| 111 | |
| 112 | LineTableMap[debug_line_offset] = state; |
| 113 | state.reset(); |
| 114 | } |
| 115 | else |
| 116 | ++offset; // Try next byte in line table |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void DWARFDebugLine::DumpingState::finalize(uint32_t offset) { |
| 121 | LineTable::dump(OS); |
| 122 | } |
| 123 | |
| 124 | void DWARFDebugLine::dump(const DataExtractor debug_line_data, raw_ostream &OS){ |
| 125 | uint32_t offset = 0; |
| 126 | DumpingState state(OS); |
| 127 | while (debug_line_data.isValidOffset(offset)) { |
| 128 | const uint32_t debug_line_offset = offset; |
| 129 | |
| 130 | if (parseStatementTable(debug_line_data, &offset, state)) { |
| 131 | // Make sure we don't don't loop infinitely |
| 132 | if (offset <= debug_line_offset) |
| 133 | break; |
| 134 | |
| 135 | state.reset(); |
| 136 | } |
| 137 | else |
| 138 | ++offset; // Try next byte in line table |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | const DWARFDebugLine::LineTable * |
| 143 | DWARFDebugLine::getLineTable(uint32_t offset) const { |
| 144 | LineTableConstIter pos = LineTableMap.find(offset); |
| 145 | if (pos != LineTableMap.end()) |
| 146 | return &pos->second; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | bool |
| 151 | DWARFDebugLine::parsePrologue(DataExtractor debug_line_data, |
| 152 | uint32_t *offset_ptr, Prologue *prologue) { |
| 153 | const uint32_t prologue_offset = *offset_ptr; |
| 154 | |
| 155 | prologue->clear(); |
| 156 | prologue->TotalLength = debug_line_data.getU32(offset_ptr); |
| 157 | prologue->Version = debug_line_data.getU16(offset_ptr); |
| 158 | if (prologue->Version != 2) |
| 159 | return false; |
| 160 | |
| 161 | prologue->PrologueLength = debug_line_data.getU32(offset_ptr); |
| 162 | const uint32_t end_prologue_offset = prologue->PrologueLength + *offset_ptr; |
| 163 | prologue->MinInstLength = debug_line_data.getU8(offset_ptr); |
| 164 | prologue->DefaultIsStmt = debug_line_data.getU8(offset_ptr); |
| 165 | prologue->LineBase = debug_line_data.getU8(offset_ptr); |
| 166 | prologue->LineRange = debug_line_data.getU8(offset_ptr); |
| 167 | prologue->OpcodeBase = debug_line_data.getU8(offset_ptr); |
| 168 | |
| 169 | prologue->StandardOpcodeLengths.reserve(prologue->OpcodeBase-1); |
| 170 | for (uint32_t i = 1; i < prologue->OpcodeBase; ++i) { |
| 171 | uint8_t op_len = debug_line_data.getU8(offset_ptr); |
| 172 | prologue->StandardOpcodeLengths.push_back(op_len); |
| 173 | } |
| 174 | |
| 175 | while (*offset_ptr < end_prologue_offset) { |
| 176 | const char *s = debug_line_data.getCStr(offset_ptr); |
| 177 | if (s && s[0]) |
| 178 | prologue->IncludeDirectories.push_back(s); |
| 179 | else |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | while (*offset_ptr < end_prologue_offset) { |
| 184 | const char *name = debug_line_data.getCStr(offset_ptr); |
| 185 | if (name && name[0]) { |
| 186 | FileNameEntry fileEntry; |
| 187 | fileEntry.Name = name; |
| 188 | fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr); |
| 189 | fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr); |
| 190 | fileEntry.Length = debug_line_data.getULEB128(offset_ptr); |
| 191 | prologue->FileNames.push_back(fileEntry); |
| 192 | } else { |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (*offset_ptr != end_prologue_offset) { |
| 198 | fprintf(stderr, "warning: parsing line table prologue at 0x%8.8x should" |
| 199 | " have ended at 0x%8.8x but it ended ad 0x%8.8x\n", |
| 200 | prologue_offset, end_prologue_offset, *offset_ptr); |
| 201 | } |
| 202 | return end_prologue_offset; |
| 203 | } |
| 204 | |
| 205 | bool |
| 206 | DWARFDebugLine::parseStatementTable(DataExtractor debug_line_data, |
| 207 | uint32_t *offset_ptr, State &state) { |
| 208 | const uint32_t debug_line_offset = *offset_ptr; |
| 209 | |
| 210 | Prologue *prologue = &state.Prologue; |
| 211 | |
| 212 | if (!parsePrologue(debug_line_data, offset_ptr, prologue)) { |
| 213 | // Restore our offset and return false to indicate failure! |
| 214 | *offset_ptr = debug_line_offset; |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | const uint32_t end_offset = debug_line_offset + prologue->TotalLength + |
| 219 | sizeof(prologue->TotalLength); |
| 220 | |
| 221 | while (*offset_ptr < end_offset) { |
| 222 | uint8_t opcode = debug_line_data.getU8(offset_ptr); |
| 223 | |
| 224 | if (opcode == 0) { |
| 225 | // Extended Opcodes always start with a zero opcode followed by |
| 226 | // a uleb128 length so you can skip ones you don't know about |
| 227 | uint32_t ext_offset = *offset_ptr; |
| 228 | uint64_t len = debug_line_data.getULEB128(offset_ptr); |
| 229 | uint32_t arg_size = len - (*offset_ptr - ext_offset); |
| 230 | |
| 231 | uint8_t sub_opcode = debug_line_data.getU8(offset_ptr); |
| 232 | switch (sub_opcode) { |
| 233 | case DW_LNE_end_sequence: |
| 234 | // Set the end_sequence register of the state machine to true and |
| 235 | // append a row to the matrix using the current values of the |
| 236 | // state-machine registers. Then reset the registers to the initial |
| 237 | // values specified above. Every statement program sequence must end |
| 238 | // with a DW_LNE_end_sequence instruction which creates a row whose |
| 239 | // address is that of the byte after the last target machine instruction |
| 240 | // of the sequence. |
| 241 | state.EndSequence = true; |
| 242 | state.appendRowToMatrix(*offset_ptr); |
| 243 | state.reset(); |
| 244 | break; |
| 245 | |
| 246 | case DW_LNE_set_address: |
| 247 | // Takes a single relocatable address as an operand. The size of the |
| 248 | // operand is the size appropriate to hold an address on the target |
| 249 | // machine. Set the address register to the value given by the |
| 250 | // relocatable address. All of the other statement program opcodes |
| 251 | // that affect the address register add a delta to it. This instruction |
| 252 | // stores a relocatable value into it instead. |
| 253 | state.Address = debug_line_data.getAddress(offset_ptr); |
| 254 | break; |
| 255 | |
| 256 | case DW_LNE_define_file: |
| 257 | // Takes 4 arguments. The first is a null terminated string containing |
| 258 | // a source file name. The second is an unsigned LEB128 number |
| 259 | // representing the directory index of the directory in which the file |
| 260 | // was found. The third is an unsigned LEB128 number representing the |
| 261 | // time of last modification of the file. The fourth is an unsigned |
| 262 | // LEB128 number representing the length in bytes of the file. The time |
| 263 | // and length fields may contain LEB128(0) if the information is not |
| 264 | // available. |
| 265 | // |
| 266 | // The directory index represents an entry in the include_directories |
| 267 | // section of the statement program prologue. The index is LEB128(0) |
| 268 | // if the file was found in the current directory of the compilation, |
| 269 | // LEB128(1) if it was found in the first directory in the |
| 270 | // include_directories section, and so on. The directory index is |
| 271 | // ignored for file names that represent full path names. |
| 272 | // |
| 273 | // The files are numbered, starting at 1, in the order in which they |
| 274 | // appear; the names in the prologue come before names defined by |
| 275 | // the DW_LNE_define_file instruction. These numbers are used in the |
| 276 | // the file register of the state machine. |
| 277 | { |
| 278 | FileNameEntry fileEntry; |
| 279 | fileEntry.Name = debug_line_data.getCStr(offset_ptr); |
| 280 | fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr); |
| 281 | fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr); |
| 282 | fileEntry.Length = debug_line_data.getULEB128(offset_ptr); |
| 283 | prologue->FileNames.push_back(fileEntry); |
| 284 | } |
| 285 | break; |
| 286 | |
| 287 | default: |
| 288 | // Length doesn't include the zero opcode byte or the length itself, but |
| 289 | // it does include the sub_opcode, so we have to adjust for that below |
| 290 | (*offset_ptr) += arg_size; |
| 291 | break; |
| 292 | } |
| 293 | } else if (opcode < prologue->OpcodeBase) { |
| 294 | switch (opcode) { |
| 295 | // Standard Opcodes |
| 296 | case DW_LNS_copy: |
| 297 | // Takes no arguments. Append a row to the matrix using the |
| 298 | // current values of the state-machine registers. Then set |
| 299 | // the basic_block register to false. |
| 300 | state.appendRowToMatrix(*offset_ptr); |
| 301 | break; |
| 302 | |
| 303 | case DW_LNS_advance_pc: |
| 304 | // Takes a single unsigned LEB128 operand, multiplies it by the |
| 305 | // min_inst_length field of the prologue, and adds the |
| 306 | // result to the address register of the state machine. |
| 307 | state.Address += debug_line_data.getULEB128(offset_ptr) * |
| 308 | prologue->MinInstLength; |
| 309 | break; |
| 310 | |
| 311 | case DW_LNS_advance_line: |
| 312 | // Takes a single signed LEB128 operand and adds that value to |
| 313 | // the line register of the state machine. |
| 314 | state.Line += debug_line_data.getSLEB128(offset_ptr); |
| 315 | break; |
| 316 | |
| 317 | case DW_LNS_set_file: |
| 318 | // Takes a single unsigned LEB128 operand and stores it in the file |
| 319 | // register of the state machine. |
| 320 | state.File = debug_line_data.getULEB128(offset_ptr); |
| 321 | break; |
| 322 | |
| 323 | case DW_LNS_set_column: |
| 324 | // Takes a single unsigned LEB128 operand and stores it in the |
| 325 | // column register of the state machine. |
| 326 | state.Column = debug_line_data.getULEB128(offset_ptr); |
| 327 | break; |
| 328 | |
| 329 | case DW_LNS_negate_stmt: |
| 330 | // Takes no arguments. Set the is_stmt register of the state |
| 331 | // machine to the logical negation of its current value. |
| 332 | state.IsStmt = !state.IsStmt; |
| 333 | break; |
| 334 | |
| 335 | case DW_LNS_set_basic_block: |
| 336 | // Takes no arguments. Set the basic_block register of the |
| 337 | // state machine to true |
| 338 | state.BasicBlock = true; |
| 339 | break; |
| 340 | |
| 341 | case DW_LNS_const_add_pc: |
| 342 | // Takes no arguments. Add to the address register of the state |
| 343 | // machine the address increment value corresponding to special |
| 344 | // opcode 255. The motivation for DW_LNS_const_add_pc is this: |
| 345 | // when the statement program needs to advance the address by a |
| 346 | // small amount, it can use a single special opcode, which occupies |
| 347 | // a single byte. When it needs to advance the address by up to |
| 348 | // twice the range of the last special opcode, it can use |
| 349 | // DW_LNS_const_add_pc followed by a special opcode, for a total |
| 350 | // of two bytes. Only if it needs to advance the address by more |
| 351 | // than twice that range will it need to use both DW_LNS_advance_pc |
| 352 | // and a special opcode, requiring three or more bytes. |
| 353 | { |
| 354 | uint8_t adjust_opcode = 255 - prologue->OpcodeBase; |
| 355 | uint64_t addr_offset = (adjust_opcode / prologue->LineRange) * |
| 356 | prologue->MinInstLength; |
| 357 | state.Address += addr_offset; |
| 358 | } |
| 359 | break; |
| 360 | |
| 361 | case DW_LNS_fixed_advance_pc: |
| 362 | // Takes a single uhalf operand. Add to the address register of |
| 363 | // the state machine the value of the (unencoded) operand. This |
| 364 | // is the only extended opcode that takes an argument that is not |
| 365 | // a variable length number. The motivation for DW_LNS_fixed_advance_pc |
| 366 | // is this: existing assemblers cannot emit DW_LNS_advance_pc or |
| 367 | // special opcodes because they cannot encode LEB128 numbers or |
| 368 | // judge when the computation of a special opcode overflows and |
| 369 | // requires the use of DW_LNS_advance_pc. Such assemblers, however, |
| 370 | // can use DW_LNS_fixed_advance_pc instead, sacrificing compression. |
| 371 | state.Address += debug_line_data.getU16(offset_ptr); |
| 372 | break; |
| 373 | |
| 374 | case DW_LNS_set_prologue_end: |
| 375 | // Takes no arguments. Set the prologue_end register of the |
| 376 | // state machine to true |
| 377 | state.PrologueEnd = true; |
| 378 | break; |
| 379 | |
| 380 | case DW_LNS_set_epilogue_begin: |
| 381 | // Takes no arguments. Set the basic_block register of the |
| 382 | // state machine to true |
| 383 | state.EpilogueBegin = true; |
| 384 | break; |
| 385 | |
| 386 | case DW_LNS_set_isa: |
| 387 | // Takes a single unsigned LEB128 operand and stores it in the |
| 388 | // column register of the state machine. |
| 389 | state.Isa = debug_line_data.getULEB128(offset_ptr); |
| 390 | break; |
| 391 | |
| 392 | default: |
| 393 | // Handle any unknown standard opcodes here. We know the lengths |
| 394 | // of such opcodes because they are specified in the prologue |
| 395 | // as a multiple of LEB128 operands for each opcode. |
| 396 | { |
Benjamin Kramer | 068d9a5 | 2011-09-15 03:20:04 +0000 | [diff] [blame^] | 397 | assert(opcode - 1U < prologue->StandardOpcodeLengths.size()); |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 398 | uint8_t opcode_length = prologue->StandardOpcodeLengths[opcode - 1]; |
| 399 | for (uint8_t i=0; i<opcode_length; ++i) |
| 400 | debug_line_data.getULEB128(offset_ptr); |
| 401 | } |
| 402 | break; |
| 403 | } |
| 404 | } else { |
| 405 | // Special Opcodes |
| 406 | |
| 407 | // A special opcode value is chosen based on the amount that needs |
| 408 | // to be added to the line and address registers. The maximum line |
| 409 | // increment for a special opcode is the value of the line_base |
| 410 | // field in the header, plus the value of the line_range field, |
| 411 | // minus 1 (line base + line range - 1). If the desired line |
| 412 | // increment is greater than the maximum line increment, a standard |
| 413 | // opcode must be used instead of a special opcode. The “address |
| 414 | // advance” is calculated by dividing the desired address increment |
| 415 | // by the minimum_instruction_length field from the header. The |
| 416 | // special opcode is then calculated using the following formula: |
| 417 | // |
| 418 | // opcode = (desired line increment - line_base) + |
| 419 | // (line_range * address advance) + opcode_base |
| 420 | // |
| 421 | // If the resulting opcode is greater than 255, a standard opcode |
| 422 | // must be used instead. |
| 423 | // |
| 424 | // To decode a special opcode, subtract the opcode_base from the |
| 425 | // opcode itself to give the adjusted opcode. The amount to |
| 426 | // increment the address register is the result of the adjusted |
| 427 | // opcode divided by the line_range multiplied by the |
| 428 | // minimum_instruction_length field from the header. That is: |
| 429 | // |
| 430 | // address increment = (adjusted opcode / line_range) * |
| 431 | // minimum_instruction_length |
| 432 | // |
| 433 | // The amount to increment the line register is the line_base plus |
| 434 | // the result of the adjusted opcode modulo the line_range. That is: |
| 435 | // |
| 436 | // line increment = line_base + (adjusted opcode % line_range) |
| 437 | |
| 438 | uint8_t adjust_opcode = opcode - prologue->OpcodeBase; |
| 439 | uint64_t addr_offset = (adjust_opcode / prologue->LineRange) * |
| 440 | prologue->MinInstLength; |
| 441 | int32_t line_offset = prologue->LineBase + |
| 442 | (adjust_opcode % prologue->LineRange); |
| 443 | state.Line += line_offset; |
| 444 | state.Address += addr_offset; |
| 445 | state.appendRowToMatrix(*offset_ptr); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | state.finalize(*offset_ptr); |
| 450 | |
| 451 | return end_offset; |
| 452 | } |
| 453 | |
| 454 | static bool findMatchingAddress(const DWARFDebugLine::Row& row1, |
| 455 | const DWARFDebugLine::Row& row2) { |
| 456 | return row1.Address < row2.Address; |
| 457 | } |
| 458 | |
| 459 | uint32_t |
| 460 | DWARFDebugLine::LineTable::lookupAddress(uint64_t address, |
| 461 | uint64_t cu_high_pc) const { |
| 462 | uint32_t index = UINT32_MAX; |
| 463 | if (!Rows.empty()) { |
| 464 | // Use the lower_bound algorithm to perform a binary search since we know |
| 465 | // that our line table data is ordered by address. |
| 466 | DWARFDebugLine::Row row; |
| 467 | row.Address = address; |
| 468 | typedef std::vector<Row>::const_iterator iterator; |
| 469 | iterator begin_pos = Rows.begin(); |
| 470 | iterator end_pos = Rows.end(); |
| 471 | iterator pos = std::lower_bound(begin_pos, end_pos, row, |
| 472 | findMatchingAddress); |
| 473 | if (pos == end_pos) { |
| 474 | if (address < cu_high_pc) |
| 475 | return Rows.size()-1; |
| 476 | } else { |
| 477 | // Rely on fact that we are using a std::vector and we can do |
| 478 | // pointer arithmetic to find the row index (which will be one less |
| 479 | // that what we found since it will find the first position after |
| 480 | // the current address) since std::vector iterators are just |
| 481 | // pointers to the container type. |
| 482 | index = pos - begin_pos; |
| 483 | if (pos->Address > address) { |
| 484 | if (index > 0) |
| 485 | --index; |
| 486 | else |
| 487 | index = UINT32_MAX; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | return index; // Failed to find address. |
| 492 | } |