1 //===-- LineTable.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 "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"
19 using namespace lldb_private;
21 //----------------------------------------------------------------------
22 // LineTable constructor
23 //----------------------------------------------------------------------
24 LineTable::LineTable(CompileUnit *comp_unit)
25 : m_comp_unit(comp_unit), m_entries() {}
27 //----------------------------------------------------------------------
29 //----------------------------------------------------------------------
30 LineTable::~LineTable() {}
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,
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);
49 // s << "\n\nBefore:\n";
50 // Dump (&s, Address::DumpStyleFileAddress);
51 m_entries.insert(pos, entry);
53 // Dump (&s, Address::DumpStyleFileAddress);
56 LineSequence::LineSequence() {}
58 void LineTable::LineSequenceImpl::Clear() { m_entries.clear(); }
60 LineSequence *LineTable::CreateLineSequenceContainer() {
61 return new LineTable::LineSequenceImpl();
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,
74 entry_collection &entries = seq->m_entries;
75 // Replace the last entry if the address is the same, otherwise append it. If
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
81 // a symbol context, could resolve to a different line entry. We really want a
83 // here to avoid these kinds of inconsistencies. We will need tor revisit this
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
91 // and one for the first instruction after the prologue. If the size of the
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;
103 entries.push_back(entry);
106 void LineTable::InsertSequence(LineSequence *sequence) {
107 assert(sequence != nullptr);
108 LineSequenceImpl *seq = reinterpret_cast<LineSequenceImpl *>(sequence);
109 if (seq->m_entries.empty())
111 Entry &entry = seq->m_entries.front();
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());
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);
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))
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);
143 m_entries.insert(pos, seq->m_entries.begin(), seq->m_entries.end());
146 //----------------------------------------------------------------------
147 LineTable::Entry::LessThanBinaryPredicate::LessThanBinaryPredicate(
148 LineTable *line_table)
149 : m_line_table(line_table) {}
151 bool LineTable::Entry::LessThanBinaryPredicate::
152 operator()(const LineTable::Entry &a, const LineTable::Entry &b) const {
153 #define LT_COMPARE(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);
171 uint32_t LineTable::GetSize() const { return m_entries.size(); }
173 bool LineTable::GetLineEntryAtIndex(uint32_t idx, LineEntry &line_entry) {
174 if (idx < m_entries.size()) {
175 ConvertEntryAtIndexToLineEntry(idx, line_entry);
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;
188 bool success = false;
190 if (so_addr.GetModule().get() == m_comp_unit->GetModule().get()) {
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)
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
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.
212 if (pos != end_pos) {
213 if (pos->file_addr != search_entry.file_addr)
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
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)
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
238 if (pos->file_addr > so_addr.GetFileAddress())
242 // Make sure we have a valid match and that the match isn't a
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;
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());
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 -
269 line_entry.range.SetByteSize(0);
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;
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) {
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;
297 for (size_t idx = start_idx; idx < count; ++idx) {
298 // Skip line table rows that terminate the previous row (is_terminal_entry
300 if (m_entries[idx].is_terminal_entry)
303 if (find(begin_pos, end_pos, m_entries[idx].file_idx) == end_pos)
306 // Exact match always wins. Otherwise try to find the closest line > the
309 // FIXME: Maybe want to find the line closest before and the line closest
311 // if they're not in the same function, don't return a match.
313 if (m_entries[idx].line < line) {
315 } else if (m_entries[idx].line == line) {
317 ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr);
320 if (best_match == UINT32_MAX)
322 else if (m_entries[idx].line < m_entries[best_match].line)
327 if (best_match != UINT32_MAX) {
329 ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr);
335 uint32_t LineTable::FindLineEntryIndexByFileIndex(uint32_t start_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;
342 for (size_t idx = start_idx; idx < count; ++idx) {
343 // Skip line table rows that terminate the previous row (is_terminal_entry
345 if (m_entries[idx].is_terminal_entry)
348 if (m_entries[idx].file_idx != file_idx)
351 // Exact match always wins. Otherwise try to find the closest line > the
354 // FIXME: Maybe want to find the line closest before and the line closest
356 // if they're not in the same function, don't return a match.
358 if (m_entries[idx].line < line) {
360 } else if (m_entries[idx].line == line) {
362 ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr);
365 if (best_match == UINT32_MAX)
367 else if (m_entries[idx].line < m_entries[best_match].line)
372 if (best_match != UINT32_MAX) {
374 ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr);
380 size_t LineTable::FineLineEntriesForFileIndex(uint32_t file_idx, bool append,
381 SymbolContextList &sc_list) {
386 size_t num_added = 0;
387 const size_t count = m_entries.size();
389 SymbolContext sc(m_comp_unit);
391 for (size_t idx = 0; idx < count; ++idx) {
392 // Skip line table rows that terminate the previous row (is_terminal_entry
394 if (m_entries[idx].is_terminal_entry)
397 if (m_entries[idx].file_idx == file_idx) {
398 if (ConvertEntryAtIndexToLineEntry(idx, sc.line_entry)) {
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;
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);
418 prev_file = line_entry.original_file;
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);
433 size_t LineTable::GetContiguousFileAddressRanges(FileAddressRanges &file_ranges,
437 const size_t initial_count = file_ranges.GetSize();
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];
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);
451 } else if (range.GetRangeBase() == LLDB_INVALID_ADDRESS) {
452 range.SetRangeBase(entry.file_addr);
455 return file_ranges.GetSize() - initial_count;
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];
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;
481 lldb::addr_t prev_end_entry_linked_file_addr = LLDB_INVALID_ADDRESS;
482 lldb::addr_t entry_linked_file_addr = LLDB_INVALID_ADDRESS;
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;
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
504 if (!sequence.m_entries.empty() &&
505 !sequence.m_entries.back().is_terminal_entry) {
506 terminate_previous_entry = true;
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;
522 // Append the sequence since we just terminated the previous one
523 line_table_ap->InsertSequence(&sequence);
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
531 sequence.m_entries.push_back(entry);
532 sequence.m_entries.back().file_addr = entry_linked_file_addr;
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);
541 prev_entry_was_linked = false;
543 prev_entry_was_linked = file_range_entry != nullptr;
545 prev_file_addr = entry.file_addr;
546 range_changed = false;
548 if (line_table_ap->m_entries.empty())
550 return line_table_ap.release();