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()) {
78 "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 GetSerializationSubclassKey(), subclass_name);
86
87 if (!success) {
88 error =
89 Status::FromErrorString("Resolver data missing subclass resolver key");
90 return result_sp;
91 }
92
93 ResolverTy resolver_type = NameToResolverTy(subclass_name);
94 if (resolver_type == UnknownResolver) {
95 error = Status::FromErrorStringWithFormatv("Unknown resolver type: {0}.",
96 subclass_name);
97 return result_sp;
98 }
99
100 StructuredData::Dictionary *subclass_options = nullptr;
101 success = resolver_dict.GetValueForKeyAsDictionary(
102 GetSerializationSubclassOptionsKey(), subclass_options);
103 if (!success || !subclass_options || !subclass_options->IsValid()) {
104 error =
105 Status::FromErrorString("Resolver data missing subclass options key.");
106 return result_sp;
107 }
108
109 lldb::offset_t offset;
110 success = subclass_options->GetValueForKeyAsInteger(
111 GetKey(OptionNames::Offset), offset);
112 if (!success) {
113 error =
114 Status::FromErrorString("Resolver data missing offset options key.");
115 return result_sp;
116 }
117
118 switch (resolver_type) {
119 case FileLineResolver:
121 *subclass_options, error);
122 break;
123 case AddressResolver:
125 *subclass_options, error);
126 break;
127 case NameResolver:
129 *subclass_options, error);
130 break;
133 *subclass_options, error);
134 break;
135 case PythonResolver:
137 *subclass_options, error);
138 break;
140 error = Status::FromErrorString("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
155 StructuredData::DictionarySP options_dict_sp) {
156 if (!options_dict_sp || !options_dict_sp->IsValid())
158
160 type_dict_sp->AddStringItem(GetSerializationSubclassKey(), GetResolverName());
161 type_dict_sp->AddItem(GetSerializationSubclassOptionsKey(), options_dict_sp);
162
163 // Add the m_offset to the dictionary:
164 options_dict_sp->AddIntegerItem(GetKey(OptionNames::Offset), m_offset);
165
166 return type_dict_sp;
167}
168
170 assert(bkpt);
171 m_breakpoint = bkpt;
173}
174
176 ModuleList &modules) {
177 filter.SearchInModuleList(*this, modules);
178}
179
181 filter.Search(*this);
182}
183
184namespace {
185struct 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
194};
195
196bool 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
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;
211
212 for (const auto &sc : sc_list) {
214 .GetEnableFilterForLineBreakpoints())
215 if (Language *lang = Language::FindPlugin(sc.GetLanguage());
216 lang && lang->IgnoreForLineBreakpoints(sc))
217 continue;
218 all_scs.push_back(sc);
219 }
220
221 while (all_scs.size()) {
222 uint32_t closest_line = UINT32_MAX;
223
224 // Move all the elements with a matching file spec to the end.
225 auto &match = all_scs[0];
226 auto worklist_begin = std::partition(
227 all_scs.begin(), all_scs.end(), [&](const SymbolContext &sc) {
228 if (sc.line_entry.GetFile() == match.line_entry.GetFile() ||
229 sc.line_entry.original_file_sp->Equal(
230 *match.line_entry.original_file_sp,
231 SupportFile::eEqualFileSpecAndChecksumIfSet)) {
232 // When a match is found, keep track of the smallest line number.
233 closest_line = std::min(closest_line, sc.line_entry.line);
234 return false;
235 }
236 return true;
237 });
238
239 // (worklist_begin, worklist_end) now contains all entries for one filespec.
240 auto worklist_end = all_scs.end();
241
242 if (column) {
243 // If a column was requested, do a more precise match and only
244 // return the first location that comes before or at the
245 // requested location.
246 SourceLoc requested(line, *column);
247 // First, filter out all entries left of the requested column.
248 worklist_end = std::remove_if(
249 worklist_begin, worklist_end,
250 [&](const SymbolContext &sc) { return requested < SourceLoc(sc); });
251 // Sort the remaining entries by (line, column).
252 llvm::sort(worklist_begin, worklist_end,
253 [](const SymbolContext &a, const SymbolContext &b) {
254 return SourceLoc(a) < SourceLoc(b);
255 });
256
257 // Filter out all locations with a source location after the closest match.
258 if (worklist_begin != worklist_end)
259 worklist_end = std::remove_if(
260 worklist_begin, worklist_end, [&](const SymbolContext &sc) {
261 return SourceLoc(*worklist_begin) < SourceLoc(sc);
262 });
263 } else {
264 // Remove all entries with a larger line number.
265 // ResolveSymbolContext will always return a number that is >=
266 // the line number you pass in. So the smaller line number is
267 // always better.
268 worklist_end = std::remove_if(worklist_begin, worklist_end,
269 [&](const SymbolContext &sc) {
270 return closest_line != sc.line_entry.line;
271 });
272 }
273
274 // Sort by file address.
275 llvm::sort(worklist_begin, worklist_end,
276 [](const SymbolContext &a, const SymbolContext &b) {
277 return a.line_entry.range.GetBaseAddress().GetFileAddress() <
279 });
280
281 // Go through and see if there are line table entries that are
282 // contiguous, and if so keep only the first of the contiguous range.
283 // We do this by picking the first location in each lexical block.
284 llvm::SmallDenseSet<Block *, 8> blocks_with_breakpoints;
285 for (auto first = worklist_begin; first != worklist_end; ++first) {
286 assert(!blocks_with_breakpoints.count(first->block));
287 blocks_with_breakpoints.insert(first->block);
288 worklist_end =
289 std::remove_if(std::next(first), worklist_end,
290 [&](const SymbolContext &sc) {
291 return blocks_with_breakpoints.count(sc.block);
292 });
293 }
294
295 // Make breakpoints out of the closest line number match.
296 for (auto &sc : llvm::make_range(worklist_begin, worklist_end))
297 AddLocation(filter, sc, skip_prologue, log_ident);
298
299 // Remove all contexts processed by this iteration.
300 all_scs.erase(worklist_begin, all_scs.end());
301 }
302}
303
305 const SymbolContext &sc,
306 bool skip_prologue,
307 llvm::StringRef log_ident) {
309 Address line_start = sc.line_entry.range.GetBaseAddress();
310 if (!line_start.IsValid()) {
311 LLDB_LOGF(log,
312 "error: Unable to set breakpoint %s at file address "
313 "0x%" PRIx64 "\n",
314 log_ident.str().c_str(), line_start.GetFileAddress());
315 return;
316 }
317
318 if (!filter.AddressPasses(line_start)) {
319 LLDB_LOGF(log,
320 "Breakpoint %s at file address 0x%" PRIx64
321 " didn't pass the filter.\n",
322 log_ident.str().c_str(), line_start.GetFileAddress());
323 }
324
325 // If the line number is before the prologue end, move it there...
326 bool skipped_prologue = false;
327 if (skip_prologue && sc.function) {
328 Address prologue_addr(sc.function->GetAddressRange().GetBaseAddress());
329 if (prologue_addr.IsValid() && (line_start == prologue_addr)) {
330 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
331 if (prologue_byte_size) {
332 prologue_addr.Slide(prologue_byte_size);
333
334 if (filter.AddressPasses(prologue_addr)) {
335 skipped_prologue = true;
336 line_start = prologue_addr;
337 }
338 }
339 }
340 }
341
342 BreakpointLocationSP bp_loc_sp(AddLocation(line_start));
343 // If the address that we resolved the location to returns a different
344 // LineEntry from the one in the incoming SC, we're probably dealing with an
345 // inlined call site, so set that as the preferred LineEntry:
346 LineEntry resolved_entry;
347 if (!skipped_prologue && bp_loc_sp &&
348 line_start.CalculateSymbolContextLineEntry(resolved_entry) &&
349 LineEntry::Compare(resolved_entry, sc.line_entry)) {
350 // FIXME: The function name will also be wrong here. Do we need to record
351 // that as well, or can we figure that out again when we report this
352 // breakpoint location.
353 if (!bp_loc_sp->SetPreferredLineEntry(sc.line_entry)) {
354 LLDB_LOG(log, "Tried to add a preferred line entry that didn't have the "
355 "same address as this location's address.");
356 }
357 }
358 if (log && bp_loc_sp && !GetBreakpoint()->IsInternal()) {
359 StreamString s;
360 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
361 LLDB_LOGF(log, "Added location (skipped prologue: %s): %s \n",
362 skipped_prologue ? "yes" : "no", s.GetData());
363 }
364}
365
367 bool *new_location) {
368 loc_addr.Slide(m_offset);
369 return GetBreakpoint()->AddLocation(loc_addr, new_location);
370}
371
373 // There may already be an offset, so we are actually adjusting location
374 // addresses by the difference.
375 // lldb::addr_t slide = offset - m_offset;
376 // FIXME: We should go fix up all the already set locations for the new
377 // slide.
378
379 m_offset = offset;
380}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:369
#define LLDB_LOGF(log,...)
Definition: Log.h:376
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:211
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
bool CalculateSymbolContextLineEntry(LineEntry &line_entry) const
Definition: Address.cpp:914
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()
DEPRECATED: Use GetAddressRanges instead.
Definition: Function.h:448
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition: Function.cpp:594
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:362
static Language * FindPlugin(lldb::LanguageType language)
Definition: Language.cpp:84
static LanguageProperties & GetGlobalLanguageProperties()
Definition: Language.cpp:40
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:115
static Status FromErrorString(const char *str)
Definition: Status.h:138
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition: Status.h:148
const char * GetData() const
Definition: StreamString.h:45
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.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:332
bool operator<(const Address &lhs, const Address &rhs)
Definition: Address.cpp:992
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
Definition: lldb-forward.h:328
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
Definition: lldb-forward.h:324
@ eDescriptionLevelVerbose
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:321
uint64_t addr_t
Definition: lldb-types.h:80
A line table entry class.
Definition: LineEntry.h:21
static int Compare(const LineEntry &lhs, const LineEntry &rhs)
Compare two LineEntry objects.
Definition: LineEntry.cpp:147
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