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"
21 #include "lldb/Core/ModuleList.h"
22 #include "lldb/Core/SearchFilter.h"
24 #include "lldb/Symbol/Function.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/Stream.h"
30 
31 using namespace lldb_private;
32 using namespace lldb;
33 
34 // BreakpointResolver:
35 const char *BreakpointResolver::g_ty_to_name[] = {"FileAndLine", "Address",
36  "SymbolName", "SourceRegex",
37  "Python", "Exception",
38  "Unknown"};
39 
40 const char *BreakpointResolver::g_option_names[static_cast<uint32_t>(
42  "AddressOffset", "Exact", "FileName", "Inlines", "Language",
43  "LineNumber", "Column", "ModuleName", "NameMask", "Offset",
44  "PythonClass", "Regex", "ScriptArgs", "SectionName", "SearchDepth",
45  "SkipPrologue", "SymbolNames"};
46 
48  if (type > LastKnownResolverType)
49  return g_ty_to_name[UnknownResolver];
50 
51  return g_ty_to_name[type];
52 }
53 
55 BreakpointResolver::NameToResolverTy(llvm::StringRef name) {
56  for (size_t i = 0; i < LastKnownResolverType; i++) {
57  if (name == g_ty_to_name[i])
58  return (ResolverTy)i;
59  }
60  return UnknownResolver;
61 }
62 
63 BreakpointResolver::BreakpointResolver(const BreakpointSP &bkpt,
64  const unsigned char resolverTy,
65  lldb::addr_t offset)
66  : m_breakpoint(bkpt), m_offset(offset), SubclassID(resolverTy) {}
67 
69 
71  const StructuredData::Dictionary &resolver_dict, Status &error) {
72  BreakpointResolverSP result_sp;
73  if (!resolver_dict.IsValid()) {
74  error.SetErrorString("Can't deserialize from an invalid data object.");
75  return result_sp;
76  }
77 
78  llvm::StringRef subclass_name;
79 
80  bool success = resolver_dict.GetValueForKeyAsString(
81  GetSerializationSubclassKey(), subclass_name);
82 
83  if (!success) {
84  error.SetErrorString("Resolver data missing subclass resolver key");
85  return result_sp;
86  }
87 
88  ResolverTy resolver_type = NameToResolverTy(subclass_name);
89  if (resolver_type == UnknownResolver) {
90  error.SetErrorStringWithFormatv("Unknown resolver type: {0}.",
91  subclass_name);
92  return result_sp;
93  }
94 
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.");
100  return result_sp;
101  }
102 
103  lldb::addr_t offset;
104  success = subclass_options->GetValueForKeyAsInteger(
105  GetKey(OptionNames::Offset), offset);
106  if (!success) {
107  error.SetErrorString("Resolver data missing offset options key.");
108  return result_sp;
109  }
110 
111  BreakpointResolver *resolver;
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;
126  case FileRegexResolver:
128  nullptr, *subclass_options, error);
129  break;
130  case PythonResolver:
132  nullptr, *subclass_options, error);
133  break;
134  case ExceptionResolver:
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.Success()) {
142  return result_sp;
143  } else {
144  // Add on the global offset option:
145  resolver->SetOffset(offset);
146  return BreakpointResolverSP(resolver);
147  }
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 
165 void BreakpointResolver::SetBreakpoint(const BreakpointSP &bkpt) {
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 
180 namespace {
181 struct SourceLoc {
182  uint32_t line = UINT32_MAX;
183  uint16_t column;
184  SourceLoc(uint32_t l, llvm::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 
192 bool 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, llvm::Optional<uint16_t> column) {
206  llvm::SmallVector<SymbolContext, 16> all_scs;
207  for (uint32_t i = 0; i < sc_list.GetSize(); ++i)
208  all_scs.push_back(sc_list[i]);
209 
210  while (all_scs.size()) {
211  uint32_t closest_line = UINT32_MAX;
212 
213  // Move all the elements with a matching file spec to the end.
214  auto &match = all_scs[0];
215  auto worklist_begin = std::partition(
216  all_scs.begin(), all_scs.end(), [&](const SymbolContext &sc) {
217  if (sc.line_entry.file == match.line_entry.file ||
218  sc.line_entry.original_file == match.line_entry.original_file) {
219  // When a match is found, keep track of the smallest line number.
220  closest_line = std::min(closest_line, sc.line_entry.line);
221  return false;
222  }
223  return true;
224  });
225 
226  // (worklist_begin, worklist_end) now contains all entries for one filespec.
227  auto worklist_end = all_scs.end();
228 
229  if (column) {
230  // If a column was requested, do a more precise match and only
231  // return the first location that comes before or at the
232  // requested location.
233  SourceLoc requested(line, *column);
234  // First, filter out all entries left of the requested column.
235  worklist_end = std::remove_if(
236  worklist_begin, worklist_end,
237  [&](const SymbolContext &sc) { return requested < SourceLoc(sc); });
238  // Sort the remaining entries by (line, column).
239  llvm::sort(worklist_begin, worklist_end,
240  [](const SymbolContext &a, const SymbolContext &b) {
241  return SourceLoc(a) < SourceLoc(b);
242  });
243 
244  // Filter out all locations with a source location after the closest match.
245  if (worklist_begin != worklist_end)
246  worklist_end = std::remove_if(
247  worklist_begin, worklist_end, [&](const SymbolContext &sc) {
248  return SourceLoc(*worklist_begin) < SourceLoc(sc);
249  });
250  } else {
251  // Remove all entries with a larger line number.
252  // ResolveSymbolContext will always return a number that is >=
253  // the line number you pass in. So the smaller line number is
254  // always better.
255  worklist_end = std::remove_if(worklist_begin, worklist_end,
256  [&](const SymbolContext &sc) {
257  return closest_line != sc.line_entry.line;
258  });
259  }
260 
261  // Sort by file address.
262  llvm::sort(worklist_begin, worklist_end,
263  [](const SymbolContext &a, const SymbolContext &b) {
264  return a.line_entry.range.GetBaseAddress().GetFileAddress() <
266  });
267 
268  // Go through and see if there are line table entries that are
269  // contiguous, and if so keep only the first of the contiguous range.
270  // We do this by picking the first location in each lexical block.
271  llvm::SmallDenseSet<Block *, 8> blocks_with_breakpoints;
272  for (auto first = worklist_begin; first != worklist_end; ++first) {
273  assert(!blocks_with_breakpoints.count(first->block));
274  blocks_with_breakpoints.insert(first->block);
275  worklist_end =
276  std::remove_if(std::next(first), worklist_end,
277  [&](const SymbolContext &sc) {
278  return blocks_with_breakpoints.count(sc.block);
279  });
280  }
281 
282  // Make breakpoints out of the closest line number match.
283  for (auto &sc : llvm::make_range(worklist_begin, worklist_end))
284  AddLocation(filter, sc, skip_prologue, log_ident);
285 
286  // Remove all contexts processed by this iteration.
287  all_scs.erase(worklist_begin, all_scs.end());
288  }
289 }
290 
291 void BreakpointResolver::AddLocation(SearchFilter &filter,
292  const SymbolContext &sc,
293  bool skip_prologue,
294  llvm::StringRef log_ident) {
296  Address line_start = sc.line_entry.range.GetBaseAddress();
297  if (!line_start.IsValid()) {
298  LLDB_LOGF(log,
299  "error: Unable to set breakpoint %s at file address "
300  "0x%" PRIx64 "\n",
301  log_ident.str().c_str(), line_start.GetFileAddress());
302  return;
303  }
304 
305  if (!filter.AddressPasses(line_start)) {
306  LLDB_LOGF(log,
307  "Breakpoint %s at file address 0x%" PRIx64
308  " didn't pass the filter.\n",
309  log_ident.str().c_str(), line_start.GetFileAddress());
310  }
311 
312  // If the line number is before the prologue end, move it there...
313  bool skipped_prologue = false;
314  if (skip_prologue && sc.function) {
315  Address prologue_addr(sc.function->GetAddressRange().GetBaseAddress());
316  if (prologue_addr.IsValid() && (line_start == prologue_addr)) {
317  const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
318  if (prologue_byte_size) {
319  prologue_addr.Slide(prologue_byte_size);
320 
321  if (filter.AddressPasses(prologue_addr)) {
322  skipped_prologue = true;
323  line_start = prologue_addr;
324  }
325  }
326  }
327  }
328 
329  BreakpointLocationSP bp_loc_sp(AddLocation(line_start));
330  if (log && bp_loc_sp && !GetBreakpoint()->IsInternal()) {
331  StreamString s;
332  bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
333  LLDB_LOGF(log, "Added location (skipped prologue: %s): %s \n",
334  skipped_prologue ? "yes" : "no", s.GetData());
335  }
336 }
337 
338 BreakpointLocationSP BreakpointResolver::AddLocation(Address loc_addr,
339  bool *new_location) {
340  loc_addr.Slide(m_offset);
341  return GetBreakpoint()->AddLocation(loc_addr, new_location);
342 }
343 
345  // There may already be an offset, so we are actually adjusting location
346  // addresses by the difference.
347  // lldb::addr_t slide = offset - m_offset;
348  // FIXME: We should go fix up all the already set locations for the new
349  // slide.
350 
351  m_offset = offset;
352 }
lldb_private::BreakpointResolver::NameToResolverTy
static ResolverTy NameToResolverTy(llvm::StringRef name)
Definition: BreakpointResolver.cpp:55
lldb_private::AddressRange::GetBaseAddress
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
lldb_private::BreakpointResolver::~BreakpointResolver
~BreakpointResolver() override
The Destructor is virtual, all significant breakpoint resolvers derive from this class.
Definition: BreakpointResolver.cpp:68
lldb_private::SymbolContext::block
Block * block
The Block for a given query.
Definition: SymbolContext.h:321
lldb_private::BreakpointResolver::ResolverTy
ResolverTy
An enumeration for keeping track of the concrete subclass that is actually instantiated.
Definition: BreakpointResolver.h:127
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
lldb_private::BreakpointResolverFileLine::CreateFromStructuredData
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &data_dict, Status &error)
Definition: BreakpointResolverFileLine.cpp:28
lldb_private::StructuredData::Object::IsValid
virtual bool IsValid() const
Definition: StructuredData.h:77
lldb_private::Address::IsValid
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:332
lldb_private::SearchFilter::AddressPasses
virtual bool AddressPasses(Address &addr)
Call this method with a Address to see if address passes the filter.
Definition: SearchFilter.cpp:141
CompileUnit.h
lldb_private::Function::GetAddressRange
const AddressRange & GetAddressRange()
Definition: Function.h:461
lldb_private::BreakpointResolver::m_breakpoint
lldb::BreakpointWP m_breakpoint
Definition: BreakpointResolver.h:220
LLDB_INVALID_COLUMN_NUMBER
#define LLDB_INVALID_COLUMN_NUMBER
Definition: lldb-defines.h:98
BreakpointResolverFileRegex.h
lldb_private::BreakpointResolver::g_option_names
static const char * g_option_names[static_cast< uint32_t >(OptionNames::LastOptionName)]
Definition: BreakpointResolver.h:188
lldb_private::BreakpointResolver::AddressResolver
@ AddressResolver
Definition: BreakpointResolver.h:129
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:249
lldb_private::SearchFilter::SearchInModuleList
virtual void SearchInModuleList(Searcher &searcher, ModuleList &modules)
Call this method to do the search using the Searcher in the module list modules.
Definition: SearchFilter.cpp:219
BreakpointLocation.h
BreakpointResolver.h
lldb_private::SymbolContextList
Definition: SymbolContext.h:378
lldb_private::SearchFilter::Search
virtual void Search(Searcher &searcher)
Call this method to do the search using the Searcher.
Definition: SearchFilter.cpp:204
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::BreakpointResolver::PythonResolver
@ PythonResolver
Definition: BreakpointResolver.h:132
lldb_private::BreakpointResolverAddress::CreateFromStructuredData
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, Status &error)
Definition: BreakpointResolverAddress.cpp:36
lldb_private::StructuredData::Dictionary::GetValueForKeyAsInteger
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
Definition: StructuredData.h:403
Target.h
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::BreakpointResolver::ResolveBreakpointInModules
virtual void ResolveBreakpointInModules(SearchFilter &filter, ModuleList &modules)
In response to this method the resolver scans the modules in the module list modules,...
Definition: BreakpointResolver.cpp:171
lldb_private::BreakpointResolver::OptionNames::LastOptionName
@ LastOptionName
lldb_private::BreakpointResolver::CreateFromStructuredData
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &resolver_dict, Status &error)
This section handles serializing and deserializing from StructuredData objects.
Definition: BreakpointResolver.cpp:70
ModuleList.h
lldb_private::BreakpointResolver::NotifyBreakpointSet
virtual void NotifyBreakpointSet()
Definition: BreakpointResolver.h:190
lldb_private::BreakpointResolver::BreakpointResolver
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.
lldb_private::ModuleList
Definition: ModuleList.h:71
lldb_private::SearchFilter
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
Log.h
lldb_private::BreakpointResolver::NameResolver
@ NameResolver
Definition: BreakpointResolver.h:130
SearchFilter.h
lldb_private::BreakpointResolver::FileLineResolver
@ FileLineResolver
Definition: BreakpointResolver.h:128
lldb_private::StreamString::GetData
const char * GetData() const
Definition: StreamString.h:43
lldb_private::BreakpointResolver::ExceptionResolver
@ ExceptionResolver
Definition: BreakpointResolver.h:133
lldb_private::SymbolContext::function
Function * function
The Function for a given query.
Definition: SymbolContext.h:320
StreamString.h
lldb_private::Address::GetFileAddress
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:290
lldb_private::BreakpointResolver::ResolverTyToName
static const char * ResolverTyToName(enum ResolverTy)
Definition: BreakpointResolver.cpp:47
lldb_private::SymbolContextList::GetSize
uint32_t GetSize() const
Get accessor for a symbol context list size.
Definition: SymbolContext.cpp:1283
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::BreakpointResolver::GetResolverName
const char * GetResolverName()
Definition: BreakpointResolver.h:154
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::BreakpointResolver::UnknownResolver
@ UnknownResolver
Definition: BreakpointResolver.h:135
lldb_private::StructuredData::DictionarySP
std::shared_ptr< Dictionary > DictionarySP
Definition: StructuredData.h:67
lldb_private::BreakpointResolver::SetBreakpoint
void SetBreakpoint(const lldb::BreakpointSP &bkpt)
This sets the breakpoint for this resolver.
Definition: BreakpointResolver.cpp:165
Address.h
lldb_private::BreakpointResolver::AddLocation
lldb::BreakpointLocationSP AddLocation(Address loc_addr, bool *new_location=nullptr)
Definition: BreakpointResolver.cpp:338
lldb_private::LineEntry::line
uint32_t line
The source line number, or zero if there is no line number information.
Definition: LineEntry.h:143
lldb_private::BreakpointResolver
General Outline: The BreakpointResolver is a Searcher.
Definition: BreakpointResolver.h:35
lldb_private::BreakpointResolver::WrapOptionsDict
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
Definition: BreakpointResolver.cpp:150
lldb_private::BreakpointResolver::OptionNames::Offset
@ Offset
lldb_private::Status
Definition: Status.h:44
lldb_private::BreakpointResolver::g_ty_to_name
static const char * g_ty_to_name[LastKnownResolverType+2]
Definition: BreakpointResolver.h:140
lldb_private::BreakpointResolver::FileRegexResolver
@ FileRegexResolver
Definition: BreakpointResolver.h:131
lldb_private::BreakpointResolverFileRegex::CreateFromStructuredData
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, Status &error)
Definition: BreakpointResolverFileRegex.cpp:29
uint32_t
LIBLLDB_LOG_BREAKPOINTS
#define LIBLLDB_LOG_BREAKPOINTS
Definition: Logging.h:19
lldb_private::Address
Definition: Address.h:59
lldb_private::BreakpointResolver::SetSCMatchesByLine
void SetSCMatchesByLine(SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue, llvm::StringRef log_ident, uint32_t line=0, llvm::Optional< uint16_t > column=llvm::None)
Takes a symbol context list of matches which supposedly represent the same file and line number in a ...
Definition: BreakpointResolver.cpp:203
lldb_private::BreakpointResolver::GetKey
static const char * GetKey(OptionNames enum_value)
Definition: BreakpointResolver.h:193
lldb_private::Function::GetPrologueByteSize
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition: Function.cpp:529
lldb_private::BreakpointResolver::GetSerializationSubclassOptionsKey
static const char * GetSerializationSubclassOptionsKey()
Definition: BreakpointResolver.h:118
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::LineEntry::range
AddressRange range
The section offset address range for this line entry.
Definition: LineEntry.h:139
lldb_private::operator<
bool operator<(const Address &lhs, const Address &rhs)
Definition: Address.cpp:955
uint16_t
lldb_private::BreakpointResolver::SetOffset
void SetOffset(lldb::addr_t offset)
This updates the offset for this breakpoint.
Definition: BreakpointResolver.cpp:344
Function.h
lldb_private::BreakpointResolver::ResolveBreakpoint
virtual void ResolveBreakpoint(SearchFilter &filter)
In response to this method the resolver scans all the modules in the breakpoint's target,...
Definition: BreakpointResolver.cpp:176
SymbolContext.h
lldb_private::BreakpointResolverScripted::CreateFromStructuredData
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, Status &error)
Definition: BreakpointResolverScripted.cpp:63
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::eDescriptionLevelVerbose
@ eDescriptionLevelVerbose
Definition: lldb-enumerations.h:210
BreakpointResolverName.h
lldb_private::StructuredData::Dictionary::GetValueForKeyAsDictionary
bool GetValueForKeyAsDictionary(llvm::StringRef key, Dictionary *&result) const
Definition: StructuredData.h:467
lldb_private::BreakpointResolverName::CreateFromStructuredData
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &data_dict, Status &error)
Definition: BreakpointResolverName.cpp:91
Breakpoint.h
lldb_private::BreakpointResolver::GetSerializationSubclassKey
static const char * GetSerializationSubclassKey()
Definition: BreakpointResolver.h:116
Stream.h
lldb_private::Log
Definition: Log.h:49
lldb_private::StructuredData::Dictionary::GetValueForKeyAsString
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
Definition: StructuredData.h:423
lldb_private::SymbolContext::line_entry
LineEntry line_entry
The LineEntry for a given query.
Definition: SymbolContext.h:322
BreakpointResolverScripted.h
BreakpointResolverAddress.h
lldb_private::Address::Slide
bool Slide(int64_t offset)
Definition: Address.h:436
lldb
Definition: SBAddress.h:15
lldb_private::BreakpointResolver::GetBreakpoint
lldb::BreakpointSP GetBreakpoint() const
This gets the breakpoint for this resolver.
Definition: BreakpointResolver.h:62
BreakpointResolverFileLine.h
lldb_private::BreakpointResolver::m_offset
lldb::addr_t m_offset
Definition: BreakpointResolver.h:221