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

source code of lldb/source/Breakpoint/BreakpointResolver.cpp