LLDB mainline
BreakpointResolver.cpp
Go to the documentation of this file.
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
10
13// Have to include the other breakpoint resolver types here so the static
14// create from StructuredData can call them.
20#include "lldb/Core/Address.h"
27#include "lldb/Target/Target.h"
29#include "lldb/Utility/Log.h"
30#include "lldb/Utility/Stream.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
51 if (type > LastKnownResolverType)
53
54 return g_ty_to_name[type];
55}
56
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
67 const unsigned char resolverTy,
68 lldb::addr_t offset)
69 : m_breakpoint(bkpt), m_offset(offset), SubclassID(resolverTy) {}
70
72
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 GetSerializationSubclassKey(), 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(subclass_name);
92 if (resolver_type == UnknownResolver) {
93 error.SetErrorStringWithFormatv("Unknown resolver type: {0}.",
94 subclass_name);
95 return result_sp;
96 }
97
98 StructuredData::Dictionary *subclass_options = nullptr;
99 success = resolver_dict.GetValueForKeyAsDictionary(
100 GetSerializationSubclassOptionsKey(), 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 GetKey(OptionNames::Offset), 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:
117 *subclass_options, error);
118 break;
119 case AddressResolver:
121 *subclass_options, error);
122 break;
123 case NameResolver:
125 *subclass_options, error);
126 break;
129 *subclass_options, error);
130 break;
131 case PythonResolver:
133 *subclass_options, error);
134 break;
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
151 StructuredData::DictionarySP options_dict_sp) {
152 if (!options_dict_sp || !options_dict_sp->IsValid())
154
156 type_dict_sp->AddStringItem(GetSerializationSubclassKey(), GetResolverName());
157 type_dict_sp->AddItem(GetSerializationSubclassOptionsKey(), options_dict_sp);
158
159 // Add the m_offset to the dictionary:
160 options_dict_sp->AddIntegerItem(GetKey(OptionNames::Offset), m_offset);
161
162 return type_dict_sp;
163}
164
166 assert(bkpt);
167 m_breakpoint = bkpt;
169}
170
172 ModuleList &modules) {
173 filter.SearchInModuleList(*this, modules);
174}
175
177 filter.Search(*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
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
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) {
210 .GetEnableFilterForLineBreakpoints())
211 if (Language *lang = Language::FindPlugin(sc.GetLanguage());
212 lang && lang->IgnoreForLineBreakpoints(sc))
213 continue;
214 all_scs.push_back(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 all_scs.begin(), all_scs.end(), [&](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(closest_line, 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 worklist_begin, worklist_end,
245 [&](const SymbolContext &sc) { return requested < SourceLoc(sc); });
246 // Sort the remaining entries by (line, column).
247 llvm::sort(worklist_begin, worklist_end,
248 [](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 worklist_begin, worklist_end, [&](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(worklist_begin, worklist_end,
264 [&](const SymbolContext &sc) {
265 return closest_line != sc.line_entry.line;
266 });
267 }
268
269 // Sort by file address.
270 llvm::sort(worklist_begin, worklist_end,
271 [](const SymbolContext &a, const SymbolContext &b) {
272 return a.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(first->block);
283 worklist_end =
284 std::remove_if(std::next(first), worklist_end,
285 [&](const SymbolContext &sc) {
286 return blocks_with_breakpoints.count(sc.block);
287 });
288 }
289
290 // Make breakpoints out of the closest line number match.
291 for (auto &sc : llvm::make_range(worklist_begin, worklist_end))
292 AddLocation(filter, sc, skip_prologue, log_ident);
293
294 // Remove all contexts processed by this iteration.
295 all_scs.erase(worklist_begin, all_scs.end());
296 }
297}
298
300 const SymbolContext &sc,
301 bool skip_prologue,
302 llvm::StringRef log_ident) {
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(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(prologue_byte_size);
328
329 if (filter.AddressPasses(prologue_addr)) {
330 skipped_prologue = true;
331 line_start = prologue_addr;
332 }
333 }
334 }
335 }
336
337 BreakpointLocationSP bp_loc_sp(AddLocation(line_start));
338 if (log && bp_loc_sp && !GetBreakpoint()->IsInternal()) {
339 StreamString s;
340 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
341 LLDB_LOGF(log, "Added location (skipped prologue: %s): %s \n",
342 skipped_prologue ? "yes" : "no", s.GetData());
343 }
344}
345
347 bool *new_location) {
348 loc_addr.Slide(m_offset);
349 return GetBreakpoint()->AddLocation(loc_addr, new_location);
350}
351
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}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:349
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
A section + offset based address class.
Definition: Address.h:62
bool Slide(int64_t offset)
Definition: Address.h:459
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:293
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &options_dict, Status &error)
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &data_dict, Status &error)
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &options_dict, Status &error)
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &data_dict, Status &error)
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &options_dict, Status &error)
BreakpointResolver(const lldb::BreakpointSP &bkpt, unsigned char resolverType, lldb::addr_t offset=0)
The breakpoint resolver need to have a breakpoint for "ResolveBreakpoint to make sense.
void SetBreakpoint(const lldb::BreakpointSP &bkpt)
This sets the breakpoint for this resolver.
~BreakpointResolver() override
The Destructor is virtual, all significant breakpoint resolvers derive from this class.
lldb::BreakpointLocationSP AddLocation(Address loc_addr, bool *new_location=nullptr)
static const char * GetKey(OptionNames enum_value)
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &resolver_dict, Status &error)
This section handles serializing and deserializing from StructuredData objects.
static ResolverTy NameToResolverTy(llvm::StringRef name)
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
ResolverTy
An enumeration for keeping track of the concrete subclass that is actually instantiated.
void SetSCMatchesByLine(SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue, llvm::StringRef log_ident, uint32_t line=0, std::optional< uint16_t > column=std::nullopt)
Takes a symbol context list of matches which supposedly represent the same file and line number in a ...
lldb::BreakpointSP GetBreakpoint() const
This gets the breakpoint for this resolver.
void SetOffset(lldb::addr_t offset)
This updates the offset for this breakpoint.
static const char * g_option_names[static_cast< uint32_t >(OptionNames::LastOptionName)]
static const char * ResolverTyToName(enum ResolverTy)
static const char * g_ty_to_name[LastKnownResolverType+2]
static const char * GetSerializationSubclassKey()
virtual void ResolveBreakpointInModules(SearchFilter &filter, ModuleList &modules)
In response to this method the resolver scans the modules in the module list modules,...
static const char * GetSerializationSubclassOptionsKey()
virtual void ResolveBreakpoint(SearchFilter &filter)
In response to this method the resolver scans all the modules in the breakpoint's target,...
const AddressRange & GetAddressRange()
Definition: Function.h:447
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition: Function.cpp:566
virtual bool IgnoreForLineBreakpoints(const SymbolContext &) const
Returns true if this SymbolContext should be ignored when setting breakpoints by line (number or rege...
Definition: Language.h:358
static Language * FindPlugin(lldb::LanguageType language)
Definition: Language.cpp:83
static LanguageProperties & GetGlobalLanguageProperties()
Definition: Language.cpp:39
A collection class for Module objects.
Definition: ModuleList.h:103
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
virtual void SearchInModuleList(Searcher &searcher, ModuleList &modules)
Call this method to do the search using the Searcher in the module list modules.
virtual bool AddressPasses(Address &addr)
Call this method with a Address to see if address passes the filter.
virtual void Search(Searcher &searcher)
Call this method to do the search using the Searcher.
An error handling class.
Definition: Status.h:44
const char * GetData() const
Definition: StreamString.h:43
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
bool GetValueForKeyAsDictionary(llvm::StringRef key, Dictionary *&result) const
std::shared_ptr< Dictionary > DictionarySP
Defines a list of symbol context objects.
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
Function * function
The Function for a given query.
Block * block
The Block for a given query.
LineEntry line_entry
The LineEntry for a given query.
#define LLDB_INVALID_COLUMN_NUMBER
Definition: lldb-defines.h:95
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:314
bool operator<(const Address &lhs, const Address &rhs)
Definition: Address.cpp:991
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
Definition: lldb-forward.h:320
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
Definition: lldb-forward.h:316
@ eDescriptionLevelVerbose
uint64_t offset_t
Definition: lldb-types.h:83
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:313
uint64_t addr_t
Definition: lldb-types.h:79
AddressRange range
The section offset address range for this line entry.
Definition: LineEntry.h:137
uint32_t line
The source line number, or LLDB_INVALID_LINE_NUMBER if there is no line number information.
Definition: LineEntry.h:147