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