1 //===-- BreakpointResolver.cpp ----------------------------------*- C++ -*-===//
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/Breakpoint/BreakpointResolver.h"
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/Target.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/Stream.h"
29 #include "lldb/Utility/StreamString.h"
31 using namespace lldb_private;
34 // BreakpointResolver:
35 const char *BreakpointResolver::g_ty_to_name[] = {"FileAndLine", "Address",
36 "SymbolName", "SourceRegex",
37 "Exception", "Unknown"};
39 const char *BreakpointResolver::g_option_names[static_cast<uint32_t>(
40 BreakpointResolver::OptionNames::LastOptionName)] = {
41 "AddressOffset", "Exact", "FileName", "Inlines", "Language",
42 "LineNumber", "Column", "ModuleName", "NameMask", "Offset",
43 "PythonClass", "Regex", "ScriptArgs", "SectionName", "SearchDepth",
44 "SkipPrologue", "SymbolNames"};
46 const char *BreakpointResolver::ResolverTyToName(enum ResolverTy type) {
47 if (type > LastKnownResolverType)
48 return g_ty_to_name[UnknownResolver];
50 return g_ty_to_name[type];
53 BreakpointResolver::ResolverTy
54 BreakpointResolver::NameToResolverTy(llvm::StringRef name) {
55 for (size_t i = 0; i < LastKnownResolverType; i++) {
56 if (name == g_ty_to_name[i])
59 return UnknownResolver;
62 BreakpointResolver::BreakpointResolver(Breakpoint *bkpt,
63 const unsigned char resolverTy,
65 : m_breakpoint(bkpt), m_offset(offset), SubclassID(resolverTy) {}
67 BreakpointResolver::~BreakpointResolver() {}
69 BreakpointResolverSP BreakpointResolver::CreateFromStructuredData(
70 const StructuredData::Dictionary &resolver_dict, Status &error) {
71 BreakpointResolverSP result_sp;
72 if (!resolver_dict.IsValid()) {
73 error.SetErrorString("Can't deserialize from an invalid data object.");
77 llvm::StringRef subclass_name;
79 bool success = resolver_dict.GetValueForKeyAsString(
80 GetSerializationSubclassKey(), subclass_name);
83 error.SetErrorStringWithFormat(
84 "Resolver data missing subclass resolver key");
88 ResolverTy resolver_type = NameToResolverTy(subclass_name);
89 if (resolver_type == UnknownResolver) {
90 error.SetErrorStringWithFormatv("Unknown resolver type: {0}.",
95 StructuredData::Dictionary *subclass_options = nullptr;
96 success = resolver_dict.GetValueForKeyAsDictionary(
97 GetSerializationSubclassOptionsKey(), subclass_options);
98 if (!success || !subclass_options || !subclass_options->IsValid()) {
99 error.SetErrorString("Resolver data missing subclass options key.");
104 success = subclass_options->GetValueForKeyAsInteger(
105 GetKey(OptionNames::Offset), offset);
107 error.SetErrorString("Resolver data missing offset options key.");
111 BreakpointResolver *resolver;
113 switch (resolver_type) {
114 case FileLineResolver:
115 resolver = BreakpointResolverFileLine::CreateFromStructuredData(
116 nullptr, *subclass_options, error);
118 case AddressResolver:
119 resolver = BreakpointResolverAddress::CreateFromStructuredData(
120 nullptr, *subclass_options, error);
123 resolver = BreakpointResolverName::CreateFromStructuredData(
124 nullptr, *subclass_options, error);
126 case FileRegexResolver:
127 resolver = BreakpointResolverFileRegex::CreateFromStructuredData(
128 nullptr, *subclass_options, error);
131 resolver = BreakpointResolverScripted::CreateFromStructuredData(
132 nullptr, *subclass_options, error);
134 case ExceptionResolver:
135 error.SetErrorString("Exception resolvers are hard.");
138 llvm_unreachable("Should never get an unresolvable resolver type.");
141 if (!error.Success()) {
144 // Add on the global offset option:
145 resolver->SetOffset(offset);
146 return BreakpointResolverSP(resolver);
150 StructuredData::DictionarySP BreakpointResolver::WrapOptionsDict(
151 StructuredData::DictionarySP options_dict_sp) {
152 if (!options_dict_sp || !options_dict_sp->IsValid())
153 return StructuredData::DictionarySP();
155 StructuredData::DictionarySP type_dict_sp(new StructuredData::Dictionary());
156 type_dict_sp->AddStringItem(GetSerializationSubclassKey(), GetResolverName());
157 type_dict_sp->AddItem(GetSerializationSubclassOptionsKey(), options_dict_sp);
159 // Add the m_offset to the dictionary:
160 options_dict_sp->AddIntegerItem(GetKey(OptionNames::Offset), m_offset);
165 void BreakpointResolver::SetBreakpoint(Breakpoint *bkpt) {
167 NotifyBreakpointSet();
170 void BreakpointResolver::ResolveBreakpointInModules(SearchFilter &filter,
171 ModuleList &modules) {
172 filter.SearchInModuleList(*this, modules);
175 void BreakpointResolver::ResolveBreakpoint(SearchFilter &filter) {
176 filter.Search(*this);
181 uint32_t line = UINT32_MAX;
183 SourceLoc(uint32_t l, uint32_t c) : line(l), column(c ? c : UINT32_MAX) {}
184 SourceLoc(const SymbolContext &sc)
185 : line(sc.line_entry.line),
186 column(sc.line_entry.column ? sc.line_entry.column : UINT32_MAX) {}
189 bool operator<(const SourceLoc a, const SourceLoc b) {
194 uint32_t a_col = a.column ? a.column : UINT32_MAX;
195 uint32_t b_col = b.column ? b.column : UINT32_MAX;
196 return a_col < b_col;
200 void BreakpointResolver::SetSCMatchesByLine(SearchFilter &filter,
201 SymbolContextList &sc_list,
203 llvm::StringRef log_ident,
204 uint32_t line, uint32_t column) {
205 llvm::SmallVector<SymbolContext, 16> all_scs;
206 for (uint32_t i = 0; i < sc_list.GetSize(); ++i)
207 all_scs.push_back(sc_list[i]);
209 while (all_scs.size()) {
210 uint32_t closest_line = UINT32_MAX;
212 // Move all the elements with a matching file spec to the end.
213 auto &match = all_scs[0];
214 auto worklist_begin = std::partition(
215 all_scs.begin(), all_scs.end(), [&](const SymbolContext &sc) {
216 if (sc.line_entry.file == match.line_entry.file ||
217 sc.line_entry.original_file == match.line_entry.original_file) {
218 // When a match is found, keep track of the smallest line number.
219 closest_line = std::min(closest_line, sc.line_entry.line);
225 // (worklist_begin, worklist_end) now contains all entries for one filespec.
226 auto worklist_end = all_scs.end();
229 // If a column was requested, do a more precise match and only
230 // return the first location that comes after or at the
231 // requested location.
232 SourceLoc requested(line, column);
233 // First, filter out all entries left of the requested column.
234 worklist_end = std::remove_if(
235 worklist_begin, worklist_end,
236 [&](const SymbolContext &sc) { return SourceLoc(sc) < requested; });
237 // Sort the remaining entries by (line, column).
238 llvm::sort(worklist_begin, worklist_end,
239 [](const SymbolContext &a, const SymbolContext &b) {
240 return SourceLoc(a) < SourceLoc(b);
243 // Filter out all locations with a source location after the closest match.
244 if (worklist_begin != worklist_end)
245 worklist_end = std::remove_if(
246 worklist_begin, worklist_end, [&](const SymbolContext &sc) {
247 return SourceLoc(*worklist_begin) < SourceLoc(sc);
250 // Remove all entries with a larger line number.
251 // ResolveSymbolContext will always return a number that is >=
252 // the line number you pass in. So the smaller line number is
254 worklist_end = std::remove_if(worklist_begin, worklist_end,
255 [&](const SymbolContext &sc) {
256 return closest_line != sc.line_entry.line;
260 // Sort by file address.
261 llvm::sort(worklist_begin, worklist_end,
262 [](const SymbolContext &a, const SymbolContext &b) {
263 return a.line_entry.range.GetBaseAddress().GetFileAddress() <
264 b.line_entry.range.GetBaseAddress().GetFileAddress();
267 // Go through and see if there are line table entries that are
268 // contiguous, and if so keep only the first of the contiguous range.
269 // We do this by picking the first location in each lexical block.
270 llvm::SmallDenseSet<Block *, 8> blocks_with_breakpoints;
271 for (auto first = worklist_begin; first != worklist_end; ++first) {
272 assert(!blocks_with_breakpoints.count(first->block));
273 blocks_with_breakpoints.insert(first->block);
275 std::remove_if(std::next(first), worklist_end,
276 [&](const SymbolContext &sc) {
277 return blocks_with_breakpoints.count(sc.block);
281 // Make breakpoints out of the closest line number match.
282 for (auto &sc : llvm::make_range(worklist_begin, worklist_end))
283 AddLocation(filter, sc, skip_prologue, log_ident);
285 // Remove all contexts processed by this iteration.
286 all_scs.erase(worklist_begin, all_scs.end());
290 void BreakpointResolver::AddLocation(SearchFilter &filter,
291 const SymbolContext &sc,
293 llvm::StringRef log_ident) {
294 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
295 Address line_start = sc.line_entry.range.GetBaseAddress();
296 if (!line_start.IsValid()) {
298 log->Printf("error: Unable to set breakpoint %s at file address "
300 log_ident.str().c_str(), line_start.GetFileAddress());
304 if (!filter.AddressPasses(line_start)) {
306 log->Printf("Breakpoint %s at file address 0x%" PRIx64
307 " didn't pass the filter.\n",
308 log_ident.str().c_str(), line_start.GetFileAddress());
311 // If the line number is before the prologue end, move it there...
312 bool skipped_prologue = false;
313 if (skip_prologue && sc.function) {
314 Address prologue_addr(sc.function->GetAddressRange().GetBaseAddress());
315 if (prologue_addr.IsValid() && (line_start == prologue_addr)) {
316 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
317 if (prologue_byte_size) {
318 prologue_addr.Slide(prologue_byte_size);
320 if (filter.AddressPasses(prologue_addr)) {
321 skipped_prologue = true;
322 line_start = prologue_addr;
328 BreakpointLocationSP bp_loc_sp(AddLocation(line_start));
329 if (log && bp_loc_sp && !m_breakpoint->IsInternal()) {
331 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
332 log->Printf("Added location (skipped prologue: %s): %s \n",
333 skipped_prologue ? "yes" : "no", s.GetData());
337 BreakpointLocationSP BreakpointResolver::AddLocation(Address loc_addr,
338 bool *new_location) {
339 loc_addr.Slide(m_offset);
340 return m_breakpoint->AddLocation(loc_addr, new_location);
343 void BreakpointResolver::SetOffset(lldb::addr_t offset) {
344 // There may already be an offset, so we are actually adjusting location
345 // addresses by the difference.
346 // lldb::addr_t slide = offset - m_offset;
347 // FIXME: We should go fix up all the already set locations for the new