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