1 //===-- BreakpointResolverFileRegex.cpp --------------------------*- C++ 2 //-*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #include "lldb/Breakpoint/BreakpointResolverFileRegex.h" 12 13 // C Includes 14 // C++ Includes 15 // Other libraries and framework includes 16 // Project includes 17 #include "lldb/Breakpoint/BreakpointLocation.h" 18 #include "lldb/Core/Log.h" 19 #include "lldb/Core/SourceManager.h" 20 #include "lldb/Core/StreamString.h" 21 #include "lldb/Symbol/CompileUnit.h" 22 #include "lldb/Target/Target.h" 23 24 using namespace lldb; 25 using namespace lldb_private; 26 27 //---------------------------------------------------------------------- 28 // BreakpointResolverFileRegex: 29 //---------------------------------------------------------------------- 30 BreakpointResolverFileRegex::BreakpointResolverFileRegex( 31 Breakpoint *bkpt, RegularExpression ®ex, 32 const std::unordered_set<std::string> &func_names, bool exact_match) 33 : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver), 34 m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) { 35 } 36 37 BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {} 38 39 BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData( 40 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, 41 Error &error) { 42 bool success; 43 44 std::string regex_string; 45 success = options_dict.GetValueForKeyAsString( 46 GetKey(OptionNames::RegexString), regex_string); 47 if (!success) { 48 error.SetErrorString("BRFR::CFSD: Couldn't find regex entry."); 49 return nullptr; 50 } 51 RegularExpression regex(regex_string.c_str()); 52 53 bool exact_match; 54 success = options_dict.GetValueForKeyAsBoolean( 55 GetKey(OptionNames::ExactMatch), exact_match); 56 if (!success) { 57 error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry."); 58 return nullptr; 59 } 60 61 // The names array is optional: 62 std::unordered_set<std::string> names_set; 63 StructuredData::Array *names_array; 64 success = options_dict.GetValueForKeyAsArray( 65 GetKey(OptionNames::SymbolNameArray), names_array); 66 if (success && names_array) { 67 size_t num_names = names_array->GetSize(); 68 for (size_t i = 0; i < num_names; i++) { 69 std::string name; 70 success = names_array->GetItemAtIndexAsString(i, name); 71 if (!success) { 72 error.SetErrorStringWithFormat( 73 "BRFR::CFSD: Malformed element %zu in the names array.", i); 74 return nullptr; 75 } 76 names_set.insert(name); 77 } 78 } 79 80 return new BreakpointResolverFileRegex(bkpt, regex, names_set, exact_match); 81 } 82 83 StructuredData::ObjectSP 84 BreakpointResolverFileRegex::SerializeToStructuredData() { 85 StructuredData::DictionarySP options_dict_sp( 86 new StructuredData::Dictionary()); 87 88 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString), 89 m_regex.GetText()); 90 options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 91 m_exact_match); 92 if (!m_function_names.empty()) { 93 StructuredData::ArraySP names_array_sp(new StructuredData::Array()); 94 for (std::string name : m_function_names) { 95 StructuredData::StringSP item(new StructuredData::String(name)); 96 names_array_sp->AddItem(item); 97 } 98 options_dict_sp->AddItem(GetKey(OptionNames::LineNumber), names_array_sp); 99 } 100 101 return WrapOptionsDict(options_dict_sp); 102 } 103 104 Searcher::CallbackReturn 105 BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter, 106 SymbolContext &context, 107 Address *addr, bool containing) { 108 109 assert(m_breakpoint != NULL); 110 if (!context.target_sp) 111 return eCallbackReturnContinue; 112 113 CompileUnit *cu = context.comp_unit; 114 FileSpec cu_file_spec = *(static_cast<FileSpec *>(cu)); 115 std::vector<uint32_t> line_matches; 116 context.target_sp->GetSourceManager().FindLinesMatchingRegex( 117 cu_file_spec, m_regex, 1, UINT32_MAX, line_matches); 118 119 uint32_t num_matches = line_matches.size(); 120 for (uint32_t i = 0; i < num_matches; i++) { 121 SymbolContextList sc_list; 122 const bool search_inlines = false; 123 124 cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines, 125 m_exact_match, eSymbolContextEverything, sc_list); 126 // Find all the function names: 127 if (!m_function_names.empty()) { 128 std::vector<size_t> sc_to_remove; 129 for (size_t i = 0; i < sc_list.GetSize(); i++) { 130 SymbolContext sc_ctx; 131 sc_list.GetContextAtIndex(i, sc_ctx); 132 std::string name( 133 sc_ctx 134 .GetFunctionName( 135 Mangled::NamePreference::ePreferDemangledWithoutArguments) 136 .AsCString()); 137 if (!m_function_names.count(name)) { 138 sc_to_remove.push_back(i); 139 } 140 } 141 142 if (!sc_to_remove.empty()) { 143 std::vector<size_t>::reverse_iterator iter; 144 std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend(); 145 for (iter = sc_to_remove.rbegin(); iter != rend; iter++) { 146 sc_list.RemoveContextAtIndex(*iter); 147 } 148 } 149 } 150 151 const bool skip_prologue = true; 152 153 BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue, 154 m_regex.GetText()); 155 } 156 assert(m_breakpoint != NULL); 157 158 return Searcher::eCallbackReturnContinue; 159 } 160 161 Searcher::Depth BreakpointResolverFileRegex::GetDepth() { 162 return Searcher::eDepthCompUnit; 163 } 164 165 void BreakpointResolverFileRegex::GetDescription(Stream *s) { 166 s->Printf("source regex = \"%s\", exact_match = %d", m_regex.GetText(), 167 m_exact_match); 168 } 169 170 void BreakpointResolverFileRegex::Dump(Stream *s) const {} 171 172 lldb::BreakpointResolverSP 173 BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) { 174 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex( 175 &breakpoint, m_regex, m_function_names, m_exact_match)); 176 return ret_sp; 177 } 178 179 void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) { 180 m_function_names.insert(func_name); 181 } 182