xref: /freebsd/contrib/llvm-project/lldb/source/Symbol/LineEntry.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- LineEntry.cpp -------------------------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Symbol/LineEntry.h"
10*0b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
11*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
12*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric using namespace lldb_private;
15*0b57cec5SDimitry Andric 
16*0b57cec5SDimitry Andric LineEntry::LineEntry()
17*0b57cec5SDimitry Andric     : range(), file(), line(LLDB_INVALID_LINE_NUMBER), column(0),
18*0b57cec5SDimitry Andric       is_start_of_statement(0), is_start_of_basic_block(0), is_prologue_end(0),
19*0b57cec5SDimitry Andric       is_epilogue_begin(0), is_terminal_entry(0) {}
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric LineEntry::LineEntry(const lldb::SectionSP &section_sp,
22*0b57cec5SDimitry Andric                      lldb::addr_t section_offset, lldb::addr_t byte_size,
23*0b57cec5SDimitry Andric                      const FileSpec &_file, uint32_t _line, uint16_t _column,
24*0b57cec5SDimitry Andric                      bool _is_start_of_statement, bool _is_start_of_basic_block,
25*0b57cec5SDimitry Andric                      bool _is_prologue_end, bool _is_epilogue_begin,
26*0b57cec5SDimitry Andric                      bool _is_terminal_entry)
27*0b57cec5SDimitry Andric     : range(section_sp, section_offset, byte_size), file(_file),
28*0b57cec5SDimitry Andric       original_file(_file), line(_line), column(_column),
29*0b57cec5SDimitry Andric       is_start_of_statement(_is_start_of_statement),
30*0b57cec5SDimitry Andric       is_start_of_basic_block(_is_start_of_basic_block),
31*0b57cec5SDimitry Andric       is_prologue_end(_is_prologue_end), is_epilogue_begin(_is_epilogue_begin),
32*0b57cec5SDimitry Andric       is_terminal_entry(_is_terminal_entry) {}
33*0b57cec5SDimitry Andric 
34*0b57cec5SDimitry Andric void LineEntry::Clear() {
35*0b57cec5SDimitry Andric   range.Clear();
36*0b57cec5SDimitry Andric   file.Clear();
37*0b57cec5SDimitry Andric   original_file.Clear();
38*0b57cec5SDimitry Andric   line = LLDB_INVALID_LINE_NUMBER;
39*0b57cec5SDimitry Andric   column = 0;
40*0b57cec5SDimitry Andric   is_start_of_statement = 0;
41*0b57cec5SDimitry Andric   is_start_of_basic_block = 0;
42*0b57cec5SDimitry Andric   is_prologue_end = 0;
43*0b57cec5SDimitry Andric   is_epilogue_begin = 0;
44*0b57cec5SDimitry Andric   is_terminal_entry = 0;
45*0b57cec5SDimitry Andric }
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric bool LineEntry::IsValid() const {
48*0b57cec5SDimitry Andric   return range.GetBaseAddress().IsValid() && line != LLDB_INVALID_LINE_NUMBER;
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
51*0b57cec5SDimitry Andric bool LineEntry::DumpStopContext(Stream *s, bool show_fullpaths) const {
52*0b57cec5SDimitry Andric   if (file) {
53*0b57cec5SDimitry Andric     if (show_fullpaths)
54*0b57cec5SDimitry Andric       file.Dump(s);
55*0b57cec5SDimitry Andric     else
56*0b57cec5SDimitry Andric       file.GetFilename().Dump(s);
57*0b57cec5SDimitry Andric 
58*0b57cec5SDimitry Andric     if (line)
59*0b57cec5SDimitry Andric       s->PutChar(':');
60*0b57cec5SDimitry Andric   }
61*0b57cec5SDimitry Andric   if (line) {
62*0b57cec5SDimitry Andric     s->Printf("%u", line);
63*0b57cec5SDimitry Andric     if (column) {
64*0b57cec5SDimitry Andric       s->PutChar(':');
65*0b57cec5SDimitry Andric       s->Printf("%u", column);
66*0b57cec5SDimitry Andric     }
67*0b57cec5SDimitry Andric   }
68*0b57cec5SDimitry Andric   return file || line;
69*0b57cec5SDimitry Andric }
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric bool LineEntry::Dump(Stream *s, Target *target, bool show_file,
72*0b57cec5SDimitry Andric                      Address::DumpStyle style,
73*0b57cec5SDimitry Andric                      Address::DumpStyle fallback_style, bool show_range) const {
74*0b57cec5SDimitry Andric   if (show_range) {
75*0b57cec5SDimitry Andric     // Show address range
76*0b57cec5SDimitry Andric     if (!range.Dump(s, target, style, fallback_style))
77*0b57cec5SDimitry Andric       return false;
78*0b57cec5SDimitry Andric   } else {
79*0b57cec5SDimitry Andric     // Show address only
80*0b57cec5SDimitry Andric     if (!range.GetBaseAddress().Dump(s, target, style, fallback_style))
81*0b57cec5SDimitry Andric       return false;
82*0b57cec5SDimitry Andric   }
83*0b57cec5SDimitry Andric   if (show_file)
84*0b57cec5SDimitry Andric     *s << ", file = " << file;
85*0b57cec5SDimitry Andric   if (line)
86*0b57cec5SDimitry Andric     s->Printf(", line = %u", line);
87*0b57cec5SDimitry Andric   if (column)
88*0b57cec5SDimitry Andric     s->Printf(", column = %u", column);
89*0b57cec5SDimitry Andric   if (is_start_of_statement)
90*0b57cec5SDimitry Andric     *s << ", is_start_of_statement = TRUE";
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric   if (is_start_of_basic_block)
93*0b57cec5SDimitry Andric     *s << ", is_start_of_basic_block = TRUE";
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric   if (is_prologue_end)
96*0b57cec5SDimitry Andric     *s << ", is_prologue_end = TRUE";
97*0b57cec5SDimitry Andric 
98*0b57cec5SDimitry Andric   if (is_epilogue_begin)
99*0b57cec5SDimitry Andric     *s << ", is_epilogue_begin = TRUE";
100*0b57cec5SDimitry Andric 
101*0b57cec5SDimitry Andric   if (is_terminal_entry)
102*0b57cec5SDimitry Andric     *s << ", is_terminal_entry = TRUE";
103*0b57cec5SDimitry Andric   return true;
104*0b57cec5SDimitry Andric }
105*0b57cec5SDimitry Andric 
106*0b57cec5SDimitry Andric bool LineEntry::GetDescription(Stream *s, lldb::DescriptionLevel level,
107*0b57cec5SDimitry Andric                                CompileUnit *cu, Target *target,
108*0b57cec5SDimitry Andric                                bool show_address_only) const {
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric   if (level == lldb::eDescriptionLevelBrief ||
111*0b57cec5SDimitry Andric       level == lldb::eDescriptionLevelFull) {
112*0b57cec5SDimitry Andric     if (show_address_only) {
113*0b57cec5SDimitry Andric       range.GetBaseAddress().Dump(s, target, Address::DumpStyleLoadAddress,
114*0b57cec5SDimitry Andric                                   Address::DumpStyleFileAddress);
115*0b57cec5SDimitry Andric     } else {
116*0b57cec5SDimitry Andric       range.Dump(s, target, Address::DumpStyleLoadAddress,
117*0b57cec5SDimitry Andric                  Address::DumpStyleFileAddress);
118*0b57cec5SDimitry Andric     }
119*0b57cec5SDimitry Andric 
120*0b57cec5SDimitry Andric     *s << ": " << file;
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric     if (line) {
123*0b57cec5SDimitry Andric       s->Printf(":%u", line);
124*0b57cec5SDimitry Andric       if (column)
125*0b57cec5SDimitry Andric         s->Printf(":%u", column);
126*0b57cec5SDimitry Andric     }
127*0b57cec5SDimitry Andric 
128*0b57cec5SDimitry Andric     if (level == lldb::eDescriptionLevelFull) {
129*0b57cec5SDimitry Andric       if (is_start_of_statement)
130*0b57cec5SDimitry Andric         *s << ", is_start_of_statement = TRUE";
131*0b57cec5SDimitry Andric 
132*0b57cec5SDimitry Andric       if (is_start_of_basic_block)
133*0b57cec5SDimitry Andric         *s << ", is_start_of_basic_block = TRUE";
134*0b57cec5SDimitry Andric 
135*0b57cec5SDimitry Andric       if (is_prologue_end)
136*0b57cec5SDimitry Andric         *s << ", is_prologue_end = TRUE";
137*0b57cec5SDimitry Andric 
138*0b57cec5SDimitry Andric       if (is_epilogue_begin)
139*0b57cec5SDimitry Andric         *s << ", is_epilogue_begin = TRUE";
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric       if (is_terminal_entry)
142*0b57cec5SDimitry Andric         *s << ", is_terminal_entry = TRUE";
143*0b57cec5SDimitry Andric     } else {
144*0b57cec5SDimitry Andric       if (is_terminal_entry)
145*0b57cec5SDimitry Andric         s->EOL();
146*0b57cec5SDimitry Andric     }
147*0b57cec5SDimitry Andric   } else {
148*0b57cec5SDimitry Andric     return Dump(s, target, true, Address::DumpStyleLoadAddress,
149*0b57cec5SDimitry Andric                 Address::DumpStyleModuleWithFileAddress, true);
150*0b57cec5SDimitry Andric   }
151*0b57cec5SDimitry Andric   return true;
152*0b57cec5SDimitry Andric }
153*0b57cec5SDimitry Andric 
154*0b57cec5SDimitry Andric bool lldb_private::operator<(const LineEntry &a, const LineEntry &b) {
155*0b57cec5SDimitry Andric   return LineEntry::Compare(a, b) < 0;
156*0b57cec5SDimitry Andric }
157*0b57cec5SDimitry Andric 
158*0b57cec5SDimitry Andric int LineEntry::Compare(const LineEntry &a, const LineEntry &b) {
159*0b57cec5SDimitry Andric   int result = Address::CompareFileAddress(a.range.GetBaseAddress(),
160*0b57cec5SDimitry Andric                                            b.range.GetBaseAddress());
161*0b57cec5SDimitry Andric   if (result != 0)
162*0b57cec5SDimitry Andric     return result;
163*0b57cec5SDimitry Andric 
164*0b57cec5SDimitry Andric   const lldb::addr_t a_byte_size = a.range.GetByteSize();
165*0b57cec5SDimitry Andric   const lldb::addr_t b_byte_size = b.range.GetByteSize();
166*0b57cec5SDimitry Andric 
167*0b57cec5SDimitry Andric   if (a_byte_size < b_byte_size)
168*0b57cec5SDimitry Andric     return -1;
169*0b57cec5SDimitry Andric   if (a_byte_size > b_byte_size)
170*0b57cec5SDimitry Andric     return +1;
171*0b57cec5SDimitry Andric 
172*0b57cec5SDimitry Andric   // Check for an end sequence entry mismatch after we have determined that the
173*0b57cec5SDimitry Andric   // address values are equal. If one of the items is an end sequence, we don't
174*0b57cec5SDimitry Andric   // care about the line, file, or column info.
175*0b57cec5SDimitry Andric   if (a.is_terminal_entry > b.is_terminal_entry)
176*0b57cec5SDimitry Andric     return -1;
177*0b57cec5SDimitry Andric   if (a.is_terminal_entry < b.is_terminal_entry)
178*0b57cec5SDimitry Andric     return +1;
179*0b57cec5SDimitry Andric 
180*0b57cec5SDimitry Andric   if (a.line < b.line)
181*0b57cec5SDimitry Andric     return -1;
182*0b57cec5SDimitry Andric   if (a.line > b.line)
183*0b57cec5SDimitry Andric     return +1;
184*0b57cec5SDimitry Andric 
185*0b57cec5SDimitry Andric   if (a.column < b.column)
186*0b57cec5SDimitry Andric     return -1;
187*0b57cec5SDimitry Andric   if (a.column > b.column)
188*0b57cec5SDimitry Andric     return +1;
189*0b57cec5SDimitry Andric 
190*0b57cec5SDimitry Andric   return FileSpec::Compare(a.file, b.file, true);
191*0b57cec5SDimitry Andric }
192*0b57cec5SDimitry Andric 
193*0b57cec5SDimitry Andric AddressRange LineEntry::GetSameLineContiguousAddressRange(
194*0b57cec5SDimitry Andric     bool include_inlined_functions) const {
195*0b57cec5SDimitry Andric   // Add each LineEntry's range to complete_line_range until we find a
196*0b57cec5SDimitry Andric   // different file / line number.
197*0b57cec5SDimitry Andric   AddressRange complete_line_range = range;
198*0b57cec5SDimitry Andric   auto symbol_context_scope = lldb::eSymbolContextLineEntry;
199*0b57cec5SDimitry Andric   Declaration start_call_site(original_file, line);
200*0b57cec5SDimitry Andric   if (include_inlined_functions)
201*0b57cec5SDimitry Andric     symbol_context_scope |= lldb::eSymbolContextBlock;
202*0b57cec5SDimitry Andric 
203*0b57cec5SDimitry Andric   while (true) {
204*0b57cec5SDimitry Andric     SymbolContext next_line_sc;
205*0b57cec5SDimitry Andric     Address range_end(complete_line_range.GetBaseAddress());
206*0b57cec5SDimitry Andric     range_end.Slide(complete_line_range.GetByteSize());
207*0b57cec5SDimitry Andric     range_end.CalculateSymbolContext(&next_line_sc, symbol_context_scope);
208*0b57cec5SDimitry Andric 
209*0b57cec5SDimitry Andric     if (!next_line_sc.line_entry.IsValid() ||
210*0b57cec5SDimitry Andric         next_line_sc.line_entry.range.GetByteSize() == 0)
211*0b57cec5SDimitry Andric       break;
212*0b57cec5SDimitry Andric 
213*0b57cec5SDimitry Andric     if (original_file == next_line_sc.line_entry.original_file &&
214*0b57cec5SDimitry Andric         (next_line_sc.line_entry.line == 0 ||
215*0b57cec5SDimitry Andric          line == next_line_sc.line_entry.line)) {
216*0b57cec5SDimitry Andric       // Include any line 0 entries - they indicate that this is compiler-
217*0b57cec5SDimitry Andric       // generated code that does not correspond to user source code.
218*0b57cec5SDimitry Andric       // next_line_sc is the same file & line as this LineEntry, so extend
219*0b57cec5SDimitry Andric       // our AddressRange by its size and continue to see if there are more
220*0b57cec5SDimitry Andric       // LineEntries that we can combine. However, if there was nothing to
221*0b57cec5SDimitry Andric       // extend we're done.
222*0b57cec5SDimitry Andric       if (!complete_line_range.Extend(next_line_sc.line_entry.range))
223*0b57cec5SDimitry Andric         break;
224*0b57cec5SDimitry Andric       continue;
225*0b57cec5SDimitry Andric     }
226*0b57cec5SDimitry Andric 
227*0b57cec5SDimitry Andric     if (include_inlined_functions && next_line_sc.block &&
228*0b57cec5SDimitry Andric         next_line_sc.block->GetContainingInlinedBlock() != nullptr) {
229*0b57cec5SDimitry Andric       // The next_line_sc might be in a different file if it's an inlined
230*0b57cec5SDimitry Andric       // function. If this is the case then we still want to expand our line
231*0b57cec5SDimitry Andric       // range to include them if the inlined function is at the same call site
232*0b57cec5SDimitry Andric       // as this line entry. The current block could represent a nested inline
233*0b57cec5SDimitry Andric       // function call so we need to need to check up the block tree to see if
234*0b57cec5SDimitry Andric       // we find one.
235*0b57cec5SDimitry Andric       auto inlined_parent_block =
236*0b57cec5SDimitry Andric           next_line_sc.block->GetContainingInlinedBlockWithCallSite(
237*0b57cec5SDimitry Andric               start_call_site);
238*0b57cec5SDimitry Andric       if (!inlined_parent_block)
239*0b57cec5SDimitry Andric         // We didn't find any parent inlined block with a call site at this line
240*0b57cec5SDimitry Andric         // entry so this inlined function is probably at another line.
241*0b57cec5SDimitry Andric         break;
242*0b57cec5SDimitry Andric       // Extend our AddressRange by the size of the inlined block, but if there
243*0b57cec5SDimitry Andric       // was nothing to add then we're done.
244*0b57cec5SDimitry Andric       if (!complete_line_range.Extend(next_line_sc.line_entry.range))
245*0b57cec5SDimitry Andric         break;
246*0b57cec5SDimitry Andric       continue;
247*0b57cec5SDimitry Andric     }
248*0b57cec5SDimitry Andric 
249*0b57cec5SDimitry Andric     break;
250*0b57cec5SDimitry Andric   }
251*0b57cec5SDimitry Andric   return complete_line_range;
252*0b57cec5SDimitry Andric }
253*0b57cec5SDimitry Andric 
254*0b57cec5SDimitry Andric void LineEntry::ApplyFileMappings(lldb::TargetSP target_sp) {
255*0b57cec5SDimitry Andric   if (target_sp) {
256*0b57cec5SDimitry Andric     // Apply any file remappings to our file
257*0b57cec5SDimitry Andric     FileSpec new_file_spec;
258*0b57cec5SDimitry Andric     if (target_sp->GetSourcePathMap().FindFile(original_file, new_file_spec))
259*0b57cec5SDimitry Andric       file = new_file_spec;
260*0b57cec5SDimitry Andric   }
261*0b57cec5SDimitry Andric }
262