LLDB  mainline
BreakpointResolverName.cpp
Go to the documentation of this file.
1 //===-- BreakpointResolverName.cpp ------------------------------*- C++ -*-===//
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 #include "lldb/Core/Architecture.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Symbol/Block.h"
16 #include "lldb/Symbol/Function.h"
17 #include "lldb/Symbol/Symbol.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/Log.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 BreakpointResolverName::BreakpointResolverName(
27  Breakpoint *bkpt, const char *name_cstr, FunctionNameType name_type_mask,
28  LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset,
29  bool skip_prologue)
30  : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset),
31  m_class_name(), m_regex(), m_match_type(type), m_language(language),
32  m_skip_prologue(skip_prologue) {
33  if (m_match_type == Breakpoint::Regexp) {
34  if (!m_regex.Compile(llvm::StringRef::withNullAsEmpty(name_cstr))) {
36 
37  if (log)
38  log->Warning("function name regexp: \"%s\" did not compile.",
39  name_cstr);
40  }
41  } else {
42  AddNameLookup(ConstString(name_cstr), name_type_mask);
43  }
44 }
45 
47  Breakpoint *bkpt, const char *names[], size_t num_names,
48  FunctionNameType name_type_mask, LanguageType language, lldb::addr_t offset,
49  bool skip_prologue)
51  m_match_type(Breakpoint::Exact), m_language(language),
52  m_skip_prologue(skip_prologue) {
53  for (size_t i = 0; i < num_names; i++) {
54  AddNameLookup(ConstString(names[i]), name_type_mask);
55  }
56 }
57 
59  std::vector<std::string> names,
60  FunctionNameType name_type_mask,
61  LanguageType language,
62  lldb::addr_t offset,
63  bool skip_prologue)
65  m_match_type(Breakpoint::Exact), m_language(language),
66  m_skip_prologue(skip_prologue) {
67  for (const std::string &name : names) {
68  AddNameLookup(ConstString(name.c_str(), name.size()), name_type_mask);
69  }
70 }
71 
73  RegularExpression &func_regex,
74  lldb::LanguageType language,
75  lldb::addr_t offset,
76  bool skip_prologue)
78  m_class_name(nullptr), m_regex(func_regex),
79  m_match_type(Breakpoint::Regexp), m_language(language),
80  m_skip_prologue(skip_prologue) {}
81 
83 
85  const BreakpointResolverName &rhs)
87  rhs.m_offset),
91 
93  Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
94  Status &error) {
96  llvm::StringRef language_name;
97  bool success = options_dict.GetValueForKeyAsString(
98  GetKey(OptionNames::LanguageName), language_name);
99  if (success) {
100  language = Language::GetLanguageTypeFromString(language_name);
101  if (language == eLanguageTypeUnknown) {
102  error.SetErrorStringWithFormatv("BRN::CFSD: Unknown language: {0}.",
103  language_name);
104  return nullptr;
105  }
106  }
107 
108  lldb::addr_t offset = 0;
109  success =
110  options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Offset), offset);
111  if (!success) {
112  error.SetErrorStringWithFormat("BRN::CFSD: Missing offset entry.");
113  return nullptr;
114  }
115 
116  bool skip_prologue;
117  success = options_dict.GetValueForKeyAsBoolean(
118  GetKey(OptionNames::SkipPrologue), skip_prologue);
119  if (!success) {
120  error.SetErrorStringWithFormat("BRN::CFSD: Missing Skip prologue entry.");
121  return nullptr;
122  }
123 
124  llvm::StringRef regex_text;
125  success = options_dict.GetValueForKeyAsString(
126  GetKey(OptionNames::RegexString), regex_text);
127  if (success) {
128  RegularExpression regex(regex_text);
129  return new BreakpointResolverName(bkpt, regex, language, offset,
130  skip_prologue);
131  } else {
132  StructuredData::Array *names_array;
133  success = options_dict.GetValueForKeyAsArray(
134  GetKey(OptionNames::SymbolNameArray), names_array);
135  if (!success) {
136  error.SetErrorStringWithFormat("BRN::CFSD: Missing symbol names entry.");
137  return nullptr;
138  }
139  StructuredData::Array *names_mask_array;
140  success = options_dict.GetValueForKeyAsArray(
141  GetKey(OptionNames::NameMaskArray), names_mask_array);
142  if (!success) {
144  "BRN::CFSD: Missing symbol names mask entry.");
145  return nullptr;
146  }
147 
148  size_t num_elem = names_array->GetSize();
149  if (num_elem != names_mask_array->GetSize()) {
150  error.SetErrorString(
151  "BRN::CFSD: names and names mask arrays have different sizes.");
152  return nullptr;
153  }
154 
155  if (num_elem == 0) {
156  error.SetErrorString(
157  "BRN::CFSD: no name entry in a breakpoint by name breakpoint.");
158  return nullptr;
159  }
160  std::vector<std::string> names;
161  std::vector<FunctionNameType> name_masks;
162  for (size_t i = 0; i < num_elem; i++) {
163  llvm::StringRef name;
164 
165  success = names_array->GetItemAtIndexAsString(i, name);
166  if (!success) {
167  error.SetErrorString("BRN::CFSD: name entry is not a string.");
168  return nullptr;
169  }
170  std::underlying_type<FunctionNameType>::type fnt;
171  success = names_mask_array->GetItemAtIndexAsInteger(i, fnt);
172  if (!success) {
173  error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
174  return nullptr;
175  }
176  names.push_back(name);
177  name_masks.push_back(static_cast<FunctionNameType>(fnt));
178  }
179 
181  bkpt, names[0].c_str(), name_masks[0], language,
182  Breakpoint::MatchType::Exact, offset, skip_prologue);
183  for (size_t i = 1; i < num_elem; i++) {
184  resolver->AddNameLookup(ConstString(names[i]), name_masks[i]);
185  }
186  return resolver;
187  }
188 }
189 
191  StructuredData::DictionarySP options_dict_sp(
193 
194  if (m_regex.IsValid()) {
195  options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
196  m_regex.GetText());
197  } else {
199  StructuredData::ArraySP name_masks_sp(new StructuredData::Array());
200  for (auto lookup : m_lookups) {
201  names_sp->AddItem(StructuredData::StringSP(
202  new StructuredData::String(lookup.GetName().AsCString())));
203  name_masks_sp->AddItem(StructuredData::IntegerSP(
204  new StructuredData::Integer(lookup.GetNameTypeMask())));
205  }
206  options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
207  options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
208  }
210  options_dict_sp->AddStringItem(
213  options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
215 
216  return WrapOptionsDict(options_dict_sp);
217 }
218 
220  FunctionNameType name_type_mask) {
221  ObjCLanguage::MethodName objc_method(name.GetCString(), false);
222  if (objc_method.IsValid(false)) {
223  std::vector<ConstString> objc_names;
224  objc_method.GetFullNames(objc_names, true);
225  for (ConstString objc_name : objc_names) {
226  Module::LookupInfo lookup;
227  lookup.SetName(name);
228  lookup.SetLookupName(objc_name);
229  lookup.SetNameTypeMask(eFunctionNameTypeFull);
230  m_lookups.push_back(lookup);
231  }
232  } else {
233  Module::LookupInfo lookup(name, name_type_mask, m_language);
234  m_lookups.push_back(lookup);
235  }
236 }
237 
238 // FIXME: Right now we look at the module level, and call the module's
239 // "FindFunctions".
240 // Greg says he will add function tables, maybe at the CompileUnit level to
241 // accelerate function lookup. At that point, we should switch the depth to
242 // CompileUnit, and look in these tables.
243 
246  SymbolContext &context, Address *addr,
247  bool containing) {
248  SymbolContextList func_list;
249  // SymbolContextList sym_list;
250 
251  uint32_t i;
252  bool new_location;
253  Address break_addr;
254  assert(m_breakpoint != nullptr);
255 
257 
258  if (m_class_name) {
259  if (log)
260  log->Warning("Class/method function specification not supported yet.\n");
262  }
263  bool filter_by_cu =
264  (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
265  bool filter_by_language = (m_language != eLanguageTypeUnknown);
266  const bool include_symbols = !filter_by_cu;
267  const bool include_inlines = true;
268  const bool append = true;
269 
270  switch (m_match_type) {
271  case Breakpoint::Exact:
272  if (context.module_sp) {
273  for (const auto &lookup : m_lookups) {
274  const size_t start_func_idx = func_list.GetSize();
275  context.module_sp->FindFunctions(
276  lookup.GetLookupName(), nullptr, lookup.GetNameTypeMask(),
277  include_symbols, include_inlines, append, func_list);
278 
279  const size_t end_func_idx = func_list.GetSize();
280 
281  if (start_func_idx < end_func_idx)
282  lookup.Prune(func_list, start_func_idx);
283  }
284  }
285  break;
286  case Breakpoint::Regexp:
287  if (context.module_sp) {
288  context.module_sp->FindFunctions(
289  m_regex,
290  !filter_by_cu, // include symbols only if we aren't filtering by CU
291  include_inlines, append, func_list);
292  }
293  break;
294  case Breakpoint::Glob:
295  if (log)
296  log->Warning("glob is not supported yet.");
297  break;
298  }
299 
300  // If the filter specifies a Compilation Unit, remove the ones that don't
301  // pass at this point.
302  if (filter_by_cu || filter_by_language) {
303  uint32_t num_functions = func_list.GetSize();
304 
305  for (size_t idx = 0; idx < num_functions; idx++) {
306  bool remove_it = false;
307  SymbolContext sc;
308  func_list.GetContextAtIndex(idx, sc);
309  if (filter_by_cu) {
310  if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
311  remove_it = true;
312  }
313 
314  if (filter_by_language) {
315  LanguageType sym_language = sc.GetLanguage();
316  if ((Language::GetPrimaryLanguage(sym_language) !=
318  (sym_language != eLanguageTypeUnknown)) {
319  remove_it = true;
320  }
321  }
322 
323  if (remove_it) {
324  func_list.RemoveContextAtIndex(idx);
325  num_functions--;
326  idx--;
327  }
328  }
329  }
330 
331  // Remove any duplicates between the function list and the symbol list
332  SymbolContext sc;
333  if (func_list.GetSize()) {
334  for (i = 0; i < func_list.GetSize(); i++) {
335  if (func_list.GetContextAtIndex(i, sc)) {
336  bool is_reexported = false;
337 
338  if (sc.block && sc.block->GetInlinedFunctionInfo()) {
339  if (!sc.block->GetStartAddress(break_addr))
340  break_addr.Clear();
341  } else if (sc.function) {
342  break_addr = sc.function->GetAddressRange().GetBaseAddress();
343  if (m_skip_prologue && break_addr.IsValid()) {
344  const uint32_t prologue_byte_size =
346  if (prologue_byte_size)
347  break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
348  }
349  } else if (sc.symbol) {
350  if (sc.symbol->GetType() == eSymbolTypeReExported) {
351  const Symbol *actual_symbol =
353  if (actual_symbol) {
354  is_reexported = true;
355  break_addr = actual_symbol->GetAddress();
356  }
357  } else {
358  break_addr = sc.symbol->GetAddress();
359  }
360 
361  if (m_skip_prologue && break_addr.IsValid()) {
362  const uint32_t prologue_byte_size =
364  if (prologue_byte_size)
365  break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
366  else {
367  const Architecture *arch =
369  if (arch)
370  arch->AdjustBreakpointAddress(*sc.symbol, break_addr);
371  }
372  }
373  }
374 
375  if (break_addr.IsValid()) {
376  if (filter.AddressPasses(break_addr)) {
377  BreakpointLocationSP bp_loc_sp(
378  AddLocation(break_addr, &new_location));
379  bp_loc_sp->SetIsReExported(is_reexported);
380  if (bp_loc_sp && new_location && !m_breakpoint->IsInternal()) {
381  if (log) {
382  StreamString s;
383  bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
384  log->Printf("Added location: %s\n", s.GetData());
385  }
386  }
387  }
388  }
389  }
390  }
391  }
392 
394 }
395 
398 }
399 
402  s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
403  else {
404  size_t num_names = m_lookups.size();
405  if (num_names == 1)
406  s->Printf("name = '%s'", m_lookups[0].GetName().GetCString());
407  else {
408  s->Printf("names = {");
409  for (size_t i = 0; i < num_names; i++) {
410  s->Printf("%s'%s'", (i == 0 ? "" : ", "),
411  m_lookups[i].GetName().GetCString());
412  }
413  s->Printf("}");
414  }
415  }
417  s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
418  }
419 }
420 
422 
423 lldb::BreakpointResolverSP
425  lldb::BreakpointResolverSP ret_sp(new BreakpointResolverName(*this));
426  ret_sp->SetBreakpoint(&breakpoint);
427  return ret_sp;
428 }
MatchType
An enum specifying the match style for breakpoint settings.
Definition: Breakpoint.h:85
Defines a list of symbol context objects.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
static lldb::LanguageType GetLanguageTypeFromString(const char *string)=delete
virtual uint32_t GetFilterRequiredItems()
This determines which items are REQUIRED for the filter to pass.
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
std::shared_ptr< Array > ArraySP
bool GetStartAddress(Address &addr)
Definition: Block.cpp:307
virtual bool CompUnitPasses(FileSpec &fileSpec)
Call this method with a FileSpec to see if file spec passes the filter as the name of a compilation u...
lldb::BreakpointLocationSP AddLocation(Address loc_addr, bool *new_location=NULL)
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:78
"lldb/Utility/RegularExpression.h" A C++ wrapper class for regex.
CompileUnit * comp_unit
The CompileUnit for a given query.
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:467
Address GetAddress() const
Definition: Symbol.h:72
const char * GetData() const
Definition: StreamString.h:43
bool GetItemAtIndexAsInteger(size_t idx, IntType &result) const
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr, bool containing) override
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:419
size_t GetFullNames(std::vector< ConstString > &names, bool append)
Symbol * symbol
The Symbol for a given query.
uint32_t GetSize() const
Get accessor for a symbol context list size.
lldb::BreakpointResolverSP CopyForBreakpoint(Breakpoint &breakpoint) override
Function * function
The Function for a given query.
std::shared_ptr< Dictionary > DictionarySP
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition: Function.cpp:476
lldb::SymbolType GetType() const
Definition: Symbol.h:136
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:317
virtual void AdjustBreakpointAddress(const Symbol &func, Address &addr) const
Adjust function breakpoint address, if needed.
Definition: Architecture.h:60
llvm::StringRef GetText() const
Access the regular expression text.
std::vector< Module::LookupInfo > m_lookups
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
bool IsValid() const
Test if valid.
LanguageType
Programming language type.
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:251
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
General Outline: The BreakpointResolver is a Searcher.
#define LIBLLDB_LOG_BREAKPOINTS
Definition: Logging.h:19
A class that encapsulates name lookup information.
Definition: Module.h:955
void SetNameTypeMask(lldb::FunctionNameType mask)
Definition: Module.h:975
bool Compile(llvm::StringRef string)
Compile a regular expression.
std::shared_ptr< Integer > IntegerSP
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:101
lldb::LanguageType GetLanguage() const
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language)
Definition: Language.cpp:285
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
void SetLookupName(ConstString name)
Definition: Module.h:971
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:343
A section + offset based address class.
Definition: Address.h:80
BreakpointResolverName(Breakpoint *bkpt, const char *name, lldb::FunctionNameType name_type_mask, lldb::LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset, bool skip_prologue)
bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result) const
std::shared_ptr< String > StringSP
void void Warning(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:163
const InlineFunctionInfo * GetInlinedFunctionInfo() const
Get const accessor for any inlined function information.
Definition: Block.h:296
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
Symbol * ResolveReExportedSymbol(Target &target) const
Definition: Symbol.cpp:466
uint64_t addr_t
Definition: lldb-types.h:83
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
A uniqued constant string class.
Definition: ConstString.h:38
Unknown or invalid language value.
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:247
lldb::ModuleSP module_sp
The Module for a given query.
Definition: SBAddress.h:15
"lldb/Breakpoint/BreakpointResolverName.h" This class sets breakpoints on a given function name...
std::shared_ptr< Object > ObjectSP
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
bool GetValueForKeyAsBoolean(llvm::StringRef key, bool &result) const
Architecture * GetArchitecturePlugin() const
Definition: Target.h:972
uint32_t GetPrologueByteSize()
Definition: Symbol.cpp:255
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:220
void SetErrorStringWithFormatv(const char *format, Args &&... args)
Definition: Status.h:185
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
const AddressRange & GetAddressRange()
Definition: Function.h:371
virtual bool AddressPasses(Address &addr)
Call this method with a Address to see if address passes the filter.
void AddNameLookup(ConstString name, lldb::FunctionNameType name_type_mask)
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
Block * block
The Block for a given query.
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:219
static BreakpointResolver * CreateFromStructuredData(Breakpoint *bkpt, const StructuredData::Dictionary &data_dict, Status &error)
static const char * GetKey(OptionNames enum_value)
StructuredData::ObjectSP SerializeToStructuredData() override
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
An error handling class.
Definition: Status.h:44
void Clear()
Clear the object&#39;s state.
Definition: Address.h:189
void SetName(ConstString name)
Definition: Module.h:967