blob: df6f5c2e4bf2308137c0d64f5ba2e8345ac96fc4 [file] [log] [blame]
Zachary Turner95eae422016-09-21 16:01:28 +00001//===-- BreakpointResolverFileRegex.cpp -------------------------*- C++-*-===//
Jim Ingham969795f2011-09-21 01:17:13 +00002//
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"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "lldb/Core/SourceManager.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Symbol/CompileUnit.h"
Jim Ingham969795f2011-09-21 01:17:13 +000019#include "lldb/Target/Target.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000020#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000021#include "lldb/Utility/StreamString.h"
Jim Ingham969795f2011-09-21 01:17:13 +000022
23using namespace lldb;
24using namespace lldb_private;
25
26//----------------------------------------------------------------------
27// BreakpointResolverFileRegex:
28//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000029BreakpointResolverFileRegex::BreakpointResolverFileRegex(
30 Breakpoint *bkpt, RegularExpression &regex,
31 const std::unordered_set<std::string> &func_names, bool exact_match)
Jim Ingham6d1e4692016-09-16 01:41:27 +000032 : BreakpointResolver(bkpt, BreakpointResolver::FileRegexResolver),
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) {
Jim Ingham969795f2011-09-21 01:17:13 +000034}
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
Jim Ingham969795f2011-09-21 01:17:13 +000037
Jim Inghame14dc262016-09-12 23:10:56 +000038BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
Jim Ingham1a81b272016-09-13 01:58:08 +000039 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
40 Error &error) {
Jim Inghame14dc262016-09-12 23:10:56 +000041 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 Turner95eae422016-09-21 16:01:28 +000050 RegularExpression regex(regex_string);
Jim Inghame14dc262016-09-12 23:10:56 +000051
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
82StructuredData::ObjectSP
83BreakpointResolverFileRegex::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 Ingham969795f2011-09-21 01:17:13 +0000103Searcher::CallbackReturn
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
105 SymbolContext &context,
106 Address *addr, bool containing) {
Jim Ingham969795f2011-09-21 01:17:13 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 assert(m_breakpoint != NULL);
109 if (!context.target_sp)
110 return eCallbackReturnContinue;
Jim Ingham969795f2011-09-21 01:17:13 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 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 Ingham76bb8d62016-04-28 01:40:57 +0000138 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 }
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 Ingham969795f2011-09-21 01:17:13 +0000148 }
Jim Ingham87df91b2011-09-23 00:54:11 +0000149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 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 Ingham969795f2011-09-21 01:17:13 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160Searcher::Depth BreakpointResolverFileRegex::GetDepth() {
161 return Searcher::eDepthCompUnit;
Jim Ingham969795f2011-09-21 01:17:13 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164void BreakpointResolverFileRegex::GetDescription(Stream *s) {
Zachary Turner95eae422016-09-21 16:01:28 +0000165 s->Printf("source regex = \"%s\", exact_match = %d",
166 m_regex.GetText().str().c_str(), m_exact_match);
Jim Ingham969795f2011-09-21 01:17:13 +0000167}
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169void BreakpointResolverFileRegex::Dump(Stream *s) const {}
Jim Ingham969795f2011-09-21 01:17:13 +0000170
Jim Ingham33df7cd2014-12-06 01:28:03 +0000171lldb::BreakpointResolverSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172BreakpointResolverFileRegex::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 Ingham33df7cd2014-12-06 01:28:03 +0000176}
177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
179 m_function_names.insert(func_name);
Jim Ingham76bb8d62016-04-28 01:40:57 +0000180}