LLDB  mainline
BreakpointResolverFileRegex.cpp
Go to the documentation of this file.
1 //===-- BreakpointResolverFileRegex.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 
14 #include "lldb/Target/Target.h"
15 #include "lldb/Utility/Log.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 // BreakpointResolverFileRegex:
22 BreakpointResolverFileRegex::BreakpointResolverFileRegex(
23  Breakpoint *bkpt, RegularExpression &regex,
24  const std::unordered_set<std::string> &func_names, bool exact_match)
25  : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver),
26  m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) {
27 }
28 
30 
32  Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
33  Status &error) {
34  bool success;
35 
36  llvm::StringRef regex_string;
37  success = options_dict.GetValueForKeyAsString(
38  GetKey(OptionNames::RegexString), regex_string);
39  if (!success) {
40  error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
41  return nullptr;
42  }
43  RegularExpression regex(regex_string);
44 
45  bool exact_match;
46  success = options_dict.GetValueForKeyAsBoolean(
47  GetKey(OptionNames::ExactMatch), exact_match);
48  if (!success) {
49  error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
50  return nullptr;
51  }
52 
53  // The names array is optional:
54  std::unordered_set<std::string> names_set;
55  StructuredData::Array *names_array;
56  success = options_dict.GetValueForKeyAsArray(
57  GetKey(OptionNames::SymbolNameArray), names_array);
58  if (success && names_array) {
59  size_t num_names = names_array->GetSize();
60  for (size_t i = 0; i < num_names; i++) {
61  llvm::StringRef name;
62  success = names_array->GetItemAtIndexAsString(i, name);
63  if (!success) {
65  "BRFR::CFSD: Malformed element %zu in the names array.", i);
66  return nullptr;
67  }
68  names_set.insert(name);
69  }
70  }
71 
72  return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match);
73 }
74 
77  StructuredData::DictionarySP options_dict_sp(
79 
80  options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
81  m_regex.GetText());
82  options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
84  if (!m_function_names.empty()) {
85  StructuredData::ArraySP names_array_sp(new StructuredData::Array());
86  for (std::string name : m_function_names) {
88  names_array_sp->AddItem(item);
89  }
90  options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
91  }
92 
93  return WrapOptionsDict(options_dict_sp);
94 }
95 
98  SymbolContext &context,
99  Address *addr, bool containing) {
100 
101  assert(m_breakpoint != NULL);
102  if (!context.target_sp)
104 
105  CompileUnit *cu = context.comp_unit;
106  FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu));
107  std::vector<uint32_t> line_matches;
108  context.target_sp->GetSourceManager().FindLinesMatchingRegex(
109  cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
110 
111  uint32_t num_matches = line_matches.size();
112  for (uint32_t i = 0; i < num_matches; i++) {
113  SymbolContextList sc_list;
114  const bool search_inlines = false;
115 
116  cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines,
117  m_exact_match, eSymbolContextEverything, sc_list);
118  // Find all the function names:
119  if (!m_function_names.empty()) {
120  std::vector<size_t> sc_to_remove;
121  for (size_t i = 0; i < sc_list.GetSize(); i++) {
122  SymbolContext sc_ctx;
123  sc_list.GetContextAtIndex(i, sc_ctx);
124  std::string name(
125  sc_ctx
126  .GetFunctionName(
127  Mangled::NamePreference::ePreferDemangledWithoutArguments)
128  .AsCString());
129  if (!m_function_names.count(name)) {
130  sc_to_remove.push_back(i);
131  }
132  }
133 
134  if (!sc_to_remove.empty()) {
135  std::vector<size_t>::reverse_iterator iter;
136  std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
137  for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
138  sc_list.RemoveContextAtIndex(*iter);
139  }
140  }
141  }
142 
143  const bool skip_prologue = true;
144 
145  BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
146  m_regex.GetText());
147  }
148  assert(m_breakpoint != NULL);
149 
151 }
152 
155 }
156 
158  s->Printf("source regex = \"%s\", exact_match = %d",
159  m_regex.GetText().str().c_str(), m_exact_match);
160 }
161 
163 
164 lldb::BreakpointResolverSP
166  lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
167  &breakpoint, m_regex, m_function_names, m_exact_match));
168  return ret_sp;
169 }
170 
171 void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
172  m_function_names.insert(func_name);
173 }
lldb::TargetSP target_sp
The Target for a given query.
Defines a list of symbol context objects.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
static BreakpointResolver * CreateFromStructuredData(Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, Status &error)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
std::shared_ptr< Array > ArraySP
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr, bool containing) override
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
A file utility class.
Definition: FileSpec.h:55
"lldb/Utility/RegularExpression.h" A C++ wrapper class for regex.
CompileUnit * comp_unit
The CompileUnit for a given query.
std::unordered_set< std::string > m_function_names
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
lldb::BreakpointResolverSP CopyForBreakpoint(Breakpoint &breakpoint) override
A class that describes a compilation unit.
Definition: CompileUnit.h:35
uint32_t GetSize() const
Get accessor for a symbol context list size.
std::shared_ptr< Dictionary > DictionarySP
#define UINT32_MAX
Definition: lldb-defines.h:31
llvm::StringRef GetText() const
Access the regular expression text.
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
void SetSCMatchesByLine(SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue, llvm::StringRef log_ident, uint32_t line=0, uint32_t column=0)
Takes a symbol context list of matches which supposedly represent the same file and line number in a ...
General Outline: The BreakpointResolver is a Searcher.
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:101
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
StructuredData::ObjectSP SerializeToStructuredData() override
A section + offset based address class.
Definition: Address.h:80
bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result) const
std::shared_ptr< String > StringSP
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
BreakpointResolverFileRegex(Breakpoint *bkpt, RegularExpression &regex, const std::unordered_set< std::string > &func_name_set, bool exact_match)
Definition: SBAddress.h:15
std::shared_ptr< Object > ObjectSP
uint32_t ResolveSymbolContext(const FileSpec &file_spec, uint32_t line, bool check_inlines, bool exact, lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list)
Resolve symbol contexts by file and line.
bool GetValueForKeyAsBoolean(llvm::StringRef key, bool &result) const
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
static const char * GetKey(OptionNames enum_value)
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
An error handling class.
Definition: Status.h:44