1435933ddSDimitry Andric //===-- BreakpointResolverFileRegex.cpp -------------------------*- C++-*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
10ac7ddfbfSEd Maste #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
11ac7ddfbfSEd Maste
12ac7ddfbfSEd Maste #include "lldb/Breakpoint/BreakpointLocation.h"
13435933ddSDimitry Andric #include "lldb/Core/SourceManager.h"
14ac7ddfbfSEd Maste #include "lldb/Symbol/CompileUnit.h"
15ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
16f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
17f678e45dSDimitry Andric #include "lldb/Utility/StreamString.h"
18ac7ddfbfSEd Maste
19ac7ddfbfSEd Maste using namespace lldb;
20ac7ddfbfSEd Maste using namespace lldb_private;
21ac7ddfbfSEd Maste
22ac7ddfbfSEd Maste //----------------------------------------------------------------------
23ac7ddfbfSEd Maste // BreakpointResolverFileRegex:
24ac7ddfbfSEd Maste //----------------------------------------------------------------------
BreakpointResolverFileRegex(Breakpoint * bkpt,RegularExpression & regex,const std::unordered_set<std::string> & func_names,bool exact_match)25435933ddSDimitry Andric BreakpointResolverFileRegex::BreakpointResolverFileRegex(
26435933ddSDimitry Andric Breakpoint *bkpt, RegularExpression ®ex,
27435933ddSDimitry Andric const std::unordered_set<std::string> &func_names, bool exact_match)
28435933ddSDimitry Andric : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver),
29435933ddSDimitry Andric m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) {
30ac7ddfbfSEd Maste }
31ac7ddfbfSEd Maste
~BreakpointResolverFileRegex()32435933ddSDimitry Andric BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
33435933ddSDimitry Andric
CreateFromStructuredData(Breakpoint * bkpt,const StructuredData::Dictionary & options_dict,Status & error)34435933ddSDimitry Andric BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
35435933ddSDimitry Andric Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
365517e702SDimitry Andric Status &error) {
37435933ddSDimitry Andric bool success;
38435933ddSDimitry Andric
395517e702SDimitry Andric llvm::StringRef regex_string;
40435933ddSDimitry Andric success = options_dict.GetValueForKeyAsString(
41435933ddSDimitry Andric GetKey(OptionNames::RegexString), regex_string);
42435933ddSDimitry Andric if (!success) {
43435933ddSDimitry Andric error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
44435933ddSDimitry Andric return nullptr;
45435933ddSDimitry Andric }
46435933ddSDimitry Andric RegularExpression regex(regex_string);
47435933ddSDimitry Andric
48435933ddSDimitry Andric bool exact_match;
49435933ddSDimitry Andric success = options_dict.GetValueForKeyAsBoolean(
50435933ddSDimitry Andric GetKey(OptionNames::ExactMatch), exact_match);
51435933ddSDimitry Andric if (!success) {
52435933ddSDimitry Andric error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
53435933ddSDimitry Andric return nullptr;
54435933ddSDimitry Andric }
55435933ddSDimitry Andric
56435933ddSDimitry Andric // The names array is optional:
57435933ddSDimitry Andric std::unordered_set<std::string> names_set;
58435933ddSDimitry Andric StructuredData::Array *names_array;
59435933ddSDimitry Andric success = options_dict.GetValueForKeyAsArray(
60435933ddSDimitry Andric GetKey(OptionNames::SymbolNameArray), names_array);
61435933ddSDimitry Andric if (success && names_array) {
62435933ddSDimitry Andric size_t num_names = names_array->GetSize();
63435933ddSDimitry Andric for (size_t i = 0; i < num_names; i++) {
645517e702SDimitry Andric llvm::StringRef name;
65435933ddSDimitry Andric success = names_array->GetItemAtIndexAsString(i, name);
66435933ddSDimitry Andric if (!success) {
67435933ddSDimitry Andric error.SetErrorStringWithFormat(
68435933ddSDimitry Andric "BRFR::CFSD: Malformed element %zu in the names array.", i);
69435933ddSDimitry Andric return nullptr;
70435933ddSDimitry Andric }
71435933ddSDimitry Andric names_set.insert(name);
72435933ddSDimitry Andric }
73435933ddSDimitry Andric }
74435933ddSDimitry Andric
75435933ddSDimitry Andric return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match);
76435933ddSDimitry Andric }
77435933ddSDimitry Andric
78435933ddSDimitry Andric StructuredData::ObjectSP
SerializeToStructuredData()79435933ddSDimitry Andric BreakpointResolverFileRegex::SerializeToStructuredData() {
80435933ddSDimitry Andric StructuredData::DictionarySP options_dict_sp(
81435933ddSDimitry Andric new StructuredData::Dictionary());
82435933ddSDimitry Andric
83435933ddSDimitry Andric options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
84435933ddSDimitry Andric m_regex.GetText());
85435933ddSDimitry Andric options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
86435933ddSDimitry Andric m_exact_match);
87435933ddSDimitry Andric if (!m_function_names.empty()) {
88435933ddSDimitry Andric StructuredData::ArraySP names_array_sp(new StructuredData::Array());
89435933ddSDimitry Andric for (std::string name : m_function_names) {
90435933ddSDimitry Andric StructuredData::StringSP item(new StructuredData::String(name));
91435933ddSDimitry Andric names_array_sp->AddItem(item);
92435933ddSDimitry Andric }
93435933ddSDimitry Andric options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
94435933ddSDimitry Andric }
95435933ddSDimitry Andric
96435933ddSDimitry Andric return WrapOptionsDict(options_dict_sp);
97ac7ddfbfSEd Maste }
98ac7ddfbfSEd Maste
99ac7ddfbfSEd Maste Searcher::CallbackReturn
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr,bool containing)100435933ddSDimitry Andric BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
101ac7ddfbfSEd Maste SymbolContext &context,
102435933ddSDimitry Andric Address *addr, bool containing) {
103ac7ddfbfSEd Maste
104ac7ddfbfSEd Maste assert(m_breakpoint != NULL);
105ac7ddfbfSEd Maste if (!context.target_sp)
106ac7ddfbfSEd Maste return eCallbackReturnContinue;
107ac7ddfbfSEd Maste
108ac7ddfbfSEd Maste CompileUnit *cu = context.comp_unit;
109ac7ddfbfSEd Maste FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu));
110ac7ddfbfSEd Maste std::vector<uint32_t> line_matches;
111435933ddSDimitry Andric context.target_sp->GetSourceManager().FindLinesMatchingRegex(
112435933ddSDimitry Andric cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
11335617911SEd Maste
114ac7ddfbfSEd Maste uint32_t num_matches = line_matches.size();
115435933ddSDimitry Andric for (uint32_t i = 0; i < num_matches; i++) {
11635617911SEd Maste SymbolContextList sc_list;
11735617911SEd Maste const bool search_inlines = false;
118ac7ddfbfSEd Maste
119435933ddSDimitry Andric cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines,
120435933ddSDimitry Andric m_exact_match, eSymbolContextEverything, sc_list);
1214bb0738eSEd Maste // Find all the function names:
122435933ddSDimitry Andric if (!m_function_names.empty()) {
1234bb0738eSEd Maste std::vector<size_t> sc_to_remove;
124435933ddSDimitry Andric for (size_t i = 0; i < sc_list.GetSize(); i++) {
1254bb0738eSEd Maste SymbolContext sc_ctx;
1264bb0738eSEd Maste sc_list.GetContextAtIndex(i, sc_ctx);
127435933ddSDimitry Andric std::string name(
128435933ddSDimitry Andric sc_ctx
129435933ddSDimitry Andric .GetFunctionName(
130435933ddSDimitry Andric Mangled::NamePreference::ePreferDemangledWithoutArguments)
131435933ddSDimitry Andric .AsCString());
132435933ddSDimitry Andric if (!m_function_names.count(name)) {
1334bb0738eSEd Maste sc_to_remove.push_back(i);
1344bb0738eSEd Maste }
1354bb0738eSEd Maste }
1364bb0738eSEd Maste
137435933ddSDimitry Andric if (!sc_to_remove.empty()) {
1384bb0738eSEd Maste std::vector<size_t>::reverse_iterator iter;
1394bb0738eSEd Maste std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
140435933ddSDimitry Andric for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
1414bb0738eSEd Maste sc_list.RemoveContextAtIndex(*iter);
1424bb0738eSEd Maste }
1434bb0738eSEd Maste }
1444bb0738eSEd Maste }
1454bb0738eSEd Maste
14635617911SEd Maste const bool skip_prologue = true;
147ac7ddfbfSEd Maste
148435933ddSDimitry Andric BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
149435933ddSDimitry Andric m_regex.GetText());
150ac7ddfbfSEd Maste }
151ac7ddfbfSEd Maste assert(m_breakpoint != NULL);
152ac7ddfbfSEd Maste
153ac7ddfbfSEd Maste return Searcher::eCallbackReturnContinue;
154ac7ddfbfSEd Maste }
155ac7ddfbfSEd Maste
GetDepth()156*b5893f02SDimitry Andric lldb::SearchDepth BreakpointResolverFileRegex::GetDepth() {
157*b5893f02SDimitry Andric return lldb::eSearchDepthCompUnit;
158ac7ddfbfSEd Maste }
159ac7ddfbfSEd Maste
GetDescription(Stream * s)160435933ddSDimitry Andric void BreakpointResolverFileRegex::GetDescription(Stream *s) {
161435933ddSDimitry Andric s->Printf("source regex = \"%s\", exact_match = %d",
162435933ddSDimitry Andric m_regex.GetText().str().c_str(), m_exact_match);
163ac7ddfbfSEd Maste }
164ac7ddfbfSEd Maste
Dump(Stream * s) const165435933ddSDimitry Andric void BreakpointResolverFileRegex::Dump(Stream *s) const {}
166ac7ddfbfSEd Maste
1677aa51b79SEd Maste lldb::BreakpointResolverSP
CopyForBreakpoint(Breakpoint & breakpoint)168435933ddSDimitry Andric BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) {
169435933ddSDimitry Andric lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
170435933ddSDimitry Andric &breakpoint, m_regex, m_function_names, m_exact_match));
1717aa51b79SEd Maste return ret_sp;
1727aa51b79SEd Maste }
1737aa51b79SEd Maste
AddFunctionName(const char * func_name)174435933ddSDimitry Andric void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
1754bb0738eSEd Maste m_function_names.insert(func_name);
1764bb0738eSEd Maste }
177