1 //===-- LineTable.cpp -------------------------------------------*- C++ -*-===// 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 "lldb/Symbol/LineTable.h" 11 #include "lldb/Core/Address.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/Section.h" 14 #include "lldb/Symbol/CompileUnit.h" 15 #include "lldb/Utility/Stream.h" 16 #include <algorithm> 17 18 using namespace lldb; 19 using namespace lldb_private; 20 21 //---------------------------------------------------------------------- 22 // LineTable constructor 23 //---------------------------------------------------------------------- 24 LineTable::LineTable(CompileUnit *comp_unit) 25 : m_comp_unit(comp_unit), m_entries() {} 26 27 //---------------------------------------------------------------------- 28 // Destructor 29 //---------------------------------------------------------------------- 30 LineTable::~LineTable() {} 31 32 void LineTable::InsertLineEntry(lldb::addr_t file_addr, uint32_t line, 33 uint16_t column, uint16_t file_idx, 34 bool is_start_of_statement, 35 bool is_start_of_basic_block, 36 bool is_prologue_end, bool is_epilogue_begin, 37 bool is_terminal_entry) { 38 Entry entry(file_addr, line, column, file_idx, is_start_of_statement, 39 is_start_of_basic_block, is_prologue_end, is_epilogue_begin, 40 is_terminal_entry); 41 42 entry_collection::iterator begin_pos = m_entries.begin(); 43 entry_collection::iterator end_pos = m_entries.end(); 44 LineTable::Entry::LessThanBinaryPredicate less_than_bp(this); 45 entry_collection::iterator pos = 46 upper_bound(begin_pos, end_pos, entry, less_than_bp); 47 48 // Stream s(stdout); 49 // s << "\n\nBefore:\n"; 50 // Dump (&s, Address::DumpStyleFileAddress); 51 m_entries.insert(pos, entry); 52 // s << "After:\n"; 53 // Dump (&s, Address::DumpStyleFileAddress); 54 } 55 56 LineSequence::LineSequence() {} 57 58 void LineTable::LineSequenceImpl::Clear() { m_entries.clear(); } 59 60 LineSequence *LineTable::CreateLineSequenceContainer() { 61 return new LineTable::LineSequenceImpl(); 62 } 63 64 void LineTable::AppendLineEntryToSequence( 65 LineSequence *sequence, lldb::addr_t file_addr, uint32_t line, 66 uint16_t column, uint16_t file_idx, bool is_start_of_statement, 67 bool is_start_of_basic_block, bool is_prologue_end, bool is_epilogue_begin, 68 bool is_terminal_entry) { 69 assert(sequence != nullptr); 70 LineSequenceImpl *seq = reinterpret_cast<LineSequenceImpl *>(sequence); 71 Entry entry(file_addr, line, column, file_idx, is_start_of_statement, 72 is_start_of_basic_block, is_prologue_end, is_epilogue_begin, 73 is_terminal_entry); 74 entry_collection &entries = seq->m_entries; 75 // Replace the last entry if the address is the same, otherwise append it. If 76 // we have multiple 77 // line entries at the same address, this indicates illegal DWARF so this 78 // "fixes" the line table 79 // to be correct. If not fixed this can cause a line entry's address that when 80 // resolved back to 81 // a symbol context, could resolve to a different line entry. We really want a 82 // 1 to 1 mapping 83 // here to avoid these kinds of inconsistencies. We will need tor revisit this 84 // if the DWARF line 85 // tables are updated to allow multiple entries at the same address legally. 86 if (!entries.empty() && entries.back().file_addr == file_addr) { 87 // GCC don't use the is_prologue_end flag to mark the first instruction 88 // after the prologue. 89 // Instead of it it is issuing a line table entry for the first instruction 90 // of the prologue 91 // and one for the first instruction after the prologue. If the size of the 92 // prologue is 0 93 // instruction then the 2 line entry will have the same file address. 94 // Removing it will remove 95 // our ability to properly detect the location of the end of prologe so we 96 // set the prologue_end 97 // flag to preserve this information (setting the prologue_end flag for an 98 // entry what is after 99 // the prologue end don't have any effect) 100 entry.is_prologue_end = entry.file_idx == entries.back().file_idx; 101 entries.back() = entry; 102 } else 103 entries.push_back(entry); 104 } 105 106 void LineTable::InsertSequence(LineSequence *sequence) { 107 assert(sequence != nullptr); 108 LineSequenceImpl *seq = reinterpret_cast<LineSequenceImpl *>(sequence); 109 if (seq->m_entries.empty()) 110 return; 111 Entry &entry = seq->m_entries.front(); 112 113 // If the first entry address in this sequence is greater than or equal to 114 // the address of the last item in our entry collection, just append. 115 if (m_entries.empty() || 116 !Entry::EntryAddressLessThan(entry, m_entries.back())) { 117 m_entries.insert(m_entries.end(), seq->m_entries.begin(), 118 seq->m_entries.end()); 119 return; 120 } 121 122 // Otherwise, find where this belongs in the collection 123 entry_collection::iterator begin_pos = m_entries.begin(); 124 entry_collection::iterator end_pos = m_entries.end(); 125 LineTable::Entry::LessThanBinaryPredicate less_than_bp(this); 126 entry_collection::iterator pos = 127 upper_bound(begin_pos, end_pos, entry, less_than_bp); 128 129 // We should never insert a sequence in the middle of another sequence 130 if (pos != begin_pos) { 131 while (pos < end_pos && !((pos - 1)->is_terminal_entry)) 132 pos++; 133 } 134 135 #ifdef LLDB_CONFIGURATION_DEBUG 136 // If we aren't inserting at the beginning, the previous entry should 137 // terminate a sequence. 138 if (pos != begin_pos) { 139 entry_collection::iterator prev_pos = pos - 1; 140 assert(prev_pos->is_terminal_entry); 141 } 142 #endif 143 m_entries.insert(pos, seq->m_entries.begin(), seq->m_entries.end()); 144 } 145 146 //---------------------------------------------------------------------- 147 LineTable::Entry::LessThanBinaryPredicate::LessThanBinaryPredicate( 148 LineTable *line_table) 149 : m_line_table(line_table) {} 150 151 bool LineTable::Entry::LessThanBinaryPredicate:: 152 operator()(const LineTable::Entry &a, const LineTable::Entry &b) const { 153 #define LT_COMPARE(a, b) \ 154 if (a != b) \ 155 return a < b 156 LT_COMPARE(a.file_addr, b.file_addr); 157 // b and a reversed on purpose below. 158 LT_COMPARE(b.is_terminal_entry, a.is_terminal_entry); 159 LT_COMPARE(a.line, b.line); 160 LT_COMPARE(a.column, b.column); 161 LT_COMPARE(a.is_start_of_statement, b.is_start_of_statement); 162 LT_COMPARE(a.is_start_of_basic_block, b.is_start_of_basic_block); 163 // b and a reversed on purpose below. 164 LT_COMPARE(b.is_prologue_end, a.is_prologue_end); 165 LT_COMPARE(a.is_epilogue_begin, b.is_epilogue_begin); 166 LT_COMPARE(a.file_idx, b.file_idx); 167 return false; 168 #undef LT_COMPARE 169 } 170 171 uint32_t LineTable::GetSize() const { return m_entries.size(); } 172 173 bool LineTable::GetLineEntryAtIndex(uint32_t idx, LineEntry &line_entry) { 174 if (idx < m_entries.size()) { 175 ConvertEntryAtIndexToLineEntry(idx, line_entry); 176 return true; 177 } 178 line_entry.Clear(); 179 return false; 180 } 181 182 bool LineTable::FindLineEntryByAddress(const Address &so_addr, 183 LineEntry &line_entry, 184 uint32_t *index_ptr) { 185 if (index_ptr != nullptr) 186 *index_ptr = UINT32_MAX; 187 188 bool success = false; 189 190 if (so_addr.GetModule().get() == m_comp_unit->GetModule().get()) { 191 Entry search_entry; 192 search_entry.file_addr = so_addr.GetFileAddress(); 193 if (search_entry.file_addr != LLDB_INVALID_ADDRESS) { 194 entry_collection::const_iterator begin_pos = m_entries.begin(); 195 entry_collection::const_iterator end_pos = m_entries.end(); 196 entry_collection::const_iterator pos = lower_bound( 197 begin_pos, end_pos, search_entry, Entry::EntryAddressLessThan); 198 if (pos != end_pos) { 199 if (pos != begin_pos) { 200 if (pos->file_addr != search_entry.file_addr) 201 --pos; 202 else if (pos->file_addr == search_entry.file_addr) { 203 // If this is a termination entry, it shouldn't match since 204 // entries with the "is_terminal_entry" member set to true 205 // are termination entries that define the range for the 206 // previous entry. 207 if (pos->is_terminal_entry) { 208 // The matching entry is a terminal entry, so we skip 209 // ahead to the next entry to see if there is another 210 // entry following this one whose section/offset matches. 211 ++pos; 212 if (pos != end_pos) { 213 if (pos->file_addr != search_entry.file_addr) 214 pos = end_pos; 215 } 216 } 217 218 if (pos != end_pos) { 219 // While in the same section/offset backup to find the first 220 // line entry that matches the address in case there are 221 // multiple 222 while (pos != begin_pos) { 223 entry_collection::const_iterator prev_pos = pos - 1; 224 if (prev_pos->file_addr == search_entry.file_addr && 225 prev_pos->is_terminal_entry == false) 226 --pos; 227 else 228 break; 229 } 230 } 231 } 232 } 233 else 234 { 235 // There might be code in the containing objfile before the first line 236 // table entry. Make sure that does not get considered part of the first 237 // line table entry. 238 if (pos->file_addr > so_addr.GetFileAddress()) 239 return false; 240 } 241 242 // Make sure we have a valid match and that the match isn't a 243 // terminating 244 // entry for a previous line... 245 if (pos != end_pos && pos->is_terminal_entry == false) { 246 uint32_t match_idx = std::distance(begin_pos, pos); 247 success = ConvertEntryAtIndexToLineEntry(match_idx, line_entry); 248 if (index_ptr != nullptr && success) 249 *index_ptr = match_idx; 250 } 251 } 252 } 253 } 254 return success; 255 } 256 257 bool LineTable::ConvertEntryAtIndexToLineEntry(uint32_t idx, 258 LineEntry &line_entry) { 259 if (idx < m_entries.size()) { 260 const Entry &entry = m_entries[idx]; 261 ModuleSP module_sp(m_comp_unit->GetModule()); 262 if (module_sp && 263 module_sp->ResolveFileAddress(entry.file_addr, 264 line_entry.range.GetBaseAddress())) { 265 if (!entry.is_terminal_entry && idx + 1 < m_entries.size()) 266 line_entry.range.SetByteSize(m_entries[idx + 1].file_addr - 267 entry.file_addr); 268 else 269 line_entry.range.SetByteSize(0); 270 271 line_entry.file = 272 m_comp_unit->GetSupportFiles().GetFileSpecAtIndex(entry.file_idx); 273 line_entry.original_file = 274 m_comp_unit->GetSupportFiles().GetFileSpecAtIndex(entry.file_idx); 275 line_entry.line = entry.line; 276 line_entry.column = entry.column; 277 line_entry.is_start_of_statement = entry.is_start_of_statement; 278 line_entry.is_start_of_basic_block = entry.is_start_of_basic_block; 279 line_entry.is_prologue_end = entry.is_prologue_end; 280 line_entry.is_epilogue_begin = entry.is_epilogue_begin; 281 line_entry.is_terminal_entry = entry.is_terminal_entry; 282 return true; 283 } 284 } 285 return false; 286 } 287 288 uint32_t LineTable::FindLineEntryIndexByFileIndex( 289 uint32_t start_idx, const std::vector<uint32_t> &file_indexes, 290 uint32_t line, bool exact, LineEntry *line_entry_ptr) { 291 292 const size_t count = m_entries.size(); 293 std::vector<uint32_t>::const_iterator begin_pos = file_indexes.begin(); 294 std::vector<uint32_t>::const_iterator end_pos = file_indexes.end(); 295 size_t best_match = UINT32_MAX; 296 297 for (size_t idx = start_idx; idx < count; ++idx) { 298 // Skip line table rows that terminate the previous row (is_terminal_entry 299 // is non-zero) 300 if (m_entries[idx].is_terminal_entry) 301 continue; 302 303 if (find(begin_pos, end_pos, m_entries[idx].file_idx) == end_pos) 304 continue; 305 306 // Exact match always wins. Otherwise try to find the closest line > the 307 // desired 308 // line. 309 // FIXME: Maybe want to find the line closest before and the line closest 310 // after and 311 // if they're not in the same function, don't return a match. 312 313 if (m_entries[idx].line < line) { 314 continue; 315 } else if (m_entries[idx].line == line) { 316 if (line_entry_ptr) 317 ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr); 318 return idx; 319 } else if (!exact) { 320 if (best_match == UINT32_MAX) 321 best_match = idx; 322 else if (m_entries[idx].line < m_entries[best_match].line) 323 best_match = idx; 324 } 325 } 326 327 if (best_match != UINT32_MAX) { 328 if (line_entry_ptr) 329 ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr); 330 return best_match; 331 } 332 return UINT32_MAX; 333 } 334 335 uint32_t LineTable::FindLineEntryIndexByFileIndex(uint32_t start_idx, 336 uint32_t file_idx, 337 uint32_t line, bool exact, 338 LineEntry *line_entry_ptr) { 339 const size_t count = m_entries.size(); 340 size_t best_match = UINT32_MAX; 341 342 for (size_t idx = start_idx; idx < count; ++idx) { 343 // Skip line table rows that terminate the previous row (is_terminal_entry 344 // is non-zero) 345 if (m_entries[idx].is_terminal_entry) 346 continue; 347 348 if (m_entries[idx].file_idx != file_idx) 349 continue; 350 351 // Exact match always wins. Otherwise try to find the closest line > the 352 // desired 353 // line. 354 // FIXME: Maybe want to find the line closest before and the line closest 355 // after and 356 // if they're not in the same function, don't return a match. 357 358 if (m_entries[idx].line < line) { 359 continue; 360 } else if (m_entries[idx].line == line) { 361 if (line_entry_ptr) 362 ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr); 363 return idx; 364 } else if (!exact) { 365 if (best_match == UINT32_MAX) 366 best_match = idx; 367 else if (m_entries[idx].line < m_entries[best_match].line) 368 best_match = idx; 369 } 370 } 371 372 if (best_match != UINT32_MAX) { 373 if (line_entry_ptr) 374 ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr); 375 return best_match; 376 } 377 return UINT32_MAX; 378 } 379 380 size_t LineTable::FineLineEntriesForFileIndex(uint32_t file_idx, bool append, 381 SymbolContextList &sc_list) { 382 383 if (!append) 384 sc_list.Clear(); 385 386 size_t num_added = 0; 387 const size_t count = m_entries.size(); 388 if (count > 0) { 389 SymbolContext sc(m_comp_unit); 390 391 for (size_t idx = 0; idx < count; ++idx) { 392 // Skip line table rows that terminate the previous row (is_terminal_entry 393 // is non-zero) 394 if (m_entries[idx].is_terminal_entry) 395 continue; 396 397 if (m_entries[idx].file_idx == file_idx) { 398 if (ConvertEntryAtIndexToLineEntry(idx, sc.line_entry)) { 399 ++num_added; 400 sc_list.Append(sc); 401 } 402 } 403 } 404 } 405 return num_added; 406 } 407 408 void LineTable::Dump(Stream *s, Target *target, Address::DumpStyle style, 409 Address::DumpStyle fallback_style, bool show_line_ranges) { 410 const size_t count = m_entries.size(); 411 LineEntry line_entry; 412 FileSpec prev_file; 413 for (size_t idx = 0; idx < count; ++idx) { 414 ConvertEntryAtIndexToLineEntry(idx, line_entry); 415 line_entry.Dump(s, target, prev_file != line_entry.original_file, style, 416 fallback_style, show_line_ranges); 417 s->EOL(); 418 prev_file = line_entry.original_file; 419 } 420 } 421 422 void LineTable::GetDescription(Stream *s, Target *target, 423 DescriptionLevel level) { 424 const size_t count = m_entries.size(); 425 LineEntry line_entry; 426 for (size_t idx = 0; idx < count; ++idx) { 427 ConvertEntryAtIndexToLineEntry(idx, line_entry); 428 line_entry.GetDescription(s, level, m_comp_unit, target, true); 429 s->EOL(); 430 } 431 } 432 433 size_t LineTable::GetContiguousFileAddressRanges(FileAddressRanges &file_ranges, 434 bool append) { 435 if (!append) 436 file_ranges.Clear(); 437 const size_t initial_count = file_ranges.GetSize(); 438 439 const size_t count = m_entries.size(); 440 LineEntry line_entry; 441 FileAddressRanges::Entry range(LLDB_INVALID_ADDRESS, 0); 442 for (size_t idx = 0; idx < count; ++idx) { 443 const Entry &entry = m_entries[idx]; 444 445 if (entry.is_terminal_entry) { 446 if (range.GetRangeBase() != LLDB_INVALID_ADDRESS) { 447 range.SetRangeEnd(entry.file_addr); 448 file_ranges.Append(range); 449 range.Clear(LLDB_INVALID_ADDRESS); 450 } 451 } else if (range.GetRangeBase() == LLDB_INVALID_ADDRESS) { 452 range.SetRangeBase(entry.file_addr); 453 } 454 } 455 return file_ranges.GetSize() - initial_count; 456 } 457 458 LineTable *LineTable::LinkLineTable(const FileRangeMap &file_range_map) { 459 std::unique_ptr<LineTable> line_table_ap(new LineTable(m_comp_unit)); 460 LineSequenceImpl sequence; 461 const size_t count = m_entries.size(); 462 LineEntry line_entry; 463 const FileRangeMap::Entry *file_range_entry = nullptr; 464 const FileRangeMap::Entry *prev_file_range_entry = nullptr; 465 lldb::addr_t prev_file_addr = LLDB_INVALID_ADDRESS; 466 bool prev_entry_was_linked = false; 467 bool range_changed = false; 468 for (size_t idx = 0; idx < count; ++idx) { 469 const Entry &entry = m_entries[idx]; 470 471 const bool end_sequence = entry.is_terminal_entry; 472 const lldb::addr_t lookup_file_addr = 473 entry.file_addr - (end_sequence ? 1 : 0); 474 if (file_range_entry == nullptr || 475 !file_range_entry->Contains(lookup_file_addr)) { 476 prev_file_range_entry = file_range_entry; 477 file_range_entry = file_range_map.FindEntryThatContains(lookup_file_addr); 478 range_changed = true; 479 } 480 481 lldb::addr_t prev_end_entry_linked_file_addr = LLDB_INVALID_ADDRESS; 482 lldb::addr_t entry_linked_file_addr = LLDB_INVALID_ADDRESS; 483 484 bool terminate_previous_entry = false; 485 if (file_range_entry) { 486 entry_linked_file_addr = entry.file_addr - 487 file_range_entry->GetRangeBase() + 488 file_range_entry->data; 489 // Determine if we need to terminate the previous entry when the previous 490 // entry was not contiguous with this one after being linked. 491 if (range_changed && prev_file_range_entry) { 492 prev_end_entry_linked_file_addr = 493 std::min<lldb::addr_t>(entry.file_addr, 494 prev_file_range_entry->GetRangeEnd()) - 495 prev_file_range_entry->GetRangeBase() + prev_file_range_entry->data; 496 if (prev_end_entry_linked_file_addr != entry_linked_file_addr) 497 terminate_previous_entry = prev_entry_was_linked; 498 } 499 } else if (prev_entry_was_linked) { 500 // This entry doesn't have a remapping and it needs to be removed. 501 // Watch out in case we need to terminate a previous entry needs to 502 // be terminated now that one line entry in a sequence is not longer 503 // valid. 504 if (!sequence.m_entries.empty() && 505 !sequence.m_entries.back().is_terminal_entry) { 506 terminate_previous_entry = true; 507 } 508 } 509 510 if (terminate_previous_entry && !sequence.m_entries.empty()) { 511 assert(prev_file_addr != LLDB_INVALID_ADDRESS); 512 UNUSED_IF_ASSERT_DISABLED(prev_file_addr); 513 sequence.m_entries.push_back(sequence.m_entries.back()); 514 if (prev_end_entry_linked_file_addr == LLDB_INVALID_ADDRESS) 515 prev_end_entry_linked_file_addr = 516 std::min<lldb::addr_t>(entry.file_addr, 517 prev_file_range_entry->GetRangeEnd()) - 518 prev_file_range_entry->GetRangeBase() + prev_file_range_entry->data; 519 sequence.m_entries.back().file_addr = prev_end_entry_linked_file_addr; 520 sequence.m_entries.back().is_terminal_entry = true; 521 522 // Append the sequence since we just terminated the previous one 523 line_table_ap->InsertSequence(&sequence); 524 sequence.Clear(); 525 } 526 527 // Now link the current entry 528 if (file_range_entry) { 529 // This entry has an address remapping and it needs to have its address 530 // relinked 531 sequence.m_entries.push_back(entry); 532 sequence.m_entries.back().file_addr = entry_linked_file_addr; 533 } 534 535 // If we have items in the sequence and the last entry is a terminal entry, 536 // insert this sequence into our new line table. 537 if (!sequence.m_entries.empty() && 538 sequence.m_entries.back().is_terminal_entry) { 539 line_table_ap->InsertSequence(&sequence); 540 sequence.Clear(); 541 prev_entry_was_linked = false; 542 } else { 543 prev_entry_was_linked = file_range_entry != nullptr; 544 } 545 prev_file_addr = entry.file_addr; 546 range_changed = false; 547 } 548 if (line_table_ap->m_entries.empty()) 549 return nullptr; 550 return line_table_ap.release(); 551 } 552