195eae423SZachary Turner //===-- BreakpointResolverFileRegex.cpp -------------------------*- C++-*-===// 2969795f1SJim Ingham // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler 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 //---------------------------------------------------------------------- 22969795f1SJim Ingham // BreakpointResolverFileRegex: 23969795f1SJim Ingham //---------------------------------------------------------------------- 24b9c1b51eSKate Stone BreakpointResolverFileRegex::BreakpointResolverFileRegex( 25b9c1b51eSKate Stone Breakpoint *bkpt, RegularExpression ®ex, 26b9c1b51eSKate Stone const std::unordered_set<std::string> &func_names, bool exact_match) 276d1e4696SJim Ingham : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver), 28b9c1b51eSKate Stone m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) { 29969795f1SJim Ingham } 30969795f1SJim Ingham 31b9c1b51eSKate Stone BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {} 32969795f1SJim Ingham 33e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData( 341a81b273SJim Ingham Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, 3597206d57SZachary Turner Status &error) { 36e14dc268SJim Ingham bool success; 37e14dc268SJim Ingham 382833321fSZachary Turner llvm::StringRef regex_string; 39e14dc268SJim Ingham success = options_dict.GetValueForKeyAsString( 40e14dc268SJim Ingham GetKey(OptionNames::RegexString), regex_string); 41e14dc268SJim Ingham if (!success) { 42e14dc268SJim Ingham error.SetErrorString("BRFR::CFSD: Couldn't find regex entry."); 43e14dc268SJim Ingham return nullptr; 44e14dc268SJim Ingham } 4595eae423SZachary Turner RegularExpression regex(regex_string); 46e14dc268SJim Ingham 47e14dc268SJim Ingham bool exact_match; 48e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 49e14dc268SJim Ingham GetKey(OptionNames::ExactMatch), exact_match); 50e14dc268SJim Ingham if (!success) { 51e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry."); 52e14dc268SJim Ingham return nullptr; 53e14dc268SJim Ingham } 54e14dc268SJim Ingham 55e14dc268SJim Ingham // The names array is optional: 56e14dc268SJim Ingham std::unordered_set<std::string> names_set; 57e14dc268SJim Ingham StructuredData::Array *names_array; 58e14dc268SJim Ingham success = options_dict.GetValueForKeyAsArray( 59e14dc268SJim Ingham GetKey(OptionNames::SymbolNameArray), names_array); 60e14dc268SJim Ingham if (success && names_array) { 61e14dc268SJim Ingham size_t num_names = names_array->GetSize(); 62e14dc268SJim Ingham for (size_t i = 0; i < num_names; i++) { 632833321fSZachary Turner llvm::StringRef name; 64e14dc268SJim Ingham success = names_array->GetItemAtIndexAsString(i, name); 65e14dc268SJim Ingham if (!success) { 66e14dc268SJim Ingham error.SetErrorStringWithFormat( 67e14dc268SJim Ingham "BRFR::CFSD: Malformed element %zu in the names array.", i); 68e14dc268SJim Ingham return nullptr; 69e14dc268SJim Ingham } 70e14dc268SJim Ingham names_set.insert(name); 71e14dc268SJim Ingham } 72e14dc268SJim Ingham } 73e14dc268SJim Ingham 74e14dc268SJim Ingham return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match); 75e14dc268SJim Ingham } 76e14dc268SJim Ingham 77e14dc268SJim Ingham StructuredData::ObjectSP 78e14dc268SJim Ingham BreakpointResolverFileRegex::SerializeToStructuredData() { 79e14dc268SJim Ingham StructuredData::DictionarySP options_dict_sp( 80e14dc268SJim Ingham new StructuredData::Dictionary()); 81e14dc268SJim Ingham 82e14dc268SJim Ingham options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString), 83e14dc268SJim Ingham m_regex.GetText()); 84e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 85e14dc268SJim Ingham m_exact_match); 86e14dc268SJim Ingham if (!m_function_names.empty()) { 87e14dc268SJim Ingham StructuredData::ArraySP names_array_sp(new StructuredData::Array()); 88e14dc268SJim Ingham for (std::string name : m_function_names) { 89e14dc268SJim Ingham StructuredData::StringSP item(new StructuredData::String(name)); 90e14dc268SJim Ingham names_array_sp->AddItem(item); 91e14dc268SJim Ingham } 92e14dc268SJim Ingham options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp); 93e14dc268SJim Ingham } 94e14dc268SJim Ingham 95e14dc268SJim Ingham return WrapOptionsDict(options_dict_sp); 96e14dc268SJim Ingham } 97e14dc268SJim Ingham 98969795f1SJim Ingham Searcher::CallbackReturn 99b9c1b51eSKate Stone BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter, 100969795f1SJim Ingham SymbolContext &context, 101b9c1b51eSKate Stone Address *addr, bool containing) { 102969795f1SJim Ingham 103969795f1SJim Ingham assert(m_breakpoint != NULL); 104969795f1SJim Ingham if (!context.target_sp) 105969795f1SJim Ingham return eCallbackReturnContinue; 106969795f1SJim Ingham 107969795f1SJim Ingham CompileUnit *cu = context.comp_unit; 108969795f1SJim Ingham FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu)); 109969795f1SJim Ingham std::vector<uint32_t> line_matches; 110b9c1b51eSKate Stone context.target_sp->GetSourceManager().FindLinesMatchingRegex( 111b9c1b51eSKate Stone cu_file_spec, m_regex, 1, UINT32_MAX, line_matches); 112f642373cSJim Ingham 113969795f1SJim Ingham uint32_t num_matches = line_matches.size(); 114b9c1b51eSKate Stone for (uint32_t i = 0; i < num_matches; i++) { 115f642373cSJim Ingham SymbolContextList sc_list; 116f642373cSJim Ingham const bool search_inlines = false; 117969795f1SJim Ingham 118b9c1b51eSKate Stone cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines, 119b9c1b51eSKate Stone m_exact_match, eSymbolContextEverything, sc_list); 12076bb8d67SJim Ingham // Find all the function names: 121b9c1b51eSKate Stone if (!m_function_names.empty()) { 12276bb8d67SJim Ingham std::vector<size_t> sc_to_remove; 123b9c1b51eSKate Stone for (size_t i = 0; i < sc_list.GetSize(); i++) { 12476bb8d67SJim Ingham SymbolContext sc_ctx; 12576bb8d67SJim Ingham sc_list.GetContextAtIndex(i, sc_ctx); 126b9c1b51eSKate Stone std::string name( 127b9c1b51eSKate Stone sc_ctx 128b9c1b51eSKate Stone .GetFunctionName( 129b9c1b51eSKate Stone Mangled::NamePreference::ePreferDemangledWithoutArguments) 130b9c1b51eSKate Stone .AsCString()); 131b9c1b51eSKate Stone if (!m_function_names.count(name)) { 13276bb8d67SJim Ingham sc_to_remove.push_back(i); 13376bb8d67SJim Ingham } 13476bb8d67SJim Ingham } 13576bb8d67SJim Ingham 136b9c1b51eSKate Stone if (!sc_to_remove.empty()) { 13776bb8d67SJim Ingham std::vector<size_t>::reverse_iterator iter; 13876bb8d67SJim Ingham std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend(); 139b9c1b51eSKate Stone for (iter = sc_to_remove.rbegin(); iter != rend; iter++) { 14076bb8d67SJim Ingham sc_list.RemoveContextAtIndex(*iter); 14176bb8d67SJim Ingham } 14276bb8d67SJim Ingham } 14376bb8d67SJim Ingham } 14476bb8d67SJim Ingham 145f642373cSJim Ingham const bool skip_prologue = true; 146969795f1SJim Ingham 147b9c1b51eSKate Stone BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue, 148b9c1b51eSKate Stone m_regex.GetText()); 149969795f1SJim Ingham } 150969795f1SJim Ingham assert(m_breakpoint != NULL); 151969795f1SJim Ingham 152969795f1SJim Ingham return Searcher::eCallbackReturnContinue; 153969795f1SJim Ingham } 154969795f1SJim Ingham 1554911d36aSJim Ingham lldb::SearchDepth BreakpointResolverFileRegex::GetDepth() { 1564911d36aSJim Ingham return lldb::eSearchDepthCompUnit; 157969795f1SJim Ingham } 158969795f1SJim Ingham 159b9c1b51eSKate Stone void BreakpointResolverFileRegex::GetDescription(Stream *s) { 16095eae423SZachary Turner s->Printf("source regex = \"%s\", exact_match = %d", 16195eae423SZachary Turner m_regex.GetText().str().c_str(), m_exact_match); 162969795f1SJim Ingham } 163969795f1SJim Ingham 164b9c1b51eSKate Stone void BreakpointResolverFileRegex::Dump(Stream *s) const {} 165969795f1SJim Ingham 16633df7cd3SJim Ingham lldb::BreakpointResolverSP 167b9c1b51eSKate Stone BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) { 168b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex( 169b9c1b51eSKate Stone &breakpoint, m_regex, m_function_names, m_exact_match)); 17033df7cd3SJim Ingham return ret_sp; 17133df7cd3SJim Ingham } 17233df7cd3SJim Ingham 173b9c1b51eSKate Stone void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) { 17476bb8d67SJim Ingham m_function_names.insert(func_name); 17576bb8d67SJim Ingham } 176