1b9c1b51eSKate Stone //===-- BreakpointResolverFileRegex.cpp --------------------------*- C++
2b9c1b51eSKate Stone //-*-===//
3969795f1SJim Ingham //
4969795f1SJim Ingham //                     The LLVM Compiler Infrastructure
5969795f1SJim Ingham //
6969795f1SJim Ingham // This file is distributed under the University of Illinois Open Source
7969795f1SJim Ingham // License. See LICENSE.TXT for details.
8969795f1SJim Ingham //
9969795f1SJim Ingham //===----------------------------------------------------------------------===//
10969795f1SJim Ingham 
11969795f1SJim Ingham #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
12969795f1SJim Ingham 
13969795f1SJim Ingham // C Includes
14969795f1SJim Ingham // C++ Includes
15969795f1SJim Ingham // Other libraries and framework includes
16969795f1SJim Ingham // Project includes
17969795f1SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
18969795f1SJim Ingham #include "lldb/Core/Log.h"
19b9c1b51eSKate Stone #include "lldb/Core/SourceManager.h"
20969795f1SJim Ingham #include "lldb/Core/StreamString.h"
211f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
22969795f1SJim Ingham #include "lldb/Target/Target.h"
23969795f1SJim Ingham 
24969795f1SJim Ingham using namespace lldb;
25969795f1SJim Ingham using namespace lldb_private;
26969795f1SJim Ingham 
27969795f1SJim Ingham //----------------------------------------------------------------------
28969795f1SJim Ingham // BreakpointResolverFileRegex:
29969795f1SJim Ingham //----------------------------------------------------------------------
30b9c1b51eSKate Stone BreakpointResolverFileRegex::BreakpointResolverFileRegex(
31b9c1b51eSKate Stone     Breakpoint *bkpt, RegularExpression &regex,
32b9c1b51eSKate Stone     const std::unordered_set<std::string> &func_names, bool exact_match)
33b9c1b51eSKate Stone     : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver),
34b9c1b51eSKate Stone       m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) {
35969795f1SJim Ingham }
36969795f1SJim Ingham 
37b9c1b51eSKate Stone BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
38969795f1SJim Ingham 
39*e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
40*e14dc268SJim Ingham     Breakpoint *bkpt, StructuredData::Dictionary &options_dict, Error &error) {
41*e14dc268SJim Ingham   bool success;
42*e14dc268SJim Ingham 
43*e14dc268SJim Ingham   std::string regex_string;
44*e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsString(
45*e14dc268SJim Ingham       GetKey(OptionNames::RegexString), regex_string);
46*e14dc268SJim Ingham   if (!success) {
47*e14dc268SJim Ingham     error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
48*e14dc268SJim Ingham     return nullptr;
49*e14dc268SJim Ingham   }
50*e14dc268SJim Ingham   RegularExpression regex(regex_string.c_str());
51*e14dc268SJim Ingham 
52*e14dc268SJim Ingham   bool exact_match;
53*e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(
54*e14dc268SJim Ingham       GetKey(OptionNames::ExactMatch), exact_match);
55*e14dc268SJim Ingham   if (!success) {
56*e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
57*e14dc268SJim Ingham     return nullptr;
58*e14dc268SJim Ingham   }
59*e14dc268SJim Ingham 
60*e14dc268SJim Ingham   // The names array is optional:
61*e14dc268SJim Ingham   std::unordered_set<std::string> names_set;
62*e14dc268SJim Ingham   StructuredData::Array *names_array;
63*e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsArray(
64*e14dc268SJim Ingham       GetKey(OptionNames::SymbolNameArray), names_array);
65*e14dc268SJim Ingham   if (success && names_array) {
66*e14dc268SJim Ingham     size_t num_names = names_array->GetSize();
67*e14dc268SJim Ingham     for (size_t i = 0; i < num_names; i++) {
68*e14dc268SJim Ingham       std::string name;
69*e14dc268SJim Ingham       success = names_array->GetItemAtIndexAsString(i, name);
70*e14dc268SJim Ingham       if (!success) {
71*e14dc268SJim Ingham         error.SetErrorStringWithFormat(
72*e14dc268SJim Ingham             "BRFR::CFSD: Malformed element %zu in the names array.", i);
73*e14dc268SJim Ingham         return nullptr;
74*e14dc268SJim Ingham       }
75*e14dc268SJim Ingham       names_set.insert(name);
76*e14dc268SJim Ingham     }
77*e14dc268SJim Ingham   }
78*e14dc268SJim Ingham 
79*e14dc268SJim Ingham   return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match);
80*e14dc268SJim Ingham }
81*e14dc268SJim Ingham 
82*e14dc268SJim Ingham StructuredData::ObjectSP
83*e14dc268SJim Ingham BreakpointResolverFileRegex::SerializeToStructuredData() {
84*e14dc268SJim Ingham   StructuredData::DictionarySP options_dict_sp(
85*e14dc268SJim Ingham       new StructuredData::Dictionary());
86*e14dc268SJim Ingham 
87*e14dc268SJim Ingham   options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
88*e14dc268SJim Ingham                                  m_regex.GetText());
89*e14dc268SJim Ingham   options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
90*e14dc268SJim Ingham                                   m_exact_match);
91*e14dc268SJim Ingham   if (!m_function_names.empty()) {
92*e14dc268SJim Ingham     StructuredData::ArraySP names_array_sp(new StructuredData::Array());
93*e14dc268SJim Ingham     for (std::string name : m_function_names) {
94*e14dc268SJim Ingham       StructuredData::StringSP item(new StructuredData::String(name));
95*e14dc268SJim Ingham       names_array_sp->AddItem(item);
96*e14dc268SJim Ingham     }
97*e14dc268SJim Ingham     options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
98*e14dc268SJim Ingham   }
99*e14dc268SJim Ingham 
100*e14dc268SJim Ingham   return WrapOptionsDict(options_dict_sp);
101*e14dc268SJim Ingham }
102*e14dc268SJim Ingham 
103969795f1SJim Ingham Searcher::CallbackReturn
104b9c1b51eSKate Stone BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
105969795f1SJim Ingham                                             SymbolContext &context,
106b9c1b51eSKate Stone                                             Address *addr, bool containing) {
107969795f1SJim Ingham 
108969795f1SJim Ingham   assert(m_breakpoint != NULL);
109969795f1SJim Ingham   if (!context.target_sp)
110969795f1SJim Ingham     return eCallbackReturnContinue;
111969795f1SJim Ingham 
112969795f1SJim Ingham   CompileUnit *cu = context.comp_unit;
113969795f1SJim Ingham   FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu));
114969795f1SJim Ingham   std::vector<uint32_t> line_matches;
115b9c1b51eSKate Stone   context.target_sp->GetSourceManager().FindLinesMatchingRegex(
116b9c1b51eSKate Stone       cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
117f642373cSJim Ingham 
118969795f1SJim Ingham   uint32_t num_matches = line_matches.size();
119b9c1b51eSKate Stone   for (uint32_t i = 0; i < num_matches; i++) {
120f642373cSJim Ingham     SymbolContextList sc_list;
121f642373cSJim Ingham     const bool search_inlines = false;
122969795f1SJim Ingham 
123b9c1b51eSKate Stone     cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines,
124b9c1b51eSKate Stone                              m_exact_match, eSymbolContextEverything, sc_list);
12576bb8d67SJim Ingham     // Find all the function names:
126b9c1b51eSKate Stone     if (!m_function_names.empty()) {
12776bb8d67SJim Ingham       std::vector<size_t> sc_to_remove;
128b9c1b51eSKate Stone       for (size_t i = 0; i < sc_list.GetSize(); i++) {
12976bb8d67SJim Ingham         SymbolContext sc_ctx;
13076bb8d67SJim Ingham         sc_list.GetContextAtIndex(i, sc_ctx);
131b9c1b51eSKate Stone         std::string name(
132b9c1b51eSKate Stone             sc_ctx
133b9c1b51eSKate Stone                 .GetFunctionName(
134b9c1b51eSKate Stone                     Mangled::NamePreference::ePreferDemangledWithoutArguments)
135b9c1b51eSKate Stone                 .AsCString());
136b9c1b51eSKate Stone         if (!m_function_names.count(name)) {
13776bb8d67SJim Ingham           sc_to_remove.push_back(i);
13876bb8d67SJim Ingham         }
13976bb8d67SJim Ingham       }
14076bb8d67SJim Ingham 
141b9c1b51eSKate Stone       if (!sc_to_remove.empty()) {
14276bb8d67SJim Ingham         std::vector<size_t>::reverse_iterator iter;
14376bb8d67SJim Ingham         std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
144b9c1b51eSKate Stone         for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
14576bb8d67SJim Ingham           sc_list.RemoveContextAtIndex(*iter);
14676bb8d67SJim Ingham         }
14776bb8d67SJim Ingham       }
14876bb8d67SJim Ingham     }
14976bb8d67SJim Ingham 
150f642373cSJim Ingham     const bool skip_prologue = true;
151969795f1SJim Ingham 
152b9c1b51eSKate Stone     BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
153b9c1b51eSKate Stone                                            m_regex.GetText());
154969795f1SJim Ingham   }
155969795f1SJim Ingham   assert(m_breakpoint != NULL);
156969795f1SJim Ingham 
157969795f1SJim Ingham   return Searcher::eCallbackReturnContinue;
158969795f1SJim Ingham }
159969795f1SJim Ingham 
160b9c1b51eSKate Stone Searcher::Depth BreakpointResolverFileRegex::GetDepth() {
161969795f1SJim Ingham   return Searcher::eDepthCompUnit;
162969795f1SJim Ingham }
163969795f1SJim Ingham 
164b9c1b51eSKate Stone void BreakpointResolverFileRegex::GetDescription(Stream *s) {
165b9c1b51eSKate Stone   s->Printf("source regex = \"%s\", exact_match = %d", m_regex.GetText(),
166b9c1b51eSKate Stone             m_exact_match);
167969795f1SJim Ingham }
168969795f1SJim Ingham 
169b9c1b51eSKate Stone void BreakpointResolverFileRegex::Dump(Stream *s) const {}
170969795f1SJim Ingham 
17133df7cd3SJim Ingham lldb::BreakpointResolverSP
172b9c1b51eSKate Stone BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) {
173b9c1b51eSKate Stone   lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
174b9c1b51eSKate Stone       &breakpoint, m_regex, m_function_names, m_exact_match));
17533df7cd3SJim Ingham   return ret_sp;
17633df7cd3SJim Ingham }
17733df7cd3SJim Ingham 
178b9c1b51eSKate Stone void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
17976bb8d67SJim Ingham   m_function_names.insert(func_name);
18076bb8d67SJim Ingham }
181