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/Core/Address.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/Section.h"
13 #include "lldb/Core/Stream.h"
14 #include "lldb/Symbol/CompileUnit.h"
15 #include "lldb/Symbol/LineTable.h"
19 using namespace lldb_private;
21 //----------------------------------------------------------------------
22 // LineTable constructor
23 //----------------------------------------------------------------------
24 LineTable::LineTable(CompileUnit* comp_unit) :
25 m_comp_unit(comp_unit),
30 //----------------------------------------------------------------------
32 //----------------------------------------------------------------------
33 LineTable::~LineTable()
38 LineTable::InsertLineEntry
40 lldb::addr_t file_addr,
44 bool is_start_of_statement,
45 bool is_start_of_basic_block,
47 bool is_epilogue_begin,
48 bool is_terminal_entry
51 Entry entry(file_addr, line, column, file_idx, is_start_of_statement, is_start_of_basic_block, is_prologue_end, is_epilogue_begin, is_terminal_entry);
53 entry_collection::iterator begin_pos = m_entries.begin();
54 entry_collection::iterator end_pos = m_entries.end();
55 LineTable::Entry::LessThanBinaryPredicate less_than_bp(this);
56 entry_collection::iterator pos = upper_bound(begin_pos, end_pos, entry, less_than_bp);
59 // s << "\n\nBefore:\n";
60 // Dump (&s, Address::DumpStyleFileAddress);
61 m_entries.insert(pos, entry);
63 // Dump (&s, Address::DumpStyleFileAddress);
66 LineSequence::LineSequence()
71 LineTable::LineSequenceImpl::Clear()
76 LineSequence* LineTable::CreateLineSequenceContainer ()
78 return new LineTable::LineSequenceImpl();
82 LineTable::AppendLineEntryToSequence
84 LineSequence* sequence,
85 lldb::addr_t file_addr,
89 bool is_start_of_statement,
90 bool is_start_of_basic_block,
92 bool is_epilogue_begin,
93 bool is_terminal_entry
96 assert(sequence != NULL);
97 LineSequenceImpl* seq = reinterpret_cast<LineSequenceImpl*>(sequence);
98 Entry entry(file_addr, line, column, file_idx, is_start_of_statement, is_start_of_basic_block, is_prologue_end, is_epilogue_begin, is_terminal_entry);
99 seq->m_entries.push_back (entry);
103 LineTable::InsertSequence (LineSequence* sequence)
105 assert(sequence != NULL);
106 LineSequenceImpl* seq = reinterpret_cast<LineSequenceImpl*>(sequence);
107 if (seq->m_entries.empty())
109 Entry& entry = seq->m_entries.front();
111 // If the first entry address in this sequence is greater than or equal to
112 // the address of the last item in our entry collection, just append.
113 if (m_entries.empty() || !Entry::EntryAddressLessThan(entry, m_entries.back()))
115 m_entries.insert(m_entries.end(),
116 seq->m_entries.begin(),
117 seq->m_entries.end());
121 // Otherwise, find where this belongs in the collection
122 entry_collection::iterator begin_pos = m_entries.begin();
123 entry_collection::iterator end_pos = m_entries.end();
124 LineTable::Entry::LessThanBinaryPredicate less_than_bp(this);
125 entry_collection::iterator pos = upper_bound(begin_pos, end_pos, entry, less_than_bp);
126 #ifdef LLDB_CONFIGURATION_DEBUG
127 // If we aren't inserting at the beginning, the previous entry should
128 // terminate a sequence.
129 if (pos != begin_pos)
131 entry_collection::iterator prev_pos = pos - 1;
132 assert(prev_pos->is_terminal_entry);
135 m_entries.insert(pos, seq->m_entries.begin(), seq->m_entries.end());
138 //----------------------------------------------------------------------
139 LineTable::Entry::LessThanBinaryPredicate::LessThanBinaryPredicate(LineTable *line_table) :
140 m_line_table (line_table)
145 LineTable::Entry::LessThanBinaryPredicate::operator() (const LineTable::Entry& a, const LineTable::Entry& b) const
147 #define LT_COMPARE(a,b) if (a != b) return a < b
148 LT_COMPARE (a.file_addr, b.file_addr);
149 // b and a reversed on purpose below.
150 LT_COMPARE (b.is_terminal_entry, a.is_terminal_entry);
151 LT_COMPARE (a.line, b.line);
152 LT_COMPARE (a.column, b.column);
153 LT_COMPARE (a.is_start_of_statement, b.is_start_of_statement);
154 LT_COMPARE (a.is_start_of_basic_block, b.is_start_of_basic_block);
155 // b and a reversed on purpose below.
156 LT_COMPARE (b.is_prologue_end, a.is_prologue_end);
157 LT_COMPARE (a.is_epilogue_begin, b.is_epilogue_begin);
158 LT_COMPARE (a.file_idx, b.file_idx);
166 LineTable::GetSize() const
168 return m_entries.size();
172 LineTable::GetLineEntryAtIndex(uint32_t idx, LineEntry& line_entry)
174 if (idx < m_entries.size())
176 ConvertEntryAtIndexToLineEntry (idx, line_entry);
184 LineTable::FindLineEntryByAddress (const Address &so_addr, LineEntry& line_entry, uint32_t *index_ptr)
186 if (index_ptr != NULL )
187 *index_ptr = UINT32_MAX;
189 bool success = false;
191 if (so_addr.GetModule().get() == m_comp_unit->GetModule().get())
194 search_entry.file_addr = so_addr.GetFileAddress();
195 if (search_entry.file_addr != LLDB_INVALID_ADDRESS)
197 entry_collection::const_iterator begin_pos = m_entries.begin();
198 entry_collection::const_iterator end_pos = m_entries.end();
199 entry_collection::const_iterator pos = lower_bound(begin_pos, end_pos, search_entry, Entry::EntryAddressLessThan);
202 if (pos != begin_pos)
204 if (pos->file_addr != search_entry.file_addr)
206 else if (pos->file_addr == search_entry.file_addr)
208 // If this is a termination entry, it should't match since
209 // entries with the "is_terminal_entry" member set to true
210 // are termination entries that define the range for the
212 if (pos->is_terminal_entry)
214 // The matching entry is a terminal entry, so we skip
215 // ahead to the next entry to see if there is another
216 // entry following this one whose section/offset matches.
220 if (pos->file_addr != search_entry.file_addr)
227 // While in the same section/offset backup to find the first
228 // line entry that matches the address in case there are
230 while (pos != begin_pos)
232 entry_collection::const_iterator prev_pos = pos - 1;
233 if (prev_pos->file_addr == search_entry.file_addr &&
234 prev_pos->is_terminal_entry == false)
244 // Make sure we have a valid match and that the match isn't a terminating
245 // entry for a previous line...
246 if (pos != end_pos && pos->is_terminal_entry == false)
248 uint32_t match_idx = std::distance (begin_pos, pos);
249 success = ConvertEntryAtIndexToLineEntry(match_idx, line_entry);
250 if (index_ptr != NULL && success)
251 *index_ptr = match_idx;
261 LineTable::ConvertEntryAtIndexToLineEntry (uint32_t idx, LineEntry &line_entry)
263 if (idx < m_entries.size())
265 const Entry& entry = m_entries[idx];
266 ModuleSP module_sp (m_comp_unit->GetModule());
267 if (module_sp && module_sp->ResolveFileAddress(entry.file_addr, line_entry.range.GetBaseAddress()))
269 if (!entry.is_terminal_entry && idx + 1 < m_entries.size())
270 line_entry.range.SetByteSize(m_entries[idx+1].file_addr - entry.file_addr);
272 line_entry.range.SetByteSize(0);
274 line_entry.file = 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;
289 LineTable::FindLineEntryIndexByFileIndex
292 const std::vector<uint32_t> &file_indexes,
295 LineEntry* line_entry_ptr
299 const size_t count = m_entries.size();
300 std::vector<uint32_t>::const_iterator begin_pos = file_indexes.begin();
301 std::vector<uint32_t>::const_iterator end_pos = file_indexes.end();
302 size_t best_match = UINT32_MAX;
304 for (size_t idx = start_idx; idx < count; ++idx)
306 // Skip line table rows that terminate the previous row (is_terminal_entry is non-zero)
307 if (m_entries[idx].is_terminal_entry)
310 if (find (begin_pos, end_pos, m_entries[idx].file_idx) == end_pos)
313 // Exact match always wins. Otherwise try to find the closest line > the desired
315 // FIXME: Maybe want to find the line closest before and the line closest after and
316 // if they're not in the same function, don't return a match.
318 if (m_entries[idx].line < line)
322 else if (m_entries[idx].line == line)
325 ConvertEntryAtIndexToLineEntry (idx, *line_entry_ptr);
330 if (best_match == UINT32_MAX)
332 else if (m_entries[idx].line < m_entries[best_match].line)
337 if (best_match != UINT32_MAX)
340 ConvertEntryAtIndexToLineEntry (best_match, *line_entry_ptr);
347 LineTable::FindLineEntryIndexByFileIndex (uint32_t start_idx, uint32_t file_idx, uint32_t line, bool exact, LineEntry* line_entry_ptr)
349 const size_t count = m_entries.size();
350 size_t best_match = UINT32_MAX;
352 for (size_t idx = start_idx; idx < count; ++idx)
354 // Skip line table rows that terminate the previous row (is_terminal_entry is non-zero)
355 if (m_entries[idx].is_terminal_entry)
358 if (m_entries[idx].file_idx != file_idx)
361 // Exact match always wins. Otherwise try to find the closest line > the desired
363 // FIXME: Maybe want to find the line closest before and the line closest after and
364 // if they're not in the same function, don't return a match.
366 if (m_entries[idx].line < line)
370 else if (m_entries[idx].line == line)
373 ConvertEntryAtIndexToLineEntry (idx, *line_entry_ptr);
378 if (best_match == UINT32_MAX)
380 else if (m_entries[idx].line < m_entries[best_match].line)
385 if (best_match != UINT32_MAX)
388 ConvertEntryAtIndexToLineEntry (best_match, *line_entry_ptr);
395 LineTable::FineLineEntriesForFileIndex (uint32_t file_idx,
397 SymbolContextList &sc_list)
403 size_t num_added = 0;
404 const size_t count = m_entries.size();
407 SymbolContext sc (m_comp_unit);
409 for (size_t idx = 0; idx < count; ++idx)
411 // Skip line table rows that terminate the previous row (is_terminal_entry is non-zero)
412 if (m_entries[idx].is_terminal_entry)
415 if (m_entries[idx].file_idx == file_idx)
417 if (ConvertEntryAtIndexToLineEntry (idx, sc.line_entry))
430 LineTable::Dump (Stream *s, Target *target, Address::DumpStyle style, Address::DumpStyle fallback_style, bool show_line_ranges)
432 const size_t count = m_entries.size();
433 LineEntry line_entry;
435 for (size_t idx = 0; idx < count; ++idx)
437 ConvertEntryAtIndexToLineEntry (idx, line_entry);
438 line_entry.Dump (s, target, prev_file != line_entry.file, style, fallback_style, show_line_ranges);
440 prev_file = line_entry.file;
446 LineTable::GetDescription (Stream *s, Target *target, DescriptionLevel level)
448 const size_t count = m_entries.size();
449 LineEntry line_entry;
450 for (size_t idx = 0; idx < count; ++idx)
452 ConvertEntryAtIndexToLineEntry (idx, line_entry);
453 line_entry.GetDescription (s, level, m_comp_unit, target, true);
459 LineTable::GetContiguousFileAddressRanges (FileAddressRanges &file_ranges, bool append)
463 const size_t initial_count = file_ranges.GetSize();
465 const size_t count = m_entries.size();
466 LineEntry line_entry;
467 FileAddressRanges::Entry range (LLDB_INVALID_ADDRESS, 0);
468 for (size_t idx = 0; idx < count; ++idx)
470 const Entry& entry = m_entries[idx];
472 if (entry.is_terminal_entry)
474 if (range.GetRangeBase() != LLDB_INVALID_ADDRESS)
476 range.SetRangeEnd(entry.file_addr);
477 file_ranges.Append(range);
478 range.Clear(LLDB_INVALID_ADDRESS);
481 else if (range.GetRangeBase() == LLDB_INVALID_ADDRESS)
483 range.SetRangeBase(entry.file_addr);
486 return file_ranges.GetSize() - initial_count;
490 LineTable::LinkLineTable (const FileRangeMap &file_range_map)
492 std::unique_ptr<LineTable> line_table_ap (new LineTable (m_comp_unit));
493 LineSequenceImpl sequence;
494 const size_t count = m_entries.size();
495 LineEntry line_entry;
496 const FileRangeMap::Entry *file_range_entry = NULL;
497 const FileRangeMap::Entry *prev_file_range_entry = NULL;
498 lldb::addr_t prev_file_addr = LLDB_INVALID_ADDRESS;
499 bool prev_entry_was_linked = false;
500 bool range_changed = false;
501 for (size_t idx = 0; idx < count; ++idx)
503 const Entry& entry = m_entries[idx];
505 const bool end_sequence = entry.is_terminal_entry;
506 const lldb::addr_t lookup_file_addr = entry.file_addr - (end_sequence ? 1 : 0);
507 if (file_range_entry == NULL || !file_range_entry->Contains(lookup_file_addr))
509 prev_file_range_entry = file_range_entry;
510 file_range_entry = file_range_map.FindEntryThatContains(lookup_file_addr);
511 range_changed = true;
514 lldb::addr_t prev_end_entry_linked_file_addr = LLDB_INVALID_ADDRESS;
515 lldb::addr_t entry_linked_file_addr = LLDB_INVALID_ADDRESS;
517 bool terminate_previous_entry = false;
518 if (file_range_entry)
520 entry_linked_file_addr = entry.file_addr - file_range_entry->GetRangeBase() + file_range_entry->data;
521 // Determine if we need to terminate the previous entry when the previous
522 // entry was not contguous with this one after being linked.
523 if (range_changed && prev_file_range_entry)
525 prev_end_entry_linked_file_addr = std::min<lldb::addr_t>(entry.file_addr, prev_file_range_entry->GetRangeEnd()) - prev_file_range_entry->GetRangeBase() + prev_file_range_entry->data;
526 if (prev_end_entry_linked_file_addr != entry_linked_file_addr)
527 terminate_previous_entry = prev_entry_was_linked;
530 else if (prev_entry_was_linked)
532 // This entry doesn't have a remapping and it needs to be removed.
533 // Watch out in case we need to terminate a previous entry needs to
534 // be terminated now that one line entry in a sequence is not longer valid.
535 if (!entry.is_terminal_entry &&
536 !sequence.m_entries.empty() &&
537 !sequence.m_entries.back().is_terminal_entry)
539 terminate_previous_entry = true;
543 if (terminate_previous_entry && !sequence.m_entries.empty())
545 assert (prev_file_addr != LLDB_INVALID_ADDRESS);
546 sequence.m_entries.push_back(sequence.m_entries.back());
547 if (prev_end_entry_linked_file_addr == LLDB_INVALID_ADDRESS)
548 prev_end_entry_linked_file_addr = std::min<lldb::addr_t>(entry.file_addr,prev_file_range_entry->GetRangeEnd()) - prev_file_range_entry->GetRangeBase() + prev_file_range_entry->data;
549 sequence.m_entries.back().file_addr = prev_end_entry_linked_file_addr;
550 sequence.m_entries.back().is_terminal_entry = true;
552 // Append the sequence since we just terminated the previous one
553 line_table_ap->InsertSequence (&sequence);
555 prev_entry_was_linked = false;
558 // Now link the current entry
559 if (file_range_entry)
561 // This entry has an address remapping and it needs to have its address relinked
562 sequence.m_entries.push_back(entry);
563 sequence.m_entries.back().file_addr = entry_linked_file_addr;
566 // If we have items in the sequence and the last entry is a terminal entry,
567 // insert this sequence into our new line table.
568 if (!sequence.m_entries.empty() && sequence.m_entries.back().is_terminal_entry)
570 line_table_ap->InsertSequence (&sequence);
572 prev_entry_was_linked = false;
576 prev_entry_was_linked = file_range_entry != NULL;
578 prev_file_addr = entry.file_addr;
579 range_changed = false;
581 if (line_table_ap->m_entries.empty())
583 return line_table_ap.release();