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"
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"
20 using namespace lldb_private;
22 //----------------------------------------------------------------------
23 // BreakpointResolverFileRegex:
24 //----------------------------------------------------------------------
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) {
32 BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
34 BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
35 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
39 llvm::StringRef regex_string;
40 success = options_dict.GetValueForKeyAsString(
41 GetKey(OptionNames::RegexString), regex_string);
43 error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
46 RegularExpression regex(regex_string);
49 success = options_dict.GetValueForKeyAsBoolean(
50 GetKey(OptionNames::ExactMatch), exact_match);
52 error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
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++) {
65 success = names_array->GetItemAtIndexAsString(i, name);
67 error.SetErrorStringWithFormat(
68 "BRFR::CFSD: Malformed element %zu in the names array.", i);
71 names_set.insert(name);
75 return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match);
78 StructuredData::ObjectSP
79 BreakpointResolverFileRegex::SerializeToStructuredData() {
80 StructuredData::DictionarySP options_dict_sp(
81 new StructuredData::Dictionary());
83 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
85 options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
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);
93 options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
96 return WrapOptionsDict(options_dict_sp);
99 Searcher::CallbackReturn
100 BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
101 SymbolContext &context,
102 Address *addr, bool containing) {
104 assert(m_breakpoint != NULL);
105 if (!context.target_sp)
106 return eCallbackReturnContinue;
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);
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;
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);
130 Mangled::NamePreference::ePreferDemangledWithoutArguments)
132 if (!m_function_names.count(name)) {
133 sc_to_remove.push_back(i);
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);
146 const bool skip_prologue = true;
148 BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
151 assert(m_breakpoint != NULL);
153 return Searcher::eCallbackReturnContinue;
156 lldb::SearchDepth BreakpointResolverFileRegex::GetDepth() {
157 return lldb::eSearchDepthCompUnit;
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);
165 void BreakpointResolverFileRegex::Dump(Stream *s) const {}
167 lldb::BreakpointResolverSP
168 BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) {
169 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
170 &breakpoint, m_regex, m_function_names, m_exact_match));
174 void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
175 m_function_names.insert(func_name);