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