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