LLDB mainline
BreakpointResolverFileRegex.cpp
Go to the documentation of this file.
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
10
14#include "lldb/Target/Target.h"
15#include "lldb/Utility/Log.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21// 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
30 const StructuredData::Dictionary &options_dict, Status &error) {
31 bool success;
32
33 llvm::StringRef regex_string;
34 success = options_dict.GetValueForKeyAsString(
35 GetKey(OptionNames::RegexString), regex_string);
36 if (!success) {
37 error = Status::FromErrorString("BRFR::CFSD: Couldn't find regex entry.");
38 return nullptr;
39 }
40 RegularExpression regex(regex_string);
41
42 bool exact_match;
43 success = options_dict.GetValueForKeyAsBoolean(
44 GetKey(OptionNames::ExactMatch), exact_match);
45 if (!success) {
46 error =
47 Status::FromErrorString("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(
56 if (success && names_array) {
57 size_t num_names = names_array->GetSize();
58 for (size_t i = 0; i < num_names; i++) {
59 std::optional<llvm::StringRef> maybe_name =
60 names_array->GetItemAtIndexAsString(i);
61 if (!maybe_name) {
63 "BRFR::CFSD: Malformed element {0} in the names array.", i);
64 return nullptr;
65 }
66 names_set.insert(std::string(*maybe_name));
67 }
68 }
69
70 return std::make_shared<BreakpointResolverFileRegex>(
71 nullptr, std::move(regex), names_set, exact_match);
72}
73
76 StructuredData::DictionarySP options_dict_sp(
78
79 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
81 options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
83 if (!m_function_names.empty()) {
85 for (std::string name : m_function_names) {
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
96 SearchFilter &filter, SymbolContext &context, Address *addr) {
97
98 if (!context.target_sp)
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 std::make_shared<SupportFile>(cu_file_spec), m_regex, 1, UINT32_MAX,
106 line_matches);
107
108 uint32_t num_matches = line_matches.size();
109 for (uint32_t i = 0; i < num_matches; i++) {
110 SymbolContextList sc_list;
111 // TODO: Handle SourceLocationSpec column information
112 SourceLocationSpec location_spec(cu_file_spec, line_matches[i],
113 /*column=*/std::nullopt,
114 /*check_inlines=*/false, m_exact_match);
115 cu->ResolveSymbolContext(location_spec, eSymbolContextEverything, sc_list);
116 // Find all the function names:
117 if (!m_function_names.empty()) {
118 std::vector<size_t> sc_to_remove;
119 for (size_t i = 0; i < sc_list.GetSize(); i++) {
120 SymbolContext sc_ctx;
121 sc_list.GetContextAtIndex(i, sc_ctx);
122 std::string name(
123 sc_ctx
124 .GetFunctionName(
126 .AsCString());
127 if (!m_function_names.count(name)) {
128 sc_to_remove.push_back(i);
129 }
130 }
131
132 if (!sc_to_remove.empty()) {
133 std::vector<size_t>::reverse_iterator iter;
134 std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
135 for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
136 sc_list.RemoveContextAtIndex(*iter);
137 }
138 }
139 }
140
141 const bool skip_prologue = true;
142
143 BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
144 m_regex.GetText());
145 }
146
148}
149
152}
153
155 s->Printf("source regex = \"%s\", exact_match = %d",
156 m_regex.GetText().str().c_str(), m_exact_match);
157}
158
160
164 breakpoint, m_regex, m_function_names, m_exact_match));
165 return ret_sp;
166}
167
169 m_function_names.insert(func_name);
170}
static llvm::raw_ostream & error(Stream &strm)
A section + offset based address class.
Definition: Address.h:62
"lldb/Breakpoint/BreakpointResolverFileRegex.h" This class sets breakpoints by file and line.
StructuredData::ObjectSP SerializeToStructuredData() override
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
std::unordered_set< std::string > m_function_names
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
BreakpointResolverFileRegex(const lldb::BreakpointSP &bkpt, RegularExpression regex, const std::unordered_set< std::string > &func_name_set, bool exact_match)
lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &options_dict, Status &error)
General Outline: The BreakpointResolver is a Searcher.
static const char * GetKey(OptionNames enum_value)
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
void SetSCMatchesByLine(SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue, llvm::StringRef log_ident, uint32_t line=0, std::optional< uint16_t > column=std::nullopt)
Takes a symbol context list of matches which supposedly represent the same file and line number in a ...
A class that describes a compilation unit.
Definition: CompileUnit.h:43
const FileSpec & GetPrimaryFile() const
Return the primary source spec associated with this compile unit.
Definition: CompileUnit.h:232
void ResolveSymbolContext(const SourceLocationSpec &src_location_spec, lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list, RealpathPrefixes *realpath_prefixes=nullptr)
Resolve symbol contexts by file and line.
A file utility class.
Definition: FileSpec.h:56
@ ePreferDemangledWithoutArguments
Definition: Mangled.h:38
llvm::StringRef GetText() const
Access the regular expression text.
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
"lldb/Core/SourceLocationSpec.h" A source location specifier class.
An error handling class.
Definition: Status.h:115
static Status FromErrorString(const char *str)
Definition: Status.h:138
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition: Status.h:148
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
std::optional< llvm::StringRef > GetItemAtIndexAsString(size_t idx) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
bool GetValueForKeyAsBoolean(llvm::StringRef key, bool &result) const
bool GetValueForKeyAsArray(llvm::StringRef key, Array *&result) const
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< String > StringSP
std::shared_ptr< Array > ArraySP
Defines a list of symbol context objects.
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
CompileUnit * comp_unit
The CompileUnit for a given query.
lldb::TargetSP target_sp
The Target for a given query.
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
Definition: lldb-forward.h:328
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:321
@ eSearchDepthCompUnit