xref: /freebsd-src/contrib/llvm-project/lldb/source/Symbol/LineTable.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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     const Entry &entry = m_entries[idx];
246     ModuleSP module_sp(m_comp_unit->GetModule());
247     if (module_sp &&
248         module_sp->ResolveFileAddress(entry.file_addr,
249                                       line_entry.range.GetBaseAddress())) {
250       if (!entry.is_terminal_entry && idx + 1 < m_entries.size())
251         line_entry.range.SetByteSize(m_entries[idx + 1].file_addr -
252                                      entry.file_addr);
253       else
254         line_entry.range.SetByteSize(0);
255 
256       line_entry.file =
257           m_comp_unit->GetSupportFiles().GetFileSpecAtIndex(entry.file_idx);
258       line_entry.original_file =
259           m_comp_unit->GetSupportFiles().GetFileSpecAtIndex(entry.file_idx);
260       line_entry.line = entry.line;
261       line_entry.column = entry.column;
262       line_entry.is_start_of_statement = entry.is_start_of_statement;
263       line_entry.is_start_of_basic_block = entry.is_start_of_basic_block;
264       line_entry.is_prologue_end = entry.is_prologue_end;
265       line_entry.is_epilogue_begin = entry.is_epilogue_begin;
266       line_entry.is_terminal_entry = entry.is_terminal_entry;
267       return true;
268     }
269   }
270   return false;
271 }
272 
273 uint32_t LineTable::FindLineEntryIndexByFileIndex(
274     uint32_t start_idx, const std::vector<uint32_t> &file_indexes,
275     uint32_t line, bool exact, LineEntry *line_entry_ptr) {
276 
277   const size_t count = m_entries.size();
278   std::vector<uint32_t>::const_iterator begin_pos = file_indexes.begin();
279   std::vector<uint32_t>::const_iterator end_pos = file_indexes.end();
280   size_t best_match = UINT32_MAX;
281 
282   for (size_t idx = start_idx; idx < count; ++idx) {
283     // Skip line table rows that terminate the previous row (is_terminal_entry
284     // is non-zero)
285     if (m_entries[idx].is_terminal_entry)
286       continue;
287 
288     if (find(begin_pos, end_pos, m_entries[idx].file_idx) == end_pos)
289       continue;
290 
291     // Exact match always wins.  Otherwise try to find the closest line > the
292     // desired line.
293     // FIXME: Maybe want to find the line closest before and the line closest
294     // after and
295     // if they're not in the same function, don't return a match.
296 
297     if (m_entries[idx].line < line) {
298       continue;
299     } else if (m_entries[idx].line == line) {
300       if (line_entry_ptr)
301         ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr);
302       return idx;
303     } else if (!exact) {
304       if (best_match == UINT32_MAX)
305         best_match = idx;
306       else if (m_entries[idx].line < m_entries[best_match].line)
307         best_match = idx;
308     }
309   }
310 
311   if (best_match != UINT32_MAX) {
312     if (line_entry_ptr)
313       ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr);
314     return best_match;
315   }
316   return UINT32_MAX;
317 }
318 
319 uint32_t LineTable::FindLineEntryIndexByFileIndex(uint32_t start_idx,
320                                                   uint32_t file_idx,
321                                                   uint32_t line, bool exact,
322                                                   LineEntry *line_entry_ptr) {
323   const size_t count = m_entries.size();
324   size_t best_match = UINT32_MAX;
325 
326   for (size_t idx = start_idx; idx < count; ++idx) {
327     // Skip line table rows that terminate the previous row (is_terminal_entry
328     // is non-zero)
329     if (m_entries[idx].is_terminal_entry)
330       continue;
331 
332     if (m_entries[idx].file_idx != file_idx)
333       continue;
334 
335     // Exact match always wins.  Otherwise try to find the closest line > the
336     // desired line.
337     // FIXME: Maybe want to find the line closest before and the line closest
338     // after and
339     // if they're not in the same function, don't return a match.
340 
341     if (m_entries[idx].line < line) {
342       continue;
343     } else if (m_entries[idx].line == line) {
344       if (line_entry_ptr)
345         ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr);
346       return idx;
347     } else if (!exact) {
348       if (best_match == UINT32_MAX)
349         best_match = idx;
350       else if (m_entries[idx].line < m_entries[best_match].line)
351         best_match = idx;
352     }
353   }
354 
355   if (best_match != UINT32_MAX) {
356     if (line_entry_ptr)
357       ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr);
358     return best_match;
359   }
360   return UINT32_MAX;
361 }
362 
363 size_t LineTable::FineLineEntriesForFileIndex(uint32_t file_idx, bool append,
364                                               SymbolContextList &sc_list) {
365 
366   if (!append)
367     sc_list.Clear();
368 
369   size_t num_added = 0;
370   const size_t count = m_entries.size();
371   if (count > 0) {
372     SymbolContext sc(m_comp_unit);
373 
374     for (size_t idx = 0; idx < count; ++idx) {
375       // Skip line table rows that terminate the previous row
376       // (is_terminal_entry is non-zero)
377       if (m_entries[idx].is_terminal_entry)
378         continue;
379 
380       if (m_entries[idx].file_idx == file_idx) {
381         if (ConvertEntryAtIndexToLineEntry(idx, sc.line_entry)) {
382           ++num_added;
383           sc_list.Append(sc);
384         }
385       }
386     }
387   }
388   return num_added;
389 }
390 
391 void LineTable::Dump(Stream *s, Target *target, Address::DumpStyle style,
392                      Address::DumpStyle fallback_style, bool show_line_ranges) {
393   const size_t count = m_entries.size();
394   LineEntry line_entry;
395   FileSpec prev_file;
396   for (size_t idx = 0; idx < count; ++idx) {
397     ConvertEntryAtIndexToLineEntry(idx, line_entry);
398     line_entry.Dump(s, target, prev_file != line_entry.original_file, style,
399                     fallback_style, show_line_ranges);
400     s->EOL();
401     prev_file = line_entry.original_file;
402   }
403 }
404 
405 void LineTable::GetDescription(Stream *s, Target *target,
406                                DescriptionLevel level) {
407   const size_t count = m_entries.size();
408   LineEntry line_entry;
409   for (size_t idx = 0; idx < count; ++idx) {
410     ConvertEntryAtIndexToLineEntry(idx, line_entry);
411     line_entry.GetDescription(s, level, m_comp_unit, target, true);
412     s->EOL();
413   }
414 }
415 
416 size_t LineTable::GetContiguousFileAddressRanges(FileAddressRanges &file_ranges,
417                                                  bool append) {
418   if (!append)
419     file_ranges.Clear();
420   const size_t initial_count = file_ranges.GetSize();
421 
422   const size_t count = m_entries.size();
423   LineEntry line_entry;
424   FileAddressRanges::Entry range(LLDB_INVALID_ADDRESS, 0);
425   for (size_t idx = 0; idx < count; ++idx) {
426     const Entry &entry = m_entries[idx];
427 
428     if (entry.is_terminal_entry) {
429       if (range.GetRangeBase() != LLDB_INVALID_ADDRESS) {
430         range.SetRangeEnd(entry.file_addr);
431         file_ranges.Append(range);
432         range.Clear(LLDB_INVALID_ADDRESS);
433       }
434     } else if (range.GetRangeBase() == LLDB_INVALID_ADDRESS) {
435       range.SetRangeBase(entry.file_addr);
436     }
437   }
438   return file_ranges.GetSize() - initial_count;
439 }
440 
441 LineTable *LineTable::LinkLineTable(const FileRangeMap &file_range_map) {
442   std::unique_ptr<LineTable> line_table_up(new LineTable(m_comp_unit));
443   LineSequenceImpl sequence;
444   const size_t count = m_entries.size();
445   LineEntry line_entry;
446   const FileRangeMap::Entry *file_range_entry = nullptr;
447   const FileRangeMap::Entry *prev_file_range_entry = nullptr;
448   lldb::addr_t prev_file_addr = LLDB_INVALID_ADDRESS;
449   bool prev_entry_was_linked = false;
450   bool range_changed = false;
451   for (size_t idx = 0; idx < count; ++idx) {
452     const Entry &entry = m_entries[idx];
453 
454     const bool end_sequence = entry.is_terminal_entry;
455     const lldb::addr_t lookup_file_addr =
456         entry.file_addr - (end_sequence ? 1 : 0);
457     if (file_range_entry == nullptr ||
458         !file_range_entry->Contains(lookup_file_addr)) {
459       prev_file_range_entry = file_range_entry;
460       file_range_entry = file_range_map.FindEntryThatContains(lookup_file_addr);
461       range_changed = true;
462     }
463 
464     lldb::addr_t prev_end_entry_linked_file_addr = LLDB_INVALID_ADDRESS;
465     lldb::addr_t entry_linked_file_addr = LLDB_INVALID_ADDRESS;
466 
467     bool terminate_previous_entry = false;
468     if (file_range_entry) {
469       entry_linked_file_addr = entry.file_addr -
470                                file_range_entry->GetRangeBase() +
471                                file_range_entry->data;
472       // Determine if we need to terminate the previous entry when the previous
473       // entry was not contiguous with this one after being linked.
474       if (range_changed && prev_file_range_entry) {
475         prev_end_entry_linked_file_addr =
476             std::min<lldb::addr_t>(entry.file_addr,
477                                    prev_file_range_entry->GetRangeEnd()) -
478             prev_file_range_entry->GetRangeBase() + prev_file_range_entry->data;
479         if (prev_end_entry_linked_file_addr != entry_linked_file_addr)
480           terminate_previous_entry = prev_entry_was_linked;
481       }
482     } else if (prev_entry_was_linked) {
483       // This entry doesn't have a remapping and it needs to be removed. Watch
484       // out in case we need to terminate a previous entry needs to be
485       // terminated now that one line entry in a sequence is not longer valid.
486       if (!sequence.m_entries.empty() &&
487           !sequence.m_entries.back().is_terminal_entry) {
488         terminate_previous_entry = true;
489       }
490     }
491 
492     if (terminate_previous_entry && !sequence.m_entries.empty()) {
493       assert(prev_file_addr != LLDB_INVALID_ADDRESS);
494       UNUSED_IF_ASSERT_DISABLED(prev_file_addr);
495       sequence.m_entries.push_back(sequence.m_entries.back());
496       if (prev_end_entry_linked_file_addr == LLDB_INVALID_ADDRESS)
497         prev_end_entry_linked_file_addr =
498             std::min<lldb::addr_t>(entry.file_addr,
499                                    prev_file_range_entry->GetRangeEnd()) -
500             prev_file_range_entry->GetRangeBase() + prev_file_range_entry->data;
501       sequence.m_entries.back().file_addr = prev_end_entry_linked_file_addr;
502       sequence.m_entries.back().is_terminal_entry = true;
503 
504       // Append the sequence since we just terminated the previous one
505       line_table_up->InsertSequence(&sequence);
506       sequence.Clear();
507     }
508 
509     // Now link the current entry
510     if (file_range_entry) {
511       // This entry has an address remapping and it needs to have its address
512       // relinked
513       sequence.m_entries.push_back(entry);
514       sequence.m_entries.back().file_addr = entry_linked_file_addr;
515     }
516 
517     // If we have items in the sequence and the last entry is a terminal entry,
518     // insert this sequence into our new line table.
519     if (!sequence.m_entries.empty() &&
520         sequence.m_entries.back().is_terminal_entry) {
521       line_table_up->InsertSequence(&sequence);
522       sequence.Clear();
523       prev_entry_was_linked = false;
524     } else {
525       prev_entry_was_linked = file_range_entry != nullptr;
526     }
527     prev_file_addr = entry.file_addr;
528     range_changed = false;
529   }
530   if (line_table_up->m_entries.empty())
531     return nullptr;
532   return line_table_up.release();
533 }
534