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(std::move(regex)), m_exact_match(exact_match), 27 m_function_names(func_names) {} 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, std::move(regex), names_set, 73 exact_match); 74 } 75 76 StructuredData::ObjectSP 77 BreakpointResolverFileRegex::SerializeToStructuredData() { 78 StructuredData::DictionarySP options_dict_sp( 79 new StructuredData::Dictionary()); 80 81 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString), 82 m_regex.GetText()); 83 options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 84 m_exact_match); 85 if (!m_function_names.empty()) { 86 StructuredData::ArraySP names_array_sp(new StructuredData::Array()); 87 for (std::string name : m_function_names) { 88 StructuredData::StringSP item(new StructuredData::String(name)); 89 names_array_sp->AddItem(item); 90 } 91 options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp); 92 } 93 94 return WrapOptionsDict(options_dict_sp); 95 } 96 97 Searcher::CallbackReturn 98 BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter, 99 SymbolContext &context, 100 Address *addr, bool containing) { 101 102 assert(m_breakpoint != nullptr); 103 if (!context.target_sp) 104 return eCallbackReturnContinue; 105 106 CompileUnit *cu = context.comp_unit; 107 FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu)); 108 std::vector<uint32_t> line_matches; 109 context.target_sp->GetSourceManager().FindLinesMatchingRegex( 110 cu_file_spec, m_regex, 1, UINT32_MAX, line_matches); 111 112 uint32_t num_matches = line_matches.size(); 113 for (uint32_t i = 0; i < num_matches; i++) { 114 SymbolContextList sc_list; 115 const bool search_inlines = false; 116 117 cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines, 118 m_exact_match, eSymbolContextEverything, sc_list); 119 // Find all the function names: 120 if (!m_function_names.empty()) { 121 std::vector<size_t> sc_to_remove; 122 for (size_t i = 0; i < sc_list.GetSize(); i++) { 123 SymbolContext sc_ctx; 124 sc_list.GetContextAtIndex(i, sc_ctx); 125 std::string name( 126 sc_ctx 127 .GetFunctionName( 128 Mangled::NamePreference::ePreferDemangledWithoutArguments) 129 .AsCString()); 130 if (!m_function_names.count(name)) { 131 sc_to_remove.push_back(i); 132 } 133 } 134 135 if (!sc_to_remove.empty()) { 136 std::vector<size_t>::reverse_iterator iter; 137 std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend(); 138 for (iter = sc_to_remove.rbegin(); iter != rend; iter++) { 139 sc_list.RemoveContextAtIndex(*iter); 140 } 141 } 142 } 143 144 const bool skip_prologue = true; 145 146 BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue, 147 m_regex.GetText()); 148 } 149 assert(m_breakpoint != nullptr); 150 151 return Searcher::eCallbackReturnContinue; 152 } 153 154 lldb::SearchDepth BreakpointResolverFileRegex::GetDepth() { 155 return lldb::eSearchDepthCompUnit; 156 } 157 158 void BreakpointResolverFileRegex::GetDescription(Stream *s) { 159 s->Printf("source regex = \"%s\", exact_match = %d", 160 m_regex.GetText().str().c_str(), m_exact_match); 161 } 162 163 void BreakpointResolverFileRegex::Dump(Stream *s) const {} 164 165 lldb::BreakpointResolverSP 166 BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) { 167 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex( 168 &breakpoint, m_regex, m_function_names, m_exact_match)); 169 return ret_sp; 170 } 171 172 void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) { 173 m_function_names.insert(func_name); 174 } 175