Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- BreakpointResolverFileRegex.cpp -----------------------------------===// |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Breakpoint/BreakpointResolverFileRegex.h" |
| 10 | |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 11 | #include "lldb/Breakpoint/BreakpointLocation.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | #include "lldb/Core/SourceManager.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 13 | #include "lldb/Symbol/CompileUnit.h" |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 14 | #include "lldb/Target/Target.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/StreamString.h" |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 21 | // BreakpointResolverFileRegex: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | BreakpointResolverFileRegex::BreakpointResolverFileRegex( |
Jonas Devlieghere | 3af3f1e | 2019-08-16 21:25:36 +0000 | [diff] [blame] | 23 | Breakpoint *bkpt, RegularExpression regex, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | const std::unordered_set<std::string> &func_names, bool exact_match) |
Jim Ingham | 6d1e469 | 2016-09-16 01:41:27 +0000 | [diff] [blame] | 25 | : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver), |
Jonas Devlieghere | 3af3f1e | 2019-08-16 21:25:36 +0000 | [diff] [blame] | 26 | m_regex(std::move(regex)), m_exact_match(exact_match), |
| 27 | m_function_names(func_names) {} |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {} |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 30 | |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 31 | BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData( |
Jim Ingham | 1a81b27 | 2016-09-13 01:58:08 +0000 | [diff] [blame] | 32 | Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 33 | Status &error) { |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 34 | bool success; |
| 35 | |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 36 | llvm::StringRef regex_string; |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 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 | } |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 43 | RegularExpression regex(regex_string); |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 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++) { |
Zachary Turner | 2833321 | 2017-05-12 05:49:54 +0000 | [diff] [blame] | 61 | llvm::StringRef name; |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 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 | } |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 68 | names_set.insert(std::string(name)); |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Jonas Devlieghere | 3af3f1e | 2019-08-16 21:25:36 +0000 | [diff] [blame] | 72 | return new BreakpointResolverFileRegex(bkpt, std::move(regex), names_set, |
| 73 | exact_match); |
Jim Ingham | e14dc26 | 2016-09-12 23:10:56 +0000 | [diff] [blame] | 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 | |
Raphael Isemann | 95e264f | 2019-10-10 11:26:51 +0000 | [diff] [blame] | 97 | Searcher::CallbackReturn BreakpointResolverFileRegex::SearchCallback( |
| 98 | SearchFilter &filter, SymbolContext &context, Address *addr) { |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 99 | |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 100 | assert(m_breakpoint != nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | if (!context.target_sp) |
| 102 | return eCallbackReturnContinue; |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | CompileUnit *cu = context.comp_unit; |
Pavel Labath | 38870af | 2019-11-28 16:22:44 +0100 | [diff] [blame] | 105 | FileSpec cu_file_spec = cu->GetPrimaryFile(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | std::vector<uint32_t> line_matches; |
| 107 | context.target_sp->GetSourceManager().FindLinesMatchingRegex( |
| 108 | cu_file_spec, m_regex, 1, UINT32_MAX, line_matches); |
| 109 | |
| 110 | uint32_t num_matches = line_matches.size(); |
| 111 | for (uint32_t i = 0; i < num_matches; i++) { |
| 112 | SymbolContextList sc_list; |
| 113 | const bool search_inlines = false; |
| 114 | |
| 115 | cu->ResolveSymbolContext(cu_file_spec, line_matches[i], search_inlines, |
| 116 | m_exact_match, eSymbolContextEverything, sc_list); |
| 117 | // Find all the function names: |
| 118 | if (!m_function_names.empty()) { |
| 119 | std::vector<size_t> sc_to_remove; |
| 120 | for (size_t i = 0; i < sc_list.GetSize(); i++) { |
| 121 | SymbolContext sc_ctx; |
| 122 | sc_list.GetContextAtIndex(i, sc_ctx); |
| 123 | std::string name( |
| 124 | sc_ctx |
| 125 | .GetFunctionName( |
| 126 | Mangled::NamePreference::ePreferDemangledWithoutArguments) |
| 127 | .AsCString()); |
| 128 | if (!m_function_names.count(name)) { |
| 129 | sc_to_remove.push_back(i); |
Jim Ingham | 76bb8d6 | 2016-04-28 01:40:57 +0000 | [diff] [blame] | 130 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (!sc_to_remove.empty()) { |
| 134 | std::vector<size_t>::reverse_iterator iter; |
| 135 | std::vector<size_t>::reverse_iterator rend = sc_to_remove.rend(); |
| 136 | for (iter = sc_to_remove.rbegin(); iter != rend; iter++) { |
| 137 | sc_list.RemoveContextAtIndex(*iter); |
| 138 | } |
| 139 | } |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 140 | } |
Jim Ingham | 87df91b | 2011-09-23 00:54:11 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | const bool skip_prologue = true; |
| 143 | |
| 144 | BreakpointResolver::SetSCMatchesByLine(filter, sc_list, skip_prologue, |
| 145 | m_regex.GetText()); |
| 146 | } |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 147 | assert(m_breakpoint != nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | |
| 149 | return Searcher::eCallbackReturnContinue; |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Jim Ingham | 4911d36 | 2018-09-07 18:43:04 +0000 | [diff] [blame] | 152 | lldb::SearchDepth BreakpointResolverFileRegex::GetDepth() { |
| 153 | return lldb::eSearchDepthCompUnit; |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | void BreakpointResolverFileRegex::GetDescription(Stream *s) { |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 157 | s->Printf("source regex = \"%s\", exact_match = %d", |
| 158 | m_regex.GetText().str().c_str(), m_exact_match); |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | void BreakpointResolverFileRegex::Dump(Stream *s) const {} |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 162 | |
Jim Ingham | 33df7cd | 2014-12-06 01:28:03 +0000 | [diff] [blame] | 163 | lldb::BreakpointResolverSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) { |
| 165 | lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex( |
| 166 | &breakpoint, m_regex, m_function_names, m_exact_match)); |
| 167 | return ret_sp; |
Jim Ingham | 33df7cd | 2014-12-06 01:28:03 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) { |
| 171 | m_function_names.insert(func_name); |
Jim Ingham | 76bb8d6 | 2016-04-28 01:40:57 +0000 | [diff] [blame] | 172 | } |