1 //===-- BreakpointResolverFileRegex.cpp -------------------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
11 #include "lldb/Breakpoint/BreakpointLocation.h"
12 #include "lldb/Core/SourceManager.h"
13 #include "lldb/Symbol/CompileUnit.h"
14 #include "lldb/Target/Target.h"
15 #include "lldb/Utility/Log.h"
16 #include "lldb/Utility/StreamString.h"
19 using namespace lldb_private;
21 // BreakpointResolverFileRegex:
22 BreakpointResolverFileRegex::BreakpointResolverFileRegex(
23 Breakpoint *bkpt, RegularExpression ®ex,
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) {
29 BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
31 BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
32 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
36 llvm::StringRef regex_string;
37 success = options_dict.GetValueForKeyAsString(
38 GetKey(OptionNames::RegexString), regex_string);
40 error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
43 RegularExpression regex(regex_string);
46 success = options_dict.GetValueForKeyAsBoolean(
47 GetKey(OptionNames::ExactMatch), exact_match);
49 error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
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++) {
62 success = names_array->GetItemAtIndexAsString(i, name);
64 error.SetErrorStringWithFormat(
65 "BRFR::CFSD: Malformed element %zu in the names array.", i);
68 names_set.insert(name);
72 return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match);
75 StructuredData::ObjectSP
76 BreakpointResolverFileRegex::SerializeToStructuredData() {
77 StructuredData::DictionarySP options_dict_sp(
78 new StructuredData::Dictionary());
80 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
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) {
87 StructuredData::StringSP item(new StructuredData::String(name));
88 names_array_sp->AddItem(item);
90 options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
93 return WrapOptionsDict(options_dict_sp);
96 Searcher::CallbackReturn
97 BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
98 SymbolContext &context,
99 Address *addr, bool containing) {
101 assert(m_breakpoint != nullptr);
102 if (!context.target_sp)
103 return eCallbackReturnContinue;
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);
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;
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);
127 Mangled::NamePreference::ePreferDemangledWithoutArguments)
129 if (!m_function_names.count(name)) {
130 sc_to_remove.push_back(i);
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);
143 const bool skip_prologue = true;
145 BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
148 assert(m_breakpoint != nullptr);
150 return Searcher::eCallbackReturnContinue;
153 lldb::SearchDepth BreakpointResolverFileRegex::GetDepth() {
154 return lldb::eSearchDepthCompUnit;
157 void BreakpointResolverFileRegex::GetDescription(Stream *s) {
158 s->Printf("source regex = \"%s\", exact_match = %d",
159 m_regex.GetText().str().c_str(), m_exact_match);
162 void BreakpointResolverFileRegex::Dump(Stream *s) const {}
164 lldb::BreakpointResolverSP
165 BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) {
166 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
167 &breakpoint, m_regex, m_function_names, m_exact_match));
171 void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
172 m_function_names.insert(func_name);