1 //===-- BreakpointResolverFileRegex.cpp -------------------------*- C++-*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
14 // Other libraries and framework includes
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Core/SourceManager.h"
18 #include "lldb/Symbol/CompileUnit.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/StreamString.h"
24 using namespace lldb_private;
26 //----------------------------------------------------------------------
27 // BreakpointResolverFileRegex:
28 //----------------------------------------------------------------------
29 BreakpointResolverFileRegex::BreakpointResolverFileRegex(
30 Breakpoint *bkpt, RegularExpression ®ex,
31 const std::unordered_set<std::string> &func_names, bool exact_match)
32 : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver),
33 m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) {
36 BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
38 BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
39 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
43 llvm::StringRef regex_string;
44 success = options_dict.GetValueForKeyAsString(
45 GetKey(OptionNames::RegexString), regex_string);
47 error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
50 RegularExpression regex(regex_string);
53 success = options_dict.GetValueForKeyAsBoolean(
54 GetKey(OptionNames::ExactMatch), exact_match);
56 error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
60 // The names array is optional:
61 std::unordered_set<std::string> names_set;
62 StructuredData::Array *names_array;
63 success = options_dict.GetValueForKeyAsArray(
64 GetKey(OptionNames::SymbolNameArray), names_array);
65 if (success && names_array) {
66 size_t num_names = names_array->GetSize();
67 for (size_t i = 0; i < num_names; i++) {
69 success = names_array->GetItemAtIndexAsString(i, name);
71 error.SetErrorStringWithFormat(
72 "BRFR::CFSD: Malformed element %zu in the names array.", i);
75 names_set.insert(name);
79 return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match);
82 StructuredData::ObjectSP
83 BreakpointResolverFileRegex::SerializeToStructuredData() {
84 StructuredData::DictionarySP options_dict_sp(
85 new StructuredData::Dictionary());
87 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
89 options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
91 if (!m_function_names.empty()) {
92 StructuredData::ArraySP names_array_sp(new StructuredData::Array());
93 for (std::string name : m_function_names) {
94 StructuredData::StringSP item(new StructuredData::String(name));
95 names_array_sp->AddItem(item);
97 options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
100 return WrapOptionsDict(options_dict_sp);
103 Searcher::CallbackReturn
104 BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
105 SymbolContext &context,
106 Address *addr, bool containing) {
108 assert(m_breakpoint != NULL);
109 if (!context.target_sp)
110 return eCallbackReturnContinue;
112 CompileUnit *cu = context.comp_unit;
113 FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu));
114 std::vector<uint32_t> line_matches;
115 context.target_sp->GetSourceManager().FindLinesMatchingRegex(
116 cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
118 uint32_t num_matches = line_matches.size();
119 for (uint32_t i = 0; i < num_matches; i++) {
120 SymbolContextList sc_list;
121 const bool search_inlines = false;
123 cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines,
124 m_exact_match, eSymbolContextEverything, sc_list);
125 // Find all the function names:
126 if (!m_function_names.empty()) {
127 std::vector<size_t> sc_to_remove;
128 for (size_t i = 0; i < sc_list.GetSize(); i++) {
129 SymbolContext sc_ctx;
130 sc_list.GetContextAtIndex(i, sc_ctx);
134 Mangled::NamePreference::ePreferDemangledWithoutArguments)
136 if (!m_function_names.count(name)) {
137 sc_to_remove.push_back(i);
141 if (!sc_to_remove.empty()) {
142 std::vector<size_t>::reverse_iterator iter;
143 std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
144 for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
145 sc_list.RemoveContextAtIndex(*iter);
150 const bool skip_prologue = true;
152 BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
155 assert(m_breakpoint != NULL);
157 return Searcher::eCallbackReturnContinue;
160 Searcher::Depth BreakpointResolverFileRegex::GetDepth() {
161 return Searcher::eDepthCompUnit;
164 void BreakpointResolverFileRegex::GetDescription(Stream *s) {
165 s->Printf("source regex = \"%s\", exact_match = %d",
166 m_regex.GetText().str().c_str(), m_exact_match);
169 void BreakpointResolverFileRegex::Dump(Stream *s) const {}
171 lldb::BreakpointResolverSP
172 BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) {
173 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
174 &breakpoint, m_regex, m_function_names, m_exact_match));
178 void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
179 m_function_names.insert(func_name);