LLDB  mainline
BreakpointResolverName.cpp
Go to the documentation of this file.
1 //===-- BreakpointResolverName.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 
12 #include "lldb/Core/Architecture.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/Block.h"
15 #include "lldb/Symbol/Function.h"
16 #include "lldb/Symbol/Symbol.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Target/Language.h"
20 #include "lldb/Utility/Log.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt,
27  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  m_regex = RegularExpression(llvm::StringRef::withNullAsEmpty(name_cstr));
35  if (!m_regex.IsValid()) {
37 
38  if (log)
39  log->Warning("function name regexp: \"%s\" did not compile.",
40  name_cstr);
41  }
42  } else {
43  AddNameLookup(ConstString(name_cstr), name_type_mask);
44  }
45 }
46 
48  const BreakpointSP &bkpt, const char *names[], size_t num_names,
49  FunctionNameType name_type_mask, LanguageType language, lldb::addr_t offset,
50  bool skip_prologue)
52  m_match_type(Breakpoint::Exact), m_language(language),
53  m_skip_prologue(skip_prologue) {
54  for (size_t i = 0; i < num_names; i++) {
55  AddNameLookup(ConstString(names[i]), name_type_mask);
56  }
57 }
58 
59 BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt,
60  std::vector<std::string> names,
61  FunctionNameType name_type_mask,
62  LanguageType language,
63  lldb::addr_t offset,
64  bool skip_prologue)
66  m_match_type(Breakpoint::Exact), m_language(language),
67  m_skip_prologue(skip_prologue) {
68  for (const std::string &name : names) {
69  AddNameLookup(ConstString(name.c_str(), name.size()), name_type_mask);
70  }
71 }
72 
73 BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt,
74  RegularExpression func_regex,
75  lldb::LanguageType language,
76  lldb::addr_t offset,
77  bool skip_prologue)
79  m_class_name(nullptr), m_regex(std::move(func_regex)),
80  m_match_type(Breakpoint::Regexp), m_language(language),
81  m_skip_prologue(skip_prologue) {}
82 
84  const BreakpointResolverName &rhs)
86  rhs.GetOffset()),
87  m_lookups(rhs.m_lookups), m_class_name(rhs.m_class_name),
88  m_regex(rhs.m_regex), m_match_type(rhs.m_match_type),
89  m_language(rhs.m_language), m_skip_prologue(rhs.m_skip_prologue) {}
90 
92  const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
93  Status &error) {
95  llvm::StringRef language_name;
96  bool success = options_dict.GetValueForKeyAsString(
97  GetKey(OptionNames::LanguageName), language_name);
98  if (success) {
99  language = Language::GetLanguageTypeFromString(language_name);
100  if (language == eLanguageTypeUnknown) {
101  error.SetErrorStringWithFormatv("BRN::CFSD: Unknown language: {0}.",
102  language_name);
103  return nullptr;
104  }
105  }
106 
107  lldb::addr_t offset = 0;
108  success =
109  options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Offset), offset);
110  if (!success) {
111  error.SetErrorStringWithFormat("BRN::CFSD: Missing offset entry.");
112  return nullptr;
113  }
114 
115  bool skip_prologue;
116  success = options_dict.GetValueForKeyAsBoolean(
117  GetKey(OptionNames::SkipPrologue), skip_prologue);
118  if (!success) {
119  error.SetErrorStringWithFormat("BRN::CFSD: Missing Skip prologue entry.");
120  return nullptr;
121  }
122 
123  llvm::StringRef regex_text;
124  success = options_dict.GetValueForKeyAsString(
125  GetKey(OptionNames::RegexString), regex_text);
126  if (success) {
127  return new BreakpointResolverName(bkpt, RegularExpression(regex_text),
128  language, offset, skip_prologue);
129  } else {
130  StructuredData::Array *names_array;
131  success = options_dict.GetValueForKeyAsArray(
132  GetKey(OptionNames::SymbolNameArray), names_array);
133  if (!success) {
134  error.SetErrorStringWithFormat("BRN::CFSD: Missing symbol names entry.");
135  return nullptr;
136  }
137  StructuredData::Array *names_mask_array;
138  success = options_dict.GetValueForKeyAsArray(
139  GetKey(OptionNames::NameMaskArray), names_mask_array);
140  if (!success) {
142  "BRN::CFSD: Missing symbol names mask entry.");
143  return nullptr;
144  }
145 
146  size_t num_elem = names_array->GetSize();
147  if (num_elem != names_mask_array->GetSize()) {
148  error.SetErrorString(
149  "BRN::CFSD: names and names mask arrays have different sizes.");
150  return nullptr;
151  }
152 
153  if (num_elem == 0) {
154  error.SetErrorString(
155  "BRN::CFSD: no name entry in a breakpoint by name breakpoint.");
156  return nullptr;
157  }
158  std::vector<std::string> names;
159  std::vector<FunctionNameType> name_masks;
160  for (size_t i = 0; i < num_elem; i++) {
161  llvm::StringRef name;
162 
163  success = names_array->GetItemAtIndexAsString(i, name);
164  if (!success) {
165  error.SetErrorString("BRN::CFSD: name entry is not a string.");
166  return nullptr;
167  }
168  std::underlying_type<FunctionNameType>::type fnt;
169  success = names_mask_array->GetItemAtIndexAsInteger(i, fnt);
170  if (!success) {
171  error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
172  return nullptr;
173  }
174  names.push_back(std::string(name));
175  name_masks.push_back(static_cast<FunctionNameType>(fnt));
176  }
177 
179  bkpt, names[0].c_str(), name_masks[0], language,
180  Breakpoint::MatchType::Exact, offset, skip_prologue);
181  for (size_t i = 1; i < num_elem; i++) {
182  resolver->AddNameLookup(ConstString(names[i]), name_masks[i]);
183  }
184  return resolver;
185  }
186 }
187 
189  StructuredData::DictionarySP options_dict_sp(
191 
192  if (m_regex.IsValid()) {
193  options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
194  m_regex.GetText());
195  } else {
197  StructuredData::ArraySP name_masks_sp(new StructuredData::Array());
198  for (auto lookup : m_lookups) {
199  names_sp->AddItem(StructuredData::StringSP(
200  new StructuredData::String(lookup.GetName().GetStringRef())));
201  name_masks_sp->AddItem(StructuredData::IntegerSP(
202  new StructuredData::Integer(lookup.GetNameTypeMask())));
203  }
204  options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp);
205  options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp);
206  }
208  options_dict_sp->AddStringItem(
211  options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
213 
214  return WrapOptionsDict(options_dict_sp);
215 }
216 
218  FunctionNameType name_type_mask) {
219 
220  Module::LookupInfo lookup(name, name_type_mask, m_language);
221  m_lookups.emplace_back(lookup);
222 
223  auto add_variant_funcs = [&](Language *lang) {
224  for (ConstString variant_name : lang->GetMethodNameVariants(name)) {
225  Module::LookupInfo variant_lookup(name, name_type_mask,
226  lang->GetLanguageType());
227  variant_lookup.SetLookupName(variant_name);
228  m_lookups.emplace_back(variant_lookup);
229  }
230  return true;
231  };
232 
234  add_variant_funcs(lang);
235  } else {
236  // Most likely m_language is eLanguageTypeUnknown. We check each language for
237  // possible variants or more qualified names and create lookups for those as
238  // well.
239  Language::ForEach(add_variant_funcs);
240  }
241 }
242 
243 // FIXME: Right now we look at the module level, and call the module's
244 // "FindFunctions".
245 // Greg says he will add function tables, maybe at the CompileUnit level to
246 // accelerate function lookup. At that point, we should switch the depth to
247 // CompileUnit, and look in these tables.
248 
251  SymbolContext &context, Address *addr) {
253 
254  if (m_class_name) {
255  if (log)
256  log->Warning("Class/method function specification not supported yet.\n");
258  }
259 
260  SymbolContextList func_list;
261  bool filter_by_cu =
262  (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
263  bool filter_by_language = (m_language != eLanguageTypeUnknown);
264  const bool include_symbols = !filter_by_cu;
265  const bool include_inlines = true;
266 
267  switch (m_match_type) {
268  case Breakpoint::Exact:
269  if (context.module_sp) {
270  for (const auto &lookup : m_lookups) {
271  const size_t start_func_idx = func_list.GetSize();
272  context.module_sp->FindFunctions(
273  lookup.GetLookupName(), CompilerDeclContext(),
274  lookup.GetNameTypeMask(), include_symbols, include_inlines,
275  func_list);
276 
277  const size_t end_func_idx = func_list.GetSize();
278 
279  if (start_func_idx < end_func_idx)
280  lookup.Prune(func_list, start_func_idx);
281  }
282  }
283  break;
284  case Breakpoint::Regexp:
285  if (context.module_sp) {
286  context.module_sp->FindFunctions(
287  m_regex,
288  !filter_by_cu, // include symbols only if we aren't filtering by CU
289  include_inlines, func_list);
290  }
291  break;
292  case Breakpoint::Glob:
293  if (log)
294  log->Warning("glob is not supported yet.");
295  break;
296  }
297 
298  // If the filter specifies a Compilation Unit, remove the ones that don't
299  // pass at this point.
300  if (filter_by_cu || filter_by_language) {
301  uint32_t num_functions = func_list.GetSize();
302 
303  for (size_t idx = 0; idx < num_functions; idx++) {
304  bool remove_it = false;
305  SymbolContext sc;
306  func_list.GetContextAtIndex(idx, sc);
307  if (filter_by_cu) {
308  if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
309  remove_it = true;
310  }
311 
312  if (filter_by_language) {
313  LanguageType sym_language = sc.GetLanguage();
314  if ((Language::GetPrimaryLanguage(sym_language) !=
316  (sym_language != eLanguageTypeUnknown)) {
317  remove_it = true;
318  }
319  }
320 
321  if (remove_it) {
322  func_list.RemoveContextAtIndex(idx);
323  num_functions--;
324  idx--;
325  }
326  }
327  }
328 
329  BreakpointSP breakpoint_sp = GetBreakpoint();
330  Breakpoint &breakpoint = *breakpoint_sp;
331  Address break_addr;
332 
333  // Remove any duplicates between the function list and the symbol list
334  SymbolContext sc;
335  if (!func_list.GetSize())
337 
338  for (uint32_t i = 0; i < func_list.GetSize(); i++) {
339  if (!func_list.GetContextAtIndex(i, sc))
340  continue;
341 
342  bool is_reexported = false;
343 
344  if (sc.block && sc.block->GetInlinedFunctionInfo()) {
345  if (!sc.block->GetStartAddress(break_addr))
346  break_addr.Clear();
347  } else if (sc.function) {
348  break_addr = sc.function->GetAddressRange().GetBaseAddress();
349  if (m_skip_prologue && break_addr.IsValid()) {
350  const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
351  if (prologue_byte_size)
352  break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
353  }
354  } else if (sc.symbol) {
355  if (sc.symbol->GetType() == eSymbolTypeReExported) {
356  const Symbol *actual_symbol =
357  sc.symbol->ResolveReExportedSymbol(breakpoint.GetTarget());
358  if (actual_symbol) {
359  is_reexported = true;
360  break_addr = actual_symbol->GetAddress();
361  }
362  } else {
363  break_addr = sc.symbol->GetAddress();
364  }
365 
366  if (m_skip_prologue && break_addr.IsValid()) {
367  const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
368  if (prologue_byte_size)
369  break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
370  else {
371  const Architecture *arch =
372  breakpoint.GetTarget().GetArchitecturePlugin();
373  if (arch)
374  arch->AdjustBreakpointAddress(*sc.symbol, break_addr);
375  }
376  }
377  }
378 
379  if (!break_addr.IsValid())
380  continue;
381 
382  if (!filter.AddressPasses(break_addr))
383  continue;
384 
385  bool new_location;
386  BreakpointLocationSP bp_loc_sp(AddLocation(break_addr, &new_location));
387  bp_loc_sp->SetIsReExported(is_reexported);
388  if (bp_loc_sp && new_location && !breakpoint.IsInternal()) {
389  if (log) {
390  StreamString s;
391  bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
392  LLDB_LOGF(log, "Added location: %s\n", s.GetData());
393  }
394  }
395  }
396 
398 }
399 
402 }
403 
406  s->Printf("regex = '%s'", m_regex.GetText().str().c_str());
407  else {
408  size_t num_names = m_lookups.size();
409  if (num_names == 1)
410  s->Printf("name = '%s'", m_lookups[0].GetName().GetCString());
411  else {
412  s->Printf("names = {");
413  for (size_t i = 0; i < num_names; i++) {
414  s->Printf("%s'%s'", (i == 0 ? "" : ", "),
415  m_lookups[i].GetName().GetCString());
416  }
417  s->Printf("}");
418  }
419  }
421  s->Printf(", language = %s", Language::GetNameForLanguageType(m_language));
422  }
423 }
424 
426 
427 lldb::BreakpointResolverSP
428 BreakpointResolverName::CopyForBreakpoint(BreakpointSP &breakpoint) {
429  lldb::BreakpointResolverSP ret_sp(new BreakpointResolverName(*this));
430  ret_sp->SetBreakpoint(breakpoint);
431  return ret_sp;
432 }
MatchType
An enum specifying the match style for breakpoint settings.
Definition: Breakpoint.h:85
Defines a list of symbol context objects.
A class that represents a running process on the host machine.
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
Represents a generic declaration context in a program.
std::shared_ptr< Array > ArraySP
bool GetStartAddress(Address &addr)
Definition: Block.cpp:306
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...
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
BreakpointResolverName(const lldb::BreakpointSP &bkpt, const char *name, lldb::FunctionNameType name_type_mask, lldb::LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset, bool skip_prologue)
static void ForEach(std::function< bool(Language *)> callback)
Definition: Language.cpp:100
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
CompileUnit * comp_unit
The CompileUnit for a given query.
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:452
lldb::BreakpointLocationSP AddLocation(Address loc_addr, bool *new_location=nullptr)
Address GetAddress() const
Definition: Symbol.h:73
const char * GetData() const
Definition: StreamString.h:43
bool GetItemAtIndexAsInteger(size_t idx, IntType &result) const
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:425
Symbol * symbol
The Symbol for a given query.
uint32_t GetSize() const
Get accessor for a symbol context list size.
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:528
lldb::SymbolType GetType() const
Definition: Symbol.h:137
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:306
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 this object contains a valid regular expression.
LanguageType
Programming language type.
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:263
static llvm::raw_ostream & error(Stream &strm)
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
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:890
std::shared_ptr< Integer > IntegerSP
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
lldb::LanguageType GetLanguage() const
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:242
static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language)
Definition: Language.cpp:290
lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
void SetLookupName(ConstString name)
Definition: Module.h:906
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:332
A section + offset based address class.
Definition: Address.h:59
bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result) const
std::shared_ptr< String > StringSP
void void void void Warning(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:171
const InlineFunctionInfo * GetInlinedFunctionInfo() const
Get const accessor for any inlined function information.
Definition: Block.h:276
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
#define LLDB_LOGF(log,...)
Definition: Log.h:249
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
Symbol * ResolveReExportedSymbol(Target &target) const
Definition: Symbol.cpp:471
uint64_t addr_t
Definition: lldb-types.h:83
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &data_dict, Status &error)
static llvm::StringRef GetName(XcodeSDK::Type type)
Definition: XcodeSDK.cpp:21
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
A uniqued constant string class.
Definition: ConstString.h:40
Unknown or invalid language value.
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:973
lldb::BreakpointSP GetBreakpoint() const
This gets the breakpoint for this resolver.
uint32_t GetPrologueByteSize()
Definition: Symbol.cpp:260
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:210
void SetErrorStringWithFormatv(const char *format, Args &&... args)
Definition: Status.h:173
static Language * FindPlugin(lldb::LanguageType language)
Definition: Language.cpp:53
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:256
const AddressRange & GetAddressRange()
Definition: Function.h:457
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.
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:206
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:178