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/Core/Stream.h"
15 #include "lldb/Symbol/CompileUnit.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)
234 // Make sure we have a valid match and that the match isn't a
236 // entry for a previous line...
237 if (pos != end_pos && pos->is_terminal_entry == false) {
238 uint32_t match_idx = std::distance(begin_pos, pos);
239 success = ConvertEntryAtIndexToLineEntry(match_idx, line_entry);
240 if (index_ptr != nullptr && success)
241 *index_ptr = match_idx;
249 bool LineTable::ConvertEntryAtIndexToLineEntry(uint32_t idx,
250 LineEntry &line_entry) {
251 if (idx < m_entries.size()) {
252 const Entry &entry = m_entries[idx];
253 ModuleSP module_sp(m_comp_unit->GetModule());
255 module_sp->ResolveFileAddress(entry.file_addr,
256 line_entry.range.GetBaseAddress())) {
257 if (!entry.is_terminal_entry && idx + 1 < m_entries.size())
258 line_entry.range.SetByteSize(m_entries[idx + 1].file_addr -
261 line_entry.range.SetByteSize(0);
264 m_comp_unit->GetSupportFiles().GetFileSpecAtIndex(entry.file_idx);
265 line_entry.original_file =
266 m_comp_unit->GetSupportFiles().GetFileSpecAtIndex(entry.file_idx);
267 line_entry.line = entry.line;
268 line_entry.column = entry.column;
269 line_entry.is_start_of_statement = entry.is_start_of_statement;
270 line_entry.is_start_of_basic_block = entry.is_start_of_basic_block;
271 line_entry.is_prologue_end = entry.is_prologue_end;
272 line_entry.is_epilogue_begin = entry.is_epilogue_begin;
273 line_entry.is_terminal_entry = entry.is_terminal_entry;
280 uint32_t LineTable::FindLineEntryIndexByFileIndex(
281 uint32_t start_idx, const std::vector<uint32_t> &file_indexes,
282 uint32_t line, bool exact, LineEntry *line_entry_ptr) {
284 const size_t count = m_entries.size();
285 std::vector<uint32_t>::const_iterator begin_pos = file_indexes.begin();
286 std::vector<uint32_t>::const_iterator end_pos = file_indexes.end();
287 size_t best_match = UINT32_MAX;
289 for (size_t idx = start_idx; idx < count; ++idx) {
290 // Skip line table rows that terminate the previous row (is_terminal_entry
292 if (m_entries[idx].is_terminal_entry)
295 if (find(begin_pos, end_pos, m_entries[idx].file_idx) == end_pos)
298 // Exact match always wins. Otherwise try to find the closest line > the
301 // FIXME: Maybe want to find the line closest before and the line closest
303 // if they're not in the same function, don't return a match.
305 if (m_entries[idx].line < line) {
307 } else if (m_entries[idx].line == line) {
309 ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr);
312 if (best_match == UINT32_MAX)
314 else if (m_entries[idx].line < m_entries[best_match].line)
319 if (best_match != UINT32_MAX) {
321 ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr);
327 uint32_t LineTable::FindLineEntryIndexByFileIndex(uint32_t start_idx,
329 uint32_t line, bool exact,
330 LineEntry *line_entry_ptr) {
331 const size_t count = m_entries.size();
332 size_t best_match = UINT32_MAX;
334 for (size_t idx = start_idx; idx < count; ++idx) {
335 // Skip line table rows that terminate the previous row (is_terminal_entry
337 if (m_entries[idx].is_terminal_entry)
340 if (m_entries[idx].file_idx != file_idx)
343 // Exact match always wins. Otherwise try to find the closest line > the
346 // FIXME: Maybe want to find the line closest before and the line closest
348 // if they're not in the same function, don't return a match.
350 if (m_entries[idx].line < line) {
352 } else if (m_entries[idx].line == line) {
354 ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr);
357 if (best_match == UINT32_MAX)
359 else if (m_entries[idx].line < m_entries[best_match].line)
364 if (best_match != UINT32_MAX) {
366 ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr);
372 size_t LineTable::FineLineEntriesForFileIndex(uint32_t file_idx, bool append,
373 SymbolContextList &sc_list) {
378 size_t num_added = 0;
379 const size_t count = m_entries.size();
381 SymbolContext sc(m_comp_unit);
383 for (size_t idx = 0; idx < count; ++idx) {
384 // Skip line table rows that terminate the previous row (is_terminal_entry
386 if (m_entries[idx].is_terminal_entry)
389 if (m_entries[idx].file_idx == file_idx) {
390 if (ConvertEntryAtIndexToLineEntry(idx, sc.line_entry)) {
400 void LineTable::Dump(Stream *s, Target *target, Address::DumpStyle style,
401 Address::DumpStyle fallback_style, bool show_line_ranges) {
402 const size_t count = m_entries.size();
403 LineEntry line_entry;
405 for (size_t idx = 0; idx < count; ++idx) {
406 ConvertEntryAtIndexToLineEntry(idx, line_entry);
407 line_entry.Dump(s, target, prev_file != line_entry.original_file, style,
408 fallback_style, show_line_ranges);
410 prev_file = line_entry.original_file;
414 void LineTable::GetDescription(Stream *s, Target *target,
415 DescriptionLevel level) {
416 const size_t count = m_entries.size();
417 LineEntry line_entry;
418 for (size_t idx = 0; idx < count; ++idx) {
419 ConvertEntryAtIndexToLineEntry(idx, line_entry);
420 line_entry.GetDescription(s, level, m_comp_unit, target, true);
425 size_t LineTable::GetContiguousFileAddressRanges(FileAddressRanges &file_ranges,
429 const size_t initial_count = file_ranges.GetSize();
431 const size_t count = m_entries.size();
432 LineEntry line_entry;
433 FileAddressRanges::Entry range(LLDB_INVALID_ADDRESS, 0);
434 for (size_t idx = 0; idx < count; ++idx) {
435 const Entry &entry = m_entries[idx];
437 if (entry.is_terminal_entry) {
438 if (range.GetRangeBase() != LLDB_INVALID_ADDRESS) {
439 range.SetRangeEnd(entry.file_addr);
440 file_ranges.Append(range);
441 range.Clear(LLDB_INVALID_ADDRESS);
443 } else if (range.GetRangeBase() == LLDB_INVALID_ADDRESS) {
444 range.SetRangeBase(entry.file_addr);
447 return file_ranges.GetSize() - initial_count;
450 LineTable *LineTable::LinkLineTable(const FileRangeMap &file_range_map) {
451 std::unique_ptr<LineTable> line_table_ap(new LineTable(m_comp_unit));
452 LineSequenceImpl sequence;
453 const size_t count = m_entries.size();
454 LineEntry line_entry;
455 const FileRangeMap::Entry *file_range_entry = nullptr;
456 const FileRangeMap::Entry *prev_file_range_entry = nullptr;
457 lldb::addr_t prev_file_addr = LLDB_INVALID_ADDRESS;
458 bool prev_entry_was_linked = false;
459 bool range_changed = false;
460 for (size_t idx = 0; idx < count; ++idx) {
461 const Entry &entry = m_entries[idx];
463 const bool end_sequence = entry.is_terminal_entry;
464 const lldb::addr_t lookup_file_addr =
465 entry.file_addr - (end_sequence ? 1 : 0);
466 if (file_range_entry == nullptr ||
467 !file_range_entry->Contains(lookup_file_addr)) {
468 prev_file_range_entry = file_range_entry;
469 file_range_entry = file_range_map.FindEntryThatContains(lookup_file_addr);
470 range_changed = true;
473 lldb::addr_t prev_end_entry_linked_file_addr = LLDB_INVALID_ADDRESS;
474 lldb::addr_t entry_linked_file_addr = LLDB_INVALID_ADDRESS;
476 bool terminate_previous_entry = false;
477 if (file_range_entry) {
478 entry_linked_file_addr = entry.file_addr -
479 file_range_entry->GetRangeBase() +
480 file_range_entry->data;
481 // Determine if we need to terminate the previous entry when the previous
482 // entry was not contiguous with this one after being linked.
483 if (range_changed && prev_file_range_entry) {
484 prev_end_entry_linked_file_addr =
485 std::min<lldb::addr_t>(entry.file_addr,
486 prev_file_range_entry->GetRangeEnd()) -
487 prev_file_range_entry->GetRangeBase() + prev_file_range_entry->data;
488 if (prev_end_entry_linked_file_addr != entry_linked_file_addr)
489 terminate_previous_entry = prev_entry_was_linked;
491 } else if (prev_entry_was_linked) {
492 // This entry doesn't have a remapping and it needs to be removed.
493 // Watch out in case we need to terminate a previous entry needs to
494 // be terminated now that one line entry in a sequence is not longer
496 if (!sequence.m_entries.empty() &&
497 !sequence.m_entries.back().is_terminal_entry) {
498 terminate_previous_entry = true;
502 if (terminate_previous_entry && !sequence.m_entries.empty()) {
503 assert(prev_file_addr != LLDB_INVALID_ADDRESS);
504 sequence.m_entries.push_back(sequence.m_entries.back());
505 if (prev_end_entry_linked_file_addr == LLDB_INVALID_ADDRESS)
506 prev_end_entry_linked_file_addr =
507 std::min<lldb::addr_t>(entry.file_addr,
508 prev_file_range_entry->GetRangeEnd()) -
509 prev_file_range_entry->GetRangeBase() + prev_file_range_entry->data;
510 sequence.m_entries.back().file_addr = prev_end_entry_linked_file_addr;
511 sequence.m_entries.back().is_terminal_entry = true;
513 // Append the sequence since we just terminated the previous one
514 line_table_ap->InsertSequence(&sequence);
518 // Now link the current entry
519 if (file_range_entry) {
520 // This entry has an address remapping and it needs to have its address
522 sequence.m_entries.push_back(entry);
523 sequence.m_entries.back().file_addr = entry_linked_file_addr;
526 // If we have items in the sequence and the last entry is a terminal entry,
527 // insert this sequence into our new line table.
528 if (!sequence.m_entries.empty() &&
529 sequence.m_entries.back().is_terminal_entry) {
530 line_table_ap->InsertSequence(&sequence);
532 prev_entry_was_linked = false;
534 prev_entry_was_linked = file_range_entry != nullptr;
536 prev_file_addr = entry.file_addr;
537 range_changed = false;
539 if (line_table_ap->m_entries.empty())
541 return line_table_ap.release();