1 //===-- LineEntry.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/LineEntry.h"
10 #include "lldb/Symbol/CompileUnit.h"
11 #include "lldb/Target/Process.h"
12 #include "lldb/Target/Target.h"
14 using namespace lldb_private
;
16 LineEntry::LineEntry()
17 : range(), file_sp(std::make_shared
<SupportFile
>()),
18 original_file_sp(std::make_shared
<SupportFile
>()),
19 is_start_of_statement(0), is_start_of_basic_block(0), is_prologue_end(0),
20 is_epilogue_begin(0), is_terminal_entry(0) {}
22 void LineEntry::Clear() {
24 file_sp
= std::make_shared
<SupportFile
>();
25 original_file_sp
= std::make_shared
<SupportFile
>();
26 line
= LLDB_INVALID_LINE_NUMBER
;
28 is_start_of_statement
= 0;
29 is_start_of_basic_block
= 0;
31 is_epilogue_begin
= 0;
32 is_terminal_entry
= 0;
35 bool LineEntry::IsValid() const {
36 return range
.GetBaseAddress().IsValid() && line
!= LLDB_INVALID_LINE_NUMBER
;
39 bool LineEntry::DumpStopContext(Stream
*s
, bool show_fullpaths
) const {
40 const FileSpec
&file
= file_sp
->GetSpecOnly();
43 file
.Dump(s
->AsRawOstream());
45 file
.GetFilename().Dump(s
);
51 s
->Printf("%u", line
);
54 s
->Printf("%u", column
);
60 bool LineEntry::Dump(Stream
*s
, Target
*target
, bool show_file
,
61 Address::DumpStyle style
,
62 Address::DumpStyle fallback_style
, bool show_range
) const {
65 if (!range
.Dump(s
, target
, style
, fallback_style
))
69 if (!range
.GetBaseAddress().Dump(s
, target
, style
, fallback_style
))
73 *s
<< ", file = " << GetFile();
75 s
->Printf(", line = %u", line
);
77 s
->Printf(", column = %u", column
);
78 if (is_start_of_statement
)
79 *s
<< ", is_start_of_statement = TRUE";
81 if (is_start_of_basic_block
)
82 *s
<< ", is_start_of_basic_block = TRUE";
85 *s
<< ", is_prologue_end = TRUE";
87 if (is_epilogue_begin
)
88 *s
<< ", is_epilogue_begin = TRUE";
90 if (is_terminal_entry
)
91 *s
<< ", is_terminal_entry = TRUE";
95 bool LineEntry::GetDescription(Stream
*s
, lldb::DescriptionLevel level
,
96 CompileUnit
*cu
, Target
*target
,
97 bool show_address_only
) const {
99 if (level
== lldb::eDescriptionLevelBrief
||
100 level
== lldb::eDescriptionLevelFull
) {
101 if (show_address_only
) {
102 range
.GetBaseAddress().Dump(s
, target
, Address::DumpStyleLoadAddress
,
103 Address::DumpStyleFileAddress
);
105 range
.Dump(s
, target
, Address::DumpStyleLoadAddress
,
106 Address::DumpStyleFileAddress
);
109 *s
<< ": " << GetFile();
112 s
->Printf(":%u", line
);
114 s
->Printf(":%u", column
);
117 if (level
== lldb::eDescriptionLevelFull
) {
118 if (is_start_of_statement
)
119 *s
<< ", is_start_of_statement = TRUE";
121 if (is_start_of_basic_block
)
122 *s
<< ", is_start_of_basic_block = TRUE";
125 *s
<< ", is_prologue_end = TRUE";
127 if (is_epilogue_begin
)
128 *s
<< ", is_epilogue_begin = TRUE";
130 if (is_terminal_entry
)
131 *s
<< ", is_terminal_entry = TRUE";
133 if (is_terminal_entry
)
137 return Dump(s
, target
, true, Address::DumpStyleLoadAddress
,
138 Address::DumpStyleModuleWithFileAddress
, true);
143 bool lldb_private::operator<(const LineEntry
&a
, const LineEntry
&b
) {
144 return LineEntry::Compare(a
, b
) < 0;
147 int LineEntry::Compare(const LineEntry
&a
, const LineEntry
&b
) {
148 int result
= Address::CompareFileAddress(a
.range
.GetBaseAddress(),
149 b
.range
.GetBaseAddress());
153 const lldb::addr_t a_byte_size
= a
.range
.GetByteSize();
154 const lldb::addr_t b_byte_size
= b
.range
.GetByteSize();
156 if (a_byte_size
< b_byte_size
)
158 if (a_byte_size
> b_byte_size
)
161 // Check for an end sequence entry mismatch after we have determined that the
162 // address values are equal. If one of the items is an end sequence, we don't
163 // care about the line, file, or column info.
164 if (a
.is_terminal_entry
> b
.is_terminal_entry
)
166 if (a
.is_terminal_entry
< b
.is_terminal_entry
)
174 if (a
.column
< b
.column
)
176 if (a
.column
> b
.column
)
179 return FileSpec::Compare(a
.GetFile(), b
.GetFile(), true);
182 AddressRange
LineEntry::GetSameLineContiguousAddressRange(
183 bool include_inlined_functions
) const {
184 // Add each LineEntry's range to complete_line_range until we find a
185 // different file / line number.
186 AddressRange complete_line_range
= range
;
187 auto symbol_context_scope
= lldb::eSymbolContextLineEntry
;
188 Declaration
start_call_site(original_file_sp
->GetSpecOnly(), line
);
189 if (include_inlined_functions
)
190 symbol_context_scope
|= lldb::eSymbolContextBlock
;
193 SymbolContext next_line_sc
;
194 Address
range_end(complete_line_range
.GetBaseAddress());
195 range_end
.Slide(complete_line_range
.GetByteSize());
196 range_end
.CalculateSymbolContext(&next_line_sc
, symbol_context_scope
);
198 if (!next_line_sc
.line_entry
.IsValid() ||
199 next_line_sc
.line_entry
.range
.GetByteSize() == 0)
202 if (original_file_sp
->Equal(*next_line_sc
.line_entry
.original_file_sp
,
203 SupportFile::eEqualFileSpecAndChecksumIfSet
) &&
204 (next_line_sc
.line_entry
.line
== 0 ||
205 line
== next_line_sc
.line_entry
.line
)) {
206 // Include any line 0 entries - they indicate that this is compiler-
207 // generated code that does not correspond to user source code.
208 // next_line_sc is the same file & line as this LineEntry, so extend
209 // our AddressRange by its size and continue to see if there are more
210 // LineEntries that we can combine. However, if there was nothing to
211 // extend we're done.
212 if (!complete_line_range
.Extend(next_line_sc
.line_entry
.range
))
217 if (include_inlined_functions
&& next_line_sc
.block
&&
218 next_line_sc
.block
->GetContainingInlinedBlock() != nullptr) {
219 // The next_line_sc might be in a different file if it's an inlined
220 // function. If this is the case then we still want to expand our line
221 // range to include them if the inlined function is at the same call site
222 // as this line entry. The current block could represent a nested inline
223 // function call so we need to need to check up the block tree to see if
225 auto inlined_parent_block
=
226 next_line_sc
.block
->GetContainingInlinedBlockWithCallSite(
228 if (!inlined_parent_block
)
229 // We didn't find any parent inlined block with a call site at this line
230 // entry so this inlined function is probably at another line.
232 // Extend our AddressRange by the size of the inlined block, but if there
233 // was nothing to add then we're done.
234 if (!complete_line_range
.Extend(next_line_sc
.line_entry
.range
))
241 return complete_line_range
;
244 void LineEntry::ApplyFileMappings(lldb::TargetSP target_sp
) {
246 // Apply any file remappings to our file.
247 if (auto new_file_spec
= target_sp
->GetSourcePathMap().FindFile(
248 original_file_sp
->GetSpecOnly())) {
249 file_sp
= std::make_shared
<SupportFile
>(*new_file_spec
,
250 original_file_sp
->GetChecksum());