1 //===-- BreakpointResolverFileRegex.cpp -------------------------*- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
11
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 #include "lldb/Core/SourceManager.h"
14 #include "lldb/Symbol/CompileUnit.h"
15 #include "lldb/Target/Target.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/StreamString.h"
18
19 using namespace lldb;
20 using namespace lldb_private;
21
22 //----------------------------------------------------------------------
23 // BreakpointResolverFileRegex:
24 //----------------------------------------------------------------------
BreakpointResolverFileRegex(Breakpoint * bkpt,RegularExpression & regex,const std::unordered_set<std::string> & func_names,bool exact_match)25 BreakpointResolverFileRegex::BreakpointResolverFileRegex(
26 Breakpoint *bkpt, RegularExpression ®ex,
27 const std::unordered_set<std::string> &func_names, bool exact_match)
28 : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver),
29 m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) {
30 }
31
~BreakpointResolverFileRegex()32 BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
33
CreateFromStructuredData(Breakpoint * bkpt,const StructuredData::Dictionary & options_dict,Status & error)34 BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
35 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
36 Status &error) {
37 bool success;
38
39 llvm::StringRef regex_string;
40 success = options_dict.GetValueForKeyAsString(
41 GetKey(OptionNames::RegexString), regex_string);
42 if (!success) {
43 error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
44 return nullptr;
45 }
46 RegularExpression regex(regex_string);
47
48 bool exact_match;
49 success = options_dict.GetValueForKeyAsBoolean(
50 GetKey(OptionNames::ExactMatch), exact_match);
51 if (!success) {
52 error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
53 return nullptr;
54 }
55
56 // The names array is optional:
57 std::unordered_set<std::string> names_set;
58 StructuredData::Array *names_array;
59 success = options_dict.GetValueForKeyAsArray(
60 GetKey(OptionNames::SymbolNameArray), names_array);
61 if (success && names_array) {
62 size_t num_names = names_array->GetSize();
63 for (size_t i = 0; i < num_names; i++) {
64 llvm::StringRef name;
65 success = names_array->GetItemAtIndexAsString(i, name);
66 if (!success) {
67 error.SetErrorStringWithFormat(
68 "BRFR::CFSD: Malformed element %zu in the names array.", i);
69 return nullptr;
70 }
71 names_set.insert(name);
72 }
73 }
74
75 return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match);
76 }
77
78 StructuredData::ObjectSP
SerializeToStructuredData()79 BreakpointResolverFileRegex::SerializeToStructuredData() {
80 StructuredData::DictionarySP options_dict_sp(
81 new StructuredData::Dictionary());
82
83 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
84 m_regex.GetText());
85 options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
86 m_exact_match);
87 if (!m_function_names.empty()) {
88 StructuredData::ArraySP names_array_sp(new StructuredData::Array());
89 for (std::string name : m_function_names) {
90 StructuredData::StringSP item(new StructuredData::String(name));
91 names_array_sp->AddItem(item);
92 }
93 options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
94 }
95
96 return WrapOptionsDict(options_dict_sp);
97 }
98
99 Searcher::CallbackReturn
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr,bool containing)100 BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
101 SymbolContext &context,
102 Address *addr, bool containing) {
103
104 assert(m_breakpoint != NULL);
105 if (!context.target_sp)
106 return eCallbackReturnContinue;
107
108 CompileUnit *cu = context.comp_unit;
109 FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu));
110 std::vector<uint32_t> line_matches;
111 context.target_sp->GetSourceManager().FindLinesMatchingRegex(
112 cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
113
114 uint32_t num_matches = line_matches.size();
115 for (uint32_t i = 0; i < num_matches; i++) {
116 SymbolContextList sc_list;
117 const bool search_inlines = false;
118
119 cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines,
120 m_exact_match, eSymbolContextEverything, sc_list);
121 // Find all the function names:
122 if (!m_function_names.empty()) {
123 std::vector<size_t> sc_to_remove;
124 for (size_t i = 0; i < sc_list.GetSize(); i++) {
125 SymbolContext sc_ctx;
126 sc_list.GetContextAtIndex(i, sc_ctx);
127 std::string name(
128 sc_ctx
129 .GetFunctionName(
130 Mangled::NamePreference::ePreferDemangledWithoutArguments)
131 .AsCString());
132 if (!m_function_names.count(name)) {
133 sc_to_remove.push_back(i);
134 }
135 }
136
137 if (!sc_to_remove.empty()) {
138 std::vector<size_t>::reverse_iterator iter;
139 std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
140 for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
141 sc_list.RemoveContextAtIndex(*iter);
142 }
143 }
144 }
145
146 const bool skip_prologue = true;
147
148 BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
149 m_regex.GetText());
150 }
151 assert(m_breakpoint != NULL);
152
153 return Searcher::eCallbackReturnContinue;
154 }
155
GetDepth()156 lldb::SearchDepth BreakpointResolverFileRegex::GetDepth() {
157 return lldb::eSearchDepthCompUnit;
158 }
159
GetDescription(Stream * s)160 void BreakpointResolverFileRegex::GetDescription(Stream *s) {
161 s->Printf("source regex = \"%s\", exact_match = %d",
162 m_regex.GetText().str().c_str(), m_exact_match);
163 }
164
Dump(Stream * s) const165 void BreakpointResolverFileRegex::Dump(Stream *s) const {}
166
167 lldb::BreakpointResolverSP
CopyForBreakpoint(Breakpoint & breakpoint)168 BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) {
169 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
170 &breakpoint, m_regex, m_function_names, m_exact_match));
171 return ret_sp;
172 }
173
AddFunctionName(const char * func_name)174 void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
175 m_function_names.insert(func_name);
176 }
177