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