]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Symbol/LineTable.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / source / Symbol / LineTable.cpp
1 //===-- LineTable.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
16 #include <algorithm>
17
18 using namespace lldb;
19 using namespace lldb_private;
20
21 //----------------------------------------------------------------------
22 // LineTable constructor
23 //----------------------------------------------------------------------
24 LineTable::LineTable(CompileUnit *comp_unit)
25     : m_comp_unit(comp_unit), m_entries() {}
26
27 //----------------------------------------------------------------------
28 // Destructor
29 //----------------------------------------------------------------------
30 LineTable::~LineTable() {}
31
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,
40               is_terminal_entry);
41
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);
47
48   //  Stream s(stdout);
49   //  s << "\n\nBefore:\n";
50   //  Dump (&s, Address::DumpStyleFileAddress);
51   m_entries.insert(pos, entry);
52   //  s << "After:\n";
53   //  Dump (&s, Address::DumpStyleFileAddress);
54 }
55
56 LineSequence::LineSequence() {}
57
58 void LineTable::LineSequenceImpl::Clear() { m_entries.clear(); }
59
60 LineSequence *LineTable::CreateLineSequenceContainer() {
61   return new LineTable::LineSequenceImpl();
62 }
63
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,
73               is_terminal_entry);
74   entry_collection &entries = seq->m_entries;
75   // Replace the last entry if the address is the same, otherwise append it. If
76   // we have multiple
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
80   // resolved back to
81   // a symbol context, could resolve to a different line entry. We really want a
82   // 1 to 1 mapping
83   // here to avoid these kinds of inconsistencies. We will need tor revisit this
84   // if the DWARF line
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
90     // of the prologue
91     // and one for the first instruction after the prologue. If the size of the
92     // prologue is 0
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;
102   } else
103     entries.push_back(entry);
104 }
105
106 void LineTable::InsertSequence(LineSequence *sequence) {
107   assert(sequence != nullptr);
108   LineSequenceImpl *seq = reinterpret_cast<LineSequenceImpl *>(sequence);
109   if (seq->m_entries.empty())
110     return;
111   Entry &entry = seq->m_entries.front();
112
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());
119     return;
120   }
121
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);
128
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))
132       pos++;
133   }
134
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);
141   }
142 #endif
143   m_entries.insert(pos, seq->m_entries.begin(), seq->m_entries.end());
144 }
145
146 //----------------------------------------------------------------------
147 LineTable::Entry::LessThanBinaryPredicate::LessThanBinaryPredicate(
148     LineTable *line_table)
149     : m_line_table(line_table) {}
150
151 bool LineTable::Entry::LessThanBinaryPredicate::
152 operator()(const LineTable::Entry &a, const LineTable::Entry &b) const {
153 #define LT_COMPARE(a, b)                                                       \
154   if (a != b)                                                                  \
155   return 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);
167   return false;
168 #undef LT_COMPARE
169 }
170
171 uint32_t LineTable::GetSize() const { return m_entries.size(); }
172
173 bool LineTable::GetLineEntryAtIndex(uint32_t idx, LineEntry &line_entry) {
174   if (idx < m_entries.size()) {
175     ConvertEntryAtIndexToLineEntry(idx, line_entry);
176     return true;
177   }
178   line_entry.Clear();
179   return false;
180 }
181
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;
187
188   bool success = false;
189
190   if (so_addr.GetModule().get() == m_comp_unit->GetModule().get()) {
191     Entry search_entry;
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)
201             --pos;
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
206             // previous entry.
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.
211               ++pos;
212               if (pos != end_pos) {
213                 if (pos->file_addr != search_entry.file_addr)
214                   pos = end_pos;
215               }
216             }
217
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
221               // multiple
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)
226                   --pos;
227                 else
228                   break;
229               }
230             }
231           }
232         }
233
234         // Make sure we have a valid match and that the match isn't a
235         // terminating
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;
242         }
243       }
244     }
245   }
246   return success;
247 }
248
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());
254     if (module_sp &&
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 -
259                                      entry.file_addr);
260       else
261         line_entry.range.SetByteSize(0);
262
263       line_entry.file =
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;
274       return true;
275     }
276   }
277   return false;
278 }
279
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) {
283
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;
288
289   for (size_t idx = start_idx; idx < count; ++idx) {
290     // Skip line table rows that terminate the previous row (is_terminal_entry
291     // is non-zero)
292     if (m_entries[idx].is_terminal_entry)
293       continue;
294
295     if (find(begin_pos, end_pos, m_entries[idx].file_idx) == end_pos)
296       continue;
297
298     // Exact match always wins.  Otherwise try to find the closest line > the
299     // desired
300     // line.
301     // FIXME: Maybe want to find the line closest before and the line closest
302     // after and
303     // if they're not in the same function, don't return a match.
304
305     if (m_entries[idx].line < line) {
306       continue;
307     } else if (m_entries[idx].line == line) {
308       if (line_entry_ptr)
309         ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr);
310       return idx;
311     } else if (!exact) {
312       if (best_match == UINT32_MAX)
313         best_match = idx;
314       else if (m_entries[idx].line < m_entries[best_match].line)
315         best_match = idx;
316     }
317   }
318
319   if (best_match != UINT32_MAX) {
320     if (line_entry_ptr)
321       ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr);
322     return best_match;
323   }
324   return UINT32_MAX;
325 }
326
327 uint32_t LineTable::FindLineEntryIndexByFileIndex(uint32_t start_idx,
328                                                   uint32_t file_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;
333
334   for (size_t idx = start_idx; idx < count; ++idx) {
335     // Skip line table rows that terminate the previous row (is_terminal_entry
336     // is non-zero)
337     if (m_entries[idx].is_terminal_entry)
338       continue;
339
340     if (m_entries[idx].file_idx != file_idx)
341       continue;
342
343     // Exact match always wins.  Otherwise try to find the closest line > the
344     // desired
345     // line.
346     // FIXME: Maybe want to find the line closest before and the line closest
347     // after and
348     // if they're not in the same function, don't return a match.
349
350     if (m_entries[idx].line < line) {
351       continue;
352     } else if (m_entries[idx].line == line) {
353       if (line_entry_ptr)
354         ConvertEntryAtIndexToLineEntry(idx, *line_entry_ptr);
355       return idx;
356     } else if (!exact) {
357       if (best_match == UINT32_MAX)
358         best_match = idx;
359       else if (m_entries[idx].line < m_entries[best_match].line)
360         best_match = idx;
361     }
362   }
363
364   if (best_match != UINT32_MAX) {
365     if (line_entry_ptr)
366       ConvertEntryAtIndexToLineEntry(best_match, *line_entry_ptr);
367     return best_match;
368   }
369   return UINT32_MAX;
370 }
371
372 size_t LineTable::FineLineEntriesForFileIndex(uint32_t file_idx, bool append,
373                                               SymbolContextList &sc_list) {
374
375   if (!append)
376     sc_list.Clear();
377
378   size_t num_added = 0;
379   const size_t count = m_entries.size();
380   if (count > 0) {
381     SymbolContext sc(m_comp_unit);
382
383     for (size_t idx = 0; idx < count; ++idx) {
384       // Skip line table rows that terminate the previous row (is_terminal_entry
385       // is non-zero)
386       if (m_entries[idx].is_terminal_entry)
387         continue;
388
389       if (m_entries[idx].file_idx == file_idx) {
390         if (ConvertEntryAtIndexToLineEntry(idx, sc.line_entry)) {
391           ++num_added;
392           sc_list.Append(sc);
393         }
394       }
395     }
396   }
397   return num_added;
398 }
399
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;
404   FileSpec prev_file;
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);
409     s->EOL();
410     prev_file = line_entry.original_file;
411   }
412 }
413
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);
421     s->EOL();
422   }
423 }
424
425 size_t LineTable::GetContiguousFileAddressRanges(FileAddressRanges &file_ranges,
426                                                  bool append) {
427   if (!append)
428     file_ranges.Clear();
429   const size_t initial_count = file_ranges.GetSize();
430
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];
436
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);
442       }
443     } else if (range.GetRangeBase() == LLDB_INVALID_ADDRESS) {
444       range.SetRangeBase(entry.file_addr);
445     }
446   }
447   return file_ranges.GetSize() - initial_count;
448 }
449
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];
462
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;
471     }
472
473     lldb::addr_t prev_end_entry_linked_file_addr = LLDB_INVALID_ADDRESS;
474     lldb::addr_t entry_linked_file_addr = LLDB_INVALID_ADDRESS;
475
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;
490       }
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
495       // valid.
496       if (!sequence.m_entries.empty() &&
497           !sequence.m_entries.back().is_terminal_entry) {
498         terminate_previous_entry = true;
499       }
500     }
501
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;
512
513       // Append the sequence since we just terminated the previous one
514       line_table_ap->InsertSequence(&sequence);
515       sequence.Clear();
516     }
517
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
521       // relinked
522       sequence.m_entries.push_back(entry);
523       sequence.m_entries.back().file_addr = entry_linked_file_addr;
524     }
525
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);
531       sequence.Clear();
532       prev_entry_was_linked = false;
533     } else {
534       prev_entry_was_linked = file_range_entry != nullptr;
535     }
536     prev_file_addr = entry.file_addr;
537     range_changed = false;
538   }
539   if (line_table_ap->m_entries.empty())
540     return nullptr;
541   return line_table_ap.release();
542 }