LLDB  mainline
BreakpointResolverFileRegex.cpp
Go to the documentation of this file.
1 //===-- BreakpointResolverFileRegex.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 
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  const lldb::BreakpointSP &bkpt, RegularExpression regex,
24  const std::unordered_set<std::string> &func_names, bool exact_match)
25  : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver),
26  m_regex(std::move(regex)), m_exact_match(exact_match),
27  m_function_names(func_names) {}
28 
30  const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
31  Status &error) {
32  bool success;
33 
34  llvm::StringRef regex_string;
35  success = options_dict.GetValueForKeyAsString(
36  GetKey(OptionNames::RegexString), regex_string);
37  if (!success) {
38  error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
39  return nullptr;
40  }
41  RegularExpression regex(regex_string);
42 
43  bool exact_match;
44  success = options_dict.GetValueForKeyAsBoolean(
45  GetKey(OptionNames::ExactMatch), exact_match);
46  if (!success) {
47  error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
48  return nullptr;
49  }
50 
51  // The names array is optional:
52  std::unordered_set<std::string> names_set;
53  StructuredData::Array *names_array;
54  success = options_dict.GetValueForKeyAsArray(
55  GetKey(OptionNames::SymbolNameArray), names_array);
56  if (success && names_array) {
57  size_t num_names = names_array->GetSize();
58  for (size_t i = 0; i < num_names; i++) {
59  llvm::StringRef name;
60  success = names_array->GetItemAtIndexAsString(i, name);
61  if (!success) {
62  error.SetErrorStringWithFormat(
63  "BRFR::CFSD: Malformed element %zu in the names array.", i);
64  return nullptr;
65  }
66  names_set.insert(std::string(name));
67  }
68  }
69 
70  return new BreakpointResolverFileRegex(bkpt, std::move(regex), names_set,
71  exact_match);
72 }
73 
76  StructuredData::DictionarySP options_dict_sp(
78 
79  options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
80  m_regex.GetText());
81  options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
83  if (!m_function_names.empty()) {
84  StructuredData::ArraySP names_array_sp(new StructuredData::Array());
85  for (std::string name : m_function_names) {
87  names_array_sp->AddItem(item);
88  }
89  options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
90  }
91 
92  return WrapOptionsDict(options_dict_sp);
93 }
94 
96  SearchFilter &filter, SymbolContext &context, Address *addr) {
97 
98  if (!context.target_sp)
100 
101  CompileUnit *cu = context.comp_unit;
102  FileSpec cu_file_spec = cu->GetPrimaryFile();
103  std::vector<uint32_t> line_matches;
104  context.target_sp->GetSourceManager().FindLinesMatchingRegex(
105  cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
106 
107  uint32_t num_matches = line_matches.size();
108  for (uint32_t i = 0; i < num_matches; i++) {
109  SymbolContextList sc_list;
110  // TODO: Handle SourceLocationSpec column information
111  SourceLocationSpec location_spec(cu_file_spec, line_matches[i],
112  /*column=*/llvm::None,
113  /*search_inlines=*/false, m_exact_match);
114  cu->ResolveSymbolContext(location_spec, eSymbolContextEverything, sc_list);
115  // Find all the function names:
116  if (!m_function_names.empty()) {
117  std::vector<size_t> sc_to_remove;
118  for (size_t i = 0; i < sc_list.GetSize(); i++) {
119  SymbolContext sc_ctx;
120  sc_list.GetContextAtIndex(i, sc_ctx);
121  std::string name(
122  sc_ctx
123  .GetFunctionName(
124  Mangled::NamePreference::ePreferDemangledWithoutArguments)
125  .AsCString());
126  if (!m_function_names.count(name)) {
127  sc_to_remove.push_back(i);
128  }
129  }
130 
131  if (!sc_to_remove.empty()) {
132  std::vector<size_t>::reverse_iterator iter;
133  std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
134  for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
135  sc_list.RemoveContextAtIndex(*iter);
136  }
137  }
138  }
139 
140  const bool skip_prologue = true;
141 
142  BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
143  m_regex.GetText());
144  }
145 
147 }
148 
151 }
152 
154  s->Printf("source regex = \"%s\", exact_match = %d",
155  m_regex.GetText().str().c_str(), m_exact_match);
156 }
157 
159 
160 lldb::BreakpointResolverSP
162  lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
163  breakpoint, m_regex, m_function_names, m_exact_match));
164  return ret_sp;
165 }
166 
167 void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
168  m_function_names.insert(func_name);
169 }
lldb::eSearchDepthCompUnit
@ eSearchDepthCompUnit
Definition: lldb-enumerations.h:287
lldb_private::SymbolContext::comp_unit
CompileUnit * comp_unit
The CompileUnit for a given query.
Definition: SymbolContext.h:319
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
lldb_private::RegularExpression
Definition: RegularExpression.h:18
lldb::SearchDepth
SearchDepth
Definition: lldb-enumerations.h:283
lldb_private::BreakpointResolverFileRegex::CopyForBreakpoint
lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override
Definition: BreakpointResolverFileRegex.cpp:161
CompileUnit.h
lldb_private::StructuredData::Array
Definition: StructuredData.h:165
lldb_private::SourceLocationSpec
Definition: SourceLocationSpec.h:27
lldb_private::SymbolContext::target_sp
lldb::TargetSP target_sp
The Target for a given query.
Definition: SymbolContext.h:317
BreakpointResolverFileRegex.h
lldb_private::BreakpointResolverFileRegex::GetDepth
lldb::SearchDepth GetDepth() override
Definition: BreakpointResolverFileRegex.cpp:149
BreakpointLocation.h
SourceManager.h
lldb_private::SymbolContextList
Definition: SymbolContext.h:378
lldb_private::SymbolContextList::GetContextAtIndex
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
Definition: SymbolContext.cpp:1267
lldb_private::Stream
Definition: Stream.h:28
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::CompileUnit
Definition: CompileUnit.h:38
lldb_private::BreakpointResolverFileRegex::GetDescription
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
Definition: BreakpointResolverFileRegex.cpp:153
Target.h
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::BreakpointResolverFileRegex::SerializeToStructuredData
StructuredData::ObjectSP SerializeToStructuredData() override
Definition: BreakpointResolverFileRegex.cpp:75
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::BreakpointResolver::OptionNames::LineNumber
@ LineNumber
lldb_private::StructuredData::StringSP
std::shared_ptr< String > StringSP
Definition: StructuredData.h:66
lldb_private::StructuredData::String
Definition: StructuredData.h:336
lldb_private::SearchFilter
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
Log.h
lldb_private::BreakpointResolverFileRegex::Dump
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
Definition: BreakpointResolverFileRegex.cpp:158
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
StreamString.h
lldb_private::BreakpointResolverFileRegex::m_exact_match
bool m_exact_match
Definition: BreakpointResolverFileRegex.h:66
lldb_private::SymbolContextList::GetSize
uint32_t GetSize() const
Get accessor for a symbol context list size.
Definition: SymbolContext.cpp:1283
lldb_private::Searcher::CallbackReturn
CallbackReturn
Definition: SearchFilter.h:44
lldb_private::BreakpointResolver::OptionNames::RegexString
@ RegexString
lldb_private::StructuredData::DictionarySP
std::shared_ptr< Dictionary > DictionarySP
Definition: StructuredData.h:67
lldb_private::CompileUnit::GetPrimaryFile
const FileSpec & GetPrimaryFile() const
Return the primary source file associated with this compile unit.
Definition: CompileUnit.h:227
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
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::Status
Definition: Status.h:44
lldb_private::BreakpointResolverFileRegex::CreateFromStructuredData
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, Status &error)
Definition: BreakpointResolverFileRegex.cpp:29
uint32_t
lldb_private::BreakpointResolverFileRegex::m_function_names
std::unordered_set< std::string > m_function_names
Definition: BreakpointResolverFileRegex.h:68
lldb_private::Address
Definition: Address.h:59
lldb_private::BreakpointResolverFileRegex::SearchCallback
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
Definition: BreakpointResolverFileRegex.cpp:95
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::BreakpointResolver::OptionNames::SymbolNameArray
@ SymbolNameArray
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::CompileUnit::ResolveSymbolContext
void ResolveSymbolContext(const SourceLocationSpec &src_location_spec, lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list)
Resolve symbol contexts by file and line.
Definition: CompileUnit.cpp:238
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::BreakpointResolverFileRegex::AddFunctionName
void AddFunctionName(const char *func_name)
Definition: BreakpointResolverFileRegex.cpp:167
lldb_private::StructuredData::Array::GetItemAtIndexAsString
bool GetItemAtIndexAsString(size_t idx, llvm::StringRef &result) const
Definition: StructuredData.h:216
lldb_private::StructuredData::ArraySP
std::shared_ptr< Array > ArraySP
Definition: StructuredData.h:62
lldb_private::StructuredData::Dictionary::GetValueForKeyAsString
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
Definition: StructuredData.h:423
lldb_private::StructuredData::Dictionary::GetValueForKeyAsArray
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
Definition: StructuredData.h:478
lldb_private::BreakpointResolverFileRegex::m_regex
RegularExpression m_regex
Definition: BreakpointResolverFileRegex.h:65
lldb_private::StructuredData::Array::GetSize
size_t GetSize() const
Definition: StructuredData.h:180
lldb_private::RegularExpression::GetText
llvm::StringRef GetText() const
Access the regular expression text.
Definition: RegularExpression.cpp:33
lldb_private::BreakpointResolverFileRegex::BreakpointResolverFileRegex
BreakpointResolverFileRegex(const lldb::BreakpointSP &bkpt, RegularExpression regex, const std::unordered_set< std::string > &func_name_set, bool exact_match)
Definition: BreakpointResolverFileRegex.cpp:22
lldb_private::BreakpointResolver::OptionNames::ExactMatch
@ ExactMatch
lldb_private::StructuredData::Dictionary::GetValueForKeyAsBoolean
bool GetValueForKeyAsBoolean(llvm::StringRef key, bool &result) const
Definition: StructuredData.h:390
lldb
Definition: SBAddress.h:15
lldb_private::SymbolContextList::RemoveContextAtIndex
bool RemoveContextAtIndex(size_t idx)
Definition: SymbolContext.cpp:1275
lldb_private::Searcher::eCallbackReturnContinue
@ eCallbackReturnContinue
Definition: SearchFilter.h:46