180814287SRaphael Isemann //===-- BreakpointResolverFileRegex.cpp -----------------------------------===//
2969795f1SJim Ingham //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6969795f1SJim Ingham //
7969795f1SJim Ingham //===----------------------------------------------------------------------===//
8969795f1SJim Ingham
9969795f1SJim Ingham #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
10969795f1SJim Ingham
11969795f1SJim Ingham #include "lldb/Breakpoint/BreakpointLocation.h"
12b9c1b51eSKate Stone #include "lldb/Core/SourceManager.h"
131f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
14969795f1SJim Ingham #include "lldb/Target/Target.h"
156f9e6901SZachary Turner #include "lldb/Utility/Log.h"
16bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
17969795f1SJim Ingham
18969795f1SJim Ingham using namespace lldb;
19969795f1SJim Ingham using namespace lldb_private;
20969795f1SJim Ingham
21969795f1SJim Ingham // BreakpointResolverFileRegex:
BreakpointResolverFileRegex(const lldb::BreakpointSP & bkpt,RegularExpression regex,const std::unordered_set<std::string> & func_names,bool exact_match)22b9c1b51eSKate Stone BreakpointResolverFileRegex::BreakpointResolverFileRegex(
236c17cc53STatyana Krasnukha const lldb::BreakpointSP &bkpt, RegularExpression regex,
24b9c1b51eSKate Stone const std::unordered_set<std::string> &func_names, bool exact_match)
256d1e4696SJim Ingham : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver),
263af3f1e8SJonas Devlieghere m_regex(std::move(regex)), m_exact_match(exact_match),
273af3f1e8SJonas Devlieghere m_function_names(func_names) {}
28969795f1SJim Ingham
CreateFromStructuredData(const lldb::BreakpointSP & bkpt,const StructuredData::Dictionary & options_dict,Status & error)29e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
306c17cc53STatyana Krasnukha const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
3197206d57SZachary Turner Status &error) {
32e14dc268SJim Ingham bool success;
33e14dc268SJim Ingham
342833321fSZachary Turner llvm::StringRef regex_string;
35e14dc268SJim Ingham success = options_dict.GetValueForKeyAsString(
36e14dc268SJim Ingham GetKey(OptionNames::RegexString), regex_string);
37e14dc268SJim Ingham if (!success) {
38e14dc268SJim Ingham error.SetErrorString("BRFR::CFSD: Couldn't find regex entry.");
39e14dc268SJim Ingham return nullptr;
40e14dc268SJim Ingham }
4195eae423SZachary Turner RegularExpression regex(regex_string);
42e14dc268SJim Ingham
43e14dc268SJim Ingham bool exact_match;
44e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean(
45e14dc268SJim Ingham GetKey(OptionNames::ExactMatch), exact_match);
46e14dc268SJim Ingham if (!success) {
47e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
48e14dc268SJim Ingham return nullptr;
49e14dc268SJim Ingham }
50e14dc268SJim Ingham
51e14dc268SJim Ingham // The names array is optional:
52e14dc268SJim Ingham std::unordered_set<std::string> names_set;
53e14dc268SJim Ingham StructuredData::Array *names_array;
54e14dc268SJim Ingham success = options_dict.GetValueForKeyAsArray(
55e14dc268SJim Ingham GetKey(OptionNames::SymbolNameArray), names_array);
56e14dc268SJim Ingham if (success && names_array) {
57e14dc268SJim Ingham size_t num_names = names_array->GetSize();
58e14dc268SJim Ingham for (size_t i = 0; i < num_names; i++) {
592833321fSZachary Turner llvm::StringRef name;
60e14dc268SJim Ingham success = names_array->GetItemAtIndexAsString(i, name);
61e14dc268SJim Ingham if (!success) {
62e14dc268SJim Ingham error.SetErrorStringWithFormat(
63e14dc268SJim Ingham "BRFR::CFSD: Malformed element %zu in the names array.", i);
64e14dc268SJim Ingham return nullptr;
65e14dc268SJim Ingham }
66adcd0268SBenjamin Kramer names_set.insert(std::string(name));
67e14dc268SJim Ingham }
68e14dc268SJim Ingham }
69e14dc268SJim Ingham
703af3f1e8SJonas Devlieghere return new BreakpointResolverFileRegex(bkpt, std::move(regex), names_set,
713af3f1e8SJonas Devlieghere exact_match);
72e14dc268SJim Ingham }
73e14dc268SJim Ingham
74e14dc268SJim Ingham StructuredData::ObjectSP
SerializeToStructuredData()75e14dc268SJim Ingham BreakpointResolverFileRegex::SerializeToStructuredData() {
76e14dc268SJim Ingham StructuredData::DictionarySP options_dict_sp(
77e14dc268SJim Ingham new StructuredData::Dictionary());
78e14dc268SJim Ingham
79e14dc268SJim Ingham options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString),
80e14dc268SJim Ingham m_regex.GetText());
81e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
82e14dc268SJim Ingham m_exact_match);
83e14dc268SJim Ingham if (!m_function_names.empty()) {
84e14dc268SJim Ingham StructuredData::ArraySP names_array_sp(new StructuredData::Array());
85e14dc268SJim Ingham for (std::string name : m_function_names) {
86e14dc268SJim Ingham StructuredData::StringSP item(new StructuredData::String(name));
87e14dc268SJim Ingham names_array_sp->AddItem(item);
88e14dc268SJim Ingham }
89e14dc268SJim Ingham options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp);
90e14dc268SJim Ingham }
91e14dc268SJim Ingham
92e14dc268SJim Ingham return WrapOptionsDict(options_dict_sp);
93e14dc268SJim Ingham }
94e14dc268SJim Ingham
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr)9595e264fcSRaphael Isemann Searcher::CallbackReturn BreakpointResolverFileRegex::SearchCallback(
9695e264fcSRaphael Isemann SearchFilter &filter, SymbolContext &context, Address *addr) {
97969795f1SJim Ingham
98969795f1SJim Ingham if (!context.target_sp)
99969795f1SJim Ingham return eCallbackReturnContinue;
100969795f1SJim Ingham
101969795f1SJim Ingham CompileUnit *cu = context.comp_unit;
10238870af8SPavel Labath FileSpec cu_file_spec = cu->GetPrimaryFile();
103969795f1SJim Ingham std::vector<uint32_t> line_matches;
104b9c1b51eSKate Stone context.target_sp->GetSourceManager().FindLinesMatchingRegex(
105b9c1b51eSKate Stone cu_file_spec, m_regex, 1, UINT32_MAX, line_matches);
106f642373cSJim Ingham
107969795f1SJim Ingham uint32_t num_matches = line_matches.size();
108b9c1b51eSKate Stone for (uint32_t i = 0; i < num_matches; i++) {
109f642373cSJim Ingham SymbolContextList sc_list;
1103e2ed744SMed Ismail Bennani // TODO: Handle SourceLocationSpec column information
1113e2ed744SMed Ismail Bennani SourceLocationSpec location_spec(cu_file_spec, line_matches[i],
1123e2ed744SMed Ismail Bennani /*column=*/llvm::None,
113*b12fd138SKazu Hirata /*check_inlines=*/false, m_exact_match);
1143e2ed744SMed Ismail Bennani cu->ResolveSymbolContext(location_spec, eSymbolContextEverything, sc_list);
11576bb8d67SJim Ingham // Find all the function names:
116b9c1b51eSKate Stone if (!m_function_names.empty()) {
11776bb8d67SJim Ingham std::vector<size_t> sc_to_remove;
118b9c1b51eSKate Stone for (size_t i = 0; i < sc_list.GetSize(); i++) {
11976bb8d67SJim Ingham SymbolContext sc_ctx;
12076bb8d67SJim Ingham sc_list.GetContextAtIndex(i, sc_ctx);
121b9c1b51eSKate Stone std::string name(
122b9c1b51eSKate Stone sc_ctx
123b9c1b51eSKate Stone .GetFunctionName(
124b9c1b51eSKate Stone Mangled::NamePreference::ePreferDemangledWithoutArguments)
125b9c1b51eSKate Stone .AsCString());
126b9c1b51eSKate Stone if (!m_function_names.count(name)) {
12776bb8d67SJim Ingham sc_to_remove.push_back(i);
12876bb8d67SJim Ingham }
12976bb8d67SJim Ingham }
13076bb8d67SJim Ingham
131b9c1b51eSKate Stone if (!sc_to_remove.empty()) {
13276bb8d67SJim Ingham std::vector<size_t>::reverse_iterator iter;
13376bb8d67SJim Ingham std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend();
134b9c1b51eSKate Stone for (iter = sc_to_remove.rbegin(); iter != rend; iter++) {
13576bb8d67SJim Ingham sc_list.RemoveContextAtIndex(*iter);
13676bb8d67SJim Ingham }
13776bb8d67SJim Ingham }
13876bb8d67SJim Ingham }
13976bb8d67SJim Ingham
140f642373cSJim Ingham const bool skip_prologue = true;
141969795f1SJim Ingham
142b9c1b51eSKate Stone BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue,
143b9c1b51eSKate Stone m_regex.GetText());
144969795f1SJim Ingham }
145969795f1SJim Ingham
146969795f1SJim Ingham return Searcher::eCallbackReturnContinue;
147969795f1SJim Ingham }
148969795f1SJim Ingham
GetDepth()1494911d36aSJim Ingham lldb::SearchDepth BreakpointResolverFileRegex::GetDepth() {
1504911d36aSJim Ingham return lldb::eSearchDepthCompUnit;
151969795f1SJim Ingham }
152969795f1SJim Ingham
GetDescription(Stream * s)153b9c1b51eSKate Stone void BreakpointResolverFileRegex::GetDescription(Stream *s) {
15495eae423SZachary Turner s->Printf("source regex = \"%s\", exact_match = %d",
15595eae423SZachary Turner m_regex.GetText().str().c_str(), m_exact_match);
156969795f1SJim Ingham }
157969795f1SJim Ingham
Dump(Stream * s) const158b9c1b51eSKate Stone void BreakpointResolverFileRegex::Dump(Stream *s) const {}
159969795f1SJim Ingham
16033df7cd3SJim Ingham lldb::BreakpointResolverSP
CopyForBreakpoint(BreakpointSP & breakpoint)1616c17cc53STatyana Krasnukha BreakpointResolverFileRegex::CopyForBreakpoint(BreakpointSP &breakpoint) {
162b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
1636c17cc53STatyana Krasnukha breakpoint, m_regex, m_function_names, m_exact_match));
16433df7cd3SJim Ingham return ret_sp;
16533df7cd3SJim Ingham }
16633df7cd3SJim Ingham
AddFunctionName(const char * func_name)167b9c1b51eSKate Stone void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
16876bb8d67SJim Ingham m_function_names.insert(func_name);
16976bb8d67SJim Ingham }
170