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"
26#include "lldb/Target/Target.h"
28#include "lldb/Utility/Log.h"
29#include "lldb/Utility/Stream.h"
31#include <optional>
32
33using namespace lldb_private;
34using namespace lldb;
35
36// BreakpointResolver:
37const char *BreakpointResolver::g_ty_to_name[] = {"FileAndLine", "Address",
38 "SymbolName", "SourceRegex",
39 "Python", "Exception",
40 "Unknown"};
41
42const char *BreakpointResolver::g_option_names[static_cast<uint32_t>(
43 BreakpointResolver::OptionNames::LastOptionName)] = {
44 "AddressOffset", "Exact", "FileName", "Inlines", "Language",
45 "LineNumber", "Column", "ModuleName", "NameMask", "Offset",
46 "PythonClass", "Regex", "ScriptArgs", "SectionName", "SearchDepth",
47 "SkipPrologue", "SymbolNames"};
48
50 if (type > LastKnownResolverType)
52
53 return g_ty_to_name[type];
54}
55
58 for (size_t i = 0; i < LastKnownResolverType; i++) {
59 if (name == g_ty_to_name[i])
60 return (ResolverTy)i;
61 }
62 return UnknownResolver;
63}
64
66 const unsigned char resolverTy,
67 lldb::addr_t offset)
68 : m_breakpoint(bkpt), m_offset(offset), SubclassID(resolverTy) {}
69
71
73 const StructuredData::Dictionary &resolver_dict, Status &error) {
74 BreakpointResolverSP result_sp;
75 if (!resolver_dict.IsValid()) {
76 error.SetErrorString("Can't deserialize from an invalid data object.");
77 return result_sp;
78 }
79
80 llvm::StringRef subclass_name;
81
82 bool success = resolver_dict.GetValueForKeyAsString(
83 GetSerializationSubclassKey(), subclass_name);
84
85 if (!success) {
86 error.SetErrorString("Resolver data missing subclass resolver key");
87 return result_sp;
88 }
89
90 ResolverTy resolver_type = NameToResolverTy(subclass_name);
91 if (resolver_type == UnknownResolver) {
92 error.SetErrorStringWithFormatv("Unknown resolver type: {0}.",
93 subclass_name);
94 return result_sp;
95 }
96
97 StructuredData::Dictionary *subclass_options = nullptr;
98 success = resolver_dict.GetValueForKeyAsDictionary(
99 GetSerializationSubclassOptionsKey(), subclass_options);
100 if (!success || !subclass_options || !subclass_options->IsValid()) {
101 error.SetErrorString("Resolver data missing subclass options key.");
102 return result_sp;
103 }
104
105 lldb::offset_t offset;
106 success = subclass_options->GetValueForKeyAsInteger(
107 GetKey(OptionNames::Offset), offset);
108 if (!success) {
109 error.SetErrorString("Resolver data missing offset options key.");
110 return result_sp;
111 }
112
113 switch (resolver_type) {
114 case FileLineResolver:
116 nullptr, *subclass_options, error);
117 break;
118 case AddressResolver:
120 nullptr, *subclass_options, error);
121 break;
122 case NameResolver:
124 nullptr, *subclass_options, error);
125 break;
128 nullptr, *subclass_options, error);
129 break;
130 case PythonResolver:
132 nullptr, *subclass_options, error);
133 break;
135 error.SetErrorString("Exception resolvers are hard.");
136 break;
137 default:
138 llvm_unreachable("Should never get an unresolvable resolver type.");
139 }
140
141 if (error.Fail() || !result_sp)
142 return {};
143
144 // Add on the global offset option:
145 result_sp->SetOffset(offset);
146 return result_sp;
147}
148
150 StructuredData::DictionarySP options_dict_sp) {
151 if (!options_dict_sp || !options_dict_sp->IsValid())
153
155 type_dict_sp->AddStringItem(GetSerializationSubclassKey(), GetResolverName());
156 type_dict_sp->AddItem(GetSerializationSubclassOptionsKey(), options_dict_sp);
157
158 // Add the m_offset to the dictionary:
159 options_dict_sp->AddIntegerItem(GetKey(OptionNames::Offset), m_offset);
160
161 return type_dict_sp;
162}
163
165 assert(bkpt);
166 m_breakpoint = bkpt;
168}
169
171 ModuleList &modules) {
172 filter.SearchInModuleList(*this, modules);
173}
174
176 filter.Search(*this);
177}
178
179namespace {
180struct SourceLoc {
181 uint32_t line = UINT32_MAX;
182 uint16_t column;
183 SourceLoc(uint32_t l, std::optional<uint16_t> c)
184 : line(l), column(c ? *c : LLDB_INVALID_COLUMN_NUMBER) {}
185 SourceLoc(const SymbolContext &sc)
186 : line(sc.line_entry.line),
187 column(sc.line_entry.column ? sc.line_entry.column
189};
190
191bool operator<(const SourceLoc lhs, const SourceLoc rhs) {
192 if (lhs.line < rhs.line)
193 return true;
194 if (lhs.line > rhs.line)
195 return false;
196 // uint32_t a_col = lhs.column ? lhs.column : LLDB_INVALID_COLUMN_NUMBER;
197 // uint32_t b_col = rhs.column ? rhs.column : LLDB_INVALID_COLUMN_NUMBER;
198 return lhs.column < rhs.column;
199}
200} // namespace
201
203 SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue,
204 llvm::StringRef log_ident, uint32_t line, std::optional<uint16_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]);
208
209 while (all_scs.size()) {
210 uint32_t closest_line = UINT32_MAX;
211
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);
220 return false;
221 }
222 return true;
223 });
224
225 // (worklist_begin, worklist_end) now contains all entries for one filespec.
226 auto worklist_end = all_scs.end();
227
228 if (column) {
229 // If a column was requested, do a more precise match and only
230 // return the first location that comes before 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 requested < SourceLoc(sc); });
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);
241 });
242
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);
248 });
249 } else {
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
253 // always better.
254 worklist_end = std::remove_if(worklist_begin, worklist_end,
255 [&](const SymbolContext &sc) {
256 return closest_line != sc.line_entry.line;
257 });
258 }
259
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() <
265 });
266
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);
274 worklist_end =
275 std::remove_if(std::next(first), worklist_end,
276 [&](const SymbolContext &sc) {
277 return blocks_with_breakpoints.count(sc.block);
278 });
279 }
280
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);
284
285 // Remove all contexts processed by this iteration.
286 all_scs.erase(worklist_begin, all_scs.end());
287 }
288}
289
291 const SymbolContext &sc,
292 bool skip_prologue,
293 llvm::StringRef log_ident) {
295 Address line_start = sc.line_entry.range.GetBaseAddress();
296 if (!line_start.IsValid()) {
297 LLDB_LOGF(log,
298 "error: Unable to set breakpoint %s at file address "
299 "0x%" PRIx64 "\n",
300 log_ident.str().c_str(), line_start.GetFileAddress());
301 return;
302 }
303
304 if (!filter.AddressPasses(line_start)) {
305 LLDB_LOGF(log,
306 "Breakpoint %s at file address 0x%" PRIx64
307 " didn't pass the filter.\n",
308 log_ident.str().c_str(), line_start.GetFileAddress());
309 }
310
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);
319
320 if (filter.AddressPasses(prologue_addr)) {
321 skipped_prologue = true;
322 line_start = prologue_addr;
323 }
324 }
325 }
326 }
327
328 BreakpointLocationSP bp_loc_sp(AddLocation(line_start));
329 if (log && bp_loc_sp && !GetBreakpoint()->IsInternal()) {
330 StreamString s;
331 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
332 LLDB_LOGF(log, "Added location (skipped prologue: %s): %s \n",
333 skipped_prologue ? "yes" : "no", s.GetData());
334 }
335}
336
338 bool *new_location) {
339 loc_addr.Slide(m_offset);
340 return GetBreakpoint()->AddLocation(loc_addr, new_location);
341}
342
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
348 // slide.
349
350 m_offset = offset;
351}
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:59
bool Slide(int64_t offset)
Definition: Address.h:449
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:291
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:345
static lldb::BreakpointResolverSP CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, Status &error)
static lldb::BreakpointResolverSP CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &data_dict, Status &error)
static lldb::BreakpointResolverSP CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, Status &error)
static lldb::BreakpointResolverSP CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &data_dict, Status &error)
static lldb::BreakpointResolverSP CreateFromStructuredData(const lldb::BreakpointSP &bkpt, 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
A collection class for Module objects.
Definition: ModuleList.h:82
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.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
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:985
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
Definition: lldb-forward.h:316
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
Definition: lldb-forward.h:312
@ eDescriptionLevelVerbose
uint64_t offset_t
Definition: lldb-types.h:83
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:309
uint64_t addr_t
Definition: lldb-types.h:79
AddressRange range
The section offset address range for this line entry.
Definition: LineEntry.h:139
uint32_t line
The source line number, or zero if there is no line number information.
Definition: LineEntry.h:143