1 | //===-- BreakpointResolver.cpp --------------------------------------------===// |
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/Breakpoint/BreakpointResolver.h" |
10 | |
11 | #include "lldb/Breakpoint/Breakpoint.h" |
12 | #include "lldb/Breakpoint/BreakpointLocation.h" |
13 | // Have to include the other breakpoint resolver types here so the static |
14 | // create from StructuredData can call them. |
15 | #include "lldb/Breakpoint/BreakpointResolverAddress.h" |
16 | #include "lldb/Breakpoint/BreakpointResolverFileLine.h" |
17 | #include "lldb/Breakpoint/BreakpointResolverFileRegex.h" |
18 | #include "lldb/Breakpoint/BreakpointResolverName.h" |
19 | #include "lldb/Breakpoint/BreakpointResolverScripted.h" |
20 | #include "lldb/Core/Address.h" |
21 | #include "lldb/Core/ModuleList.h" |
22 | #include "lldb/Core/SearchFilter.h" |
23 | #include "lldb/Symbol/CompileUnit.h" |
24 | #include "lldb/Symbol/Function.h" |
25 | #include "lldb/Symbol/SymbolContext.h" |
26 | #include "lldb/Target/Language.h" |
27 | #include "lldb/Target/Target.h" |
28 | #include "lldb/Utility/LLDBLog.h" |
29 | #include "lldb/Utility/Log.h" |
30 | #include "lldb/Utility/Stream.h" |
31 | #include "lldb/Utility/StreamString.h" |
32 | #include <optional> |
33 | |
34 | using namespace lldb_private; |
35 | using namespace lldb; |
36 | |
37 | // BreakpointResolver: |
38 | const char *BreakpointResolver::g_ty_to_name[] = {"FileAndLine" , "Address" , |
39 | "SymbolName" , "SourceRegex" , |
40 | "Python" , "Exception" , |
41 | "Unknown" }; |
42 | |
43 | const char *BreakpointResolver::g_option_names[static_cast<uint32_t>( |
44 | BreakpointResolver::OptionNames::LastOptionName)] = { |
45 | "AddressOffset" , "Exact" , "FileName" , "Inlines" , "Language" , |
46 | "LineNumber" , "Column" , "ModuleName" , "NameMask" , "Offset" , |
47 | "PythonClass" , "Regex" , "ScriptArgs" , "SectionName" , "SearchDepth" , |
48 | "SkipPrologue" , "SymbolNames" }; |
49 | |
50 | const char *BreakpointResolver::ResolverTyToName(enum ResolverTy type) { |
51 | if (type > LastKnownResolverType) |
52 | return g_ty_to_name[UnknownResolver]; |
53 | |
54 | return g_ty_to_name[type]; |
55 | } |
56 | |
57 | BreakpointResolver::ResolverTy |
58 | BreakpointResolver::NameToResolverTy(llvm::StringRef name) { |
59 | for (size_t i = 0; i < LastKnownResolverType; i++) { |
60 | if (name == g_ty_to_name[i]) |
61 | return (ResolverTy)i; |
62 | } |
63 | return UnknownResolver; |
64 | } |
65 | |
66 | BreakpointResolver::BreakpointResolver(const BreakpointSP &bkpt, |
67 | const unsigned char resolverTy, |
68 | lldb::addr_t offset) |
69 | : m_breakpoint(bkpt), m_offset(offset), SubclassID(resolverTy) {} |
70 | |
71 | BreakpointResolver::~BreakpointResolver() = default; |
72 | |
73 | BreakpointResolverSP BreakpointResolver::CreateFromStructuredData( |
74 | const StructuredData::Dictionary &resolver_dict, Status &error) { |
75 | BreakpointResolverSP result_sp; |
76 | if (!resolver_dict.IsValid()) { |
77 | error = Status::FromErrorString( |
78 | str: "Can't deserialize from an invalid data object." ); |
79 | return result_sp; |
80 | } |
81 | |
82 | llvm::StringRef subclass_name; |
83 | |
84 | bool success = resolver_dict.GetValueForKeyAsString( |
85 | key: GetSerializationSubclassKey(), result&: subclass_name); |
86 | |
87 | if (!success) { |
88 | error = |
89 | Status::FromErrorString(str: "Resolver data missing subclass resolver key" ); |
90 | return result_sp; |
91 | } |
92 | |
93 | ResolverTy resolver_type = NameToResolverTy(name: subclass_name); |
94 | if (resolver_type == UnknownResolver) { |
95 | error = Status::FromErrorStringWithFormatv(format: "Unknown resolver type: {0}." , |
96 | args&: subclass_name); |
97 | return result_sp; |
98 | } |
99 | |
100 | StructuredData::Dictionary *subclass_options = nullptr; |
101 | success = resolver_dict.GetValueForKeyAsDictionary( |
102 | key: GetSerializationSubclassOptionsKey(), result&: subclass_options); |
103 | if (!success || !subclass_options || !subclass_options->IsValid()) { |
104 | error = |
105 | Status::FromErrorString(str: "Resolver data missing subclass options key." ); |
106 | return result_sp; |
107 | } |
108 | |
109 | lldb::offset_t offset; |
110 | success = subclass_options->GetValueForKeyAsInteger( |
111 | key: GetKey(enum_value: OptionNames::Offset), result&: offset); |
112 | if (!success) { |
113 | error = |
114 | Status::FromErrorString(str: "Resolver data missing offset options key." ); |
115 | return result_sp; |
116 | } |
117 | |
118 | switch (resolver_type) { |
119 | case FileLineResolver: |
120 | result_sp = BreakpointResolverFileLine::CreateFromStructuredData( |
121 | data_dict: *subclass_options, error); |
122 | break; |
123 | case AddressResolver: |
124 | result_sp = BreakpointResolverAddress::CreateFromStructuredData( |
125 | options_dict: *subclass_options, error); |
126 | break; |
127 | case NameResolver: |
128 | result_sp = BreakpointResolverName::CreateFromStructuredData( |
129 | data_dict: *subclass_options, error); |
130 | break; |
131 | case FileRegexResolver: |
132 | result_sp = BreakpointResolverFileRegex::CreateFromStructuredData( |
133 | options_dict: *subclass_options, error); |
134 | break; |
135 | case PythonResolver: |
136 | result_sp = BreakpointResolverScripted::CreateFromStructuredData( |
137 | options_dict: *subclass_options, error); |
138 | break; |
139 | case ExceptionResolver: |
140 | error = Status::FromErrorString(str: "Exception resolvers are hard." ); |
141 | break; |
142 | default: |
143 | llvm_unreachable("Should never get an unresolvable resolver type." ); |
144 | } |
145 | |
146 | if (error.Fail() || !result_sp) |
147 | return {}; |
148 | |
149 | // Add on the global offset option: |
150 | result_sp->SetOffset(offset); |
151 | return result_sp; |
152 | } |
153 | |
154 | StructuredData::DictionarySP BreakpointResolver::WrapOptionsDict( |
155 | StructuredData::DictionarySP options_dict_sp) { |
156 | if (!options_dict_sp || !options_dict_sp->IsValid()) |
157 | return StructuredData::DictionarySP(); |
158 | |
159 | StructuredData::DictionarySP type_dict_sp(new StructuredData::Dictionary()); |
160 | type_dict_sp->AddStringItem(key: GetSerializationSubclassKey(), value: GetResolverName()); |
161 | type_dict_sp->AddItem(key: GetSerializationSubclassOptionsKey(), value_sp: options_dict_sp); |
162 | |
163 | // Add the m_offset to the dictionary: |
164 | options_dict_sp->AddIntegerItem(key: GetKey(enum_value: OptionNames::Offset), value: m_offset); |
165 | |
166 | return type_dict_sp; |
167 | } |
168 | |
169 | void BreakpointResolver::SetBreakpoint(const BreakpointSP &bkpt) { |
170 | assert(bkpt); |
171 | m_breakpoint = bkpt; |
172 | NotifyBreakpointSet(); |
173 | } |
174 | |
175 | void BreakpointResolver::ResolveBreakpointInModules(SearchFilter &filter, |
176 | ModuleList &modules) { |
177 | filter.SearchInModuleList(searcher&: *this, modules); |
178 | } |
179 | |
180 | void BreakpointResolver::ResolveBreakpoint(SearchFilter &filter) { |
181 | filter.Search(searcher&: *this); |
182 | } |
183 | |
184 | namespace { |
185 | struct SourceLoc { |
186 | uint32_t line = UINT32_MAX; |
187 | uint16_t column; |
188 | SourceLoc(uint32_t l, std::optional<uint16_t> c) |
189 | : line(l), column(c ? *c : LLDB_INVALID_COLUMN_NUMBER) {} |
190 | SourceLoc(const SymbolContext &sc) |
191 | : line(sc.line_entry.line), |
192 | column(sc.line_entry.column ? sc.line_entry.column |
193 | : LLDB_INVALID_COLUMN_NUMBER) {} |
194 | }; |
195 | |
196 | bool operator<(const SourceLoc lhs, const SourceLoc rhs) { |
197 | if (lhs.line < rhs.line) |
198 | return true; |
199 | if (lhs.line > rhs.line) |
200 | return false; |
201 | // uint32_t a_col = lhs.column ? lhs.column : LLDB_INVALID_COLUMN_NUMBER; |
202 | // uint32_t b_col = rhs.column ? rhs.column : LLDB_INVALID_COLUMN_NUMBER; |
203 | return lhs.column < rhs.column; |
204 | } |
205 | } // namespace |
206 | |
207 | void BreakpointResolver::SetSCMatchesByLine( |
208 | SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue, |
209 | llvm::StringRef log_ident, uint32_t line, std::optional<uint16_t> column) { |
210 | llvm::SmallVector<SymbolContext, 16> all_scs(sc_list.begin(), sc_list.end()); |
211 | |
212 | // Let the language plugin filter `sc_list`. Because all symbol contexts in |
213 | // sc_list are assumed to belong to the same File, Line and CU, the code below |
214 | // assumes they have the same language. |
215 | if (!sc_list.IsEmpty() && Language::GetGlobalLanguageProperties() |
216 | .GetEnableFilterForLineBreakpoints()) |
217 | if (Language *lang = Language::FindPlugin(language: sc_list[0].GetLanguage())) |
218 | lang->FilterForLineBreakpoints(all_scs); |
219 | |
220 | while (all_scs.size()) { |
221 | uint32_t closest_line = UINT32_MAX; |
222 | |
223 | // Move all the elements with a matching file spec to the end. |
224 | auto &match = all_scs[0]; |
225 | auto worklist_begin = std::partition( |
226 | first: all_scs.begin(), last: all_scs.end(), pred: [&](const SymbolContext &sc) { |
227 | if (sc.line_entry.GetFile() == match.line_entry.GetFile() || |
228 | sc.line_entry.original_file_sp->Equal( |
229 | other: *match.line_entry.original_file_sp, |
230 | equality: SupportFile::eEqualFileSpecAndChecksumIfSet)) { |
231 | // When a match is found, keep track of the smallest line number. |
232 | closest_line = std::min(a: closest_line, b: sc.line_entry.line); |
233 | return false; |
234 | } |
235 | return true; |
236 | }); |
237 | |
238 | // (worklist_begin, worklist_end) now contains all entries for one filespec. |
239 | auto worklist_end = all_scs.end(); |
240 | |
241 | if (column) { |
242 | // If a column was requested, do a more precise match and only |
243 | // return the first location that comes before or at the |
244 | // requested location. |
245 | SourceLoc requested(line, *column); |
246 | // First, filter out all entries left of the requested column. |
247 | worklist_end = std::remove_if( |
248 | first: worklist_begin, last: worklist_end, |
249 | pred: [&](const SymbolContext &sc) { return requested < SourceLoc(sc); }); |
250 | // Sort the remaining entries by (line, column). |
251 | llvm::sort(Start: worklist_begin, End: worklist_end, |
252 | Comp: [](const SymbolContext &a, const SymbolContext &b) { |
253 | return SourceLoc(a) < SourceLoc(b); |
254 | }); |
255 | |
256 | // Filter out all locations with a source location after the closest match. |
257 | if (worklist_begin != worklist_end) |
258 | worklist_end = std::remove_if( |
259 | first: worklist_begin, last: worklist_end, pred: [&](const SymbolContext &sc) { |
260 | return SourceLoc(*worklist_begin) < SourceLoc(sc); |
261 | }); |
262 | } else { |
263 | // Remove all entries with a larger line number. |
264 | // ResolveSymbolContext will always return a number that is >= |
265 | // the line number you pass in. So the smaller line number is |
266 | // always better. |
267 | worklist_end = std::remove_if(first: worklist_begin, last: worklist_end, |
268 | pred: [&](const SymbolContext &sc) { |
269 | return closest_line != sc.line_entry.line; |
270 | }); |
271 | } |
272 | |
273 | // Sort by file address. |
274 | llvm::sort(Start: worklist_begin, End: worklist_end, |
275 | Comp: [](const SymbolContext &a, const SymbolContext &b) { |
276 | return a.line_entry.range.GetBaseAddress().GetFileAddress() < |
277 | b.line_entry.range.GetBaseAddress().GetFileAddress(); |
278 | }); |
279 | |
280 | // Go through and see if there are line table entries that are |
281 | // contiguous, and if so keep only the first of the contiguous range. |
282 | // We do this by picking the first location in each lexical block. |
283 | llvm::SmallDenseSet<Block *, 8> blocks_with_breakpoints; |
284 | for (auto first = worklist_begin; first != worklist_end; ++first) { |
285 | assert(!blocks_with_breakpoints.count(first->block)); |
286 | blocks_with_breakpoints.insert(V: first->block); |
287 | worklist_end = |
288 | std::remove_if(first: std::next(x: first), last: worklist_end, |
289 | pred: [&](const SymbolContext &sc) { |
290 | return blocks_with_breakpoints.count(V: sc.block); |
291 | }); |
292 | } |
293 | |
294 | // Make breakpoints out of the closest line number match. |
295 | for (auto &sc : llvm::make_range(x: worklist_begin, y: worklist_end)) |
296 | AddLocation(filter, sc, skip_prologue, log_ident); |
297 | |
298 | // Remove all contexts processed by this iteration. |
299 | all_scs.erase(CS: worklist_begin, CE: all_scs.end()); |
300 | } |
301 | } |
302 | |
303 | void BreakpointResolver::AddLocation(SearchFilter &filter, |
304 | const SymbolContext &sc, |
305 | bool skip_prologue, |
306 | llvm::StringRef log_ident) { |
307 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
308 | Address line_start = sc.line_entry.range.GetBaseAddress(); |
309 | if (!line_start.IsValid()) { |
310 | LLDB_LOGF(log, |
311 | "error: Unable to set breakpoint %s at file address " |
312 | "0x%" PRIx64 "\n" , |
313 | log_ident.str().c_str(), line_start.GetFileAddress()); |
314 | return; |
315 | } |
316 | |
317 | if (!filter.AddressPasses(addr&: line_start)) { |
318 | LLDB_LOGF(log, |
319 | "Breakpoint %s at file address 0x%" PRIx64 |
320 | " didn't pass the filter.\n" , |
321 | log_ident.str().c_str(), line_start.GetFileAddress()); |
322 | } |
323 | |
324 | // If the line number is before the prologue end, move it there... |
325 | bool skipped_prologue = false; |
326 | if (skip_prologue && sc.function) { |
327 | Address prologue_addr = sc.function->GetAddress(); |
328 | if (prologue_addr.IsValid() && (line_start == prologue_addr)) { |
329 | const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize(); |
330 | if (prologue_byte_size) { |
331 | prologue_addr.Slide(offset: prologue_byte_size); |
332 | |
333 | if (filter.AddressPasses(addr&: prologue_addr)) { |
334 | skipped_prologue = true; |
335 | line_start = prologue_addr; |
336 | } |
337 | } |
338 | } |
339 | } |
340 | |
341 | BreakpointLocationSP bp_loc_sp(AddLocation(loc_addr: line_start)); |
342 | // If the address that we resolved the location to returns a different |
343 | // LineEntry from the one in the incoming SC, we're probably dealing with an |
344 | // inlined call site, so set that as the preferred LineEntry: |
345 | LineEntry resolved_entry; |
346 | if (!skipped_prologue && bp_loc_sp && |
347 | line_start.CalculateSymbolContextLineEntry(line_entry&: resolved_entry) && |
348 | LineEntry::Compare(lhs: resolved_entry, rhs: sc.line_entry)) { |
349 | // FIXME: The function name will also be wrong here. Do we need to record |
350 | // that as well, or can we figure that out again when we report this |
351 | // breakpoint location. |
352 | if (!bp_loc_sp->SetPreferredLineEntry(sc.line_entry)) { |
353 | LLDB_LOG(log, "Tried to add a preferred line entry that didn't have the " |
354 | "same address as this location's address." ); |
355 | } |
356 | } |
357 | if (log && bp_loc_sp && !GetBreakpoint()->IsInternal()) { |
358 | StreamString s; |
359 | bp_loc_sp->GetDescription(s: &s, level: lldb::eDescriptionLevelVerbose); |
360 | LLDB_LOGF(log, "Added location (skipped prologue: %s): %s \n" , |
361 | skipped_prologue ? "yes" : "no" , s.GetData()); |
362 | } |
363 | } |
364 | |
365 | BreakpointLocationSP BreakpointResolver::AddLocation(Address loc_addr, |
366 | bool *new_location) { |
367 | loc_addr.Slide(offset: m_offset); |
368 | return GetBreakpoint()->AddLocation(addr: loc_addr, new_location); |
369 | } |
370 | |
371 | void BreakpointResolver::SetOffset(lldb::addr_t offset) { |
372 | // There may already be an offset, so we are actually adjusting location |
373 | // addresses by the difference. |
374 | // lldb::addr_t slide = offset - m_offset; |
375 | // FIXME: We should go fix up all the already set locations for the new |
376 | // slide. |
377 | |
378 | m_offset = offset; |
379 | } |
380 | |