blob: ae2e0949f9f58ec73eabb3bdf5e45c06d1fa48e7 [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- BreakpointResolverFileRegex.cpp --------------------------*- C++
2//-*-===//
Jim Ingham969795f2011-09-21 01:17:13 +00003//
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"
Jim Ingham969795f2011-09-21 01:17:13 +000018#include "lldb/Core/Log.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include "lldb/Core/SourceManager.h"
Jim Ingham969795f2011-09-21 01:17:13 +000020#include "lldb/Core/StreamString.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Symbol/CompileUnit.h"
Jim Ingham969795f2011-09-21 01:17:13 +000022#include "lldb/Target/Target.h"
Jim Ingham969795f2011-09-21 01:17:13 +000023
24using namespace lldb;
25using namespace lldb_private;
26
27//----------------------------------------------------------------------
28// BreakpointResolverFileRegex:
29//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000030BreakpointResolverFileRegex::BreakpointResolverFileRegex(
31 Breakpoint *bkpt, RegularExpression &regex,
32 const std::unordered_set<std::string> &func_names, bool exact_match)
33 : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver),
34 m_regex(regex), m_exact_match(exact_match), m_function_names(func_names) {
Jim Ingham969795f2011-09-21 01:17:13 +000035}
36
Kate Stoneb9c1b512016-09-06 20:57:50 +000037BreakpointResolverFileRegex::~BreakpointResolverFileRegex() {}
Jim Ingham969795f2011-09-21 01:17:13 +000038
Jim Inghame14dc262016-09-12 23:10:56 +000039BreakpointResolver *BreakpointResolverFileRegex::CreateFromStructuredData(
Jim Ingham1a81b272016-09-13 01:58:08 +000040 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
41 Error &error) {
Jim Inghame14dc262016-09-12 23:10:56 +000042 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
83StructuredData::ObjectSP
84BreakpointResolverFileRegex::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
Jim Ingham969795f2011-09-21 01:17:13 +0000104Searcher::CallbackReturn
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105BreakpointResolverFileRegex::SearchCallback(SearchFilter &filter,
106 SymbolContext &context,
107 Address *addr, bool containing) {
Jim Ingham969795f2011-09-21 01:17:13 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 assert(m_breakpoint != NULL);
110 if (!context.target_sp)
111 return eCallbackReturnContinue;
Jim Ingham969795f2011-09-21 01:17:13 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 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);
Jim Ingham76bb8d62016-04-28 01:40:57 +0000139 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 }
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 }
Jim Ingham969795f2011-09-21 01:17:13 +0000149 }
Jim Ingham87df91b2011-09-23 00:54:11 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 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;
Jim Ingham969795f2011-09-21 01:17:13 +0000159}
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161Searcher::Depth BreakpointResolverFileRegex::GetDepth() {
162 return Searcher::eDepthCompUnit;
Jim Ingham969795f2011-09-21 01:17:13 +0000163}
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165void BreakpointResolverFileRegex::GetDescription(Stream *s) {
166 s->Printf("source regex = \"%s\", exact_match = %d", m_regex.GetText(),
167 m_exact_match);
Jim Ingham969795f2011-09-21 01:17:13 +0000168}
169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170void BreakpointResolverFileRegex::Dump(Stream *s) const {}
Jim Ingham969795f2011-09-21 01:17:13 +0000171
Jim Ingham33df7cd2014-12-06 01:28:03 +0000172lldb::BreakpointResolverSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173BreakpointResolverFileRegex::CopyForBreakpoint(Breakpoint &breakpoint) {
174 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileRegex(
175 &breakpoint, m_regex, m_function_names, m_exact_match));
176 return ret_sp;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000177}
178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179void BreakpointResolverFileRegex::AddFunctionName(const char *func_name) {
180 m_function_names.insert(func_name);
Jim Ingham76bb8d62016-04-28 01:40:57 +0000181}