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