1 //===-- DWARFDebugLine.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFDebugLine.h"
12 //#define ENABLE_DEBUG_PRINTF // DO NOT LEAVE THIS DEFINED: DEBUG ONLY!!!
15 #include "lldb/Core/FileSpecList.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Host/Host.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Timer.h"
21 #include "LogChannelDWARF.h"
22 #include "SymbolFileDWARF.h"
25 using namespace lldb_private;
28 //----------------------------------------------------------------------
31 // Parse all information in the debug_line_data into an internal
33 //----------------------------------------------------------------------
34 void DWARFDebugLine::Parse(const DWARFDataExtractor &debug_line_data) {
35 m_lineTableMap.clear();
36 lldb::offset_t offset = 0;
37 LineTable::shared_ptr line_table_sp(new LineTable);
38 while (debug_line_data.ValidOffset(offset)) {
39 const lldb::offset_t debug_line_offset = offset;
41 if (line_table_sp.get() == NULL)
44 if (ParseStatementTable(debug_line_data, &offset, line_table_sp.get())) {
45 // Make sure we don't don't loop infinitely
46 if (offset <= debug_line_offset)
48 // DEBUG_PRINTF("m_lineTableMap[0x%8.8x] = line_table_sp\n",
49 // debug_line_offset);
50 m_lineTableMap[debug_line_offset] = line_table_sp;
51 line_table_sp.reset(new LineTable);
53 ++offset; // Try next byte in line table
57 void DWARFDebugLine::ParseIfNeeded(const DWARFDataExtractor &debug_line_data) {
58 if (m_lineTableMap.empty())
59 Parse(debug_line_data);
62 //----------------------------------------------------------------------
63 // DWARFDebugLine::GetLineTable
64 //----------------------------------------------------------------------
65 DWARFDebugLine::LineTable::shared_ptr
66 DWARFDebugLine::GetLineTable(const dw_offset_t offset) const {
67 DWARFDebugLine::LineTable::shared_ptr line_table_shared_ptr;
68 LineTableConstIter pos = m_lineTableMap.find(offset);
69 if (pos != m_lineTableMap.end())
70 line_table_shared_ptr = pos->second;
71 return line_table_shared_ptr;
74 //----------------------------------------------------------------------
76 //----------------------------------------------------------------------
77 static void DumpStateToFile(dw_offset_t offset,
78 const DWARFDebugLine::State &state,
80 Log *log = (Log *)userData;
81 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
82 // If the row is zero we are being called with the prologue only
83 state.prologue->Dump(log);
84 log->PutCString("Address Line Column File");
85 log->PutCString("------------------ ------ ------ ------");
86 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
87 // Done parsing line table
89 log->Printf("0x%16.16" PRIx64 " %6u %6u %6u%s\n", state.address, state.line,
90 state.column, state.file, state.end_sequence ? " END" : "");
94 //----------------------------------------------------------------------
95 // DWARFDebugLine::DumpLineTableRows
96 //----------------------------------------------------------------------
97 bool DWARFDebugLine::DumpLineTableRows(Log *log, SymbolFileDWARF *dwarf2Data,
98 dw_offset_t debug_line_offset) {
99 const DWARFDataExtractor &debug_line_data = dwarf2Data->get_debug_line_data();
101 if (debug_line_offset == DW_INVALID_OFFSET) {
102 // Dump line table to a single file only
103 debug_line_offset = 0;
104 while (debug_line_data.ValidOffset(debug_line_offset))
106 DumpStatementTable(log, debug_line_data, debug_line_offset);
108 // Dump line table to a single file only
109 DumpStatementTable(log, debug_line_data, debug_line_offset);
114 //----------------------------------------------------------------------
115 // DWARFDebugLine::DumpStatementTable
116 //----------------------------------------------------------------------
118 DWARFDebugLine::DumpStatementTable(Log *log,
119 const DWARFDataExtractor &debug_line_data,
120 const dw_offset_t debug_line_offset) {
121 if (debug_line_data.ValidOffset(debug_line_offset)) {
122 lldb::offset_t offset = debug_line_offset;
123 log->Printf("--------------------------------------------------------------"
125 "debug_line[0x%8.8x]\n"
126 "--------------------------------------------------------------"
130 if (ParseStatementTable(debug_line_data, &offset, DumpStateToFile, log))
133 return debug_line_offset + 1; // Skip to next byte in .debug_line section
136 return DW_INVALID_OFFSET;
139 //----------------------------------------------------------------------
141 //----------------------------------------------------------------------
142 bool DWARFDebugLine::DumpOpcodes(Log *log, SymbolFileDWARF *dwarf2Data,
143 dw_offset_t debug_line_offset,
144 uint32_t dump_flags) {
145 const DWARFDataExtractor &debug_line_data = dwarf2Data->get_debug_line_data();
147 if (debug_line_data.GetByteSize() == 0) {
148 log->Printf("< EMPTY >\n");
152 if (debug_line_offset == DW_INVALID_OFFSET) {
153 // Dump line table to a single file only
154 debug_line_offset = 0;
155 while (debug_line_data.ValidOffset(debug_line_offset))
156 debug_line_offset = DumpStatementOpcodes(log, debug_line_data,
157 debug_line_offset, dump_flags);
159 // Dump line table to a single file only
160 DumpStatementOpcodes(log, debug_line_data, debug_line_offset, dump_flags);
165 //----------------------------------------------------------------------
166 // DumpStatementOpcodes
167 //----------------------------------------------------------------------
168 dw_offset_t DWARFDebugLine::DumpStatementOpcodes(
169 Log *log, const DWARFDataExtractor &debug_line_data,
170 const dw_offset_t debug_line_offset, uint32_t flags) {
171 lldb::offset_t offset = debug_line_offset;
172 if (debug_line_data.ValidOffset(offset)) {
175 if (ParsePrologue(debug_line_data, &offset, &prologue)) {
176 log->PutCString("--------------------------------------------------------"
178 log->Printf("debug_line[0x%8.8x]", debug_line_offset);
179 log->PutCString("--------------------------------------------------------"
183 offset = debug_line_offset;
184 log->Printf("0x%8.8" PRIx64 ": skipping pad byte %2.2x", offset,
185 debug_line_data.GetU8(&offset));
189 Row row(prologue.default_is_stmt);
190 const dw_offset_t end_offset = debug_line_offset + prologue.total_length +
191 sizeof(prologue.total_length);
193 assert(debug_line_data.ValidOffset(end_offset - 1));
195 while (offset < end_offset) {
196 const uint32_t op_offset = offset;
197 uint8_t opcode = debug_line_data.GetU8(&offset);
199 case 0: // Extended Opcodes always start with a zero opcode followed by
200 { // a uleb128 length so you can skip ones you don't know about
202 dw_offset_t ext_offset = offset;
203 dw_uleb128_t len = debug_line_data.GetULEB128(&offset);
204 dw_offset_t arg_size = len - (offset - ext_offset);
205 uint8_t sub_opcode = debug_line_data.GetU8(&offset);
207 // log->Printf( "Extended: <%u> %2.2x ", len,
210 switch (sub_opcode) {
211 case DW_LNE_end_sequence:
212 log->Printf("0x%8.8x: DW_LNE_end_sequence", op_offset);
214 row.Reset(prologue.default_is_stmt);
217 case DW_LNE_set_address: {
218 row.address = debug_line_data.GetMaxU64(&offset, arg_size);
219 log->Printf("0x%8.8x: DW_LNE_set_address (0x%" PRIx64 ")", op_offset,
223 case DW_LNE_define_file: {
224 FileNameEntry fileEntry;
225 fileEntry.name = debug_line_data.GetCStr(&offset);
226 fileEntry.dir_idx = debug_line_data.GetULEB128(&offset);
227 fileEntry.mod_time = debug_line_data.GetULEB128(&offset);
228 fileEntry.length = debug_line_data.GetULEB128(&offset);
229 log->Printf("0x%8.8x: DW_LNE_define_file('%s', dir=%i, "
230 "mod_time=0x%8.8x, length=%i )",
231 op_offset, fileEntry.name, fileEntry.dir_idx,
232 fileEntry.mod_time, fileEntry.length);
233 prologue.file_names.push_back(fileEntry);
236 case DW_LNE_set_discriminator: {
237 uint64_t discriminator = debug_line_data.GetULEB128(&offset);
238 log->Printf("0x%8.8x: DW_LNE_set_discriminator (0x%" PRIx64 ")",
239 op_offset, discriminator);
242 log->Printf("0x%8.8x: DW_LNE_??? (%2.2x) - Skipping unknown upcode",
244 // Length doesn't include the zero opcode byte or the length itself,
245 // but it does include the sub_opcode, so we have to adjust for that
254 log->Printf("0x%8.8x: DW_LNS_copy", op_offset);
258 case DW_LNS_advance_pc: {
259 dw_uleb128_t addr_offset_n = debug_line_data.GetULEB128(&offset);
260 dw_uleb128_t addr_offset = addr_offset_n * prologue.min_inst_length;
261 log->Printf("0x%8.8x: DW_LNS_advance_pc (0x%x)", op_offset,
263 row.address += addr_offset;
266 case DW_LNS_advance_line: {
267 dw_sleb128_t line_offset = debug_line_data.GetSLEB128(&offset);
268 log->Printf("0x%8.8x: DW_LNS_advance_line (%i)", op_offset,
270 row.line += line_offset;
273 case DW_LNS_set_file:
274 row.file = debug_line_data.GetULEB128(&offset);
275 log->Printf("0x%8.8x: DW_LNS_set_file (%u)", op_offset, row.file);
278 case DW_LNS_set_column:
279 row.column = debug_line_data.GetULEB128(&offset);
280 log->Printf("0x%8.8x: DW_LNS_set_column (%u)", op_offset, row.column);
283 case DW_LNS_negate_stmt:
284 row.is_stmt = !row.is_stmt;
285 log->Printf("0x%8.8x: DW_LNS_negate_stmt", op_offset);
288 case DW_LNS_set_basic_block:
289 row.basic_block = true;
290 log->Printf("0x%8.8x: DW_LNS_set_basic_block", op_offset);
293 case DW_LNS_const_add_pc: {
294 uint8_t adjust_opcode = 255 - prologue.opcode_base;
295 dw_addr_t addr_offset =
296 (adjust_opcode / prologue.line_range) * prologue.min_inst_length;
297 log->Printf("0x%8.8x: DW_LNS_const_add_pc (0x%8.8" PRIx64 ")",
298 op_offset, addr_offset);
299 row.address += addr_offset;
302 case DW_LNS_fixed_advance_pc: {
303 uint16_t pc_offset = debug_line_data.GetU16(&offset);
304 log->Printf("0x%8.8x: DW_LNS_fixed_advance_pc (0x%4.4x)", op_offset,
306 row.address += pc_offset;
309 case DW_LNS_set_prologue_end:
310 row.prologue_end = true;
311 log->Printf("0x%8.8x: DW_LNS_set_prologue_end", op_offset);
314 case DW_LNS_set_epilogue_begin:
315 row.epilogue_begin = true;
316 log->Printf("0x%8.8x: DW_LNS_set_epilogue_begin", op_offset);
320 row.isa = debug_line_data.GetULEB128(&offset);
321 log->Printf("0x%8.8x: DW_LNS_set_isa (%u)", op_offset, row.isa);
326 if (opcode < prologue.opcode_base) {
327 // We have an opcode that this parser doesn't know about, skip the
328 // number of ULEB128 numbers that is says to skip in the prologue's
329 // standard_opcode_lengths array
330 uint8_t n = prologue.standard_opcode_lengths[opcode - 1];
331 log->Printf("0x%8.8x: Special : Unknown skipping %u ULEB128 values.",
334 debug_line_data.GetULEB128(&offset);
338 uint8_t adjust_opcode = opcode - prologue.opcode_base;
339 dw_addr_t addr_offset =
340 (adjust_opcode / prologue.line_range) * prologue.min_inst_length;
341 int32_t line_offset =
342 prologue.line_base + (adjust_opcode % prologue.line_range);
343 log->Printf("0x%8.8x: address += 0x%" PRIx64 ", line += %i\n",
344 op_offset, (uint64_t)addr_offset, line_offset);
345 row.address += addr_offset;
346 row.line += line_offset;
354 return DW_INVALID_OFFSET;
357 //----------------------------------------------------------------------
360 // Parse the entire line table contents calling callback each time a new
361 // prologue is parsed and every time a new row is to be added to the line
363 //----------------------------------------------------------------------
364 void DWARFDebugLine::Parse(const DWARFDataExtractor &debug_line_data,
365 DWARFDebugLine::State::Callback callback,
367 lldb::offset_t offset = 0;
368 if (debug_line_data.ValidOffset(offset)) {
369 if (!ParseStatementTable(debug_line_data, &offset, callback, userData))
370 ++offset; // Skip to next byte in .debug_line section
374 //----------------------------------------------------------------------
375 // DWARFDebugLine::ParsePrologue
376 //----------------------------------------------------------------------
377 bool DWARFDebugLine::ParsePrologue(const DWARFDataExtractor &debug_line_data,
378 lldb::offset_t *offset_ptr,
379 Prologue *prologue) {
380 const lldb::offset_t prologue_offset = *offset_ptr;
382 // DEBUG_PRINTF("0x%8.8x: ParsePrologue()\n", *offset_ptr);
387 prologue->total_length = debug_line_data.GetDWARFInitialLength(offset_ptr);
388 prologue->version = debug_line_data.GetU16(offset_ptr);
389 if (prologue->version < 2 || prologue->version > 4)
392 prologue->prologue_length = debug_line_data.GetDWARFOffset(offset_ptr);
393 const lldb::offset_t end_prologue_offset =
394 prologue->prologue_length + *offset_ptr;
395 prologue->min_inst_length = debug_line_data.GetU8(offset_ptr);
396 if (prologue->version >= 4)
397 prologue->maximum_operations_per_instruction =
398 debug_line_data.GetU8(offset_ptr);
400 prologue->maximum_operations_per_instruction = 1;
401 prologue->default_is_stmt = debug_line_data.GetU8(offset_ptr);
402 prologue->line_base = debug_line_data.GetU8(offset_ptr);
403 prologue->line_range = debug_line_data.GetU8(offset_ptr);
404 prologue->opcode_base = debug_line_data.GetU8(offset_ptr);
406 prologue->standard_opcode_lengths.reserve(prologue->opcode_base - 1);
408 for (i = 1; i < prologue->opcode_base; ++i) {
409 uint8_t op_len = debug_line_data.GetU8(offset_ptr);
410 prologue->standard_opcode_lengths.push_back(op_len);
413 while (*offset_ptr < end_prologue_offset) {
414 s = debug_line_data.GetCStr(offset_ptr);
416 prologue->include_directories.push_back(s);
421 while (*offset_ptr < end_prologue_offset) {
422 const char *name = debug_line_data.GetCStr(offset_ptr);
423 if (name && name[0]) {
424 FileNameEntry fileEntry;
425 fileEntry.name = name;
426 fileEntry.dir_idx = debug_line_data.GetULEB128(offset_ptr);
427 fileEntry.mod_time = debug_line_data.GetULEB128(offset_ptr);
428 fileEntry.length = debug_line_data.GetULEB128(offset_ptr);
429 prologue->file_names.push_back(fileEntry);
434 // XXX GNU as is broken for 64-Bit DWARF
435 if (*offset_ptr != end_prologue_offset) {
436 Host::SystemLog(Host::eSystemLogWarning,
437 "warning: parsing line table prologue at 0x%8.8" PRIx64
438 " should have ended at 0x%8.8" PRIx64
439 " but it ended at 0x%8.8" PRIx64 "\n",
440 prologue_offset, end_prologue_offset, *offset_ptr);
442 return end_prologue_offset;
445 bool DWARFDebugLine::ParseSupportFiles(
446 const lldb::ModuleSP &module_sp, const DWARFDataExtractor &debug_line_data,
447 const lldb_private::FileSpec &cu_comp_dir, dw_offset_t stmt_list,
448 FileSpecList &support_files) {
449 lldb::offset_t offset = stmt_list;
452 if (!ParsePrologue(debug_line_data, &offset, &prologue)) {
453 Host::SystemLog(Host::eSystemLogError, "error: parsing line table prologue "
454 "at 0x%8.8x (parsing ended around "
455 "0x%8.8" PRIx64 "\n",
461 std::string remapped_file;
463 for (uint32_t file_idx = 1;
464 prologue.GetFile(file_idx, cu_comp_dir, file_spec); ++file_idx) {
465 if (module_sp->RemapSourceFile(file_spec.GetPath(), remapped_file))
466 file_spec.SetFile(remapped_file, false, FileSpec::Style::native);
467 support_files.Append(file_spec);
472 //----------------------------------------------------------------------
473 // ParseStatementTable
475 // Parse a single line table (prologue and all rows) and call the callback
476 // function once for the prologue (row in state will be zero) and each time a
477 // row is to be added to the line table.
478 //----------------------------------------------------------------------
479 bool DWARFDebugLine::ParseStatementTable(
480 const DWARFDataExtractor &debug_line_data, lldb::offset_t *offset_ptr,
481 DWARFDebugLine::State::Callback callback, void *userData) {
482 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_LINE));
483 Prologue::shared_ptr prologue(new Prologue());
485 const dw_offset_t debug_line_offset = *offset_ptr;
487 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
489 func_cat, "DWARFDebugLine::ParseStatementTable (.debug_line[0x%8.8x])",
492 if (!ParsePrologue(debug_line_data, offset_ptr, prologue.get())) {
494 log->Error("failed to parse DWARF line table prologue");
495 // Restore our offset and return false to indicate failure!
496 *offset_ptr = debug_line_offset;
503 const dw_offset_t end_offset =
504 debug_line_offset + prologue->total_length +
505 (debug_line_data.GetDWARFSizeofInitialLength());
507 State state(prologue, log, callback, userData);
509 while (*offset_ptr < end_offset) {
510 // DEBUG_PRINTF("0x%8.8x: ", *offset_ptr);
511 uint8_t opcode = debug_line_data.GetU8(offset_ptr);
514 // Extended Opcodes always start with a zero opcode followed by a uleb128
515 // length so you can skip ones you don't know about
516 lldb::offset_t ext_offset = *offset_ptr;
517 dw_uleb128_t len = debug_line_data.GetULEB128(offset_ptr);
518 dw_offset_t arg_size = len - (*offset_ptr - ext_offset);
520 // DEBUG_PRINTF("Extended: <%2u> ", len);
521 uint8_t sub_opcode = debug_line_data.GetU8(offset_ptr);
522 switch (sub_opcode) {
523 case DW_LNE_end_sequence:
524 // Set the end_sequence register of the state machine to true and
525 // append a row to the matrix using the current values of the state-
526 // machine registers. Then reset the registers to the initial values
527 // specified above. Every statement program sequence must end with a
528 // DW_LNE_end_sequence instruction which creates a row whose address is
529 // that of the byte after the last target machine instruction of the
531 state.end_sequence = true;
532 state.AppendRowToMatrix(*offset_ptr);
536 case DW_LNE_set_address:
537 // Takes a single relocatable address as an operand. The size of the
538 // operand is the size appropriate to hold an address on the target
539 // machine. Set the address register to the value given by the
540 // relocatable address. All of the other statement program opcodes that
541 // affect the address register add a delta to it. This instruction
542 // stores a relocatable value into it instead.
544 state.address = debug_line_data.GetU32(offset_ptr);
545 else // arg_size == 8
546 state.address = debug_line_data.GetU64(offset_ptr);
549 case DW_LNE_define_file:
550 // Takes 4 arguments. The first is a null terminated string containing
551 // a source file name. The second is an unsigned LEB128 number
552 // representing the directory index of the directory in which the file
553 // was found. The third is an unsigned LEB128 number representing the
554 // time of last modification of the file. The fourth is an unsigned
555 // LEB128 number representing the length in bytes of the file. The time
556 // and length fields may contain LEB128(0) if the information is not
559 // The directory index represents an entry in the include_directories
560 // section of the statement program prologue. The index is LEB128(0) if
561 // the file was found in the current directory of the compilation,
562 // LEB128(1) if it was found in the first directory in the
563 // include_directories section, and so on. The directory index is
564 // ignored for file names that represent full path names.
566 // The files are numbered, starting at 1, in the order in which they
567 // appear; the names in the prologue come before names defined by the
568 // DW_LNE_define_file instruction. These numbers are used in the file
569 // register of the state machine.
571 FileNameEntry fileEntry;
572 fileEntry.name = debug_line_data.GetCStr(offset_ptr);
573 fileEntry.dir_idx = debug_line_data.GetULEB128(offset_ptr);
574 fileEntry.mod_time = debug_line_data.GetULEB128(offset_ptr);
575 fileEntry.length = debug_line_data.GetULEB128(offset_ptr);
576 state.prologue->file_names.push_back(fileEntry);
581 // Length doesn't include the zero opcode byte or the length itself,
582 // but it does include the sub_opcode, so we have to adjust for that
584 (*offset_ptr) += arg_size;
587 } else if (opcode < prologue->opcode_base) {
591 // Takes no arguments. Append a row to the matrix using the current
592 // values of the state-machine registers. Then set the basic_block
593 // register to false.
594 state.AppendRowToMatrix(*offset_ptr);
597 case DW_LNS_advance_pc:
598 // Takes a single unsigned LEB128 operand, multiplies it by the
599 // min_inst_length field of the prologue, and adds the result to the
600 // address register of the state machine.
602 debug_line_data.GetULEB128(offset_ptr) * prologue->min_inst_length;
605 case DW_LNS_advance_line:
606 // Takes a single signed LEB128 operand and adds that value to the line
607 // register of the state machine.
608 state.line += debug_line_data.GetSLEB128(offset_ptr);
611 case DW_LNS_set_file:
612 // Takes a single unsigned LEB128 operand and stores it in the file
613 // register of the state machine.
614 state.file = debug_line_data.GetULEB128(offset_ptr);
617 case DW_LNS_set_column:
618 // Takes a single unsigned LEB128 operand and stores it in the column
619 // register of the state machine.
620 state.column = debug_line_data.GetULEB128(offset_ptr);
623 case DW_LNS_negate_stmt:
624 // Takes no arguments. Set the is_stmt register of the state machine to
625 // the logical negation of its current value.
626 state.is_stmt = !state.is_stmt;
629 case DW_LNS_set_basic_block:
630 // Takes no arguments. Set the basic_block register of the state
632 state.basic_block = true;
635 case DW_LNS_const_add_pc:
636 // Takes no arguments. Add to the address register of the state machine
637 // the address increment value corresponding to special opcode 255. The
638 // motivation for DW_LNS_const_add_pc is this: when the statement
639 // program needs to advance the address by a small amount, it can use a
640 // single special opcode, which occupies a single byte. When it needs
641 // to advance the address by up to twice the range of the last special
642 // opcode, it can use DW_LNS_const_add_pc followed by a special opcode,
643 // for a total of two bytes. Only if it needs to advance the address by
644 // more than twice that range will it need to use both
645 // DW_LNS_advance_pc and a special opcode, requiring three or more
648 uint8_t adjust_opcode = 255 - prologue->opcode_base;
649 dw_addr_t addr_offset = (adjust_opcode / prologue->line_range) *
650 prologue->min_inst_length;
651 state.address += addr_offset;
655 case DW_LNS_fixed_advance_pc:
656 // Takes a single uhalf operand. Add to the address register of the
657 // state machine the value of the (unencoded) operand. This is the only
658 // extended opcode that takes an argument that is not a variable length
659 // number. The motivation for DW_LNS_fixed_advance_pc is this: existing
660 // assemblers cannot emit DW_LNS_advance_pc or special opcodes because
661 // they cannot encode LEB128 numbers or judge when the computation of a
662 // special opcode overflows and requires the use of DW_LNS_advance_pc.
663 // Such assemblers, however, can use DW_LNS_fixed_advance_pc instead,
664 // sacrificing compression.
665 state.address += debug_line_data.GetU16(offset_ptr);
668 case DW_LNS_set_prologue_end:
669 // Takes no arguments. Set the prologue_end register of the state
671 state.prologue_end = true;
674 case DW_LNS_set_epilogue_begin:
675 // Takes no arguments. Set the basic_block register of the state
677 state.epilogue_begin = true;
681 // Takes a single unsigned LEB128 operand and stores it in the column
682 // register of the state machine.
683 state.isa = debug_line_data.GetULEB128(offset_ptr);
687 // Handle any unknown standard opcodes here. We know the lengths of
688 // such opcodes because they are specified in the prologue as a
689 // multiple of LEB128 operands for each opcode.
692 assert(static_cast<size_t>(opcode - 1) <
693 prologue->standard_opcode_lengths.size());
694 const uint8_t opcode_length =
695 prologue->standard_opcode_lengths[opcode - 1];
696 for (i = 0; i < opcode_length; ++i)
697 debug_line_data.Skip_LEB128(offset_ptr);
704 // A special opcode value is chosen based on the amount that needs
705 // to be added to the line and address registers. The maximum line
706 // increment for a special opcode is the value of the line_base field in
707 // the header, plus the value of the line_range field, minus 1 (line base
708 // + line range - 1). If the desired line increment is greater than the
709 // maximum line increment, a standard opcode must be used instead of a
710 // special opcode. The "address advance" is calculated by dividing the
711 // desired address increment by the minimum_instruction_length field from
712 // the header. The special opcode is then calculated using the following
715 // opcode = (desired line increment - line_base) + (line_range * address
716 // advance) + opcode_base
718 // If the resulting opcode is greater than 255, a standard opcode must be
721 // To decode a special opcode, subtract the opcode_base from the opcode
722 // itself to give the adjusted opcode. The amount to increment the
723 // address register is the result of the adjusted opcode divided by the
724 // line_range multiplied by the minimum_instruction_length field from the
727 // address increment = (adjusted opcode / line_range) *
728 // minimum_instruction_length
730 // The amount to increment the line register is the line_base plus the
731 // result of the adjusted opcode modulo the line_range. That is:
733 // line increment = line_base + (adjusted opcode % line_range)
735 uint8_t adjust_opcode = opcode - prologue->opcode_base;
736 dw_addr_t addr_offset =
737 (adjust_opcode / prologue->line_range) * prologue->min_inst_length;
738 int32_t line_offset =
739 prologue->line_base + (adjust_opcode % prologue->line_range);
740 state.line += line_offset;
741 state.address += addr_offset;
742 state.AppendRowToMatrix(*offset_ptr);
746 state.Finalize(*offset_ptr);
751 //----------------------------------------------------------------------
752 // ParseStatementTableCallback
753 //----------------------------------------------------------------------
754 static void ParseStatementTableCallback(dw_offset_t offset,
755 const DWARFDebugLine::State &state,
757 DWARFDebugLine::LineTable *line_table = (DWARFDebugLine::LineTable *)userData;
758 if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
759 // Just started parsing the line table, so lets keep a reference to the
760 // prologue using the supplied shared pointer
761 line_table->prologue = state.prologue;
762 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
763 // Done parsing line table, nothing to do for the cleanup
765 // We have a new row, lets append it
766 line_table->AppendRow(state);
770 //----------------------------------------------------------------------
771 // ParseStatementTable
773 // Parse a line table at offset and populate the LineTable class with the
774 // prologue and all rows.
775 //----------------------------------------------------------------------
776 bool DWARFDebugLine::ParseStatementTable(
777 const DWARFDataExtractor &debug_line_data, lldb::offset_t *offset_ptr,
778 LineTable *line_table) {
779 return ParseStatementTable(debug_line_data, offset_ptr,
780 ParseStatementTableCallback, line_table);
783 inline bool DWARFDebugLine::Prologue::IsValid() const {
784 return SymbolFileDWARF::SupportedVersion(version);
787 //----------------------------------------------------------------------
788 // DWARFDebugLine::Prologue::Dump
789 //----------------------------------------------------------------------
790 void DWARFDebugLine::Prologue::Dump(Log *log) {
793 log->Printf("Line table prologue:");
794 log->Printf(" total_length: 0x%8.8x", total_length);
795 log->Printf(" version: %u", version);
796 log->Printf("prologue_length: 0x%8.8x", prologue_length);
797 log->Printf("min_inst_length: %u", min_inst_length);
798 log->Printf("default_is_stmt: %u", default_is_stmt);
799 log->Printf(" line_base: %i", line_base);
800 log->Printf(" line_range: %u", line_range);
801 log->Printf(" opcode_base: %u", opcode_base);
803 for (i = 0; i < standard_opcode_lengths.size(); ++i) {
804 log->Printf("standard_opcode_lengths[%s] = %u", DW_LNS_value_to_name(i + 1),
805 standard_opcode_lengths[i]);
808 if (!include_directories.empty()) {
809 for (i = 0; i < include_directories.size(); ++i) {
810 log->Printf("include_directories[%3u] = '%s'", i + 1,
811 include_directories[i]);
815 if (!file_names.empty()) {
816 log->PutCString(" Dir Mod Time File Len File Name");
817 log->PutCString(" ---- ---------- ---------- "
818 "---------------------------");
819 for (i = 0; i < file_names.size(); ++i) {
820 const FileNameEntry &fileEntry = file_names[i];
821 log->Printf("file_names[%3u] %4u 0x%8.8x 0x%8.8x %s", i + 1,
822 fileEntry.dir_idx, fileEntry.mod_time, fileEntry.length,
828 //----------------------------------------------------------------------
829 // DWARFDebugLine::ParsePrologue::Append
831 // Append the contents of the prologue to the binary stream buffer
832 //----------------------------------------------------------------------
834 // DWARFDebugLine::Prologue::Append(BinaryStreamBuf& buff) const
838 // buff.Append32(total_length);
839 // buff.Append16(version);
840 // buff.Append32(prologue_length);
841 // buff.Append8(min_inst_length);
842 // buff.Append8(default_is_stmt);
843 // buff.Append8(line_base);
844 // buff.Append8(line_range);
845 // buff.Append8(opcode_base);
847 // for (i=0; i<standard_opcode_lengths.size(); ++i)
848 // buff.Append8(standard_opcode_lengths[i]);
850 // for (i=0; i<include_directories.size(); ++i)
851 // buff.AppendCStr(include_directories[i].c_str());
852 // buff.Append8(0); // Terminate the include directory section with empty
855 // for (i=0; i<file_names.size(); ++i)
857 // buff.AppendCStr(file_names[i].name.c_str());
858 // buff.Append32_as_ULEB128(file_names[i].dir_idx);
859 // buff.Append32_as_ULEB128(file_names[i].mod_time);
860 // buff.Append32_as_ULEB128(file_names[i].length);
862 // buff.Append8(0); // Terminate the file names section with empty string
865 bool DWARFDebugLine::Prologue::GetFile(uint32_t file_idx,
866 const lldb_private::FileSpec &comp_dir, FileSpec &file) const {
867 uint32_t idx = file_idx - 1; // File indexes are 1 based...
868 if (idx < file_names.size()) {
869 file.SetFile(file_names[idx].name, false, FileSpec::Style::native);
870 if (file.IsRelative()) {
871 if (file_names[idx].dir_idx > 0) {
872 const uint32_t dir_idx = file_names[idx].dir_idx - 1;
873 if (dir_idx < include_directories.size()) {
874 file.PrependPathComponent(include_directories[dir_idx]);
875 if (!file.IsRelative())
881 file.PrependPathComponent(comp_dir);
888 //----------------------------------------------------------------------
889 // DWARFDebugLine::LineTable::Dump
890 //----------------------------------------------------------------------
891 void DWARFDebugLine::LineTable::Dump(Log *log) const {
896 log->PutCString("Address Line Column File ISA Flags");
898 "------------------ ------ ------ ------ --- -------------");
899 Row::const_iterator pos = rows.begin();
900 Row::const_iterator end = rows.end();
908 void DWARFDebugLine::LineTable::AppendRow(const DWARFDebugLine::Row &state) {
909 rows.push_back(state);
912 //----------------------------------------------------------------------
913 // Compare function for the binary search in
914 // DWARFDebugLine::LineTable::LookupAddress()
915 //----------------------------------------------------------------------
916 static bool FindMatchingAddress(const DWARFDebugLine::Row &row1,
917 const DWARFDebugLine::Row &row2) {
918 return row1.address < row2.address;
921 //----------------------------------------------------------------------
922 // DWARFDebugLine::LineTable::LookupAddress
923 //----------------------------------------------------------------------
924 uint32_t DWARFDebugLine::LineTable::LookupAddress(dw_addr_t address,
925 dw_addr_t cu_high_pc) const {
926 uint32_t index = UINT32_MAX;
928 // Use the lower_bound algorithm to perform a binary search since we know
929 // that our line table data is ordered by address.
930 DWARFDebugLine::Row row;
931 row.address = address;
932 Row::const_iterator begin_pos = rows.begin();
933 Row::const_iterator end_pos = rows.end();
934 Row::const_iterator pos =
935 lower_bound(begin_pos, end_pos, row, FindMatchingAddress);
936 if (pos == end_pos) {
937 if (address < cu_high_pc)
938 return rows.size() - 1;
940 // Rely on fact that we are using a std::vector and we can do pointer
941 // arithmetic to find the row index (which will be one less that what we
942 // found since it will find the first position after the current address)
943 // since std::vector iterators are just pointers to the container type.
944 index = pos - begin_pos;
945 if (pos->address > address) {
953 return index; // Failed to find address
956 //----------------------------------------------------------------------
957 // DWARFDebugLine::Row::Row
958 //----------------------------------------------------------------------
959 DWARFDebugLine::Row::Row(bool default_is_stmt)
960 : address(0), line(1), column(0), file(1), is_stmt(default_is_stmt),
961 basic_block(false), end_sequence(false), prologue_end(false),
962 epilogue_begin(false), isa(0) {}
964 //----------------------------------------------------------------------
965 // Called after a row is appended to the matrix
966 //----------------------------------------------------------------------
967 void DWARFDebugLine::Row::PostAppend() {
969 prologue_end = false;
970 epilogue_begin = false;
973 //----------------------------------------------------------------------
974 // DWARFDebugLine::Row::Reset
975 //----------------------------------------------------------------------
976 void DWARFDebugLine::Row::Reset(bool default_is_stmt) {
981 is_stmt = default_is_stmt;
983 end_sequence = false;
984 prologue_end = false;
985 epilogue_begin = false;
988 //----------------------------------------------------------------------
989 // DWARFDebugLine::Row::Dump
990 //----------------------------------------------------------------------
991 void DWARFDebugLine::Row::Dump(Log *log) const {
992 log->Printf("0x%16.16" PRIx64 " %6u %6u %6u %3u %s%s%s%s%s", address, line,
993 column, file, isa, is_stmt ? " is_stmt" : "",
994 basic_block ? " basic_block" : "",
995 prologue_end ? " prologue_end" : "",
996 epilogue_begin ? " epilogue_begin" : "",
997 end_sequence ? " end_sequence" : "");
1000 //----------------------------------------------------------------------
1001 // Compare function LineTable structures
1002 //----------------------------------------------------------------------
1003 static bool AddressLessThan(const DWARFDebugLine::Row &a,
1004 const DWARFDebugLine::Row &b) {
1005 return a.address < b.address;
1008 // Insert a row at the correct address if the addresses can be out of order
1009 // which can only happen when we are linking a line table that may have had
1010 // it's contents rearranged.
1011 void DWARFDebugLine::Row::Insert(Row::collection &state_coll,
1013 // If we don't have anything yet, or if the address of the last state in our
1014 // line table is less than the current one, just append the current state
1015 if (state_coll.empty() || AddressLessThan(state_coll.back(), state)) {
1016 state_coll.push_back(state);
1018 // Do a binary search for the correct entry
1019 pair<Row::iterator, Row::iterator> range(equal_range(
1020 state_coll.begin(), state_coll.end(), state, AddressLessThan));
1022 // If the addresses are equal, we can safely replace the previous entry
1023 // with the current one if the one it is replacing is an end_sequence
1024 // entry. We currently always place an extra end sequence when ever we exit
1025 // a valid address range for a function in case the functions get
1026 // rearranged by optimizations or by order specifications. These extra end
1027 // sequences will disappear by getting replaced with valid consecutive
1028 // entries within a compile unit if there are no gaps.
1029 if (range.first == range.second) {
1030 state_coll.insert(range.first, state);
1032 if ((distance(range.first, range.second) == 1) &&
1033 range.first->end_sequence == true) {
1034 *range.first = state;
1036 state_coll.insert(range.second, state);
1042 void DWARFDebugLine::Row::Dump(Log *log, const Row::collection &state_coll) {
1043 std::for_each(state_coll.begin(), state_coll.end(),
1044 bind2nd(std::mem_fun_ref(&Row::Dump), log));
1047 //----------------------------------------------------------------------
1048 // DWARFDebugLine::State::State
1049 //----------------------------------------------------------------------
1050 DWARFDebugLine::State::State(Prologue::shared_ptr &p, Log *l,
1051 DWARFDebugLine::State::Callback cb, void *userData)
1052 : Row(p->default_is_stmt), prologue(p), log(l), callback(cb),
1053 callbackUserData(userData), row(StartParsingLineTable) {
1054 // Call the callback with the initial row state of zero for the prologue
1056 callback(0, *this, callbackUserData);
1059 //----------------------------------------------------------------------
1060 // DWARFDebugLine::State::Reset
1061 //----------------------------------------------------------------------
1062 void DWARFDebugLine::State::Reset() { Row::Reset(prologue->default_is_stmt); }
1064 //----------------------------------------------------------------------
1065 // DWARFDebugLine::State::AppendRowToMatrix
1066 //----------------------------------------------------------------------
1067 void DWARFDebugLine::State::AppendRowToMatrix(dw_offset_t offset) {
1068 // Each time we are to add an entry into the line table matrix call the
1069 // callback function so that someone can do something with the current state
1070 // of the state machine (like build a line table or dump the line table!)
1073 log->PutCString("Address Line Column File ISA Flags");
1075 "------------------ ------ ------ ------ --- -------------");
1080 ++row; // Increase the row number before we call our callback for a real row
1082 callback(offset, *this, callbackUserData);
1086 //----------------------------------------------------------------------
1087 // DWARFDebugLine::State::Finalize
1088 //----------------------------------------------------------------------
1089 void DWARFDebugLine::State::Finalize(dw_offset_t offset) {
1090 // Call the callback with a special row state when we are done parsing a line
1092 row = DoneParsingLineTable;
1094 callback(offset, *this, callbackUserData);
1098 // DWARFDebugLine::AppendLineTableData
1100 // const DWARFDebugLine::Prologue* prologue,
1101 // const DWARFDebugLine::Row::collection& state_coll,
1102 // const uint32_t addr_size,
1103 // BinaryStreamBuf &debug_line_data
1106 // if (state_coll.empty())
1108 // // We have no entries, just make an empty line table
1109 // debug_line_data.Append8(0);
1110 // debug_line_data.Append8(1);
1111 // debug_line_data.Append8(DW_LNE_end_sequence);
1115 // DWARFDebugLine::Row::const_iterator pos;
1116 // Row::const_iterator end = state_coll.end();
1117 // bool default_is_stmt = prologue->default_is_stmt;
1118 // const DWARFDebugLine::Row reset_state(default_is_stmt);
1119 // const DWARFDebugLine::Row* prev_state = &reset_state;
1120 // const int32_t max_line_increment_for_special_opcode =
1121 // prologue->MaxLineIncrementForSpecialOpcode();
1122 // for (pos = state_coll.begin(); pos != end; ++pos)
1124 // const DWARFDebugLine::Row& curr_state = *pos;
1125 // int32_t line_increment = 0;
1126 // dw_addr_t addr_offset = curr_state.address - prev_state->address;
1127 // dw_addr_t addr_advance = (addr_offset) / prologue->min_inst_length;
1128 // line_increment = (int32_t)(curr_state.line - prev_state->line);
1130 // // If our previous state was the reset state, then let's emit the
1131 // // address to keep GDB's DWARF parser happy. If we don't start each
1132 // // sequence with a DW_LNE_set_address opcode, the line table won't
1133 // // get slid properly in GDB.
1135 // if (prev_state == &reset_state)
1137 // debug_line_data.Append8(0); // Extended opcode
1138 // debug_line_data.Append32_as_ULEB128(addr_size + 1); // Length of
1140 // debug_line_data.Append8(DW_LNE_set_address);
1141 // debug_line_data.AppendMax64(curr_state.address, addr_size);
1142 // addr_advance = 0;
1145 // if (prev_state->file != curr_state.file)
1147 // debug_line_data.Append8(DW_LNS_set_file);
1148 // debug_line_data.Append32_as_ULEB128(curr_state.file);
1151 // if (prev_state->column != curr_state.column)
1153 // debug_line_data.Append8(DW_LNS_set_column);
1154 // debug_line_data.Append32_as_ULEB128(curr_state.column);
1157 // // Don't do anything fancy if we are at the end of a sequence
1158 // // as we don't want to push any extra rows since the
1159 // DW_LNE_end_sequence
1160 // // will push a row itself!
1161 // if (curr_state.end_sequence)
1163 // if (line_increment != 0)
1165 // debug_line_data.Append8(DW_LNS_advance_line);
1166 // debug_line_data.Append32_as_SLEB128(line_increment);
1169 // if (addr_advance > 0)
1171 // debug_line_data.Append8(DW_LNS_advance_pc);
1172 // debug_line_data.Append32_as_ULEB128(addr_advance);
1175 // // Now push the end sequence on!
1176 // debug_line_data.Append8(0);
1177 // debug_line_data.Append8(1);
1178 // debug_line_data.Append8(DW_LNE_end_sequence);
1180 // prev_state = &reset_state;
1184 // if (line_increment || addr_advance)
1186 // if (line_increment > max_line_increment_for_special_opcode)
1188 // debug_line_data.Append8(DW_LNS_advance_line);
1189 // debug_line_data.Append32_as_SLEB128(line_increment);
1190 // line_increment = 0;
1193 // uint32_t special_opcode = (line_increment >=
1194 // prologue->line_base) ? ((line_increment -
1195 // prologue->line_base) + (prologue->line_range * addr_advance)
1196 // + prologue->opcode_base) : 256;
1197 // if (special_opcode > 255)
1199 // // Both the address and line won't fit in one special
1201 // // check to see if just the line advance will?
1202 // uint32_t special_opcode_line = ((line_increment >=
1203 // prologue->line_base) && (line_increment != 0)) ?
1204 // ((line_increment - prologue->line_base) +
1205 // prologue->opcode_base) : 256;
1208 // if (special_opcode_line > 255)
1210 // // Nope, the line advance won't fit by itself, check
1211 // the address increment by itself
1212 // uint32_t special_opcode_addr = addr_advance ?
1213 // ((0 - prologue->line_base) +
1214 // (prologue->line_range * addr_advance) +
1215 // prologue->opcode_base) : 256;
1217 // if (special_opcode_addr > 255)
1219 // // Neither the address nor the line will fit in
1221 // // special opcode, we must manually enter both
1223 // // do a DW_LNS_copy to push a row (special
1225 // // automatically imply a new row is pushed)
1226 // if (line_increment != 0)
1228 // debug_line_data.Append8(DW_LNS_advance_line);
1229 // debug_line_data.Append32_as_SLEB128(line_increment);
1232 // if (addr_advance > 0)
1234 // debug_line_data.Append8(DW_LNS_advance_pc);
1235 // debug_line_data.Append32_as_ULEB128(addr_advance);
1238 // // Now push a row onto the line table manually
1239 // debug_line_data.Append8(DW_LNS_copy);
1244 // // The address increment alone will fit into a
1246 // // so modify our line change, then issue a
1248 // // for the address increment and it will push a
1251 // if (line_increment != 0)
1253 // debug_line_data.Append8(DW_LNS_advance_line);
1254 // debug_line_data.Append32_as_SLEB128(line_increment);
1257 // // Advance of line and address will fit into a
1258 // single byte special opcode
1259 // // and this will also push a row onto the line
1261 // debug_line_data.Append8(special_opcode_addr);
1266 // // The line change alone will fit into a special
1268 // // so modify our address increment first, then issue
1270 // // special opcode for the line change and it will
1272 // // a row into the line table
1273 // if (addr_advance > 0)
1275 // debug_line_data.Append8(DW_LNS_advance_pc);
1276 // debug_line_data.Append32_as_ULEB128(addr_advance);
1279 // // Advance of line and address will fit into a
1280 // single byte special opcode
1281 // // and this will also push a row onto the line table
1282 // debug_line_data.Append8(special_opcode_line);
1287 // // Advance of line and address will fit into a single
1288 // byte special opcode
1289 // // and this will also push a row onto the line table
1290 // debug_line_data.Append8(special_opcode);
1293 // prev_state = &curr_state;